polyadvent

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

skybox.c (4122B)


      1 
      2 #include "skybox.h"
      3 #include "util.h"
      4 #include "texture.h"
      5 
      6 static GLfloat skybox_vertices[] = {
      7   1.0, 1.0, 1.0,  -1.0, 1.0, 1.0,  -1.0,-1.0, 1.0,   1.0,-1.0, 1.0,    // v0-v1-v2-v3 top
      8   1.0, 1.0,-1.0,   1.0, 1.0, 1.0,   1.0,-1.0, 1.0,   1.0,-1.0,-1.0,    // v5-v0-v3-v4 right
      9   -1.0, 1.0, 1.0,   1.0, 1.0, 1.0,   1.0, 1.0,-1.0,  -1.0, 1.0,-1.0,   // v1-v0-v5-v6 front
     10   -1.0, 1.0, 1.0,  -1.0, 1.0,-1.0,  -1.0,-1.0,-1.0,  -1.0,-1.0, 1.0,   // v1-v6-v7-v2 left
     11   1.0,-1.0, 1.0,  -1.0,-1.0, 1.0,  -1.0,-1.0,-1.0,   1.0,-1.0,-1.0,    // v3-v2-v7-v4 back
     12   -1.0, 1.0,-1.0,   1.0, 1.0,-1.0,   1.0,-1.0,-1.0,  -1.0,-1.0,-1.0     // v4-v7-v6-v5 bottom
     13 };
     14 
     15 
     16 static GLfloat skybox_uvs[] = {
     17   1.0, 1.0, 1.0,  -1.0, 1.0, 1.0,  -1.0,-1.0, 1.0,   1.0,-1.0, 1.0,
     18   1.0, 1.0,-1.0,   1.0, 1.0, 1.0,   1.0,-1.0, 1.0,   1.0,-1.0,-1.0,
     19   -1.0, 1.0, 1.0,   1.0, 1.0, 1.0,   1.0, 1.0,-1.0,  -1.0, 1.0,-1.0,
     20   -1.0, 1.0, 1.0,  -1.0, 1.0,-1.0,  -1.0,-1.0,-1.0,  -1.0,-1.0, 1.0,
     21   1.0,-1.0, 1.0,  -1.0,-1.0, 1.0,  -1.0,-1.0,-1.0,   1.0,-1.0,-1.0,
     22   -1.0, 1.0,-1.0,   1.0, 1.0,-1.0,   1.0,-1.0,-1.0,  -1.0,-1.0,-1.0
     23 };
     24 
     25 
     26 static u32 skybox_indices[] = {
     27   0, 1, 2,   0, 2, 3,    // top     (+z)
     28   4, 5, 6,   4, 6, 7,    // right   (+x)
     29   8, 9,10,   8, 10,11,   // front (+y)
     30   12,13,14,  12,14,15,   // left    (-x)
     31   16,17,18,  16,18,19,   // back    (-y)
     32   20,21,22,  20,22,23    // bottom  (-z)
     33 };
     34 
     35 
     36 void create_skybox(struct skybox *skybox, struct gpu_program *program) {
     37     struct shader vertex, frag;
     38     struct shader *shaders[] = {&vertex, &frag};
     39     int ok;
     40     (void)ok;
     41 
     42     node_init(&skybox->node);
     43     init_id(&skybox->model_id);
     44 
     45     skybox->program = program;
     46 
     47     struct make_geometry mkgeom;
     48     init_make_geometry(&mkgeom);
     49 
     50     mkgeom.vertices = skybox_vertices;
     51     mkgeom.indices = skybox_indices;
     52     /* geom->tex_coords = skybox_uvs; */
     53 
     54     /* geom->num_uv_components = 3; */
     55     mkgeom.num_verts = ARRAY_SIZE(skybox_vertices);
     56     mkgeom.num_indices = ARRAY_SIZE(skybox_indices);
     57 
     58     struct model *model = new_model(&skybox->model_id); assert(model);
     59     struct geometry *geom = get_geometry(&model->geom_id); assert(geom);
     60 
     61     make_buffer_geometry(&mkgeom, geom);
     62 
     63     static const char *faces[6] = {
     64       CUBEMAP("hw_sahara/sahara_rt_flip.tga"),
     65       CUBEMAP("hw_sahara/sahara_lf_flip.tga"),
     66       CUBEMAP("hw_sahara/sahara_ft_flip.tga"),
     67       CUBEMAP("hw_sahara/sahara_bk.tga"),
     68       CUBEMAP("hw_sahara/sahara_up_flip.tga"),
     69       CUBEMAP("hw_sahara/sahara_dn_flip.tga"),
     70     };
     71 
     72 
     73     /* static const char *faces[6] = { */
     74     /*   CUBEMAP("ame_siege/siege_rt_flip.tga"), */
     75     /*   CUBEMAP("ame_siege/siege_lf_flip.tga"), */
     76     /*   CUBEMAP("ame_siege/siege_ft_flip.tga"), */
     77     /*   CUBEMAP("ame_siege/siege_bk.tga"), */
     78     /*   CUBEMAP("ame_siege/siege_up_flip.tga"), */
     79     /*   CUBEMAP("ame_siege/siege_dn_flip.tga"), */
     80     /* }; */
     81 
     82     model->texture = create_cubemap(faces);
     83 
     84     make_shader(GL_VERTEX_SHADER, SHADER("skybox.v.glsl"), &vertex);
     85     check_gl();
     86     make_shader(GL_FRAGMENT_SHADER, SHADER("skybox.f.glsl"), &frag);
     87     check_gl();
     88 
     89     ok = make_program_from_shaders(shaders, 2, skybox->program);
     90     assert(ok);
     91     check_gl();
     92 
     93     skybox->uniforms.mvp =
     94         glGetUniformLocation(skybox->program->handle, "mvp");
     95 
     96     skybox->attrs[va_position] = (gpu_addr)
     97         glGetAttribLocation(skybox->program->handle, "position");
     98 
     99     skybox->attrs[va_tex_coord] = (gpu_addr)
    100         glGetAttribLocation(skybox->program->handle, "tex_coord");
    101 }
    102 
    103 
    104 void render_skybox(struct skybox *skybox, mat4 *camera) {
    105 
    106     struct model *model = get_model(&skybox->model_id); assert(model);
    107     struct geometry *geom = get_geometry(&model->geom_id); assert(geom);
    108 
    109     glDepthFunc(GL_LEQUAL);
    110     glDepthMask(GL_FALSE);
    111     check_gl();
    112 
    113     glUseProgram(skybox->program->handle);
    114     check_gl();
    115 
    116     glUniformMatrix4fv(skybox->uniforms.mvp, 1, 0, camera);
    117     check_gl();
    118 
    119     glBindTexture(GL_TEXTURE_CUBE_MAP, model->texture);
    120     check_gl();
    121 
    122     render_geometry(geom, skybox->attrs, skybox->program);
    123     check_gl();
    124 
    125     glDepthMask(GL_TRUE);
    126     check_gl();
    127     glDepthFunc(GL_LESS);
    128 }