rev |
line source |
nuclear@2
|
1 #include <stdio.h>
|
nuclear@18
|
2 #include <math.h>
|
nuclear@2
|
3 #include <assert.h>
|
nuclear@3
|
4 #include <unistd.h>
|
nuclear@1
|
5 #include "opengl.h"
|
nuclear@0
|
6 #include "istereo.h"
|
nuclear@2
|
7 #include "sanegl.h"
|
nuclear@2
|
8 #include "sdr.h"
|
nuclear@4
|
9 #include "respath.h"
|
nuclear@14
|
10 #include "tex.h"
|
nuclear@18
|
11 #include "config.h"
|
nuclear@4
|
12
|
nuclear@4
|
13 static unsigned int get_shader_program(const char *vfile, const char *pfile);
|
nuclear@18
|
14 static float get_sec(void);
|
nuclear@2
|
15
|
nuclear@2
|
16 unsigned int prog;
|
nuclear@14
|
17 unsigned int tex;
|
nuclear@0
|
18
|
nuclear@18
|
19 int stereo;
|
nuclear@18
|
20
|
nuclear@18
|
21 /* construction parameters */
|
nuclear@18
|
22 int sides = 24;
|
nuclear@18
|
23 int segm = 20;
|
nuclear@18
|
24 float ring_height = 0.5;
|
nuclear@18
|
25
|
nuclear@18
|
26
|
nuclear@0
|
27 int init(void)
|
nuclear@0
|
28 {
|
nuclear@4
|
29 add_resource_path("sdr");
|
nuclear@14
|
30 add_resource_path("data");
|
nuclear@3
|
31
|
nuclear@4
|
32 if(!(prog = get_shader_program("test.v.glsl", "test.p.glsl"))) {
|
nuclear@2
|
33 fprintf(stderr, "failed to load shader program\n");
|
nuclear@2
|
34 return -1;
|
nuclear@2
|
35 }
|
nuclear@14
|
36
|
nuclear@14
|
37 if(!(tex = load_texture(find_resource("tiles.ppm", 0, 0)))) {
|
nuclear@14
|
38 fprintf(stderr, "failed to load texture\n");
|
nuclear@14
|
39 return -1;
|
nuclear@14
|
40 }
|
nuclear@14
|
41
|
nuclear@0
|
42 return 0;
|
nuclear@0
|
43 }
|
nuclear@0
|
44
|
nuclear@0
|
45 void cleanup(void)
|
nuclear@0
|
46 {
|
nuclear@2
|
47 free_program(prog);
|
nuclear@0
|
48 }
|
nuclear@0
|
49
|
nuclear@0
|
50 void redraw(void)
|
nuclear@0
|
51 {
|
nuclear@18
|
52 float t = get_sec();
|
nuclear@18
|
53
|
nuclear@5
|
54 glClearColor(0.4, 0.6, 1.0, 1.0);
|
nuclear@0
|
55 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@2
|
56
|
nuclear@2
|
57 bind_program(prog);
|
nuclear@2
|
58
|
nuclear@13
|
59 gl_matrix_mode(GL_MODELVIEW);
|
nuclear@2
|
60 gl_load_identity();
|
nuclear@13
|
61 gl_translatef(0, 0, -8);
|
nuclear@18
|
62 gl_rotatef(t * 100.0, 0, 0, 1);
|
nuclear@2
|
63
|
nuclear@16
|
64 bind_texture(tex, 0);
|
nuclear@17
|
65 set_uniform_int(prog, "tex", 0);
|
nuclear@6
|
66
|
nuclear@13
|
67 gl_begin(GL_QUADS);
|
nuclear@14
|
68 gl_texcoord2f(0, 0);
|
nuclear@2
|
69 gl_color3f(1, 0, 0);
|
nuclear@2
|
70 gl_vertex3f(-1, -1, 0);
|
nuclear@14
|
71 gl_texcoord2f(1, 0);
|
nuclear@2
|
72 gl_color3f(0, 1, 0);
|
nuclear@2
|
73 gl_vertex3f(1, -1, 0);
|
nuclear@14
|
74 gl_texcoord2f(1, 1);
|
nuclear@2
|
75 gl_color3f(0, 0, 1);
|
nuclear@2
|
76 gl_vertex3f(1, 1, 0);
|
nuclear@14
|
77 gl_texcoord2f(0, 1);
|
nuclear@2
|
78 gl_color3f(1, 1, 0);
|
nuclear@2
|
79 gl_vertex3f(-1, 1, 0);
|
nuclear@13
|
80 gl_end();
|
nuclear@2
|
81
|
nuclear@16
|
82 bind_texture(0, 0);
|
nuclear@14
|
83
|
nuclear@2
|
84 assert(glGetError() == GL_NO_ERROR);
|
nuclear@0
|
85 }
|
nuclear@2
|
86
|
nuclear@2
|
87 void reshape(int x, int y)
|
nuclear@2
|
88 {
|
nuclear@2
|
89 glViewport(0, 0, x, y);
|
nuclear@2
|
90
|
nuclear@2
|
91 gl_matrix_mode(GL_PROJECTION);
|
nuclear@2
|
92 gl_load_identity();
|
nuclear@2
|
93 glu_perspective(45.0, (float)x / (float)y, 1.0, 1000.0);
|
nuclear@2
|
94 }
|
nuclear@4
|
95
|
nuclear@4
|
96 static unsigned int get_shader_program(const char *vfile, const char *pfile)
|
nuclear@4
|
97 {
|
nuclear@4
|
98 unsigned int prog, vs, ps;
|
nuclear@4
|
99
|
nuclear@4
|
100 if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
|
nuclear@4
|
101 return -1;
|
nuclear@4
|
102 }
|
nuclear@4
|
103 if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
|
nuclear@4
|
104 return -1;
|
nuclear@4
|
105 }
|
nuclear@4
|
106
|
nuclear@4
|
107 if(!(prog = create_program_link(vs, ps))) {
|
nuclear@4
|
108 return -1;
|
nuclear@4
|
109 }
|
nuclear@4
|
110 return prog;
|
nuclear@4
|
111 }
|
nuclear@18
|
112
|
nuclear@18
|
113
|
nuclear@18
|
114 #ifdef IPHONE
|
nuclear@18
|
115 #include <QuartzCore/QuartzCore.h>
|
nuclear@18
|
116
|
nuclear@18
|
117 static float get_sec(void)
|
nuclear@18
|
118 {
|
nuclear@18
|
119 static float first;
|
nuclear@18
|
120 static int init;
|
nuclear@18
|
121
|
nuclear@18
|
122 if(!init) {
|
nuclear@18
|
123 init = 1;
|
nuclear@18
|
124 first = CACurrentMediaTime();
|
nuclear@18
|
125 return 0.0f;
|
nuclear@18
|
126 }
|
nuclear@18
|
127 return CACurrentMediaTime() - first;
|
nuclear@18
|
128 }
|
nuclear@18
|
129
|
nuclear@18
|
130 #else
|
nuclear@18
|
131
|
nuclear@18
|
132 static float get_sec(void)
|
nuclear@18
|
133 {
|
nuclear@18
|
134 return (float)glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
|
nuclear@18
|
135 }
|
nuclear@18
|
136 #endif
|