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@6
|
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@6
|
70 COCAST(CoPRS, obj->get_component("prs"))->pos = Vector3(5, 1, 0);
|
nuclear@6
|
71 objects.push_back(obj);
|
nuclear@6
|
72
|
nuclear@6
|
73 obj = new GObject;
|
nuclear@6
|
74 obj->add_component(new CoSphereVis);
|
nuclear@6
|
75 obj->add_component(new CoXForm);
|
nuclear@6
|
76 obj->add_component(new CoPRS);
|
nuclear@6
|
77 COCAST(CoPRS, obj->get_component("prs"))->pos = Vector3(-5, 1, 0);
|
nuclear@6
|
78 objects.push_back(obj);
|
nuclear@6
|
79
|
nuclear@0
|
80 return true;
|
nuclear@0
|
81 }
|
nuclear@0
|
82
|
nuclear@0
|
83 static void cleanup()
|
nuclear@0
|
84 {
|
nuclear@0
|
85 }
|
nuclear@0
|
86
|
nuclear@6
|
87 static void update()
|
nuclear@6
|
88 {
|
nuclear@6
|
89 static unsigned int prev_upd;
|
nuclear@6
|
90 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
|
nuclear@6
|
91 float dt = (float)(msec - prev_upd) / 1000.0f;
|
nuclear@6
|
92 prev_upd = msec;
|
nuclear@6
|
93
|
nuclear@6
|
94 for(size_t i=0; i<objects.size(); i++) {
|
nuclear@6
|
95 objects[i]->update(dt);
|
nuclear@6
|
96 }
|
nuclear@6
|
97 }
|
nuclear@6
|
98
|
nuclear@0
|
99 static void display()
|
nuclear@0
|
100 {
|
nuclear@6
|
101 update();
|
nuclear@6
|
102
|
nuclear@0
|
103 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@0
|
104
|
nuclear@6
|
105 glMatrixMode(GL_MODELVIEW);
|
nuclear@6
|
106 glLoadIdentity();
|
nuclear@6
|
107 glTranslatef(0, 0, -cam_dist);
|
nuclear@6
|
108 glRotatef(cam_phi, 1, 0, 0);
|
nuclear@6
|
109 glRotatef(cam_theta, 0, 1, 0);
|
nuclear@6
|
110
|
nuclear@6
|
111 float floor_col[] = {0.2, 0.8, 0.2, 1};
|
nuclear@6
|
112 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, floor_col);
|
nuclear@6
|
113
|
nuclear@6
|
114 glBegin(GL_QUADS);
|
nuclear@6
|
115 glNormal3f(0, 1, 0);
|
nuclear@6
|
116 glVertex3f(-10, 0, 10);
|
nuclear@6
|
117 glVertex3f(10, 0, 10);
|
nuclear@6
|
118 glVertex3f(10, 0, -10);
|
nuclear@6
|
119 glVertex3f(-10, 0, -10);
|
nuclear@6
|
120 glEnd();
|
nuclear@6
|
121
|
nuclear@6
|
122 for(size_t i=0; i<objects.size(); i++) {
|
nuclear@6
|
123 CoSphereVis *co = COCAST(CoSphereVis, objects[i]->get_component("spherevis"));
|
nuclear@6
|
124 if(co) {
|
nuclear@6
|
125 co->draw();
|
nuclear@6
|
126 }
|
nuclear@6
|
127 }
|
nuclear@6
|
128
|
nuclear@0
|
129 glutSwapBuffers();
|
nuclear@0
|
130 assert(glGetError() == GL_NO_ERROR);
|
nuclear@0
|
131 }
|
nuclear@0
|
132
|
nuclear@0
|
133 static void idle()
|
nuclear@0
|
134 {
|
nuclear@0
|
135 glutPostRedisplay();
|
nuclear@0
|
136 }
|
nuclear@0
|
137
|
nuclear@0
|
138 static void reshape(int x, int y)
|
nuclear@0
|
139 {
|
nuclear@0
|
140 glViewport(0, 0, x, y);
|
nuclear@0
|
141
|
nuclear@2
|
142 glMatrixMode(GL_PROJECTION);
|
nuclear@0
|
143 glLoadIdentity();
|
nuclear@0
|
144 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
|
nuclear@0
|
145 }
|
nuclear@0
|
146
|
nuclear@0
|
147 static void keyb(unsigned char key, int x, int y)
|
nuclear@0
|
148 {
|
nuclear@0
|
149 switch(key) {
|
nuclear@0
|
150 case 27:
|
nuclear@0
|
151 exit(0);
|
nuclear@0
|
152 }
|
nuclear@0
|
153 }
|
nuclear@0
|
154
|
nuclear@6
|
155 static bool bnstate[16];
|
nuclear@0
|
156 static int prev_x, prev_y;
|
nuclear@0
|
157
|
nuclear@0
|
158 static void mouse(int bn, int st, int x, int y)
|
nuclear@0
|
159 {
|
nuclear@6
|
160 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
|
nuclear@6
|
161 prev_x = x;
|
nuclear@6
|
162 prev_y = y;
|
nuclear@0
|
163 }
|
nuclear@0
|
164
|
nuclear@0
|
165 static void motion(int x, int y)
|
nuclear@0
|
166 {
|
nuclear@6
|
167 int dx = x - prev_x;
|
nuclear@6
|
168 int dy = y - prev_y;
|
nuclear@6
|
169 prev_x = x;
|
nuclear@6
|
170 prev_y = y;
|
nuclear@6
|
171
|
nuclear@6
|
172 if(bnstate[0]) {
|
nuclear@6
|
173 cam_theta += dx * 0.5;
|
nuclear@6
|
174 cam_phi += dy * 0.5;
|
nuclear@6
|
175
|
nuclear@6
|
176 if(cam_phi < -90) cam_phi = -90;
|
nuclear@6
|
177 if(cam_phi > 90) cam_phi = 90;
|
nuclear@6
|
178
|
nuclear@6
|
179 glutPostRedisplay();
|
nuclear@6
|
180 }
|
nuclear@6
|
181 if(bnstate[2]) {
|
nuclear@6
|
182 cam_dist += dy * 0.1;
|
nuclear@6
|
183
|
nuclear@6
|
184 if(cam_dist < 0.0) cam_dist = 0.0;
|
nuclear@6
|
185 }
|
nuclear@0
|
186 }
|