Transformation matrices

This commit is contained in:
2022-02-01 21:44:13 +01:00
parent cc2801dcc8
commit f211508e15
5 changed files with 73 additions and 2 deletions

View File

@@ -1,5 +1,15 @@
#include "Input.h"
extern float pos[3];
/* 0: escape
* 1: W
* 2: S
* 3:
*/
static bool pressed[5];
void key_pressed(GLFWwindow* window, int key, int scancode, int action, int mods){
if ( key == GLFW_KEY_ESCAPE && action == GLFW_PRESS){
glfwSetWindowShouldClose(window, true);
@@ -7,5 +17,26 @@ void key_pressed(GLFWwindow* window, int key, int scancode, int action, int mods
if ( key == GLFW_KEY_Q && action == GLFW_PRESS ){
glfwSetWindowShouldClose(window, true);
}
if ( key == GLFW_KEY_W && action == GLFW_PRESS ){
pressed[1] = true;
}
if ( key == GLFW_KEY_W && action == GLFW_RELEASE ){
pressed[1] = false;
}
if ( key == GLFW_KEY_S && action == GLFW_PRESS ){
pressed[2] = true;
}
if ( key == GLFW_KEY_S && action == GLFW_RELEASE ) {
pressed[2] = false;
}
}
void processInput(){
if (pressed[1] == true){
pos[1] += 0.01f;
}
if (pressed[2] == true){
pos[1] -= 0.01f;
}
}

View File

@@ -8,5 +8,6 @@
void key_pressed();
void processInput();
#endif

View File

@@ -9,6 +9,9 @@
#include "Input.h"
float pos[3] = {0.0f, 0.0f, 0.0f};
int main() {
GLFWwindow* window;
window = initOpenGL();
@@ -23,8 +26,8 @@ int main() {
};
// CreateShader
char* vertex1 = "../assets/shaders/vertex3.glsl\0";
char* fragment1 = "../assets/shaders/fragment3.glsl\0";
char* vertex1 = "../assets/shaders/vertex4.glsl\0";
char* fragment1 = "../assets/shaders/fragment4.glsl\0";
unsigned int shader1;
shader1 = createShader(vertex1, fragment1);
@@ -49,6 +52,7 @@ int main() {
unsigned int texture;
texture = createTexture("../assets/textures/wall.jpg");
while(!glfwWindowShouldClose(window)) {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
@@ -58,10 +62,19 @@ int main() {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glBindVertexArray(VAO);
mat4x4 transform;
mat4x4_identity(transform);
mat4x4_translate(transform, pos[0], pos[1], pos[2]);
unsigned int transformLoc = glGetUniformLocation(shader1, "transform");
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, *transform);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
glfwPollEvents();
processInput();
}
glDeleteVertexArrays(1, &VAO);