goat3dgfx
view examples/cubemap/src/main.cc @ 25:6236080aaea4
LIBOVR conditional compilation fix
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 27 Feb 2014 01:40:12 +0200 |
parents | 25b911c7c35c |
children |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <algorithm>
4 #include <goat3dgfx/goat3dgfx.h>
5 #include <vmath/vmath.h>
7 using namespace goatgfx;
9 #define CUBEMAP_FILENAME "data/cubemap2.jpg"
11 static bool init();
12 static void cleanup();
13 static void display();
14 static void skybox(const TextureCube *cubemap = 0);
15 static void reshape(int x, int y);
16 static void keyboard(unsigned char key, int x, int y);
17 static void mouse(int bn, int st, int x, int y);
18 static void motion(int x, int y);
20 static float cam_theta, cam_phi;
22 static TextureCube *cubemap;
23 static ShaderProg *sdrsky;
25 int main(int argc, char **argv)
26 {
27 glutInit(&argc, argv);
28 glutInitWindowSize(800, 600);
29 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
30 glutCreateWindow("cubemap");
32 glutDisplayFunc(display);
33 glutReshapeFunc(reshape);
34 glutKeyboardFunc(keyboard);
35 glutMouseFunc(mouse);
36 glutMotionFunc(motion);
38 if(!init()) {
39 return 1;
40 }
41 atexit(cleanup);
43 glutMainLoop();
44 return 0;
45 }
47 static bool init()
48 {
49 glewInit();
51 glEnable(GL_DEPTH_TEST);
52 //glEnable(GL_CULL_FACE);
54 cubemap = new TextureCube;
55 if(!cubemap->load(CUBEMAP_FILENAME)) {
56 fatal_log("Failed to load cubemap: %s\n", CUBEMAP_FILENAME);
57 return false;
58 }
60 if(!(sdrsky = get_sdrprog("sdr/sky.v.glsl", "sdr/sky.p.glsl"))) {
61 fatal_log("failed to load skybox shader\n");
62 return false;
63 }
65 return true;
66 }
68 static void cleanup()
69 {
70 delete cubemap;
71 }
73 static void display()
74 {
75 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
77 Matrix4x4 view_matrix;
78 view_matrix.rotate(Vector3(1, 0, 0), M_PI * cam_phi / 180.0);
79 view_matrix.rotate(Vector3(0, 1, 0), M_PI * cam_theta / 180.0);
80 set_view_matrix(view_matrix);
82 setup_gl_matrices();
84 skybox(cubemap);
86 glutSwapBuffers();
87 CHECKGLERR;
88 }
90 static void skybox(const TextureCube *cubemap)
91 {
92 static Mesh *skybox;
94 if(!skybox) {
95 skybox = new Mesh;
96 gen_sphere(skybox, 10.0, 12, 6);
97 }
99 glPushAttrib(GL_ENABLE_BIT | GL_TEXTURE_BIT);
100 glDisable(GL_DEPTH_TEST);
101 glDisable(GL_CULL_FACE);
102 glDisable(GL_LIGHTING);
104 glEnable(GL_TEXTURE_CUBE_MAP);
106 if(cubemap) cubemap->bind();
107 sdrsky->bind();
109 skybox->draw();
111 glUseProgram(0);
112 glPopAttrib();
113 }
115 static void reshape(int x, int y)
116 {
117 glViewport(0, 0, x, y);
119 Matrix4x4 proj;
120 proj.set_perspective(M_PI / 4.0, (float)x / (float)y, 0.5, 500.0);
121 set_projection_matrix(proj);
122 }
124 static void keyboard(unsigned char key, int x, int y)
125 {
126 switch(key) {
127 case 27:
128 exit(0);
129 }
130 }
132 static bool bnstate[16];
133 static int prev_x, prev_y;
135 static void mouse(int bn, int st, int x, int y)
136 {
137 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
138 prev_x = x;
139 prev_y = y;
140 }
142 static void motion(int x, int y)
143 {
144 int dx = x - prev_x;
145 int dy = y - prev_y;
146 prev_x = x;
147 prev_y = y;
149 if(!dx && !dy) return;
151 if(bnstate[0]) {
152 cam_theta += dx * 0.5;
153 cam_phi += dy * 0.5;
154 cam_phi = std::max(-90.0f, std::min(90.0f, cam_phi));
155 glutPostRedisplay();
156 }
157 }