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