fixed missing cube vertex and implemented perspective projection
authormatthew <matthew@owens.tech>
Tue, 18 Sep 2018 17:15:30 +0000 (18:15 +0100)
committermatthew <matthew@owens.tech>
Tue, 18 Sep 2018 17:15:30 +0000 (18:15 +0100)
gl/cube.c
gl/cube.h
gl/main.c
gl/shader.c
shaders/crate/crate.vert

index aa46a79..4ddbab3 100644 (file)
--- a/gl/cube.c
+++ b/gl/cube.c
@@ -6,84 +6,39 @@
 #include <stb/stb_image.h>
 #include "err.h"
 
-/*
-static float vertices[] = {
-       -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
-        0.5f, -0.5f, -0.5f,  1.0f, 0.0f,
-        0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
-        0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
-       -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
-       -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
-
-       -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
-        0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
-        0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
-        0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
-       -0.5f,  0.5f,  0.5f,  0.0f, 1.0f,
-       -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
-
-       -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
-       -0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
-       -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
-       -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
-       -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
-       -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
-
-        0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
-        0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
-        0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
-        0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
-        0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
-        0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
-
-       -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
-        0.5f, -0.5f, -0.5f,  1.0f, 1.0f,
-        0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
-        0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
-       -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
-       -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
-
-       -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
-        0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
-        0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
-        0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
-       -0.5f,  0.5f,  0.5f,  0.0f, 0.0f,
-       -0.5f,  0.5f, -0.5f,  0.0f, 1.0f
-};*/
-
 static GLfloat vertices[] = {
-       //  X     Y     Z       U     V
+       //X     Y     Z      U     V
        // bottom
-       -1.0f,-1.0f,-1.0f,   0.0f, 0.0f,
-       1.0f,-1.0f,-1.0f,   1.0f, 0.0f,
-       -1.0f,-1.0f, 1.0f,   0.0f, 1.0f,
-       1.0f,-1.0f,-1.0f,   1.0f, 0.0f,
-       1.0f,-1.0f, 1.0f,   1.0f, 1.0f,
-       -1.0f,-1.0f, 1.0f,   0.0f, 1.0f,
+       -1.0f,-1.0f,-1.0f,  0.0f, 0.0f,
+        1.0f,-1.0f,-1.0f,  1.0f, 0.0f,
+       -1.0f,-1.0f, 1.0f,  0.0f, 1.0f,
+        1.0f,-1.0f,-1.0f,  1.0f, 0.0f,
+        1.0f,-1.0f, 1.0f,  1.0f, 1.0f,
+       -1.0f,-1.0f, 1.0f,  0.0f, 1.0f,
 
        // top
        -1.0f, 1.0f,-1.0f,   0.0f, 0.0f,
        -1.0f, 1.0f, 1.0f,   0.0f, 1.0f,
-       1.0f, 1.0f,-1.0f,   1.0f, 0.0f,
-       1.0f, 1.0f,-1.0f,   1.0f, 0.0f,
+        1.0f, 1.0f,-1.0f,   1.0f, 0.0f,
+        1.0f, 1.0f,-1.0f,   1.0f, 0.0f,
        -1.0f, 1.0f, 1.0f,   0.0f, 1.0f,
-       1.0f, 1.0f, 1.0f,   1.0f, 1.0f,
+        1.0f, 1.0f, 1.0f,   1.0f, 1.0f,
 
        // front
        -1.0f,-1.0f, 1.0f,   1.0f, 0.0f,
-       1.0f,-1.0f, 1.0f,   0.0f, 0.0f,
+        1.0f,-1.0f, 1.0f,   0.0f, 0.0f,
        -1.0f, 1.0f, 1.0f,   1.0f, 1.0f,
-       1.0f,-1.0f, 1.0f,   0.0f, 0.0f,
-       1.0f, 1.0f, 1.0f,   0.0f, 1.0f,
+        1.0f,-1.0f, 1.0f,   0.0f, 0.0f,
+        1.0f, 1.0f, 1.0f,   0.0f, 1.0f,
        -1.0f, 1.0f, 1.0f,   1.0f, 1.0f,
 
        // back
        -1.0f,-1.0f,-1.0f,   0.0f, 0.0f,
        -1.0f, 1.0f,-1.0f,   0.0f, 1.0f,
-       1.0f,-1.0f,-1.0f,   1.0f, 0.0f,
-       1.0f,-1.0f,-1.0f,   1.0f, 0.0f,
+        1.0f,-1.0f,-1.0f,   1.0f, 0.0f,
+        1.0f,-1.0f,-1.0f,   1.0f, 0.0f,
        -1.0f, 1.0f,-1.0f,   0.0f, 1.0f,
-       1.0f, 1.0f,-1.0f,   1.0f, 1.0f,
+        1.0f, 1.0f,-1.0f,   1.0f, 1.0f,
 
        // left
        -1.0f,-1.0f, 1.0f,   0.0f, 1.0f,
@@ -99,10 +54,11 @@ static GLfloat vertices[] = {
        1.0f, 1.0f,-1.0f,   0.0f, 0.0f,
        1.0f,-1.0f, 1.0f,   1.0f, 1.0f,
        1.0f, 1.0f,-1.0f,   0.0f, 0.0f,
+       1.0f, 1.0f, 1.0f,   0.0f, 1.0f
 };
 
 static bool initilised = false;
-static unsigned int vbo, vao, transformLoc;
+static unsigned int vbo, vao, transformLoc, cameraLoc;
 static crpgShader *shader = NULL;
 static crpgTexture *tex = NULL;
 
@@ -140,6 +96,15 @@ void initShader(Cube_t *ct)
        glUniformMatrix4fv(transformLoc, 1, GL_FALSE, &ct->transform);
 }
 
