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