From 318c00039192b65ef7d04a473f3c48c27ab0a7f2 Mon Sep 17 00:00:00 2001
From: Abdelrahman <said.abdelrahman89@gmail.com>
Date: Sun, 1 Dec 2024 01:09:47 +0000
Subject: [PATCH] Complete transformation and coordinate system chapters

---
 shaders/frag.glsl |  1 -
 shaders/vert.glsl | 11 +++---
 src/main.cc       | 90 +++++++++++++++++++++++++++++++++++------------
 3 files changed, 72 insertions(+), 30 deletions(-)

diff --git a/shaders/frag.glsl b/shaders/frag.glsl
index 12261d8..a8b171d 100644
--- a/shaders/frag.glsl
+++ b/shaders/frag.glsl
@@ -1,6 +1,5 @@
 #version 330 core
 
-in vec3 vert_color;
 in vec2 tex_coords;
 
 out vec4 color;
diff --git a/shaders/vert.glsl b/shaders/vert.glsl
index 671db63..687ffbd 100644
--- a/shaders/vert.glsl
+++ b/shaders/vert.glsl
@@ -1,16 +1,15 @@
 #version 330 core
 
 layout(location=0) in vec3 position;
-layout(location=1) in vec3 color;
-layout(location=2) in vec2 uv;
+layout(location=1) in vec2 uv;
 
-uniform mat4 matrix;
+uniform mat4 model;
+uniform mat4 view;
+uniform mat4 projection;
 
-out vec3 vert_color;
 out vec2 tex_coords;
 
 void main() {
   tex_coords  = uv;
-  vert_color  = color;
-  gl_Position = matrix * vec4(position, 1.0);
+  gl_Position = projection * view * model * vec4(position, 1.0);
 };
diff --git a/src/main.cc b/src/main.cc
index af818a2..fa90384 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -1,4 +1,5 @@
 // GLAD
+#include "SDL_timer.h"
 #include "glad/glad.h"
 
 // GLM
@@ -90,22 +91,42 @@ int main() {
 
   glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
 
+  glEnable(GL_DEPTH_TEST);
+
   std::vector<GLfloat> vertices = {
-    -0.5f, -0.5f, 0.0f, // position
-     1.0f,  0.0f, 0.0f, // colour
-     0.0f,  0.0f,       // uv
-     0.5f, -0.5f, 0.0f, // position
-     0.0f,  0.0f, 1.0f, // colour
-     1.0f,  0.0f,       // uv
-    -0.5f,  0.5f, 0.0f, // position
-     0.0f,  1.0f, 0.0f, // colour
-     0.0f,  1.0f,       // uv
-     0.5f,  0.5f, 0.0f, // position
-     0.5f,  0.5f, 0.5f, // colour
-     1.0f,  1.0f,       // uv
+     0.5f, -0.5f,  0.5f,  0.0f, 0.0f, // position, uv
+    -0.5f,  0.5f,  0.5f,  1.0f, 0.0f, // position, uv
+     0.5f,  0.5f,  0.5f,  1.0f, 0.0f, // position, uv
+     0.5f, -0.5f,  0.5f,  1.0f, 0.0f, // position, uv
+    -0.5f, -0.5f,  0.5f,  0.0f, 0.0f, // position, uv
+     0.5f, -0.5f, -0.5f,  1.0f, 0.0f, // position, uv
+    -0.5f,  0.5f, -0.5f,  1.0f, 1.0f, // position, uv
+    -0.5f,  0.5f, -0.5f,  0.0f, 1.0f, // position, uv
+    -0.5f,  0.5f,  0.5f,  0.0f, 1.0f, // position, uv
+     0.5f,  0.5f, -0.5f,  1.0f, 1.0f, // position, uv
+     0.5f, -0.5f, -0.5f,  1.0f, 1.0f, // position, uv
+    -0.5f,  0.5f,  0.5f,  0.0f, 0.0f, // position, uv
+     0.5f,  0.5f,  0.5f,  1.0f, 1.0f, // position, uv
+    -0.5f, -0.5f, -0.5f,  0.0f, 1.0f, // position, uv
+    -0.5f, -0.5f, -0.5f,  0.0f, 0.0f, // position, uv
+     0.5f, -0.5f, -0.5f,  0.0f, 1.0f, // position, uv
+    -0.5f,  0.5f, -0.5f,  0.0f, 1.0f  // position, uv
   };
 
-  std::vector<GLuint> indices = {0, 1, 2, 2, 1, 3};
+  std::vector<GLuint> indices = {
+    14, 5, 9,
+    9, 7, 14,
+    4, 3, 12,
+    12, 8, 4,
+    1, 6, 13,
+    13, 4, 1,
+    2, 9, 15,
+    15, 0, 2,
+    13, 10, 3,
+    3, 4, 13,
+    7, 9, 2,
+    2, 11, 16
+  };
 
   GLuint vao = 0;
   glGenVertexArrays(1, &vao);
@@ -121,12 +142,10 @@ int main() {
   glBindBuffer(GL_ARRAY_BUFFER, vbo);
   glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), vertices.data(), GL_STATIC_DRAW);
 
