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