istereo

view src/istereo.c @ 7:557d86c8d7ed

foo
author John Tsiombikas <nuclear@mutantstargoat.com>
date Wed, 07 Sep 2011 07:43:22 +0300
parents f72c585aec26
children aa70df4bb0e5
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 int init(void)
28 {
29 add_resource_path("sdr");
31 if(!(prog = get_shader_program("test.v.glsl", "test.p.glsl"))) {
32 fprintf(stderr, "failed to load shader program\n");
33 return -1;
34 }
36 return 0;
37 }
39 void cleanup(void)
40 {
41 free_program(prog);
42 }
44 void redraw(void)
45 {
46 glClearColor(0.4, 0.6, 1.0, 1.0);
47 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
49 bind_program(prog);
51 gl_matrix_mode(GL_MODELVIEW);
52 gl_load_identity();
53 gl_translatef(0, 0, -8);
55 dbg_draw();
57 gl_begin(GL_QUADS);
58 gl_color3f(1, 0, 0);
59 gl_vertex3f(-1, -1, 0);
60 gl_color3f(0, 1, 0);
61 gl_vertex3f(1, -1, 0);
62 gl_color3f(0, 0, 1);
63 gl_vertex3f(1, 1, 0);
64 gl_color3f(1, 1, 0);
65 gl_vertex3f(-1, 1, 0);
66 gl_end();
68 assert(glGetError() == GL_NO_ERROR);
69 }
71 void reshape(int x, int y)
72 {
73 glViewport(0, 0, x, y);
75 gl_matrix_mode(GL_PROJECTION);
76 gl_load_identity();
77 glu_perspective(45.0, (float)x / (float)y, 1.0, 1000.0);
78 }
80 static unsigned int get_shader_program(const char *vfile, const char *pfile)
81 {
82 unsigned int prog, vs, ps;
84 if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
85 return -1;
86 }
87 if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
88 return -1;
89 }
91 if(!(prog = create_program_link(vs, ps))) {
92 return -1;
93 }
94 return prog;
95 }
97 void dbg_draw(void)
98 {
99 int vloc, cloc;
101 gl_apply_xform(prog);
103 vloc = glGetAttribLocation(prog, "attr_vertex");
104 cloc = glGetAttribLocation(prog, "attr_color");
105 assert(vloc != -1 && cloc != -1);
107 glVertexAttribPointer(vloc, 3, GL_FLOAT, 0, 0, varr);
108 glEnableVertexAttribArray(vloc);
109 glVertexAttribPointer(cloc, 3, GL_FLOAT, 0, 0, carr);
110 glEnableVertexAttribArray(cloc);
112 glDrawArrays(GL_TRIANGLES, 0, 3);
114 glDisableVertexAttribArray(vloc);
115 glDisableVertexAttribArray(cloc);
116 }