istereo
view src/istereo.c @ 14:b39d8607f4bb
added textures
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Wed, 07 Sep 2011 09:03:51 +0300 |
parents | fe1cb1c567cc |
children | 20a9d3db38cb |
line source
1 #include <stdio.h>
2 #include <assert.h>
3 #include <unistd.h>
4 #include "opengl.h"
5 #include "istereo.h"
6 #include "sanegl.h"
7 #include "sdr.h"
8 #include "respath.h"
9 #include "tex.h"
11 static unsigned int get_shader_program(const char *vfile, const char *pfile);
13 unsigned int prog;
14 unsigned int tex;
16 int init(void)
17 {
18 add_resource_path("sdr");
19 add_resource_path("data");
21 if(!(prog = get_shader_program("test.v.glsl", "test.p.glsl"))) {
22 fprintf(stderr, "failed to load shader program\n");
23 return -1;
24 }
26 if(!(tex = load_texture(find_resource("tiles.ppm", 0, 0)))) {
27 fprintf(stderr, "failed to load texture\n");
28 return -1;
29 }
31 return 0;
32 }
34 void cleanup(void)
35 {
36 free_program(prog);
37 }
39 void redraw(void)
40 {
41 glClearColor(0.4, 0.6, 1.0, 1.0);
42 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
44 bind_program(prog);
46 gl_matrix_mode(GL_MODELVIEW);
47 gl_load_identity();
48 gl_translatef(0, 0, -8);
50 glEnable(GL_TEXTURE_2D);
51 glBindTexture(GL_TEXTURE_2D, tex);
53 gl_begin(GL_QUADS);
54 gl_texcoord2f(0, 0);
55 gl_color3f(1, 0, 0);
56 gl_vertex3f(-1, -1, 0);
57 gl_texcoord2f(1, 0);
58 gl_color3f(0, 1, 0);
59 gl_vertex3f(1, -1, 0);
60 gl_texcoord2f(1, 1);
61 gl_color3f(0, 0, 1);
62 gl_vertex3f(1, 1, 0);
63 gl_texcoord2f(0, 1);
64 gl_color3f(1, 1, 0);
65 gl_vertex3f(-1, 1, 0);
66 gl_end();
68 glDisable(GL_TEXTURE_2D);
70 assert(glGetError() == GL_NO_ERROR);
71 }
73 void reshape(int x, int y)
74 {
75 glViewport(0, 0, x, y);
77 gl_matrix_mode(GL_PROJECTION);
78 gl_load_identity();
79 glu_perspective(45.0, (float)x / (float)y, 1.0, 1000.0);
80 }
82 static unsigned int get_shader_program(const char *vfile, const char *pfile)
83 {
84 unsigned int prog, vs, ps;
86 if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
87 return -1;
88 }
89 if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
90 return -1;
91 }
93 if(!(prog = create_program_link(vs, ps))) {
94 return -1;
95 }
96 return prog;
97 }