goat3dgfx

view examples/cubemap/src/main.cc @ 5:18879c956eb1

- skycube example - added fatal_log - changed the dataset to keep the whole path while searching for data files
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 17 Nov 2013 03:22:40 +0200
parents
children 3d96734fd477
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/cubemap3.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();
84 /*glBegin(GL_QUADS);
85 glColor3f(1, 1, 1);
86 glNormal3f(0, 1, 0);
87 glVertex3f(-0.8, -1, 0.8);
88 glVertex3f(0.8, -1, 0.8);
89 glVertex3f(0.8, -1, -0.8);
90 glVertex3f(-0.8, -1, -0.8);
91 glEnd();*/
93 glutSwapBuffers();
94 CHECKGLERR;
95 }
97 static void skybox(const TextureCube *cubemap)
98 {
99 glPushAttrib(GL_ENABLE_BIT | GL_TEXTURE_BIT);
100 glDisable(GL_DEPTH_TEST);
101 glDisable(GL_CULL_FACE);
102 glDisable(GL_LIGHTING);
104 if(cubemap) cubemap->bind();
105 sdrsky->bind();
107 glBegin(GL_QUADS);
108 // +X
109 glColor3f(1, 0, 0);
110 glVertex3f(1, -1, 1);
111 glVertex3f(1, -1, -1);
112 glVertex3f(1, 1, -1);
113 glVertex3f(1, 1, 1);
114 // -Z
115 glColor3f(0, 1, 0);
116 glVertex3f(1, -1, -1);
117 glVertex3f(-1, -1, -1);
118 glVertex3f(-1, 1, -1);
119 glVertex3f(1, 1, -1);
120 // -X
121 glColor3f(0, 0, 1);
122 glVertex3f(-1, -1, -1);
123 glVertex3f(-1, -1, 1);
124 glVertex3f(-1, 1, 1);
125 glVertex3f(-1, 1, -1);
126 // +Z
127 glColor3f(1, 1, 0);
128 glVertex3f(-1, -1, 1);
129 glVertex3f(1, -1, 1);
130 glVertex3f(1, 1, 1);
131 glVertex3f(-1, 1, 1);
132 // +Y
133 glColor3f(0, 1, 1);
134 glVertex3f(-1, 1, 1);
135 glVertex3f(1, 1, 1);
136 glVertex3f(1, 1, -1);
137 glVertex3f(-1, 1, -1);
138 // -Y
139 glColor3f(1, 0, 1);
140 glVertex3f(-1, -1, -1);
141 glVertex3f(1, -1, -1);
142 glVertex3f(1, -1, 1);
143 glVertex3f(-1, -1, 1);
144 glEnd();
146 glUseProgram(0);
147 glPopAttrib();
148 }
150 static void reshape(int x, int y)
151 {
152 glViewport(0, 0, x, y);
154 Matrix4x4 proj;
155 proj.set_perspective(M_PI / 4.0, (float)x / (float)y, 0.5, 500.0);
156 set_projection_matrix(proj);
157 }
159 static void keyboard(unsigned char key, int x, int y)
160 {
161 switch(key) {
162 case 27:
163 exit(0);
164 }
165 }
167 static bool bnstate[16];
168 static int prev_x, prev_y;
170 static void mouse(int bn, int st, int x, int y)
171 {
172 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
173 prev_x = x;
174 prev_y = y;
175 }
177 static void motion(int x, int y)
178 {
179 int dx = x - prev_x;
180 int dy = y - prev_y;
181 prev_x = x;
182 prev_y = y;
184 if(!dx && !dy) return;
186 if(bnstate[0]) {
187 cam_theta += dx * 0.5;
188 cam_phi += dy * 0.5;
189 cam_phi = std::max(-90.0f, std::min(90.0f, cam_phi));
190 glutPostRedisplay();
191 }
192 }