goat3dgfx

annotate examples/cubemap/src/main.cc @ 15:7d6b667821cf

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