polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

render.c (2128B)


      1 
      2 #include <assert.h>
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 
      6 #include "gl.h"
      7 #include "engine.h"
      8 #include "mat4.h"
      9 #include "vec3.h"
     10 #include "vbo.h"
     11 #include "shader.h"
     12 #include "geometry.h"
     13 #include "debug.h"
     14 #include "render.h"
     15 #include "skybox.h"
     16 #include "util.h"
     17 
     18 
     19 
     20 //    v6----- v5
     21 //   /|      /|
     22 //  v1------v0|
     23 //  | |     | |
     24 //  | |v7---|-|v4
     25 //  |/      |/
     26 //  v2------v3
     27 
     28 static const GLfloat cube_vertices[] = {
     29   0.5, 0.5, 0.5,  -0.5, 0.5, 0.5,  -0.5,-0.5, 0.5,   0.5,-0.5, 0.5,    // v0-v1-v2-v3 front
     30   0.5, 0.5,-0.5,   0.5, 0.5, 0.5,   0.5,-0.5, 0.5,   0.5,-0.5,-0.5,    // v5-v0-v3-v4 right
     31   -0.5, 0.5, 0.5,   0.5, 0.5, 0.5,   0.5, 0.5,-0.5,  -0.5, 0.5,-0.5,   // v1-v0-v5-v6 top
     32   -0.5, 0.5, 0.5,  -0.5, 0.5,-0.5,  -0.5,-0.5,-0.5,  -0.5,-0.5, 0.5,   // v1-v6-v7-v2 left
     33   0.5,-0.5, 0.5,  -0.5,-0.5, 0.5,  -0.5,-0.5,-0.5,   0.5,-0.5,-0.5,    // v3-v2-v7-v4 bottom
     34  -0.5, 0.5,-0.5,   0.5, 0.5,-0.5,   0.5,-0.5,-0.5,  -0.5,-0.5,-0.5     // v4-v7-v6-v5 back
     35 };
     36 
     37 
     38 static const GLfloat cube_normals[] = {
     39   0, 0, 1,   0, 0, 1,   0, 0, 1,   0, 0, 1, // front
     40   1, 0, 0,   1, 0, 0,   1, 0, 0,   1, 0, 0, // right
     41   0, 1, 0,   0, 1, 0,   0, 1, 0,   0, 1, 0, // top
     42  -1, 0, 0,  -1, 0, 0,  -1, 0, 0,  -1, 0, 0, // left
     43   0,-1, 0,   0,-1, 0,   0,-1, 0,   0,-1, 0, // bottom
     44   0, 0,-1,   0, 0,-1,   0, 0,-1,   0, 0,-1  // back
     45 };
     46 
     47 
     48 static const GLushort cube_indices[] = {
     49    0, 1, 2,   0, 2, 3,    // front
     50    4, 5, 6,   4, 6, 7,    // right
     51    8, 9,10,   8,10,11,    // top
     52   12,13,14,  12,14,15,    // left
     53   16,17,18,  16,18,19,    // bottom
     54   20,21,22,  20,22,23
     55 };
     56 
     57 void init_gl()
     58 {
     59     glEnable(GL_DEPTH_TEST);
     60     glEnable(GL_CULL_FACE);
     61     glCullFace(GL_BACK);
     62 
     63     glEnable(GL_MULTISAMPLE);
     64     check_gl();
     65 }
     66 
     67 
     68 void recalc_normals(GLint nm_uniform, mat4 *model_view, mat4 *normal)
     69 {
     70 	mat4_inverse(model_view, normal);
     71 	mat4_transpose(normal, normal);
     72 	/* mat4_copy(model_view, normal); */
     73 	glUniformMatrix4fv(nm_uniform, 1, 0, normal);
     74 }
     75 
     76 static void gamma_correct(float *c, float *d) {
     77     float gamma = 1.0/2.2;
     78     d[0] = powf(c[0], gamma);
     79     d[1] = powf(c[1], gamma);
     80     d[2] = powf(c[2], gamma);
     81 }
     82