goat3dgfx
view examples/cubemap/src/main.cc @ 15:7d6b667821cf
wrapped everything in the goatgfx namespace
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 30 Nov 2013 20:52:21 +0200 |
parents | 3d96734fd477 |
children | f2da87b02fcd |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <algorithm>
4 #include <goat3dgfx/goat3dgfx.h>
5 #include <vmath/vmath.h>
7 #define CUBEMAP_FILENAME "data/cubemap2.jpg"
9 static bool init();
10 static void cleanup();
11 static void display();
12 static void skybox(const TextureCube *cubemap = 0);
13 static void reshape(int x, int y);
14 static void keyboard(unsigned char key, int x, int y);
15 static void mouse(int bn, int st, int x, int y);
16 static void motion(int x, int y);
18 static float cam_theta, cam_phi;
20 static TextureCube *cubemap;
21 static ShaderProg *sdrsky;
23 int main(int argc, char **argv)
24 {
25 glutInit(&argc, argv);
26 glutInitWindowSize(800, 600);
27 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
28 glutCreateWindow("cubemap");
30 glutDisplayFunc(display);
31 glutReshapeFunc(reshape);
32 glutKeyboardFunc(keyboard);
33 glutMouseFunc(mouse);
34 glutMotionFunc(motion);
36 if(!init()) {
37 return 1;
38 }
39 atexit(cleanup);
41 glutMainLoop();
42 return 0;
43 }
45 static bool init()
46 {
47 glewInit();
49 glEnable(GL_DEPTH_TEST);
50 //glEnable(GL_CULL_FACE);
52 cubemap = new TextureCube;
53 if(!cubemap->load(CUBEMAP_FILENAME)) {
54 fatal_log("Failed to load cubemap: %s\n", CUBEMAP_FILENAME);
55 return false;
56 }
58 if(!(sdrsky = get_sdrprog("sdr/sky.v.glsl", "sdr/sky.p.glsl"))) {
59 fatal_log("failed to load skybox shader\n");
60 return false;
61 }
63 return true;
64 }
66 static void cleanup()
67 {
68 delete cubemap;
69 }
71 static void display()
72 {
73 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
75 Matrix4x4 view_matrix;
76 view_matrix.rotate(Vector3(1, 0, 0), M_PI * cam_phi / 180.0);
77 view_matrix.rotate(Vector3(0, 1, 0), M_PI * cam_theta / 180.0);
78 set_view_matrix(view_matrix);
80 setup_gl_matrices();
82 skybox(cubemap);
84 glutSwapBuffers();
85 CHECKGLERR;
86 }
88 static void skybox(const TextureCube *cubemap)
89 {
90 static Mesh *skybox;
92 if(!skybox) {
93 skybox = new Mesh;
94 gen_sphere(skybox, 10.0, 12, 6);
95 }
97 glPushAttrib(GL_ENABLE_BIT | GL_TEXTURE_BIT);
98 glDisable(GL_DEPTH_TEST);
99 glDisable(GL_CULL_FACE);
100 glDisable(GL_LIGHTING);
102 glEnable(GL_TEXTURE_CUBE_MAP);
104 if(cubemap) cubemap->bind();
105 sdrsky->bind();
107 skybox->draw();
109 glUseProgram(0);
110 glPopAttrib();
111 }
113 static void reshape(int x, int y)
114 {
115 glViewport(0, 0, x, y);
117 Matrix4x4 proj;
118 proj.set_perspective(M_PI / 4.0, (float)x / (float)y, 0.5, 500.0);
119 set_projection_matrix(proj);
120 }
122 static void keyboard(unsigned char key, int x, int y)
123 {
124 switch(key) {
125 case 27:
126 exit(0);
127 }
128 }
130 static bool bnstate[16];
131 static int prev_x, prev_y;
133 static void mouse(int bn, int st, int x, int y)
134 {
135 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
136 prev_x = x;
137 prev_y = y;
138 }
140 static void motion(int x, int y)
141 {
142 int dx = x - prev_x;
143 int dy = y - prev_y;
144 prev_x = x;
145 prev_y = y;
147 if(!dx && !dy) return;
149 if(bnstate[0]) {
150 cam_theta += dx * 0.5;
151 cam_phi += dy * 0.5;
152 cam_phi = std::max(-90.0f, std::min(90.0f, cam_phi));
153 glutPostRedisplay();
154 }
155 }