-  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void *)0);
+  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (void *)0);
   glEnableVertexAttribArray(0);
-  glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void *)(3 * sizeof(GLfloat)));
+  glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (void *)(3 * sizeof(GLfloat)));
   glEnableVertexAttribArray(1);
-  glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void *)(6 * sizeof(GLfloat)));
-  glEnableVertexAttribArray(2);
 
   glBindVertexArray(0);
   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
@@ -175,11 +194,28 @@ int main() {
 
   float texture_mix_factor = 0.2f;
 
-  glm::mat4 mat = glm::mat4(1.0f);
-  mat = glm::rotate(mat, glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f));
-  mat = glm::scale(mat, glm::vec3(0.5f, 0.5f, 0.5f));
+  glm::vec3 cube_positions[] = {
+      glm::vec3( 0.0f,  0.0f,  0.0f), 
+      glm::vec3( 5.0f,  8.0f, -25.0f), 
+      glm::vec3(-4.5f, -5.2f, -12.5f),  
+      glm::vec3(-6.8f, -5.0f, -22.3f),  
+      glm::vec3( 5.4f, -3.4f, -13.5f),  
+      glm::vec3(-4.7f,  6.0f, -17.5f),  
+      glm::vec3( 4.3f, -5.0f, -12.5f),  
+      glm::vec3( 4.5f,  5.0f, -12.5f), 
+      glm::vec3( 4.5f,  3.2f, -11.5f), 
+      glm::vec3(-4.3f,  4.0f, -11.5f)  
+  };
 
-  glUniformMatrix4fv(glGetUniformLocation(main_shader.program, "matrix"), 1, GL_FALSE, glm::value_ptr(mat));
+  glm::mat4 model = glm::mat4(1.0f);
+
+  glm::mat4 view = glm::mat4(1.0f);
+  view = glm::translate(view, glm::vec3(0.0f, 0.0f, -4.0f));
+
+  glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)WINDOW_WIDTH / (float)WINDOW_HEIGHT, 0.1f, 100.0f);
+
+  glUniformMatrix4fv(glGetUniformLocation(main_shader.program, "view"), 1, GL_FALSE, glm::value_ptr(view));
+  glUniformMatrix4fv(glGetUniformLocation(main_shader.program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
 
   bool running = true;
   SDL_Event event = {};
@@ -212,12 +248,13 @@ int main() {
       }
     }
 
+
     texture_mix_factor = texture_mix_factor < 0.0f ?
                          0.0f : texture_mix_factor > 1.0f ?
                          1.0f : texture_mix_factor;
 
     glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
-    glClear(GL_COLOR_BUFFER_BIT);
+    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
     main_shader.activate();
     glUniform1f(glGetUniformLocation(main_shader.program, "mix_factor"), texture_mix_factor);
@@ -226,7 +263,14 @@ int main() {
     glActiveTexture(GL_TEXTURE1);
     glBindTexture(GL_TEXTURE_2D, textures[1]);
     glBindVertexArray(vao);
-    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, NULL);
+
+    for (int i = 0; i < sizeof(cube_positions) / sizeof(cube_positions[0]); ++i) {
+      float angle = 20.0f * i;
+      model = glm::translate(glm::mat4(1.0f), cube_positions[i]);
+      model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
+      glUniformMatrix4fv(glGetUniformLocation(main_shader.program, "model"), 1, GL_FALSE, glm::value_ptr(model));
+      glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, (void *)0);
+    }
 
     SDL_GL_SwapWindow(window);
   }