initial commit
[shapestoy] / src / demo.c
1 #include <stdio.h>
2 #include <stdint.h>
3 #include "demo.h"
4 #include "opengl.h"
5 #include "sanegl.h"
6 #include "assman.h"
7 #include "cmesh.h"
8
9 static void gen_default_textures(void);
10
11 static unsigned int sdr_foo;
12 static unsigned int tex_logo;
13
14 int demo_init(void)
15 {
16         if(init_opengl() == -1) {
17                 return -1;
18         }
19         if(init_assman() == -1) {
20                 return -1;
21         }
22
23         /* global "debug" shader which just visualizes normals */
24         if(!(sdr_dbg = get_sdrprog("sdr/dbg.v.glsl", "sdr/dbg.p.glsl"))) {
25                 return -1;
26         }
27         cmesh_bind_sdrloc(sdr_dbg);
28
29         gen_default_textures();
30
31         if(!(sdr_foo = get_sdrprog("sdr/foo.v.glsl", "sdr/foo.p.glsl"))) {
32                 return -1;
33         }
34         if(!(tex_logo = get_tex2d("data/ml_logo_old.png"))) {
35                 return -1;
36         }
37         glBindTexture(GL_TEXTURE_2D, tex_logo);
38         glUseProgram(sdr_foo);
39         gl_begin(GL_QUADS);
40         gl_color3f(1, 1, 1);
41         gl_texcoord2f(0, 1);
42         gl_vertex2f(-1, -1);
43         gl_texcoord2f(1, 1);
44         gl_vertex2f(1, -1);
45         gl_texcoord2f(1, 0);
46         gl_vertex2f(1, 1);
47         gl_texcoord2f(0, 0);
48         gl_vertex2f(-1, 1);
49         gl_end();
50         swap_buffers();
51
52         glEnable(GL_DEPTH_TEST);
53         glEnable(GL_CULL_FACE);
54         return 0;
55 }
56
57 void demo_cleanup(void)
58 {
59         destroy_assman();
60
61         glDeleteTextures(1, &deftex_white);
62         glDeleteTextures(1, &deftex_black);
63         glDeleteTextures(1, &deftex_normal);
64 }
65
66 void demo_display(void)
67 {
68         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
69
70         glBindTexture(GL_TEXTURE_2D, tex_logo);
71         glUseProgram(sdr_foo);
72         gl_begin(GL_QUADS);
73         gl_color3f(1, 1, 1);
74         gl_texcoord2f(0, 1);
75         gl_vertex2f(-1, -1);
76         gl_texcoord2f(1, 1);
77         gl_vertex2f(1, -1);
78         gl_texcoord2f(1, 0);
79         gl_vertex2f(1, 1);
80         gl_texcoord2f(0, 0);
81         gl_vertex2f(-1, 1);
82         gl_end();
83 }
84
85 void demo_reshape(int x, int y)
86 {
87         glViewport(0, 0, x, y);
88         win_width = x;
89         win_height = y;
90         win_aspect = (float)x / (float)y;
91 }
92
93 void demo_keyboard(int key, int pressed)
94 {
95 }
96
97 void demo_mouse(int bn, int pressed, int x, int y)
98 {
99 }
100
101 void demo_motion(int x, int y)
102 {
103 }
104
105 static void gen_default_textures(void)
106 {
107         static const uint32_t col[] = {0xff000000, 0xffffffff, 0xffff7f7f};
108         int i;
109         unsigned int tex[3];
110
111         glGenTextures(3, tex);
112         for(i=0; i<3; i++) {
113                 glBindTexture(GL_TEXTURE_2D, tex[i]);
114                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
115                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
116                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, col + i);
117         }
118
119         deftex_black = tex[0];
120         deftex_white = tex[1];
121         deftex_normal = tex[2];
122 }