rev |
line source |
nuclear@0
|
1 #include <stdio.h>
|
nuclear@0
|
2 #include <stdlib.h>
|
nuclear@0
|
3 #include <assert.h>
|
nuclear@6
|
4 #include <vector>
|
nuclear@0
|
5
|
nuclear@0
|
6 #ifndef __APPLE__
|
nuclear@0
|
7 #include <GL/glut.h>
|
nuclear@0
|
8 #else
|
nuclear@0
|
9 #include <GLUT/glut.h>
|
nuclear@0
|
10 #endif
|
nuclear@0
|
11
|
nuclear@6
|
12 #include "gobj.h"
|
nuclear@6
|
13 #include "co_xform.h"
|
nuclear@6
|
14 #include "co_dbgvis.h"
|
nuclear@6
|
15 #include "sim.h"
|
nuclear@6
|
16
|
nuclear@0
|
17 static bool init();
|
nuclear@0
|
18 static void cleanup();
|
nuclear@0
|
19 static void display();
|
nuclear@0
|
20 static void idle();
|
nuclear@0
|
21 static void reshape(int x, int y);
|
nuclear@0
|
22 static void keyb(unsigned char key, int x, int y);
|
nuclear@0
|
23 static void mouse(int bn, int st, int x, int y);
|
nuclear@0
|
24 static void motion(int x, int y);
|
nuclear@0
|
25
|
nuclear@6
|
26 float cam_theta, cam_phi = 25, cam_dist = 8;
|
nuclear@6
|
27
|
nuclear@6
|
28 std::vector<GObject*> objects;
|
nuclear@6
|
29 SimWorld simworld;
|
nuclear@6
|
30
|
nuclear@0
|
31
|
nuclear@0
|
32 int main(int argc, char **argv)
|
nuclear@0
|
33 {
|
nuclear@0
|
34 glutInit(&argc, argv);
|
nuclear@0
|
35 glutInitWindowSize(800, 600);
|
nuclear@0
|
36 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
|
nuclear@0
|
37 glutCreateWindow("component system test");
|
nuclear@0
|
38
|
nuclear@0
|
39 glutDisplayFunc(display);
|
nuclear@7
|
40 glutIdleFunc(idle);
|
nuclear@0
|
41 glutReshapeFunc(reshape);
|
nuclear@0
|
42 glutKeyboardFunc(keyb);
|
nuclear@0
|
43 glutMouseFunc(mouse);
|
nuclear@0
|
44 glutMotionFunc(motion);
|
nuclear@0
|
45
|
nuclear@0
|
46 if(!init()) {
|
nuclear@0
|
47 return 1;
|
nuclear@0
|
48 }
|
nuclear@0
|
49 atexit(cleanup);
|
nuclear@0
|
50
|
nuclear@0
|
51 glutMainLoop();
|
nuclear@0
|
52 return 0;
|
nuclear@0
|
53 }
|
nuclear@0
|
54
|
nuclear@0
|
55 static bool init()
|
nuclear@0
|
56 {
|
nuclear@0
|
57 glEnable(GL_DEPTH_TEST);
|
nuclear@0
|
58 glEnable(GL_CULL_FACE);
|
nuclear@0
|
59
|
nuclear@6
|
60 glEnable(GL_LIGHTING);
|
nuclear@6
|
61 glEnable(GL_LIGHT0);
|
nuclear@6
|
62 float ldir[] = {-1, 1, 2, 0};
|
nuclear@6
|
63 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
|
nuclear@6
|
64
|
nuclear@6
|
65 GObject *obj = new GObject;
|
nuclear@6
|
66 obj->add_component(new CoSphereVis);
|
nuclear@6
|
67 obj->add_component(new CoXForm);
|
nuclear@6
|
68 obj->add_component(new CoPRS);
|
nuclear@6
|
69 obj->add_component(new CoRigid);
|
nuclear@7
|
70 COCAST(CoPRS, obj->get_component("prs"))->pos = Vector3(5, 2, 0);
|
nuclear@6
|
71 objects.push_back(obj);
|
nuclear@7
|
72 simworld.add_object(obj);
|
nuclear@6
|
73
|
nuclear@6
|
74 obj = new GObject;
|
nuclear@6
|
75 obj->add_component(new CoSphereVis);
|
nuclear@6
|
76 obj->add_component(new CoXForm);
|
nuclear@6
|
77 obj->add_component(new CoPRS);
|
nuclear@6
|
78 COCAST(CoPRS, obj->get_component("prs"))->pos = Vector3(-5, 1, 0);
|
nuclear@6
|
79 objects.push_back(obj);
|
nuclear@6
|
80
|
nuclear@0
|
81 return true;
|
nuclear@0
|
82 }
|
nuclear@0
|
83
|
nuclear@0
|
84 static void cleanup()
|
nuclear@0
|
85 {
|
nuclear@0
|
86 }
|
nuclear@0
|
87
|
nuclear@6
|
88 static void update()
|
nuclear@6
|
89 {
|
nuclear@6
|
90 static unsigned int prev_upd;
|
nuclear@6
|
91 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
|
nuclear@6
|
92 float dt = (float)(msec - prev_upd) / 1000.0f;
|
nuclear@6
|
93 prev_upd = msec;
|
nuclear@6
|
94
|
nuclear@6
|
95 for(size_t i=0; i<objects.size(); i++) {
|
nuclear@6
|
96 objects[i]->update(dt);
|
nuclear@6
|
97 }
|
nuclear@6
|
98 }
|
nuclear@6
|
99
|
nuclear@0
|
100 static void display()
|
nuclear@0
|
101 {
|
nuclear@6
|
102 update();
|
nuclear@6
|
103
|
nuclear@0
|
104 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@0
|
105
|
nuclear@6
|
106 glMatrixMode(GL_MODELVIEW);
|
nuclear@6
|
107 glLoadIdentity();
|
nuclear@6
|
108 glTranslatef(0, 0, -cam_dist);
|
nuclear@6
|
109 glRotatef(cam_phi, 1, 0, 0);
|
nuclear@6
|
110 glRotatef(cam_theta, 0, 1, 0);
|
nuclear@6
|
111
|
nuclear@6
|
112 float floor_col[] = {0.2, 0.8, 0.2, 1};
|
nuclear@6
|
113 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, floor_col);
|
nuclear@6
|
114
|
nuclear@6
|
115 glBegin(GL_QUADS);
|
nuclear@6
|
116 glNormal3f(0, 1, 0);
|
nuclear@6
|
117 glVertex3f(-10, 0, 10);
|
nuclear@6
|
118 glVertex3f(10, 0, 10);
|
nuclear@6
|
119 glVertex3f(10, 0, -10);
|
nuclear@6
|
120 glVertex3f(-10, 0, -10);
|
nuclear@6
|
121 glEnd();
|
nuclear@6
|
122
|
nuclear@6
|
123 for(size_t i=0; i<objects.size(); i++) {
|
nuclear@6
|
124 CoSphereVis *co = COCAST(CoSphereVis, objects[i]->get_component("spherevis"));
|
nuclear@6
|
125 if(co) {
|
nuclear@6
|
126 co->draw();
|
nuclear@6
|
127 }
|
nuclear@6
|
128 }
|
nuclear@6
|
129
|
nuclear@0
|
130 glutSwapBuffers();
|
nuclear@0
|
131 assert(glGetError() == GL_NO_ERROR);
|
nuclear@0
|
132 }
|
nuclear@0
|
133
|
nuclear@0
|
134 static void idle()
|
nuclear@0
|
135 {
|
nuclear@0
|
136 glutPostRedisplay();
|
nuclear@0
|
137 }
|
nuclear@0
|
138
|
nuclear@0
|
139 static void reshape(int x, int y)
|
nuclear@0
|
140 {
|
nuclear@0
|
141 glViewport(0, 0, x, y);
|
nuclear@0
|
142
|
nuclear@2
|
143 glMatrixMode(GL_PROJECTION);
|
nuclear@0
|
144 glLoadIdentity();
|
nuclear@0
|
145 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
|
nuclear@0
|
146 }
|
nuclear@0
|
147
|
nuclear@0
|
148 static void keyb(unsigned char key, int x, int y)
|
nuclear@0
|
149 {
|
nuclear@0
|
150 switch(key) {
|
nuclear@0
|
151 case 27:
|
nuclear@0
|
152 exit(0);
|
nuclear@0
|
153 }
|
nuclear@0
|
154 }
|
nuclear@0
|
155
|
nuclear@6
|
156 static bool bnstate[16];
|
nuclear@0
|
157 static int prev_x, prev_y;
|
nuclear@0
|
158
|
nuclear@0
|
159 static void mouse(int bn, int st, int x, int y)
|
nuclear@0
|
160 {
|
nuclear@6
|
161 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
|
nuclear@6
|
162 prev_x = x;
|
nuclear@6
|
163 prev_y = y;
|
nuclear@0
|
164 }
|
nuclear@0
|
165
|
nuclear@0
|
166 static void motion(int x, int y)
|
nuclear@0
|
167 {
|
nuclear@6
|
168 int dx = x - prev_x;
|
nuclear@6
|
169 int dy = y - prev_y;
|
nuclear@6
|
170 prev_x = x;
|
nuclear@6
|
171 prev_y = y;
|
nuclear@6
|
172
|
nuclear@6
|
173 if(bnstate[0]) {
|
nuclear@6
|
174 cam_theta += dx * 0.5;
|
nuclear@6
|
175 cam_phi += dy * 0.5;
|
nuclear@6
|
176
|
nuclear@6
|
177 if(cam_phi < -90) cam_phi = -90;
|
nuclear@6
|
178 if(cam_phi > 90) cam_phi = 90;
|
nuclear@6
|
179
|
nuclear@6
|
180 glutPostRedisplay();
|
nuclear@6
|
181 }
|
nuclear@6
|
182 if(bnstate[2]) {
|
nuclear@6
|
183 cam_dist += dy * 0.1;
|
nuclear@6
|
184
|
nuclear@6
|
185 if(cam_dist < 0.0) cam_dist = 0.0;
|
nuclear@6
|
186 }
|
nuclear@0
|
187 }
|