goat3dgfx

annotate 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
rev   line source
nuclear@5 1 #include <stdio.h>
nuclear@5 2 #include <stdlib.h>
nuclear@5 3 #include <algorithm>
nuclear@5 4 #include <goat3dgfx/goat3dgfx.h>
nuclear@5 5 #include <vmath/vmath.h>
nuclear@5 6
nuclear@5 7 #define CUBEMAP_FILENAME "data/cubemap3.jpg"
nuclear@5 8
nuclear@5 9 static bool init();
nuclear@5 10 static void cleanup();
nuclear@5 11 static void display();
nuclear@5 12 static void skybox(const TextureCube *cubemap = 0);
nuclear@5 13 static void reshape(int x, int y);
nuclear@5 14 static void keyboard(unsigned char key, int x, int y);
nuclear@5 15 static void mouse(int bn, int st, int x, int y);
nuclear@5 16 static void motion(int x, int y);
nuclear@5 17
nuclear@5 18 static float cam_theta, cam_phi;
nuclear@5 19
nuclear@5 20 static TextureCube *cubemap;
nuclear@5 21 static ShaderProg *sdrsky;
nuclear@5 22
nuclear@5 23 int main(int argc, char **argv)
nuclear@5 24 {
nuclear@5 25 glutInit(&argc, argv);
nuclear@5 26 glutInitWindowSize(800, 600);
nuclear@5 27 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
nuclear@5 28 glutCreateWindow("cubemap");
nuclear@5 29
nuclear@5 30 glutDisplayFunc(display);
nuclear@5 31 glutReshapeFunc(reshape);
nuclear@5 32 glutKeyboardFunc(keyboard);
nuclear@5 33 glutMouseFunc(mouse);
nuclear@5 34 glutMotionFunc(motion);
nuclear@5 35
nuclear@5 36 if(!init()) {
nuclear@5 37 return 1;
nuclear@5 38 }
nuclear@5 39 atexit(cleanup);
nuclear@5 40
nuclear@5 41 glutMainLoop();
nuclear@5 42 return 0;
nuclear@5 43 }
nuclear@5 44
nuclear@5 45 static bool init()
nuclear@5 46 {
nuclear@5 47 glewInit();
nuclear@5 48
nuclear@5 49 glEnable(GL_DEPTH_TEST);
nuclear@5 50 //glEnable(GL_CULL_FACE);
nuclear@5 51
nuclear@5 52 cubemap = new TextureCube;
nuclear@5 53 if(!cubemap->load(CUBEMAP_FILENAME)) {
nuclear@5 54 fatal_log("Failed to load cubemap: %s\n", CUBEMAP_FILENAME);
nuclear@5 55 return false;
nuclear@5 56 }
nuclear@5 57
nuclear@5 58 if(!(sdrsky = get_sdrprog("sdr/sky.v.glsl", "sdr/sky.p.glsl"))) {
nuclear@5 59 fatal_log("failed to load skybox shader\n");
nuclear@5 60 return false;
nuclear@5 61 }
nuclear@5 62
nuclear@5 63 return true;
nuclear@5 64 }
nuclear@5 65
nuclear@5 66 static void cleanup()
nuclear@5 67 {
nuclear@5 68 delete cubemap;
nuclear@5 69 }
nuclear@5 70
nuclear@5 71 static void display()
nuclear@5 72 {
nuclear@5 73 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@5 74
nuclear@5 75 Matrix4x4 view_matrix;
nuclear@5 76 view_matrix.rotate(Vector3(1, 0, 0), M_PI * cam_phi / 180.0);
nuclear@5 77 view_matrix.rotate(Vector3(0, 1, 0), M_PI * cam_theta / 180.0);
nuclear@5 78 set_view_matrix(view_matrix);
nuclear@5 79
nuclear@5 80 setup_gl_matrices();
nuclear@5 81
nuclear@5 82 skybox();
nuclear@5 83
nuclear@5 84 /*glBegin(GL_QUADS);
nuclear@5 85 glColor3f(1, 1, 1);
nuclear@5 86 glNormal3f(0, 1, 0);
nuclear@5 87 glVertex3f(-0.8, -1, 0.8);
nuclear@5 88 glVertex3f(0.8, -1, 0.8);
nuclear@5 89 glVertex3f(0.8, -1, -0.8);
nuclear@5 90 glVertex3f(-0.8, -1, -0.8);
nuclear@5 91 glEnd();*/
nuclear@5 92
nuclear@5 93 glutSwapBuffers();
nuclear@5 94 CHECKGLERR;
nuclear@5 95 }
nuclear@5 96
nuclear@5 97 static void skybox(const TextureCube *cubemap)
nuclear@5 98 {
nuclear@5 99 glPushAttrib(GL_ENABLE_BIT | GL_TEXTURE_BIT);
nuclear@5 100 glDisable(GL_DEPTH_TEST);
nuclear@5 101 glDisable(GL_CULL_FACE);
nuclear@5 102 glDisable(GL_LIGHTING);
nuclear@5 103
nuclear@5 104 if(cubemap) cubemap->bind();
nuclear@5 105 sdrsky->bind();
nuclear@5 106
nuclear@5 107 glBegin(GL_QUADS);
nuclear@5 108 // +X
nuclear@5 109 glColor3f(1, 0, 0);
nuclear@5 110 glVertex3f(1, -1, 1);
nuclear@5 111 glVertex3f(1, -1, -1);
nuclear@5 112 glVertex3f(1, 1, -1);
nuclear@5 113 glVertex3f(1, 1, 1);
nuclear@5 114 // -Z
nuclear@5 115 glColor3f(0, 1, 0);
nuclear@5 116 glVertex3f(1, -1, -1);
nuclear@5 117 glVertex3f(-1, -1, -1);
nuclear@5 118 glVertex3f(-1, 1, -1);
nuclear@5 119 glVertex3f(1, 1, -1);
nuclear@5 120 // -X
nuclear@5 121 glColor3f(0, 0, 1);
nuclear@5 122 glVertex3f(-1, -1, -1);
nuclear@5 123 glVertex3f(-1, -1, 1);
nuclear@5 124 glVertex3f(-1, 1, 1);
nuclear@5 125 glVertex3f(-1, 1, -1);
nuclear@5 126 // +Z
nuclear@5 127 glColor3f(1, 1, 0);
nuclear@5 128 glVertex3f(-1, -1, 1);
nuclear@5 129 glVertex3f(1, -1, 1);
nuclear@5 130 glVertex3f(1, 1, 1);
nuclear@5 131 glVertex3f(-1, 1, 1);
nuclear@5 132 // +Y
nuclear@5 133 glColor3f(0, 1, 1);
nuclear@5 134 glVertex3f(-1, 1, 1);
nuclear@5 135 glVertex3f(1, 1, 1);
nuclear@5 136 glVertex3f(1, 1, -1);
nuclear@5 137 glVertex3f(-1, 1, -1);
nuclear@5 138 // -Y
nuclear@5 139 glColor3f(1, 0, 1);
nuclear@5 140 glVertex3f(-1, -1, -1);
nuclear@5 141 glVertex3f(1, -1, -1);
nuclear@5 142 glVertex3f(1, -1, 1);
nuclear@5 143 glVertex3f(-1, -1, 1);
nuclear@5 144 glEnd();
nuclear@5 145
nuclear@5 146 glUseProgram(0);
nuclear@5 147 glPopAttrib();
nuclear@5 148 }
nuclear@5 149
nuclear@5 150 static void reshape(int x, int y)
nuclear@5 151 {
nuclear@5 152 glViewport(0, 0, x, y);
nuclear@5 153
nuclear@5 154 Matrix4x4 proj;
nuclear@5 155 proj.set_perspective(M_PI / 4.0, (float)x / (float)y, 0.5, 500.0);
nuclear@5 156 set_projection_matrix(proj);
nuclear@5 157 }
nuclear@5 158
nuclear@5 159 static void keyboard(unsigned char key, int x, int y)
nuclear@5 160 {
nuclear@5 161 switch(key) {
nuclear@5 162 case 27:
nuclear@5 163 exit(0);
nuclear@5 164 }
nuclear@5 165 }
nuclear@5 166
nuclear@5 167 static bool bnstate[16];
nuclear@5 168 static int prev_x, prev_y;
nuclear@5 169
nuclear@5 170 static void mouse(int bn, int st, int x, int y)
nuclear@5 171 {
nuclear@5 172 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
nuclear@5 173 prev_x = x;
nuclear@5 174 prev_y = y;
nuclear@5 175 }
nuclear@5 176
nuclear@5 177 static void motion(int x, int y)
nuclear@5 178 {
nuclear@5 179 int dx = x - prev_x;
nuclear@5 180 int dy = y - prev_y;
nuclear@5 181 prev_x = x;
nuclear@5 182 prev_y = y;
nuclear@5 183
nuclear@5 184 if(!dx && !dy) return;
nuclear@5 185
nuclear@5 186 if(bnstate[0]) {
nuclear@5 187 cam_theta += dx * 0.5;
nuclear@5 188 cam_phi += dy * 0.5;
nuclear@5 189 cam_phi = std::max(-90.0f, std::min(90.0f, cam_phi));
nuclear@5 190 glutPostRedisplay();
nuclear@5 191 }
nuclear@5 192 }