goat3dgfx

view examples/viewscn/src/main.cc @ 22:92bfb0206969

- made all XFormNode functions virtual - added XFormNode::get_parent()
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 28 Dec 2013 06:48:23 +0200
parents f61cc1df533c
children 6236080aaea4
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, cam_dist = 8;
19 static const char *scene_filename;
20 static Scene *scn;
22 static ShaderProg *sdr;
24 int main(int argc, char **argv)
25 {
26 glutInit(&argc, argv);
27 glutInitWindowSize(800, 600);
28 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
29 glutCreateWindow("viewscn");
31 glutDisplayFunc(display);
32 glutReshapeFunc(reshape);
33 glutKeyboardFunc(keyboard);
34 glutMouseFunc(mouse);
35 glutMotionFunc(motion);
37 if(!parse_args(argc, argv)) {
38 return 1;
39 }
41 if(!init()) {
42 return 1;
43 }
44 atexit(cleanup);
46 glutMainLoop();
47 return 0;
48 }
50 static bool init()
51 {
52 glewInit();
54 glClearColor(0.1, 0.1, 0.1, 1);
56 glEnable(GL_DEPTH_TEST);
57 glEnable(GL_CULL_FACE);
59 if(!(sdr = get_sdrprog("sdr/foo.v.glsl", "sdr/foo.p.glsl"))) {
60 return false;
61 }
63 scn = new Scene;
64 if(!scn->load(scene_filename)) {
65 fatal_log("failed to load scene: %s\n", scene_filename);
66 return false;
67 }
69 return true;
70 }
72 static void cleanup()
73 {
74 delete scn;
75 }
77 static void display()
78 {
79 glClearColor(0.25, 0.25, 0.25, 1);
80 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
82 Matrix4x4 view_matrix;
83 view_matrix.translate(Vector3(0, 0, -cam_dist));
84 view_matrix.rotate(Vector3(1, 0, 0), M_PI * cam_phi / 180.0);
85 view_matrix.rotate(Vector3(0, 1, 0), M_PI * cam_theta / 180.0);
86 set_view_matrix(view_matrix);
88 //setup_gl_matrices();
90 bind_shader(sdr);
91 scn->draw();
92 bind_shader(0);
94 /*glBegin(GL_QUADS);
95 glColor3f(1, 1, 1);
96 glVertex3f(-1, -1, 0);
97 glVertex3f(1, -1, 0);
98 glVertex3f(1, 1, 0);
99 glVertex3f(-1, 1, 0);
100 glEnd();*/
102 glutSwapBuffers();
103 CHECKGLERR;
104 }
106 static void reshape(int x, int y)
107 {
108 glViewport(0, 0, x, y);
110 Matrix4x4 proj;
111 proj.set_perspective(M_PI / 4.0, (float)x / (float)y, 0.5, 500.0);
112 set_projection_matrix(proj);
113 }
115 static void keyboard(unsigned char key, int x, int y)
116 {
117 switch(key) {
118 case 27:
119 exit(0);
120 }
121 }
123 static bool bnstate[16];
124 static int prev_x, prev_y;
126 static void mouse(int bn, int st, int x, int y)
127 {
128 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
129 prev_x = x;
130 prev_y = y;
131 }
133 static void motion(int x, int y)
134 {
135 int dx = x - prev_x;
136 int dy = y - prev_y;
137 prev_x = x;
138 prev_y = y;
140 if(!dx && !dy) return;
142 if(bnstate[0]) {
143 cam_theta += dx * 0.5;
144 cam_phi += dy * 0.5;
145 cam_phi = std::max(-90.0f, std::min(90.0f, cam_phi));
146 glutPostRedisplay();
147 }
148 }
150 static bool parse_args(int argc, char **argv)
151 {
152 for(int i=1; i<argc; i++) {
153 if(argv[i][0] == '-' && argv[i][2] == 0) {
154 switch(argv[i][1]) {
155 case 'h':
156 default:
157 printf("usage: %s <filename>\n", argv[i]);
158 exit(0);
159 }
160 } else {
161 if(scene_filename) {
162 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
163 return false;
164 }
165 scene_filename = argv[i];
166 }
167 }
169 if(!scene_filename) {
170 fprintf(stderr, "you must specify the scene file to view\n");
171 return false;
172 }
174 return true;
175 }