Transformation matrices
This commit is contained in:
11
assets/shaders/fragment4.glsl
Normal file
11
assets/shaders/fragment4.glsl
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#version 330 core
|
||||||
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
in vec3 ourColor;
|
||||||
|
in vec2 TextureCords;
|
||||||
|
|
||||||
|
uniform sampler2D Texture;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
FragColor = texture(Texture, TextureCords) * vec4(ourColor, 1.0);
|
||||||
|
}
|
||||||
15
assets/shaders/vertex4.glsl
Normal file
15
assets/shaders/vertex4.glsl
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#version 330 core
|
||||||
|
layout (location =0 ) in vec3 aPos;
|
||||||
|
layout (location = 1) in vec3 aColor;
|
||||||
|
layout (location = 2) in vec2 aTextureCords;
|
||||||
|
|
||||||
|
out vec3 ourColor;
|
||||||
|
out vec2 TextureCords;
|
||||||
|
|
||||||
|
uniform mat4 transform;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
gl_Position = transform * vec4(aPos, 1.0);
|
||||||
|
ourColor = aColor;
|
||||||
|
TextureCords = aTextureCords;
|
||||||
|
}
|
||||||
31
src/Input.c
31
src/Input.c
@@ -1,5 +1,15 @@
|
|||||||
#include "Input.h"
|
#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){
|
void key_pressed(GLFWwindow* window, int key, int scancode, int action, int mods){
|
||||||
if ( key == GLFW_KEY_ESCAPE && action == GLFW_PRESS){
|
if ( key == GLFW_KEY_ESCAPE && action == GLFW_PRESS){
|
||||||
glfwSetWindowShouldClose(window, true);
|
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 ){
|
if ( key == GLFW_KEY_Q && action == GLFW_PRESS ){
|
||||||
glfwSetWindowShouldClose(window, true);
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,5 +8,6 @@
|
|||||||
|
|
||||||
void key_pressed();
|
void key_pressed();
|
||||||
|
|
||||||
|
void processInput();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
17
src/main.c
17
src/main.c
@@ -9,6 +9,9 @@
|
|||||||
#include "Input.h"
|
#include "Input.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
float pos[3] = {0.0f, 0.0f, 0.0f};
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
GLFWwindow* window;
|
GLFWwindow* window;
|
||||||
window = initOpenGL();
|
window = initOpenGL();
|
||||||
@@ -23,8 +26,8 @@ int main() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// CreateShader
|
// CreateShader
|
||||||
char* vertex1 = "../assets/shaders/vertex3.glsl\0";
|
char* vertex1 = "../assets/shaders/vertex4.glsl\0";
|
||||||
char* fragment1 = "../assets/shaders/fragment3.glsl\0";
|
char* fragment1 = "../assets/shaders/fragment4.glsl\0";
|
||||||
unsigned int shader1;
|
unsigned int shader1;
|
||||||
shader1 = createShader(vertex1, fragment1);
|
shader1 = createShader(vertex1, fragment1);
|
||||||
|
|
||||||
@@ -49,6 +52,7 @@ int main() {
|
|||||||
unsigned int texture;
|
unsigned int texture;
|
||||||
texture = createTexture("../assets/textures/wall.jpg");
|
texture = createTexture("../assets/textures/wall.jpg");
|
||||||
|
|
||||||
|
|
||||||
while(!glfwWindowShouldClose(window)) {
|
while(!glfwWindowShouldClose(window)) {
|
||||||
|
|
||||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||||
@@ -58,10 +62,19 @@ int main() {
|
|||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
glBindTexture(GL_TEXTURE_2D, texture);
|
glBindTexture(GL_TEXTURE_2D, texture);
|
||||||
glBindVertexArray(VAO);
|
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);
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||||
|
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
processInput();
|
||||||
}
|
}
|
||||||
|
|
||||||
glDeleteVertexArrays(1, &VAO);
|
glDeleteVertexArrays(1, &VAO);
|
||||||
|
|||||||
Reference in New Issue
Block a user