goat3dgfx
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/examples/cubemap/src/main.cc Sun Nov 17 03:22:40 2013 +0200 1.3 @@ -0,0 +1,192 @@ 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/cubemap3.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(); 1.86 + 1.87 + /*glBegin(GL_QUADS); 1.88 + glColor3f(1, 1, 1); 1.89 + glNormal3f(0, 1, 0); 1.90 + glVertex3f(-0.8, -1, 0.8); 1.91 + glVertex3f(0.8, -1, 0.8); 1.92 + glVertex3f(0.8, -1, -0.8); 1.93 + glVertex3f(-0.8, -1, -0.8); 1.94 + glEnd();*/ 1.95 + 1.96 + glutSwapBuffers(); 1.97 + CHECKGLERR; 1.98 +} 1.99 + 1.100 +static void skybox(const TextureCube *cubemap) 1.101 +{ 1.102 + glPushAttrib(GL_ENABLE_BIT | GL_TEXTURE_BIT); 1.103 + glDisable(GL_DEPTH_TEST); 1.104 + glDisable(GL_CULL_FACE); 1.105 + glDisable(GL_LIGHTING); 1.106 + 1.107 + if(cubemap) cubemap->bind(); 1.108 + sdrsky->bind(); 1.109 + 1.110 + glBegin(GL_QUADS); 1.111 + // +X 1.112 + glColor3f(1, 0, 0); 1.113 + glVertex3f(1, -1, 1); 1.114 + glVertex3f(1, -1, -1); 1.115 + glVertex3f(1, 1, -1); 1.116 + glVertex3f(1, 1, 1); 1.117 + // -Z 1.118 + glColor3f(0, 1, 0); 1.119 + glVertex3f(1, -1, -1); 1.120 + glVertex3f(-1, -1, -1); 1.121 + glVertex3f(-1, 1, -1); 1.122 + glVertex3f(1, 1, -1); 1.123 + // -X 1.124 + glColor3f(0, 0, 1); 1.125 + glVertex3f(-1, -1, -1); 1.126 + glVertex3f(-1, -1, 1); 1.127 + glVertex3f(-1, 1, 1); 1.128 + glVertex3f(-1, 1, -1); 1.129 + // +Z 1.130 + glColor3f(1, 1, 0); 1.131 + glVertex3f(-1, -1, 1); 1.132 + glVertex3f(1, -1, 1); 1.133 + glVertex3f(1, 1, 1); 1.134 + glVertex3f(-1, 1, 1); 1.135 + // +Y 1.136 + glColor3f(0, 1, 1); 1.137 + glVertex3f(-1, 1, 1); 1.138 + glVertex3f(1, 1, 1); 1.139 + glVertex3f(1, 1, -1); 1.140 + glVertex3f(-1, 1, -1); 1.141 + // -Y 1.142 + glColor3f(1, 0, 1); 1.143 + glVertex3f(-1, -1, -1); 1.144 + glVertex3f(1, -1, -1); 1.145 + glVertex3f(1, -1, 1); 1.146 + glVertex3f(-1, -1, 1); 1.147 + glEnd(); 1.148 + 1.149 + glUseProgram(0); 1.150 + glPopAttrib(); 1.151 +} 1.152 + 1.153 +static void reshape(int x, int y) 1.154 +{ 1.155 + glViewport(0, 0, x, y); 1.156 + 1.157 + Matrix4x4 proj; 1.158 + proj.set_perspective(M_PI / 4.0, (float)x / (float)y, 0.5, 500.0); 1.159 + set_projection_matrix(proj); 1.160 +} 1.161 + 1.162 +static void keyboard(unsigned char key, int x, int y) 1.163 +{ 1.164 + switch(key) { 1.165 + case 27: 1.166 + exit(0); 1.167 + } 1.168 +} 1.169 + 1.170 +static bool bnstate[16]; 1.171 +static int prev_x, prev_y; 1.172 + 1.173 +static void mouse(int bn, int st, int x, int y) 1.174 +{ 1.175 + bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN; 1.176 + prev_x = x; 1.177 + prev_y = y; 1.178 +} 1.179 + 1.180 +static void motion(int x, int y) 1.181 +{ 1.182 + int dx = x - prev_x; 1.183 + int dy = y - prev_y; 1.184 + prev_x = x; 1.185 + prev_y = y; 1.186 + 1.187 + if(!dx && !dy) return; 1.188 + 1.189 + if(bnstate[0]) { 1.190 + cam_theta += dx * 0.5; 1.191 + cam_phi += dy * 0.5; 1.192 + cam_phi = std::max(-90.0f, std::min(90.0f, cam_phi)); 1.193 + glutPostRedisplay(); 1.194 + } 1.195 +}