goat3dgfx

view examples/viewscn/src/main.cc @ 16:f61cc1df533c

added viewscn example (under dev)
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 30 Nov 2013 20:53:26 +0200
parents
children 6f82b9b6d6c3
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <algorithm>
4 #include <goat3dgfx/goat3dgfx.h>
5 #include <vmath/vmath.h>
7 using namespace goatgfx;
9 static bool init();
10 static void cleanup();
11 static void display();
12 static void reshape(int x, int y);
13 static void keyboard(unsigned char key, int x, int y);
14 static void mouse(int bn, int st, int x, int y);
15 static void motion(int x, int y);
16 static bool parse_args(int argc, char **argv);
18 static float cam_theta, cam_phi;
19 static const char *scene_filename;
20 static Scene *scn;
22 int main(int argc, char **argv)
23 {
24 glutInit(&argc, argv);
25 glutInitWindowSize(800, 600);
26 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
27 glutCreateWindow("viewscn");
29 glutDisplayFunc(display);
30 glutReshapeFunc(reshape);
31 glutKeyboardFunc(keyboard);
32 glutMouseFunc(mouse);
33 glutMotionFunc(motion);
35 if(!parse_args(argc, argv)) {
36 return 1;
37 }
39 if(!init()) {
40 return 1;
41 }
42 atexit(cleanup);
44 glutMainLoop();
45 return 0;
46 }
48 static bool init()
49 {
50 glewInit();
52 glEnable(GL_DEPTH_TEST);
53 glEnable(GL_CULL_FACE);
55 scn = new Scene;
56 if(!scn->load(scene_filename)) {
57 fatal_log("failed to load scene: %s\n", scene_filename);
58 return false;
59 }
61 return true;
62 }
64 static void cleanup()
65 {
66 delete scn;
67 }
69 static void display()
70 {
71 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
73 Matrix4x4 view_matrix;
74 view_matrix.rotate(Vector3(1, 0, 0), M_PI * cam_phi / 180.0);
75 view_matrix.rotate(Vector3(0, 1, 0), M_PI * cam_theta / 180.0);
76 set_view_matrix(view_matrix);
78 setup_gl_matrices();
80 scn->draw();
82 glutSwapBuffers();
83 CHECKGLERR;
84 }
86 static void reshape(int x, int y)
87 {
88 glViewport(0, 0, x, y);
90 Matrix4x4 proj;
91 proj.set_perspective(M_PI / 4.0, (float)x / (float)y, 0.5, 500.0);
92 set_projection_matrix(proj);
93 }
95 static void keyboard(unsigned char key, int x, int y)
96 {
97 switch(key) {
98 case 27:
99 exit(0);
100 }
101 }
103 static bool bnstate[16];
104 static int prev_x, prev_y;
106 static void mouse(int bn, int st, int x, int y)
107 {
108 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
109 prev_x = x;
110 prev_y = y;
111 }
113 static void motion(int x, int y)
114 {
115 int dx = x - prev_x;
116 int dy = y - prev_y;
117 prev_x = x;
118 prev_y = y;
120 if(!dx && !dy) return;
122 if(bnstate[0]) {
123 cam_theta += dx * 0.5;
124 cam_phi += dy * 0.5;
125 cam_phi = std::max(-90.0f, std::min(90.0f, cam_phi));
126 glutPostRedisplay();
127 }
128 }
130 static bool parse_args(int argc, char **argv)
131 {
132 for(int i=1; i<argc; i++) {
133 if(argv[i][0] == '-' && argv[i][2] == 0) {
134 switch(argv[i][1]) {
135 case 'h':
136 default:
137 printf("usage: %s <filename>\n", argv[i]);
138 exit(0);
139 }
140 } else {
141 if(scene_filename) {
142 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
143 return false;
144 }
145 scene_filename = argv[i];
146 }
147 }
149 if(!scene_filename) {
150 fprintf(stderr, "you must specify the scene file to view\n");
151 return false;
152 }
154 return true;
155 }