istereo

view src/istereo.c @ 9:22dc37e3ca05

what the fucking fuck
author John Tsiombikas <nuclear@mutantstargoat.com>
date Wed, 07 Sep 2011 08:16:06 +0300
parents aa70df4bb0e5
children f72f96c93972
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 void dbg_draw(void);
11 static unsigned int get_shader_program(const char *vfile, const char *pfile);
13 unsigned int prog;
15 int init(void)
16 {
17 add_resource_path("sdr");
19 if(!(prog = get_shader_program("test.v.glsl", "test.p.glsl"))) {
20 fprintf(stderr, "failed to load shader program\n");
21 return -1;
22 }
23 return 0;
24 }
26 void cleanup(void)
27 {
28 free_program(prog);
29 }
31 void redraw(void)
32 {
33 glClearColor(0.4, 0.6, 1.0, 1.0);
34 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
36 bind_program(prog);
38 gl_matrix_mode(GL_MODELVIEW);
39 gl_load_identity();
40 gl_translatef(0, 0, -8);
42 dbg_draw();
44 /*gl_begin(GL_QUADS);
45 gl_color3f(1, 0, 0);
46 gl_vertex3f(-1, -1, 0);
47 gl_color3f(0, 1, 0);
48 gl_vertex3f(1, -1, 0);
49 gl_color3f(0, 0, 1);
50 gl_vertex3f(1, 1, 0);
51 gl_color3f(1, 1, 0);
52 gl_vertex3f(-1, 1, 0);
53 gl_end();*/
55 assert(glGetError() == GL_NO_ERROR);
56 }
58 void reshape(int x, int y)
59 {
60 glViewport(0, 0, x, y);
62 gl_matrix_mode(GL_PROJECTION);
63 gl_load_identity();
64 glu_perspective(45.0, (float)x / (float)y, 1.0, 1000.0);
65 }
67 static unsigned int get_shader_program(const char *vfile, const char *pfile)
68 {
69 unsigned int prog, vs, ps;
71 if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
72 return -1;
73 }
74 if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
75 return -1;
76 }
78 if(!(prog = create_program_link(vs, ps))) {
79 return -1;
80 }
81 return prog;
82 }
84 void dbg_draw(void)
85 {
86 static const GLfloat squareVertices[] = {
87 -0.5f, -0.33f,
88 0.5f, -0.33f,
89 -0.5f, 0.33f,
90 0.5f, 0.33f,
91 };
93 static const GLubyte squareColors[] = {
94 255, 255, 0, 255,
95 0, 255, 255, 255,
96 0, 0, 0, 0,
97 255, 0, 255, 255,
98 };
100 int vloc, cloc;
102 glUseProgram(prog);
104 gl_apply_xform(prog);
106 vloc = glGetAttribLocation(prog, "attr_vertex");
107 cloc = glGetAttribLocation(prog, "attr_color");
108 assert(vloc >= 0 && cloc >= 0);
110 glVertexAttribPointer(vloc, 2, GL_FLOAT, 0, 0, squareVertices);
111 glEnableVertexAttribArray(vloc);
112 glVertexAttribPointer(cloc, 4, GL_UNSIGNED_BYTE, 1, 0, squareColors);
113 glEnableVertexAttribArray(cloc);
115 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
117 glDisableVertexAttribArray(vloc);
118 glDisableVertexAttribArray(cloc);
119 }