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