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@11
|
38 /*gl_matrix_mode(GL_MODELVIEW);
|
nuclear@2
|
39 gl_load_identity();
|
nuclear@11
|
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@10
|
81
|
nuclear@10
|
82 glBindAttribLocation(prog, 0, "attr_vertex");
|
nuclear@10
|
83 glBindAttribLocation(prog, 1, "attr_color");
|
nuclear@10
|
84 glLinkProgram(prog);
|
nuclear@10
|
85
|
nuclear@12
|
86 assert(prog);
|
nuclear@12
|
87
|
nuclear@4
|
88 return prog;
|
nuclear@4
|
89 }
|
nuclear@6
|
90
|
nuclear@6
|
91 void dbg_draw(void)
|
nuclear@6
|
92 {
|
nuclear@9
|
93 static const GLfloat squareVertices[] = {
|
nuclear@9
|
94 -0.5f, -0.33f,
|
nuclear@9
|
95 0.5f, -0.33f,
|
nuclear@9
|
96 -0.5f, 0.33f,
|
nuclear@9
|
97 0.5f, 0.33f,
|
nuclear@9
|
98 };
|
nuclear@9
|
99
|
nuclear@9
|
100 static const GLubyte squareColors[] = {
|
nuclear@9
|
101 255, 255, 0, 255,
|
nuclear@9
|
102 0, 255, 255, 255,
|
nuclear@9
|
103 0, 0, 0, 0,
|
nuclear@9
|
104 255, 0, 255, 255,
|
nuclear@9
|
105 };
|
nuclear@9
|
106
|
nuclear@6
|
107 int vloc, cloc;
|
nuclear@6
|
108
|
nuclear@9
|
109 glUseProgram(prog);
|
nuclear@9
|
110
|
nuclear@11
|
111 /*gl_apply_xform(prog);*/
|
nuclear@6
|
112
|
nuclear@11
|
113
|
nuclear@11
|
114 vloc = 0;/*glGetAttribLocation(prog, "attr_vertex");*/
|
nuclear@11
|
115 cloc = 1;/*glGetAttribLocation(prog, "attr_color");*/
|
nuclear@9
|
116 assert(vloc >= 0 && cloc >= 0);
|
nuclear@6
|
117
|
nuclear@9
|
118 glVertexAttribPointer(vloc, 2, GL_FLOAT, 0, 0, squareVertices);
|
nuclear@9
|
119 glEnableVertexAttribArray(vloc);
|
nuclear@9
|
120 glVertexAttribPointer(cloc, 4, GL_UNSIGNED_BYTE, 1, 0, squareColors);
|
nuclear@9
|
121 glEnableVertexAttribArray(cloc);
|
nuclear@8
|
122
|
nuclear@9
|
123 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
nuclear@6
|
124
|
nuclear@6
|
125 glDisableVertexAttribArray(vloc);
|
nuclear@6
|
126 glDisableVertexAttribArray(cloc);
|
nuclear@6
|
127 }
|