feat(renderer) Render player quad with projection
this allows us to render a player controlled entity on screen using a 2D projection
This commit is contained in:
parent
d6d3581e5b
commit
dc4dd650a4
@ -7,7 +7,7 @@ set(CMAKE_CXX_STANDARD 17)
|
|||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
# include directories..
|
# include directories..
|
||||||
include_directories(libs)
|
include_directories(vendor)
|
||||||
|
|
||||||
# Deps.
|
# Deps.
|
||||||
find_package(SDL3 REQUIRED)
|
find_package(SDL3 REQUIRED)
|
||||||
|
|||||||
@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
layout (location = 0) in vec3 aPos;
|
layout (location = 0) in vec3 aPos;
|
||||||
|
|
||||||
|
uniform mat4 projection;
|
||||||
|
uniform mat4 model;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
|
gl_Position = projection * model * vec4(aPos, 1.0);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,6 +5,9 @@
|
|||||||
#include <SDL3/SDL_stdinc.h>
|
#include <SDL3/SDL_stdinc.h>
|
||||||
#include <SDL3/SDL_timer.h>
|
#include <SDL3/SDL_timer.h>
|
||||||
#include <SDL3/SDL_video.h>
|
#include <SDL3/SDL_video.h>
|
||||||
|
#include <glm/ext/matrix_transform.hpp> /* TODO */
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <glm/gtc/type_ptr.hpp>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
#include "bettola.h"
|
#include "bettola.h"
|
||||||
@ -31,8 +34,10 @@ Bettola::~Bettola(void) {
|
|||||||
|
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
|
|
||||||
glDeleteVertexArrays(1, &_vao);
|
if(_gl_context) {
|
||||||
glDeleteBuffers(1, &_vbo);
|
glDeleteVertexArrays(1, &_vao);
|
||||||
|
glDeleteBuffers(1, &_vbo);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int Bettola::run(void) {
|
int Bettola::run(void) {
|
||||||
@ -46,7 +51,12 @@ int Bettola::run(void) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
setup_triangle();
|
_projection = glm::ortho(0.0f, 800.0f, 600.0f, 0.0f, -1.0f, 1.0f);
|
||||||
|
_shader.use();
|
||||||
|
unsigned int proj_loc = glGetUniformLocation(_shader.get_id(), "projection");
|
||||||
|
glUniformMatrix4fv(proj_loc, 1, GL_FALSE, glm::value_ptr(_projection));
|
||||||
|
|
||||||
|
setup_quad();
|
||||||
|
|
||||||
glViewport(0,0,SCREEN_WIDTH, SCREEN_HEIGHT);
|
glViewport(0,0,SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||||
|
|
||||||
@ -95,8 +105,16 @@ void Bettola::render(void) {
|
|||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
_shader.use();
|
_shader.use();
|
||||||
|
|
||||||
|
glm::mat4 model = glm::mat4(1.0f);
|
||||||
|
model = glm::translate(model, glm::vec3(_player.get_x(), _player.get_y(), 0.0f));
|
||||||
|
model = glm::scale(model, glm::vec3(_player.get_width(), _player.get_height(), 1.0f));
|
||||||
|
|
||||||
|
unsigned int model_loc = glGetUniformLocation(_shader.get_id(), "model");
|
||||||
|
glUniformMatrix4fv(model_loc, 1, GL_FALSE, glm::value_ptr(model));
|
||||||
|
|
||||||
glBindVertexArray(_vao);
|
glBindVertexArray(_vao);
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
|
|
||||||
SDL_GL_SwapWindow(_window);
|
SDL_GL_SwapWindow(_window);
|
||||||
@ -146,12 +164,15 @@ bool Bettola::create_gl_context(void) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Bettola::setup_triangle(void) {
|
void Bettola::setup_quad(void) {
|
||||||
float vertices[] = {
|
float vertices[] = {
|
||||||
/* Should be a triangle??!?? */
|
/* Should be a quad??!?? */
|
||||||
-0.5f, -0.5f, 0.0f,
|
-0.5f, -0.5f, 0.0f,
|
||||||
0.5f, -0.5f, 0.0f,
|
0.5f, -0.5f, 0.0f,
|
||||||
-0.0f, 0.5f, 0.0f,
|
0.5f, 0.5f, 0.0f,
|
||||||
|
0.5f, 0.5f, 0.0f,
|
||||||
|
-0.5f, 0.5f, 0.0f,
|
||||||
|
-0.5f, -0.5f, 0.0f,
|
||||||
};
|
};
|
||||||
|
|
||||||
glGenVertexArrays(1, &_vao);
|
glGenVertexArrays(1, &_vao);
|
||||||
|
|||||||
@ -1,8 +1,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
#include "graphics/shader.h"
|
#include "graphics/shader.h"
|
||||||
|
#include "game/player.h"
|
||||||
|
|
||||||
class Bettola {
|
class Bettola {
|
||||||
public:
|
public:
|
||||||
@ -20,7 +22,7 @@ private:
|
|||||||
bool init_glew(void);
|
bool init_glew(void);
|
||||||
bool create_window(void);
|
bool create_window(void);
|
||||||
bool create_gl_context(void);
|
bool create_gl_context(void);
|
||||||
void setup_triangle(void);
|
void setup_quad(void);
|
||||||
|
|
||||||
bool _is_running;
|
bool _is_running;
|
||||||
|
|
||||||
@ -30,4 +32,7 @@ private:
|
|||||||
Shader _shader;
|
Shader _shader;
|
||||||
unsigned int _vao;
|
unsigned int _vao;
|
||||||
unsigned int _vbo;
|
unsigned int _vbo;
|
||||||
|
|
||||||
|
glm::mat4 _projection;
|
||||||
|
Player _player;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -7,5 +7,5 @@ Player::Player(void) :
|
|||||||
_height(50.0f) {}
|
_height(50.0f) {}
|
||||||
|
|
||||||
void Player::update(double dt) {
|
void Player::update(double dt) {
|
||||||
|
/* Do some shit? */
|
||||||
}
|
}
|
||||||
|
|||||||
0
libs/glm/ext.hpp → vendor/glm/ext.hpp
vendored
0
libs/glm/ext.hpp → vendor/glm/ext.hpp
vendored
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user