grav

view src/main.cc @ 1:3d541da6e48c

lalalala
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 23 May 2014 18:27:42 +0300
parents 68db0e456733
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <GL/glut.h>
5 #include "sim.h"
7 static bool init();
8 static void cleanup();
9 static void update(long tmsec);
10 static void display();
11 static void idle();
12 static void reshape(int x, int y);
13 static void keyb(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 void draw_grid(float sz, int nlines, float alpha);
18 static SimWorld sim;
19 static float cam_theta, cam_phi = 25, cam_dist = 8;
21 int main(int argc, char **argv)
22 {
23 glutInitWindowSize(1024, 768);
24 glutInit(&argc, argv);
25 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
26 glutCreateWindow("gravity");
28 glutDisplayFunc(display);
29 glutIdleFunc(idle);
30 glutReshapeFunc(reshape);
31 glutKeyboardFunc(keyb);
32 glutMouseFunc(mouse);
33 glutMotionFunc(motion);
35 if(!init()) {
36 return 1;
37 }
38 atexit(cleanup);
40 glutMainLoop();
41 return 0;
42 }
45 static bool init()
46 {
47 glEnable(GL_DEPTH_TEST);
48 glEnable(GL_CULL_FACE);
49 glEnable(GL_LIGHTING);
50 glEnable(GL_LIGHT0);
52 Particle p;
53 p.mass = 10.0;
54 sim.add_particle(p);
55 return true;
56 }
58 static void cleanup()
59 {
60 }
62 static void update(long tmsec)
63 {
64 static long prev_upd;
65 long interval = tmsec - prev_upd;
67 if(interval >= 1000 / 60) {
68 prev_upd = tmsec;
70 float dt = (float)interval / 1000.0f;
71 sim.step(dt);
72 }
73 }
75 static void display()
76 {
77 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
78 update(msec);
80 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
82 glMatrixMode(GL_MODELVIEW);
83 glLoadIdentity();
84 glTranslatef(0, 0, -cam_dist);
85 glRotatef(cam_phi, 1, 0, 0);
86 glRotatef(cam_theta, 0, 1, 0);
88 sim.draw();
90 draw_grid(10, 10, 1);
92 glutSwapBuffers();
93 assert(glGetError() == 0);
94 }
96 static void generate()
97 {
98 Particle p;
99 p.mass = (float)rand() / (float)RAND_MAX + 0.25;
101 p.pos.x = ((float)rand() / (float)RAND_MAX - 0.5) * 10.0;
102 p.pos.y = ((float)rand() / (float)RAND_MAX - 0.5) * 10.0;
103 p.pos.z = ((float)rand() / (float)RAND_MAX - 0.5) * 10.0;
105 p.vel.x = ((float)rand() / (float)RAND_MAX - 0.5) * 1.0;
106 p.vel.y = ((float)rand() / (float)RAND_MAX - 0.5) * 1.0;
107 p.vel.z = ((float)rand() / (float)RAND_MAX - 0.5) * 1.0;
109 sim.add_particle(p);
110 }
112 static void idle()
113 {
114 glutPostRedisplay();
115 }
117 static void reshape(int x, int y)
118 {
119 glMatrixMode(GL_PROJECTION);
120 glLoadIdentity();
121 gluPerspective(50.0, (float)x / (float)y, 0.5, 1000.0);
123 glViewport(0, 0, x, y);
124 }
126 static void keyb(unsigned char key, int x, int y)
127 {
128 switch(key) {
129 case 27:
130 exit(0);
132 case ' ':
133 generate();
134 break;
135 }
136 }
138 static bool bnstate[16];
139 static int prev_x, prev_y;
141 static void mouse(int bn, int st, int x, int y)
142 {
143 prev_x = x;
144 prev_y = y;
145 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
146 }
148 static void motion(int x, int y)
149 {
150 int dx = x - prev_x;
151 int dy = y - prev_y;
152 prev_x = x;
153 prev_y = y;
155 if(!dx && !dy) return;
157 if(bnstate[0]) {
158 cam_theta += dx * 0.5;
159 cam_phi += dy * 0.5;
161 if(cam_phi < -90) cam_phi = -90;
162 if(cam_phi > 90) cam_phi = 90;
163 }
164 if(bnstate[2]) {
165 cam_dist += dy * 0.1;
166 if(cam_dist < 0.0) cam_dist = 0.0;
167 }
168 }
170 static void draw_grid(float sz, int nlines, float alpha)
171 {
172 float hsz = sz / 2.0;
173 float offs = sz / (float)nlines;
175 glPushAttrib(GL_ENABLE_BIT);
176 glDisable(GL_LIGHTING);
177 glDisable(GL_TEXTURE_2D);
179 glLineWidth(2.0);
180 glBegin(GL_LINES);
181 glColor4f(1, 0, 0, alpha);
182 glVertex3f(-hsz, 0, 0);
183 glVertex3f(hsz, 0, 0);
184 glColor4f(0, 0, 1, alpha);
185 glVertex3f(0, 0, -hsz);
186 glVertex3f(0, 0, hsz);
187 glEnd();
189 glLineWidth(1.0);
190 glBegin(GL_LINES);
191 glColor4f(0.5, 0.5, 0.5, alpha);
192 for(int i=0; i<nlines / 2; i++) {
193 float dist = (float)(i + 1) * offs;
194 for(int j=0; j<2; j++) {
195 float sign = j > 0 ? -1.0 : 1.0;
196 glVertex3f(-hsz, 0, dist * sign);
197 glVertex3f(hsz, 0, dist * sign);
198 glVertex3f(dist * sign, 0, -hsz);
199 glVertex3f(dist * sign, 0, hsz);
200 }
201 }
202 glEnd();
204 glPopAttrib();