istereo

annotate src/istereo.c @ 8:aa70df4bb0e5

added vbo test
author John Tsiombikas <nuclear@mutantstargoat.com>
date Wed, 07 Sep 2011 07:58:53 +0300
parents 557d86c8d7ed
children 22dc37e3ca05
rev   line source
nuclear@2 1 #include <stdio.h>
nuclear@2 2 #include <assert.h>
nuclear@3 3 #include <unistd.h>
nuclear@1 4 #include "opengl.h"
nuclear@0 5 #include "istereo.h"
nuclear@2 6 #include "sanegl.h"
nuclear@2 7 #include "sdr.h"
nuclear@4 8 #include "respath.h"
nuclear@4 9
nuclear@6 10 float varr[] = {
nuclear@7 11 -2, 1.5, 0,
nuclear@7 12 2, 1.5, 0,
nuclear@7 13 0, 4, 0
nuclear@6 14 };
nuclear@6 15
nuclear@6 16 float carr[] = {
nuclear@6 17 1, 0, 0,
nuclear@6 18 0, 1, 0,
nuclear@6 19 0, 0, 1
nuclear@6 20 };
nuclear@6 21
nuclear@6 22 void dbg_draw(void);
nuclear@4 23 static unsigned int get_shader_program(const char *vfile, const char *pfile);
nuclear@2 24
nuclear@2 25 unsigned int prog;
nuclear@0 26
nuclear@8 27 unsigned int vbo, cbo;
nuclear@8 28
nuclear@0 29 int init(void)
nuclear@0 30 {
nuclear@4 31 add_resource_path("sdr");
nuclear@3 32
nuclear@4 33 if(!(prog = get_shader_program("test.v.glsl", "test.p.glsl"))) {
nuclear@2 34 fprintf(stderr, "failed to load shader program\n");
nuclear@2 35 return -1;
nuclear@2 36 }
nuclear@2 37
nuclear@8 38 glGenBuffers(1, &vbo);
nuclear@8 39 glBindBuffer(GL_ARRAY_BUFFER, vbo);
nuclear@8 40 glBufferData(GL_ARRAY_BUFFER, sizeof varr, varr, GL_STATIC_DRAW);
nuclear@8 41
nuclear@8 42 glGenBuffers(1, &cbo);
nuclear@8 43 glBindBuffer(GL_ARRAY_BUFFER, cbo);
nuclear@8 44 glBufferData(GL_ARRAY_BUFFER, sizeof carr, carr, GL_STATIC_DRAW);
nuclear@8 45
nuclear@8 46 glBindBuffer(GL_ARRAY_BUFFER, 0);
nuclear@8 47
nuclear@0 48 return 0;
nuclear@0 49 }
nuclear@0 50
nuclear@0 51 void cleanup(void)
nuclear@0 52 {
nuclear@2 53 free_program(prog);
nuclear@0 54 }
nuclear@0 55
nuclear@0 56 void redraw(void)
nuclear@0 57 {
nuclear@5 58 glClearColor(0.4, 0.6, 1.0, 1.0);
nuclear@0 59 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@2 60
nuclear@2 61 bind_program(prog);
nuclear@2 62
nuclear@2 63 gl_matrix_mode(GL_MODELVIEW);
nuclear@2 64 gl_load_identity();
nuclear@2 65 gl_translatef(0, 0, -8);
nuclear@2 66
nuclear@6 67 dbg_draw();
nuclear@6 68
nuclear@2 69 gl_begin(GL_QUADS);
nuclear@2 70 gl_color3f(1, 0, 0);
nuclear@2 71 gl_vertex3f(-1, -1, 0);
nuclear@2 72 gl_color3f(0, 1, 0);
nuclear@2 73 gl_vertex3f(1, -1, 0);
nuclear@2 74 gl_color3f(0, 0, 1);
nuclear@2 75 gl_vertex3f(1, 1, 0);
nuclear@2 76 gl_color3f(1, 1, 0);
nuclear@2 77 gl_vertex3f(-1, 1, 0);
nuclear@2 78 gl_end();
nuclear@2 79
nuclear@2 80 assert(glGetError() == GL_NO_ERROR);
nuclear@0 81 }
nuclear@2 82
nuclear@2 83 void reshape(int x, int y)
nuclear@2 84 {
nuclear@2 85 glViewport(0, 0, x, y);
nuclear@2 86
nuclear@2 87 gl_matrix_mode(GL_PROJECTION);
nuclear@2 88 gl_load_identity();
nuclear@2 89 glu_perspective(45.0, (float)x / (float)y, 1.0, 1000.0);
nuclear@2 90 }
nuclear@4 91
nuclear@4 92 static unsigned int get_shader_program(const char *vfile, const char *pfile)
nuclear@4 93 {
nuclear@4 94 unsigned int prog, vs, ps;
nuclear@4 95
nuclear@4 96 if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
nuclear@4 97 return -1;
nuclear@4 98 }
nuclear@4 99 if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
nuclear@4 100 return -1;
nuclear@4 101 }
nuclear@4 102
nuclear@4 103 if(!(prog = create_program_link(vs, ps))) {
nuclear@4 104 return -1;
nuclear@4 105 }
nuclear@4 106 return prog;
nuclear@4 107 }
nuclear@6 108
nuclear@6 109 void dbg_draw(void)
nuclear@6 110 {
nuclear@6 111 int vloc, cloc;
nuclear@6 112
nuclear@6 113 gl_apply_xform(prog);
nuclear@6 114
nuclear@6 115 vloc = glGetAttribLocation(prog, "attr_vertex");
nuclear@6 116 cloc = glGetAttribLocation(prog, "attr_color");
nuclear@6 117 assert(vloc != -1 && cloc != -1);
nuclear@6 118
nuclear@8 119 glBindBuffer(GL_ARRAY_BUFFER, vbo);
nuclear@8 120 glVertexAttribPointer(vloc, 3, GL_FLOAT, 0, 0, 0);
nuclear@6 121 glEnableVertexAttribArray(vloc);
nuclear@8 122
nuclear@8 123 glBindBuffer(GL_ARRAY_BUFFER, cbo);
nuclear@8 124 glVertexAttribPointer(cloc, 3, GL_FLOAT, 0, 0, 0);
nuclear@6 125 glEnableVertexAttribArray(cloc);
nuclear@6 126
nuclear@8 127 glBindBuffer(GL_ARRAY_BUFFER, 0);
nuclear@8 128
nuclear@6 129 glDrawArrays(GL_TRIANGLES, 0, 3);
nuclear@6 130
nuclear@6 131 glDisableVertexAttribArray(vloc);
nuclear@6 132 glDisableVertexAttribArray(cloc);
nuclear@6 133 }