polyadvent

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

ui.c (3418B)


      1 
      2 #include "ui.h"
      3 #include "mat4.h"
      4 #include "vbo.h"
      5 #include "geometry.h"
      6 #include "util.h"
      7 #include "common.h"
      8 
      9 
     10 //  v1------v0
     11 //  |       |
     12 //  |       |
     13 //  |       |
     14 //  v2------v3
     15 
     16 static GLfloat quad_vertices[] = {
     17   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
     18 };
     19 
     20 
     21 static GLfloat quad_normals[] = {
     22   0, 0, 1,   0, 0, 1,   0, 0, 1,   0, 0, 1, // front
     23 };
     24 
     25 static GLuint quad_indices[] = {0, 1, 2,  0, 2, 3};
     26 
     27 
     28 static GLfloat quad_uvs[] =
     29 {
     30     1.0, 1.0, // v0
     31     0.0, 1.0, // v1
     32     0.0, 0.0, // v2
     33     1.0, 0.0, // v3
     34 };
     35 
     36 
     37 static void create_quad(geometry_id *id)
     38 {
     39     struct make_geometry mkgeom = {
     40         .indices = quad_indices,
     41         .vertices = quad_vertices,
     42         .normals = quad_normals,
     43         .tex_coords = quad_uvs,
     44         .num_indices = ARRAY_SIZE(quad_indices),
     45         .num_verts = ARRAY_SIZE(quad_vertices),
     46         .num_uv_components = 2
     47     };
     48     init_id(id);
     49     struct geometry *geom = new_geometry(id);
     50     make_buffer_geometry(&mkgeom, geom);
     51     check_gl();
     52 }
     53 
     54 void render_ui(struct ui *ui, float *view) {
     55     static float mvp[MAT4_ELEMS];
     56 	glDisable(GL_DEPTH_TEST);
     57     glUseProgram(ui->shader->handle);
     58     check_gl();
     59 
     60     float uipos[2] = {0.01, 0.01};
     61     float uisize[2] = {0.4, 0.4};
     62 
     63     mat4_multiply(ui->ortho, view, mvp);
     64 
     65     // setup camera for shader
     66     /* glUniform2f(ui->uipos_uniform, uipos[0] * uisize[0], uipos[1] * uisize[1]); */
     67     glUniform2f(ui->uniforms.uipos, uipos[0], uipos[1]);
     68     glUniform2f(ui->uniforms.uisize, uisize[0], uisize[1]);
     69     glUniform1i(ui->uniforms.texture, 0);
     70 
     71     glUniformMatrix4fv(ui->uniforms.mvp, 1, 0, ui->ortho);
     72     check_gl();
     73 
     74     // render quad
     75     render_geometry(get_geometry(&ui->quad_geom_id), ui->attrs, ui->shader);
     76     check_gl();
     77 }
     78 
     79 
     80 void resize_ui(struct ui *ui, int width, int height) {
     81     float left = 0.0;
     82     float right = 1.0;
     83     float bottom = 0.0;
     84     float top = 1.0;
     85     float near = -1.0;
     86     float far = 2.0;
     87 
     88     mat4_ortho(left, right, bottom, top, near, far, ui->ortho);
     89 }
     90 
     91 
     92 void create_ui(struct ui *ui, int width, int height, struct gpu_program *shader) {
     93     struct shader vertex;
     94     struct shader fragment;
     95     int ok = 0;
     96     // TODO: handle runtime ui errors (there shouldn't be any...)
     97     (void)ok;
     98 
     99     ui->shader = shader;
    100     create_quad(&ui->quad_geom_id);
    101     check_gl();
    102 
    103     ok = make_shader(GL_VERTEX_SHADER, SHADER("ui.v.glsl"), &vertex);
    104     assert(ok && "ui vertex shader");
    105 
    106     ok = make_shader(GL_FRAGMENT_SHADER, SHADER("ui.f.glsl"), &fragment);
    107     assert(ok && "ui fragment shader");
    108 
    109     ok = make_program(&vertex, &fragment, ui->shader);
    110     assert(ok && "ui program");
    111 
    112     GLuint program = ui->shader->handle;
    113 
    114     ui->uniforms.mvp            = glGetUniformLocation(program, "mvp");
    115     ui->uniforms.uipos          = glGetUniformLocation(program, "uipos");
    116     ui->uniforms.uisize         = glGetUniformLocation(program, "uisize");
    117 
    118     ui->uniforms.texture =
    119         glGetUniformLocation(program, "screen_texture");
    120 
    121     /* ui->attrs.normal   = (gpu_addr)glGetAttribLocation(program, "normal"); */
    122     ui->attrs[va_position]  = (gpu_addr)glGetAttribLocation(program, "position");
    123     ui->attrs[va_color]     = (gpu_addr)glGetAttribLocation(program, "color");
    124     ui->attrs[va_tex_coord] = (gpu_addr)glGetAttribLocation(program, "tex_coords");
    125 
    126     check_gl();
    127     resize_ui(ui, width, height);
    128     check_gl();
    129 }