Add translation, rotation and scale matrices
This commit is contained in:
parent
1f593b96ac
commit
e9f6f59678
54
src/main.cc
54
src/main.cc
@ -1,4 +1,5 @@
|
|||||||
#include "glad/glad.h"
|
#include "glad/glad.h"
|
||||||
|
#include "glm/ext/matrix_transform.hpp"
|
||||||
#include "glm/gtc/type_ptr.hpp"
|
#include "glm/gtc/type_ptr.hpp"
|
||||||
#include "glm/trigonometric.hpp"
|
#include "glm/trigonometric.hpp"
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
@ -49,9 +50,14 @@ struct App {
|
|||||||
GLuint shader_program;
|
GLuint shader_program;
|
||||||
|
|
||||||
float speed;
|
float speed;
|
||||||
|
glm::mat4 translation;
|
||||||
|
glm::mat4 rotation;
|
||||||
|
glm::mat4 scale;
|
||||||
|
|
||||||
// Model matrix to be sent as uniform to the vertex shader
|
// Model matrix to be sent as uniform to the vertex shader
|
||||||
glm::mat4 model_mat;
|
glm::mat4 model_mat;
|
||||||
GLint u_model_idx;
|
GLint u_model_idx;
|
||||||
|
bool recalc_model_mat;
|
||||||
|
|
||||||
// Projection matrix to be sent as uniform to the vertex shader
|
// Projection matrix to be sent as uniform to the vertex shader
|
||||||
glm::mat4 projection_mat;
|
glm::mat4 projection_mat;
|
||||||
@ -129,7 +135,10 @@ int init(App &app) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
app.speed = 0.02f;
|
app.speed = 0.02f;
|
||||||
app.model_mat = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f));
|
app.translation = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -1.5f));
|
||||||
|
app.rotation = glm::mat4(1.0f);
|
||||||
|
app.scale = glm::mat4(1.0f);
|
||||||
|
app.model_mat = app.translation * app.rotation * app.scale;
|
||||||
app.projection_mat = glm::perspective(glm::radians(60.0f),
|
app.projection_mat = glm::perspective(glm::radians(60.0f),
|
||||||
(float)WINDOW_WIDTH / (float)WINDOW_HEIGHT,
|
(float)WINDOW_WIDTH / (float)WINDOW_HEIGHT,
|
||||||
0.1f,
|
0.1f,
|
||||||
@ -142,16 +151,16 @@ int init(App &app) {
|
|||||||
void create_vertex_spec(App &app) {
|
void create_vertex_spec(App &app) {
|
||||||
const std::vector<GLfloat> vertices = {
|
const std::vector<GLfloat> vertices = {
|
||||||
// vert0
|
// vert0
|
||||||
-0.5f, -0.5f, -1.5f, // position
|
-0.5f, -0.5f, 0.0f, // position
|
||||||
1.0f, 0.0f, 0.0f, // colour
|
1.0f, 0.0f, 0.0f, // colour
|
||||||
// vert1
|
// vert1
|
||||||
0.5f, -0.5f, -1.5f, // position
|
0.5f, -0.5f, 0.0f, // position
|
||||||
0.0f, 1.0f, 0.0f, // colour
|
0.0f, 1.0f, 0.0f, // colour
|
||||||
// vert2
|
// vert2
|
||||||
-0.5f, 0.5f, -1.5f, // position
|
-0.5f, 0.5f, 0.0f, // position
|
||||||
0.0f, 0.0f, 1.0f, // colour
|
0.0f, 0.0f, 1.0f, // colour
|
||||||
// vert3
|
// vert3
|
||||||
0.5f, 0.5f, -1.5f, // position
|
0.5f, 0.5f, 0.0f, // position
|
||||||
1.0f, 1.0f, 0.0f, // colour
|
1.0f, 1.0f, 0.0f, // colour
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -238,11 +247,38 @@ void run_main_loop(App &app) {
|
|||||||
app.running = false;
|
app.running = false;
|
||||||
break;
|
break;
|
||||||
case SDL_KEYDOWN:
|
case SDL_KEYDOWN:
|
||||||
if (app.event.key.keysym.sym == SDLK_UP) {
|
if (app.event.key.keysym.sym == SDLK_w) {
|
||||||
app.model_mat = glm::translate(app.model_mat, glm::vec3(0.0f, app.speed, 0.0f));
|
app.translation = glm::translate(app.translation, glm::vec3(0.0f, 0.0f, app.speed));
|
||||||
} else if (app.event.key.keysym.sym == SDLK_DOWN) {
|
app.recalc_model_mat = true;
|
||||||
app.model_mat = glm::translate(app.model_mat, glm::vec3(0.0f, -app.speed, 0.0f));
|
} else if (app.event.key.keysym.sym == SDLK_s) {
|
||||||
|
app.translation = glm::translate(app.translation, glm::vec3(0.0f, 0.0f, -app.speed));
|
||||||
|
app.recalc_model_mat = true;
|
||||||
|
} else if (app.event.key.keysym.sym == SDLK_d) {
|
||||||
|
app.translation = glm::translate(app.translation, glm::vec3(app.speed, 0.0f, 0.0f));
|
||||||
|
app.recalc_model_mat = true;
|
||||||
|
} else if (app.event.key.keysym.sym == SDLK_a) {
|
||||||
|
app.translation = glm::translate(app.translation, glm::vec3(-app.speed, 0.0f, 0.0f));
|
||||||
|
app.recalc_model_mat = true;
|
||||||
|
} else if (app.event.key.keysym.sym == SDLK_RIGHT) {
|
||||||
|
app.rotation = glm::rotate(app.rotation, glm::radians(10.0f), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||||
|
app.recalc_model_mat = true;
|
||||||
|
} else if (app.event.key.keysym.sym == SDLK_LEFT) {
|
||||||
|
app.rotation = glm::rotate(app.rotation, glm::radians(-10.0f), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||||
|
app.recalc_model_mat = true;
|
||||||
|
} else if (app.event.key.keysym.sym == SDLK_r) {
|
||||||
|
app.scale = glm::scale(app.scale, glm::vec3(1.2f));
|
||||||
|
app.recalc_model_mat = true;
|
||||||
|
} else if (app.event.key.keysym.sym == SDLK_e) {
|
||||||
|
app.scale = glm::scale(app.scale, glm::vec3(0.8f));
|
||||||
|
app.recalc_model_mat = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (app.recalc_model_mat) {
|
||||||
|
app.recalc_model_mat = false;
|
||||||
|
app.model_mat = app.translation * app.rotation * app.scale;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user