+void crpgCubeSetCamera(crpgCube *c, mat4_t *cam)
+{
+       Cube_t *ct = (Cube_t *) c;
+       crpgShaderUse(shader);
+       unsigned int loc = glGetUniformLocation(crpgShaderID(shader), "camera");
+       glUniformMatrix4fv(loc, 1, GL_FALSE, cam);
+       crpgShaderUse(0);
+}
+
 static bool printed = false;
 void crpgCubeRender(crpgCube *c)
 {
index 7ee7804..2d144c1 100644 (file)
--- a/gl/cube.h
+++ b/gl/cube.h
@@ -10,4 +10,5 @@ void crpgCubeScale(crpgCube *c, vec3_t scale);
 void crpgCubeRotation(crpgCube *c, float rot, vec3_t axis);
 void crpgCubeRender(crpgCube *c);
 void crpgCubeFree(crpgCube *c);
+void crpgCubeSetCamera(crpgCube *c, mat4_t *cam);
 #endif//CRPGCUBE_H
index e67f0db..072e90a 100644 (file)
--- a/gl/main.c
+++ b/gl/main.c
@@ -38,7 +38,7 @@ static float blendVal = 0.2f;
 static crpgShader *shader = NULL;
 static crpgTexture *tex[2];
 static crpgCube *cube = NULL;
-static mat4_t projection, transform, world_to_screen;
+static mat4_t projection, transform, camera, world_to_screen;
 static vec3_t from, to, up, screenSpace, worldSpace;
 
 static void initShapes()
@@ -87,19 +87,25 @@ static void initShapes()
        glUniformMatrix4fv(transformLoc, 1, GL_FALSE, &transform);
 
        cube = crpgCubeNew();
-       //crpgCubePosition(cube, vec3(0.5, 0.5, 0.f));
+       //crpgCubePosition(cube, vec3(1.5, 0.5, 0.f));
 }
 
 static void initView()
 {
        projection = m4_perspective(60, (float)screen_width/(float)screen_height, 1, 10);
-       from = vec3(0, 0.5, 2);
+       //from = vec3(0, 0.5, 2);
+       from = vec3(3,3,3);
        to = vec3(0,0,0);
        up = vec3(0,1,0);
-       transform = m4_look_at(from, to, up);
+       camera = m4_look_at(from, to, up);
        worldSpace = vec3(1, 1, -1);
-       world_to_screen = m4_mul(projection, transform);
+       world_to_screen = m4_mul(projection, camera);
        screenSpace = m4_mul_pos(world_to_screen, worldSpace);
+
+       crpgCubeSetCamera(cube, &world_to_screen);
+
+       glEnable(GL_DEPTH_TEST);
+       glDepthFunc(GL_LESS);
 }
 
 static bool init()
@@ -164,7 +170,7 @@ static void update()
 static void render()
 {
        glClearColor(0.2, 0.3, 0.3, 1.0);
-       glClear(GL_COLOR_BUFFER_BIT);
+       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
        crpgShaderUse(shader);
 
index d3c8aa5..76d99ea 100644 (file)
@@ -128,8 +128,12 @@ void crpgShaderFree(crpgShader *s)
 
 void crpgShaderUse(crpgShader *s)
 {
-       Shader_t *st = (Shader_t *)s;
-       glUseProgram(st->id);
+       if(s == NULL){
+               glUseProgram(0);
+       } else {
+               Shader_t *st = (Shader_t *)s;
+               glUseProgram(st->id);
+       }
 }
 
 void crpgShaderSetBool(crpgShader *s, const char *name, bool val)
index ea04ea3..75f6f25 100644 (file)
@@ -4,9 +4,12 @@ layout (location = 1) in vec2 aTex;
 
 out vec2 vTex;
 uniform mat4 transform;
+uniform mat4 camera;
 
 void main()
 {
-       gl_Position = transform * vec4(aPos, 1.0);
+       //gl_Position = transform * vec4(aPos, 1.0);
+       //gl_Position = camera * transform * vec4(aPos, 1.0);
+       gl_Position = camera * vec4(aPos, 1.0);
        vTex = aTex;
 }