goat3dgfx

annotate examples/cubemap/src/main.cc @ 7:89179bce7543

property sheets had not been saved previously
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 17 Nov 2013 09:13:27 +0200
parents 18879c956eb1
children 25b911c7c35c
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@6 7 #define CUBEMAP_FILENAME "data/cubemap2.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@6 82 skybox(cubemap);
nuclear@5 83
nuclear@5 84 glutSwapBuffers();
nuclear@5 85 CHECKGLERR;
nuclear@5 86 }
nuclear@5 87
nuclear@5 88 static void skybox(const TextureCube *cubemap)
nuclear@5 89 {
nuclear@6 90 static Mesh *skybox;
nuclear@6 91
nuclear@6 92 if(!skybox) {
nuclear@6 93 skybox = new Mesh;
nuclear@6 94 gen_sphere(skybox, 10.0, 12, 6);
nuclear@6 95 }
nuclear@6 96
nuclear@5 97 glPushAttrib(GL_ENABLE_BIT | GL_TEXTURE_BIT);
nuclear@5 98 glDisable(GL_DEPTH_TEST);
nuclear@5 99 glDisable(GL_CULL_FACE);
nuclear@5 100 glDisable(GL_LIGHTING);
nuclear@5 101
nuclear@6 102 glEnable(GL_TEXTURE_CUBE_MAP);
nuclear@6 103
nuclear@5 104 if(cubemap) cubemap->bind();
nuclear@5 105 sdrsky->bind();
nuclear@5 106
nuclear@6 107 skybox->draw();
nuclear@5 108
nuclear@5 109 glUseProgram(0);
nuclear@5 110 glPopAttrib();
nuclear@5 111 }
nuclear@5 112
nuclear@5 113 static void reshape(int x, int y)
nuclear@5 114 {
nuclear@5 115 glViewport(0, 0, x, y);
nuclear@5 116
nuclear@5 117 Matrix4x4 proj;
nuclear@5 118 proj.set_perspective(M_PI / 4.0, (float)x / (float)y, 0.5, 500.0);
nuclear@5 119 set_projection_matrix(proj);
nuclear@5 120 }
nuclear@5 121
nuclear@5 122 static void keyboard(unsigned char key, int x, int y)
nuclear@5 123 {
nuclear@5 124 switch(key) {
nuclear@5 125 case 27:
nuclear@5 126 exit(0);
nuclear@5 127 }
nuclear@5 128 }
nuclear@5 129
nuclear@5 130 static bool bnstate[16];
nuclear@5 131 static int prev_x, prev_y;
nuclear@5 132
nuclear@5 133 static void mouse(int bn, int st, int x, int y)
nuclear@5 134 {
nuclear@5 135 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
nuclear@5 136 prev_x = x;
nuclear@5 137 prev_y = y;
nuclear@5 138 }
nuclear@5 139
nuclear@5 140 static void motion(int x, int y)
nuclear@5 141 {
nuclear@5 142 int dx = x - prev_x;
nuclear@5 143 int dy = y - prev_y;
nuclear@5 144 prev_x = x;
nuclear@5 145 prev_y = y;
nuclear@5 146
nuclear@5 147 if(!dx && !dy) return;
nuclear@5 148
nuclear@5 149 if(bnstate[0]) {
nuclear@5 150 cam_theta += dx * 0.5;
nuclear@5 151 cam_phi += dy * 0.5;
nuclear@5 152 cam_phi = std::max(-90.0f, std::min(90.0f, cam_phi));
nuclear@5 153 glutPostRedisplay();
nuclear@5 154 }
nuclear@5 155 }