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@14
|
9 #include "tex.h"
|
nuclear@4
|
10
|
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@14
|
14 unsigned int tex;
|
nuclear@0
|
15
|
nuclear@0
|
16 int init(void)
|
nuclear@0
|
17 {
|
nuclear@4
|
18 add_resource_path("sdr");
|
nuclear@14
|
19 add_resource_path("data");
|
nuclear@3
|
20
|
nuclear@4
|
21 if(!(prog = get_shader_program("test.v.glsl", "test.p.glsl"))) {
|
nuclear@2
|
22 fprintf(stderr, "failed to load shader program\n");
|
nuclear@2
|
23 return -1;
|
nuclear@2
|
24 }
|
nuclear@14
|
25
|
nuclear@14
|
26 if(!(tex = load_texture(find_resource("tiles.ppm", 0, 0)))) {
|
nuclear@14
|
27 fprintf(stderr, "failed to load texture\n");
|
nuclear@14
|
28 return -1;
|
nuclear@14
|
29 }
|
nuclear@14
|
30
|
nuclear@0
|
31 return 0;
|
nuclear@0
|
32 }
|
nuclear@0
|
33
|
nuclear@0
|
34 void cleanup(void)
|
nuclear@0
|
35 {
|
nuclear@2
|
36 free_program(prog);
|
nuclear@0
|
37 }
|
nuclear@0
|
38
|
nuclear@0
|
39 void redraw(void)
|
nuclear@0
|
40 {
|
nuclear@5
|
41 glClearColor(0.4, 0.6, 1.0, 1.0);
|
nuclear@0
|
42 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@2
|
43
|
nuclear@2
|
44 bind_program(prog);
|
nuclear@2
|
45
|
nuclear@13
|
46 gl_matrix_mode(GL_MODELVIEW);
|
nuclear@2
|
47 gl_load_identity();
|
nuclear@13
|
48 gl_translatef(0, 0, -8);
|
nuclear@2
|
49
|
nuclear@14
|
50 glEnable(GL_TEXTURE_2D);
|
nuclear@14
|
51 glBindTexture(GL_TEXTURE_2D, tex);
|
nuclear@6
|
52
|
nuclear@13
|
53 gl_begin(GL_QUADS);
|
nuclear@14
|
54 gl_texcoord2f(0, 0);
|
nuclear@2
|
55 gl_color3f(1, 0, 0);
|
nuclear@2
|
56 gl_vertex3f(-1, -1, 0);
|
nuclear@14
|
57 gl_texcoord2f(1, 0);
|
nuclear@2
|
58 gl_color3f(0, 1, 0);
|
nuclear@2
|
59 gl_vertex3f(1, -1, 0);
|
nuclear@14
|
60 gl_texcoord2f(1, 1);
|
nuclear@2
|
61 gl_color3f(0, 0, 1);
|
nuclear@2
|
62 gl_vertex3f(1, 1, 0);
|
nuclear@14
|
63 gl_texcoord2f(0, 1);
|
nuclear@2
|
64 gl_color3f(1, 1, 0);
|
nuclear@2
|
65 gl_vertex3f(-1, 1, 0);
|
nuclear@13
|
66 gl_end();
|
nuclear@2
|
67
|
nuclear@14
|
68 glDisable(GL_TEXTURE_2D);
|
nuclear@14
|
69
|
nuclear@2
|
70 assert(glGetError() == GL_NO_ERROR);
|
nuclear@0
|
71 }
|
nuclear@2
|
72
|
nuclear@2
|
73 void reshape(int x, int y)
|
nuclear@2
|
74 {
|
nuclear@2
|
75 glViewport(0, 0, x, y);
|
nuclear@2
|
76
|
nuclear@2
|
77 gl_matrix_mode(GL_PROJECTION);
|
nuclear@2
|
78 gl_load_identity();
|
nuclear@2
|
79 glu_perspective(45.0, (float)x / (float)y, 1.0, 1000.0);
|
nuclear@2
|
80 }
|
nuclear@4
|
81
|
nuclear@4
|
82 static unsigned int get_shader_program(const char *vfile, const char *pfile)
|
nuclear@4
|
83 {
|
nuclear@4
|
84 unsigned int prog, vs, ps;
|
nuclear@4
|
85
|
nuclear@4
|
86 if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
|
nuclear@4
|
87 return -1;
|
nuclear@4
|
88 }
|
nuclear@4
|
89 if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
|
nuclear@4
|
90 return -1;
|
nuclear@4
|
91 }
|
nuclear@4
|
92
|
nuclear@4
|
93 if(!(prog = create_program_link(vs, ps))) {
|
nuclear@4
|
94 return -1;
|
nuclear@4
|
95 }
|
nuclear@4
|
96 return prog;
|
nuclear@4
|
97 }
|