Add compile script and make the triangle move based off of a Matrix 4

This commit is contained in:
Nathan Chapman 2025-07-02 08:47:50 -06:00
parent 56cf609849
commit d10cb1cdc5
2 changed files with 27 additions and 8 deletions

13
compile Executable file
View File

@ -0,0 +1,13 @@
#!/bin/sh
if [ "$1" = "--clean" ]; then
echo "Cleaning build directory..."
rm -rf build/*
fi
(
cd build || exit 1
cmake ..
make
)

View File

@ -3,6 +3,9 @@
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <cmath>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
// Window dimensions
const GLint WIDTH = 800, HEIGHT = 600;
@ -13,10 +16,10 @@ static const char *vertex_shader = " \n\
\n\
layout (location = 0) in vec3 pos; \n\
\n\
uniform float x_move; \n\
uniform mat4 model; \n\
\n\
void main() { \n\
gl_Position = vec4(pos.x + x_move, pos.y, pos.z, 1.0); \n\
gl_Position = model * vec4(pos.x, pos.y, pos.z, 1.0); \n\
}";
// Fragment shader
@ -54,7 +57,7 @@ bool add_shader(GLuint *program, const char *shader_code, GLenum shader_type) {
return true;
}
bool compile_shaders(GLuint *shader_program, GLuint *x_move) {
bool compile_shaders(GLuint *shader_program, GLuint *uniform_model) {
*shader_program = glCreateProgram();
if (!shader_program) {
@ -86,7 +89,7 @@ bool compile_shaders(GLuint *shader_program, GLuint *x_move) {
return false;
}
*x_move = glGetUniformLocation(*shader_program, "x_move");
*uniform_model = glGetUniformLocation(*shader_program, "model");
return true;
}
@ -114,11 +117,11 @@ void create_triangle(GLuint *VAO, GLuint *VBO) {
}
int main() {
GLuint VAO, VBO, shader_program, x_move;
GLuint VAO, VBO, shader_program, uniform_model;
bool direction = true;
float tri_offset = 0.0f;
float tri_max_offset = 0.7f;
float tri_increment = 0.005f;
float tri_increment = 0.05f;
// Initialize GLFW
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_WAYLAND);
@ -177,7 +180,7 @@ int main() {
// Create our triangle
create_triangle(&VAO, &VBO);
compile_shaders(&shader_program, &x_move);
compile_shaders(&shader_program, &uniform_model);
// Loop until window is closed
while (!glfwWindowShouldClose(window)) {
@ -200,7 +203,10 @@ int main() {
glUseProgram(shader_program);
glUniform1f(x_move, tri_offset);
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(tri_offset, 0.0f, 0.0f));
glUniformMatrix4fv(uniform_model, 1, GL_FALSE, glm::value_ptr(model));
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);