goat3dgfx
diff examples/cubemap/src/main.cc @ 9:25b911c7c35c
fixed some line endings
fixed the cubemap2.jpg file in examples/cubemap/data which was resized improperly causing seams...
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 18 Nov 2013 04:10:19 +0200 |
parents | 3d96734fd477 |
children | f2da87b02fcd |
line diff
1.1 --- a/examples/cubemap/src/main.cc Mon Nov 18 03:35:20 2013 +0200 1.2 +++ b/examples/cubemap/src/main.cc Mon Nov 18 04:10:19 2013 +0200 1.3 @@ -1,155 +1,155 @@ 1.4 -#include <stdio.h> 1.5 -#include <stdlib.h> 1.6 -#include <algorithm> 1.7 -#include <goat3dgfx/goat3dgfx.h> 1.8 -#include <vmath/vmath.h> 1.9 - 1.10 -#define CUBEMAP_FILENAME "data/cubemap2.jpg" 1.11 - 1.12 -static bool init(); 1.13 -static void cleanup(); 1.14 -static void display(); 1.15 -static void skybox(const TextureCube *cubemap = 0); 1.16 -static void reshape(int x, int y); 1.17 -static void keyboard(unsigned char key, int x, int y); 1.18 -static void mouse(int bn, int st, int x, int y); 1.19 -static void motion(int x, int y); 1.20 - 1.21 -static float cam_theta, cam_phi; 1.22 - 1.23 -static TextureCube *cubemap; 1.24 -static ShaderProg *sdrsky; 1.25 - 1.26 -int main(int argc, char **argv) 1.27 -{ 1.28 - glutInit(&argc, argv); 1.29 - glutInitWindowSize(800, 600); 1.30 - glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); 1.31 - glutCreateWindow("cubemap"); 1.32 - 1.33 - glutDisplayFunc(display); 1.34 - glutReshapeFunc(reshape); 1.35 - glutKeyboardFunc(keyboard); 1.36 - glutMouseFunc(mouse); 1.37 - glutMotionFunc(motion); 1.38 - 1.39 - if(!init()) { 1.40 - return 1; 1.41 - } 1.42 - atexit(cleanup); 1.43 - 1.44 - glutMainLoop(); 1.45 - return 0; 1.46 -} 1.47 - 1.48 -static bool init() 1.49 -{ 1.50 - glewInit(); 1.51 - 1.52 - glEnable(GL_DEPTH_TEST); 1.53 - //glEnable(GL_CULL_FACE); 1.54 - 1.55 - cubemap = new TextureCube; 1.56 - if(!cubemap->load(CUBEMAP_FILENAME)) { 1.57 - fatal_log("Failed to load cubemap: %s\n", CUBEMAP_FILENAME); 1.58 - return false; 1.59 - } 1.60 - 1.61 - if(!(sdrsky = get_sdrprog("sdr/sky.v.glsl", "sdr/sky.p.glsl"))) { 1.62 - fatal_log("failed to load skybox shader\n"); 1.63 - return false; 1.64 - } 1.65 - 1.66 - return true; 1.67 -} 1.68 - 1.69 -static void cleanup() 1.70 -{ 1.71 - delete cubemap; 1.72 -} 1.73 - 1.74 -static void display() 1.75 -{ 1.76 - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 1.77 - 1.78 - Matrix4x4 view_matrix; 1.79 - view_matrix.rotate(Vector3(1, 0, 0), M_PI * cam_phi / 180.0); 1.80 - view_matrix.rotate(Vector3(0, 1, 0), M_PI * cam_theta / 180.0); 1.81 - set_view_matrix(view_matrix); 1.82 - 1.83 - setup_gl_matrices(); 1.84 - 1.85 - skybox(cubemap); 1.86 - 1.87 - glutSwapBuffers(); 1.88 - CHECKGLERR; 1.89 -} 1.90 - 1.91 -static void skybox(const TextureCube *cubemap) 1.92 -{ 1.93 - static Mesh *skybox; 1.94 - 1.95 - if(!skybox) { 1.96 - skybox = new Mesh; 1.97 - gen_sphere(skybox, 10.0, 12, 6); 1.98 - } 1.99 - 1.100 - glPushAttrib(GL_ENABLE_BIT | GL_TEXTURE_BIT); 1.101 - glDisable(GL_DEPTH_TEST); 1.102 - glDisable(GL_CULL_FACE); 1.103 - glDisable(GL_LIGHTING); 1.104 - 1.105 - glEnable(GL_TEXTURE_CUBE_MAP); 1.106 - 1.107 - if(cubemap) cubemap->bind(); 1.108 - sdrsky->bind(); 1.109 - 1.110 - skybox->draw(); 1.111 - 1.112 - glUseProgram(0); 1.113 - glPopAttrib(); 1.114 -} 1.115 - 1.116 -static void reshape(int x, int y) 1.117 -{ 1.118 - glViewport(0, 0, x, y); 1.119 - 1.120 - Matrix4x4 proj; 1.121 - proj.set_perspective(M_PI / 4.0, (float)x / (float)y, 0.5, 500.0); 1.122 - set_projection_matrix(proj); 1.123 -} 1.124 - 1.125 -static void keyboard(unsigned char key, int x, int y) 1.126 -{ 1.127 - switch(key) { 1.128 - case 27: 1.129 - exit(0); 1.130 - } 1.131 -} 1.132 - 1.133 -static bool bnstate[16]; 1.134 -static int prev_x, prev_y; 1.135 - 1.136 -static void mouse(int bn, int st, int x, int y) 1.137 -{ 1.138 - bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN; 1.139 - prev_x = x; 1.140 - prev_y = y; 1.141 -} 1.142 - 1.143 -static void motion(int x, int y) 1.144 -{ 1.145 - int dx = x - prev_x; 1.146 - int dy = y - prev_y; 1.147 - prev_x = x; 1.148 - prev_y = y; 1.149 - 1.150 - if(!dx && !dy) return; 1.151 - 1.152 - if(bnstate[0]) { 1.153 - cam_theta += dx * 0.5; 1.154 - cam_phi += dy * 0.5; 1.155 - cam_phi = std::max(-90.0f, std::min(90.0f, cam_phi)); 1.156 - glutPostRedisplay(); 1.157 - } 1.158 -} 1.159 +#include <stdio.h> 1.160 +#include <stdlib.h> 1.161 +#include <algorithm> 1.162 +#include <goat3dgfx/goat3dgfx.h> 1.163 +#include <vmath/vmath.h> 1.164 + 1.165 +#define CUBEMAP_FILENAME "data/cubemap2.jpg" 1.166 + 1.167 +static bool init(); 1.168 +static void cleanup(); 1.169 +static void display(); 1.170 +static void skybox(const TextureCube *cubemap = 0); 1.171 +static void reshape(int x, int y); 1.172 +static void keyboard(unsigned char key, int x, int y); 1.173 +static void mouse(int bn, int st, int x, int y); 1.174 +static void motion(int x, int y); 1.175 + 1.176 +static float cam_theta, cam_phi; 1.177 + 1.178 +static TextureCube *cubemap; 1.179 +static ShaderProg *sdrsky; 1.180 + 1.181 +int main(int argc, char **argv) 1.182 +{ 1.183 + glutInit(&argc, argv); 1.184 + glutInitWindowSize(800, 600); 1.185 + glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); 1.186 + glutCreateWindow("cubemap"); 1.187 + 1.188 + glutDisplayFunc(display); 1.189 + glutReshapeFunc(reshape); 1.190 + glutKeyboardFunc(keyboard); 1.191 + glutMouseFunc(mouse); 1.192 + glutMotionFunc(motion); 1.193 + 1.194 + if(!init()) { 1.195 + return 1; 1.196 + } 1.197 + atexit(cleanup); 1.198 + 1.199 + glutMainLoop(); 1.200 + return 0; 1.201 +} 1.202 + 1.203 +static bool init() 1.204 +{ 1.205 + glewInit(); 1.206 + 1.207 + glEnable(GL_DEPTH_TEST); 1.208 + //glEnable(GL_CULL_FACE); 1.209 + 1.210 + cubemap = new TextureCube; 1.211 + if(!cubemap->load(CUBEMAP_FILENAME)) { 1.212 + fatal_log("Failed to load cubemap: %s\n", CUBEMAP_FILENAME); 1.213 + return false; 1.214 + } 1.215 + 1.216 + if(!(sdrsky = get_sdrprog("sdr/sky.v.glsl", "sdr/sky.p.glsl"))) { 1.217 + fatal_log("failed to load skybox shader\n"); 1.218 + return false; 1.219 + } 1.220 + 1.221 + return true; 1.222 +} 1.223 + 1.224 +static void cleanup() 1.225 +{ 1.226 + delete cubemap; 1.227 +} 1.228 + 1.229 +static void display() 1.230 +{ 1.231 + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 1.232 + 1.233 + Matrix4x4 view_matrix; 1.234 + view_matrix.rotate(Vector3(1, 0, 0), M_PI * cam_phi / 180.0); 1.235 + view_matrix.rotate(Vector3(0, 1, 0), M_PI * cam_theta / 180.0); 1.236 + set_view_matrix(view_matrix); 1.237 + 1.238 + setup_gl_matrices(); 1.239 + 1.240 + skybox(cubemap); 1.241 + 1.242 + glutSwapBuffers(); 1.243 + CHECKGLERR; 1.244 +} 1.245 + 1.246 +static void skybox(const TextureCube *cubemap) 1.247 +{ 1.248 + static Mesh *skybox; 1.249 + 1.250 + if(!skybox) { 1.251 + skybox = new Mesh; 1.252 + gen_sphere(skybox, 10.0, 12, 6); 1.253 + } 1.254 + 1.255 + glPushAttrib(GL_ENABLE_BIT | GL_TEXTURE_BIT); 1.256 + glDisable(GL_DEPTH_TEST); 1.257 + glDisable(GL_CULL_FACE); 1.258 + glDisable(GL_LIGHTING); 1.259 + 1.260 + glEnable(GL_TEXTURE_CUBE_MAP); 1.261 + 1.262 + if(cubemap) cubemap->bind(); 1.263 + sdrsky->bind(); 1.264 + 1.265 + skybox->draw(); 1.266 + 1.267 + glUseProgram(0); 1.268 + glPopAttrib(); 1.269 +} 1.270 + 1.271 +static void reshape(int x, int y) 1.272 +{ 1.273 + glViewport(0, 0, x, y); 1.274 + 1.275 + Matrix4x4 proj; 1.276 + proj.set_perspective(M_PI / 4.0, (float)x / (float)y, 0.5, 500.0); 1.277 + set_projection_matrix(proj); 1.278 +} 1.279 + 1.280 +static void keyboard(unsigned char key, int x, int y) 1.281 +{ 1.282 + switch(key) { 1.283 + case 27: 1.284 + exit(0); 1.285 + } 1.286 +} 1.287 + 1.288 +static bool bnstate[16]; 1.289 +static int prev_x, prev_y; 1.290 + 1.291 +static void mouse(int bn, int st, int x, int y) 1.292 +{ 1.293 + bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN; 1.294 + prev_x = x; 1.295 + prev_y = y; 1.296 +} 1.297 + 1.298 +static void motion(int x, int y) 1.299 +{ 1.300 + int dx = x - prev_x; 1.301 + int dy = y - prev_y; 1.302 + prev_x = x; 1.303 + prev_y = y; 1.304 + 1.305 + if(!dx && !dy) return; 1.306 + 1.307 + if(bnstate[0]) { 1.308 + cam_theta += dx * 0.5; 1.309 + cam_phi += dy * 0.5; 1.310 + cam_phi = std::max(-90.0f, std::min(90.0f, cam_phi)); 1.311 + glutPostRedisplay(); 1.312 + } 1.313 +}