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