istereo

view 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
line source
1 #include <stdio.h>
2 #include <assert.h>
3 #include <unistd.h>
4 #include "opengl.h"
5 #include "istereo.h"
6 #include "sanegl.h"
7 #include "sdr.h"
8 #include "respath.h"
10 float varr[] = {
11 -2, 1.5, 0,
12 2, 1.5, 0,
13 0, 4, 0
14 };
16 float carr[] = {
17 1, 0, 0,
18 0, 1, 0,
19 0, 0, 1
20 };
22 void dbg_draw(void);
23 static unsigned int get_shader_program(const char *vfile, const char *pfile);
25 unsigned int prog;
27 unsigned int vbo, cbo;
29 int init(void)
30 {
31 add_resource_path("sdr");
33 if(!(prog = get_shader_program("test.v.glsl", "test.p.glsl"))) {
34 fprintf(stderr, "failed to load shader program\n");
35 return -1;
36 }
38 glGenBuffers(1, &vbo);
39 glBindBuffer(GL_ARRAY_BUFFER, vbo);
40 glBufferData(GL_ARRAY_BUFFER, sizeof varr, varr, GL_STATIC_DRAW);
42 glGenBuffers(1, &cbo);
43 glBindBuffer(GL_ARRAY_BUFFER, cbo);
44 glBufferData(GL_ARRAY_BUFFER, sizeof carr, carr, GL_STATIC_DRAW);
46 glBindBuffer(GL_ARRAY_BUFFER, 0);
48 return 0;
49 }
51 void cleanup(void)
52 {
53 free_program(prog);
54 }
56 void redraw(void)
57 {
58 glClearColor(0.4, 0.6, 1.0, 1.0);
59 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
61 bind_program(prog);
63 gl_matrix_mode(GL_MODELVIEW);
64 gl_load_identity();
65 gl_translatef(0, 0, -8);
67 dbg_draw();
69 gl_begin(GL_QUADS);
70 gl_color3f(1, 0, 0);
71 gl_vertex3f(-1, -1, 0);
72 gl_color3f(0, 1, 0);
73 gl_vertex3f(1, -1, 0);
74 gl_color3f(0, 0, 1);
75 gl_vertex3f(1, 1, 0);
76 gl_color3f(1, 1, 0);
77 gl_vertex3f(-1, 1, 0);
78 gl_end();
80 assert(glGetError() == GL_NO_ERROR);
81 }
83 void reshape(int x, int y)
84 {
85 glViewport(0, 0, x, y);
87 gl_matrix_mode(GL_PROJECTION);
88 gl_load_identity();
89 glu_perspective(45.0, (float)x / (float)y, 1.0, 1000.0);
90 }
92 static unsigned int get_shader_program(const char *vfile, const char *pfile)
93 {
94 unsigned int prog, vs, ps;
96 if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
97 return -1;
98 }
99 if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
100 return -1;
101 }
103 if(!(prog = create_program_link(vs, ps))) {
104 return -1;
105 }
106 return prog;
107 }
109 void dbg_draw(void)
110 {
111 int vloc, cloc;
113 gl_apply_xform(prog);
115 vloc = glGetAttribLocation(prog, "attr_vertex");
116 cloc = glGetAttribLocation(prog, "attr_color");
117 assert(vloc != -1 && cloc != -1);
119 glBindBuffer(GL_ARRAY_BUFFER, vbo);
120 glVertexAttribPointer(vloc, 3, GL_FLOAT, 0, 0, 0);
121 glEnableVertexAttribArray(vloc);
123 glBindBuffer(GL_ARRAY_BUFFER, cbo);
124 glVertexAttribPointer(cloc, 3, GL_FLOAT, 0, 0, 0);
125 glEnableVertexAttribArray(cloc);
127 glBindBuffer(GL_ARRAY_BUFFER, 0);
129 glDrawArrays(GL_TRIANGLES, 0, 3);
131 glDisableVertexAttribArray(vloc);
132 glDisableVertexAttribArray(cloc);
133 }