Browse Source

Implement simple Pog lighting

rexim 4 years ago
parent
commit
ee7a4998e3
8 changed files with 60 additions and 26 deletions
  1. 1 1
      README.md
  2. BIN
      images/ayaya.png
  3. 2 1
      scene.conf
  4. 6 1
      shaders/main.frag
  5. 15 6
      shaders/main.vert
  6. 12 13
      src/geo.c
  7. 0 4
      src/geo.h
  8. 24 0
      src/main.c

+ 1 - 1
README.md

@@ -29,4 +29,4 @@ $ ./kidito
 - [x] Render the cube mesh with perspective projection
 - [x] Texture the cube
 - [x] Fog
-- [ ] Simple lighting (the classical one with normals and stuff)
+- [x] Simple lighting (the classical one with normals and stuff)

BIN
images/ayaya.png


+ 2 - 1
scene.conf

@@ -7,4 +7,5 @@ vert_shader = ./shaders/main.vert
 # texture = ./images/poggers.png
 # texture = ./images/sleep.png
 # texture = ./images/jebaited.png
-texture = ./images/rms.png
+# texture = ./images/rms.png
+texture = ./images/ayaya.png

+ 6 - 1
shaders/main.frag

@@ -7,6 +7,7 @@ uniform float time;
 
 in vec2 uv;
 in vec4 vertex;
+in vec4 normal;
 out vec4 frag_color;
 
 #define FOG_MIN 1.0
@@ -20,8 +21,12 @@ float fog_factor(float d)
 }
 
 void main(void) {
+    vec3 light_source = vec3(0.0, 0.0, 00.0);
+    float a = abs(dot(normalize(light_source - vertex.xyz), normal.xyz));
+    vec4 t = texture(pog, uv);
+
     frag_color = mix(
-        texture(pog, uv),
+        vec4(t.xyz * vec3(1.0, 1.0, 1.0) * a, 1.0),
         vec4(0.0, 0.0, 0.0, 1.0),
         fog_factor(length(vertex)));
 }

+ 15 - 6
shaders/main.vert

@@ -7,9 +7,11 @@ uniform vec2 resolution;
 
 layout(location = 0) in vec4 vertex_position;
 layout(location = 1) in vec2 vertex_uv;
+layout(location = 2) in vec4 vertex_normal;
 
 out vec2 uv;
 out vec4 vertex;
+out vec4 normal;
 
 mat4 mat4_translate(vec3 dir)
 {
@@ -66,22 +68,29 @@ void main(void)
 
     mat4 camera = (
         mat4_translate(vec3(0.0, 0.0, -30.0 + 30.0 * sin(time))) *
-        mat4_scale(vec3(25.0, 25.0, 25.0)) *
         mat4_rotate_z(time) *
         mat4_rotate_y(time) *
+        mat4_translate(vertex_normal.xyz * 20.0 * ((sin(time) + 1.0) / 2.0)) *
+        mat4_scale(vec3(25.0, 25.0, 25.0)) *
         mat4_translate(vec3(-0.5, -0.5, -0.5)) *
         mat4(1.0)
     );
 
-    gl_Position = (
-        mat4_perspective(fovy, aspect, 1.0, 500.0) *
+    vec4 camera_pos = (
         camera *
         vertex_position
     );
 
+    gl_Position = (
+        mat4_perspective(fovy, aspect, 1.0, 500.0) *
+        camera_pos
+    );
+
     uv = vertex_uv;
-    vertex = (
-        camera *
-        vertex_position
+    vertex = camera_pos;
+    normal = (
+        mat4_rotate_z(time) *
+        mat4_rotate_y(time) *
+        vertex_normal
     );
 }

+ 12 - 13
src/geo.c

@@ -51,35 +51,34 @@ void generate_cube_mesh(V4 mesh[TRIS_PER_CUBE][TRI_VERTICES],
                     // Mesh
                     {
 
-                        mesh[count + tri][vert].cs[A] = (float) (strip_index & 1);
-                        mesh[count + tri][vert].cs[B] = (float) (strip_index >> 1);
-                        mesh[count + tri][vert].cs[C] = (float) pair_comp_index;;
-                        mesh[count + tri][vert].cs[W] = 1.0f;
+                        mesh[count][vert].cs[A] = (float) (strip_index & 1);
+                        mesh[count][vert].cs[B] = (float) (strip_index >> 1);
+                        mesh[count][vert].cs[C] = (float) pair_comp_index;;
+                        mesh[count][vert].cs[W] = 1.0f;
                     }
 
                     // Color
                     {
-                        colors[count + tri][vert] =
+                        colors[count][vert] =
                             face_colors[face_pair_index][pair_comp_index];
                     }
 
                     // UVs
                     {
-                        uvs[count + tri][vert].cs[X] = (float) (strip_index & 1);
-                        uvs[count + tri][vert].cs[Y] = (float) (strip_index >> 1);
+                        uvs[count][vert].cs[X] = (float) (strip_index & 1);
+                        uvs[count][vert].cs[Y] = (float) (strip_index >> 1);
                     }
 
                     // Normals
                     {
-                        normals[count + tri][vert].cs[A] = 0.0f;
-                        normals[count + tri][vert].cs[B] = 0.0f;
-                        normals[count + tri][vert].cs[Z] = (float) (2 * (int) pair_comp_index - 1);
-                        normals[count + tri][vert].cs[W] = 1.0f;
+                        normals[count][vert].cs[A] = 0.0f;
+                        normals[count][vert].cs[B] = 0.0f;
+                        normals[count][vert].cs[C] = (float) (2 * (int) pair_comp_index - 1);
+                        normals[count][vert].cs[W] = 1.0f;
                     }
                 }
+                count += 1;
             }
-
-            count += TRIS_PER_FACE;
         }
     }
 

+ 0 - 4
src/geo.h

@@ -40,10 +40,6 @@ V4 v4_scale(V4 a, float s);
 #define TRIS_PER_FACE 2
 #define TRIS_PER_CUBE (CUBE_FACES * TRIS_PER_FACE)
 
-void generate_cube_face_mesh(size_t a, size_t b,
-                             size_t c, float cv,
-                             size_t d, float dv,
-                             V4 mesh[TRIS_PER_FACE][TRI_VERTICES]);
 void generate_cube_mesh(V4 mesh[TRIS_PER_CUBE][TRI_VERTICES],
                         RGBA colors[TRIS_PER_CUBE][TRI_VERTICES],
                         V2 uvs[TRIS_PER_CUBE][TRI_VERTICES],

+ 24 - 0
src/main.c

@@ -387,6 +387,12 @@ int main()
 
     generate_cube_mesh(mesh, colors, uvs, normals);
 
+    for (size_t tri = 0; tri < TRIS_PER_CUBE; ++tri) {
+        for (size_t vert = 0; vert < TRI_VERTICES; ++vert) {
+            printf(V4_Fmt"\n", V4_Arg(normals[tri][vert]));
+        }
+    }
+
     {
         GLuint position_buffer_id;
         glGenBuffers(1, &position_buffer_id);
@@ -423,6 +429,24 @@ int main()
                               NULL);
     }
 
+    {
+        GLuint normal_buffer_id;
+        glGenBuffers(1, &normal_buffer_id);
+        glBindBuffer(GL_ARRAY_BUFFER, normal_buffer_id);
+        glBufferData(GL_ARRAY_BUFFER,
+                     sizeof(normals),
+                     normals,
+                     GL_STATIC_DRAW);
+        GLuint normal_index = 2;
+        glEnableVertexAttribArray(normal_index);
+        glVertexAttribPointer(normal_index,
+                              V4_COMPS,
+                              GL_FLOAT,
+                              GL_FALSE,
+                              0,
+                              NULL);
+    }
+
     glfwSetKeyCallback(window, key_callback);
     glfwSetFramebufferSizeCallback(window, window_size_callback);
     double prev_time = 0.0;