rev |
line source |
nuclear@1
|
1 #include <stdio.h>
|
nuclear@1
|
2 #include <stdlib.h>
|
nuclear@1
|
3
|
nuclear@1
|
4 #ifndef __APPLE__
|
nuclear@1
|
5 #include <GL/glut.h>
|
nuclear@1
|
6 #else
|
nuclear@1
|
7 #include <GLUT/glut.h>
|
nuclear@1
|
8 #endif
|
nuclear@1
|
9
|
nuclear@1
|
10 #include <vmath.h>
|
nuclear@1
|
11 #include "psys.h"
|
nuclear@1
|
12
|
nuclear@1
|
13 void disp(void);
|
nuclear@1
|
14 void idle(void);
|
nuclear@1
|
15 void reshape(int x, int y);
|
nuclear@1
|
16 void keyb(unsigned char key, int x, int y);
|
nuclear@1
|
17 void mouse(int bn, int state, int x, int y);
|
nuclear@1
|
18 void motion(int x, int y);
|
nuclear@1
|
19 vec3_t get_mouse_hit(float x, float y);
|
nuclear@1
|
20
|
nuclear@1
|
21 struct psys_emitter *ps;
|
nuclear@1
|
22
|
nuclear@1
|
23 int main(int argc, char **argv)
|
nuclear@1
|
24 {
|
nuclear@1
|
25 glutInitWindowSize(800, 600);
|
nuclear@1
|
26 glutInit(&argc, argv);
|
nuclear@1
|
27 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
|
nuclear@1
|
28 glutCreateWindow("libpsys example: simple");
|
nuclear@1
|
29
|
nuclear@1
|
30 glutDisplayFunc(disp);
|
nuclear@1
|
31 glutReshapeFunc(reshape);
|
nuclear@1
|
32 glutKeyboardFunc(keyb);
|
nuclear@1
|
33 glutMouseFunc(mouse);
|
nuclear@1
|
34 glutMotionFunc(motion);
|
nuclear@1
|
35 glutIdleFunc(idle);
|
nuclear@1
|
36
|
nuclear@1
|
37 glEnable(GL_CULL_FACE);
|
nuclear@1
|
38
|
nuclear@1
|
39 if(!(ps = psys_create())) {
|
nuclear@1
|
40 return 1;
|
nuclear@1
|
41 }
|
nuclear@1
|
42 psys_set_grav(ps, v3_cons(0, -1, 0), 0);
|
nuclear@1
|
43 psys_set_life(ps, 2, 0);
|
nuclear@1
|
44
|
nuclear@1
|
45 glutMainLoop();
|
nuclear@1
|
46 return 0;
|
nuclear@1
|
47 }
|
nuclear@1
|
48
|
nuclear@1
|
49 void disp(void)
|
nuclear@1
|
50 {
|
nuclear@1
|
51 static unsigned int prev_msec;
|
nuclear@1
|
52 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
|
nuclear@1
|
53
|
nuclear@1
|
54 glMatrixMode(GL_MODELVIEW);
|
nuclear@1
|
55 glLoadIdentity();
|
nuclear@1
|
56 glTranslatef(0, 0, -10);
|
nuclear@1
|
57
|
nuclear@1
|
58 glClear(GL_COLOR_BUFFER_BIT);
|
nuclear@1
|
59
|
nuclear@1
|
60 psys_update(ps, (msec - prev_msec) / 1000.0);
|
nuclear@1
|
61 psys_draw(ps);
|
nuclear@1
|
62
|
nuclear@1
|
63 glutSwapBuffers();
|
nuclear@1
|
64 }
|
nuclear@1
|
65
|
nuclear@1
|
66 void idle(void)
|
nuclear@1
|
67 {
|
nuclear@1
|
68 glutPostRedisplay();
|
nuclear@1
|
69 }
|
nuclear@1
|
70
|
nuclear@1
|
71 void reshape(int x, int y)
|
nuclear@1
|
72 {
|
nuclear@1
|
73 glViewport(0, 0, x, y);
|
nuclear@1
|
74
|
nuclear@1
|
75 glMatrixMode(GL_PROJECTION);
|
nuclear@1
|
76 glLoadIdentity();
|
nuclear@1
|
77 gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0);
|
nuclear@1
|
78 }
|
nuclear@1
|
79
|
nuclear@1
|
80 void keyb(unsigned char key, int x, int y)
|
nuclear@1
|
81 {
|
nuclear@1
|
82 switch(key) {
|
nuclear@1
|
83 case 27:
|
nuclear@1
|
84 exit(0);
|
nuclear@1
|
85 }
|
nuclear@1
|
86 }
|
nuclear@1
|
87
|
nuclear@1
|
88 static int bnstate[32];
|
nuclear@1
|
89 void mouse(int bn, int state, int x, int y)
|
nuclear@1
|
90 {
|
nuclear@1
|
91 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
|
nuclear@1
|
92 if(bn == GLUT_LEFT_BUTTON) {
|
nuclear@1
|
93 psys_set_rate(ps, state == GLUT_DOWN ? 1.0 : 0.0, 0);
|
nuclear@1
|
94 psys_set_pos(ps, get_mouse_hit(x, y), 0);
|
nuclear@1
|
95 }
|
nuclear@1
|
96 }
|
nuclear@1
|
97
|
nuclear@1
|
98 void motion(int x, int y)
|
nuclear@1
|
99 {
|
nuclear@1
|
100 if(bnstate[0]) {
|
nuclear@1
|
101 psys_set_pos(ps, get_mouse_hit(x, y), 0);
|
nuclear@1
|
102 }
|
nuclear@1
|
103 }
|
nuclear@1
|
104
|
nuclear@1
|
105 vec3_t get_mouse_hit(float x, float y)
|
nuclear@1
|
106 {
|
nuclear@1
|
107 double mv[16], proj[16];
|
nuclear@1
|
108 int vp[4];
|
nuclear@1
|
109 double res_x, res_y, res_z;
|
nuclear@1
|
110 float t;
|
nuclear@1
|
111 vec3_t res, pnear, pfar;
|
nuclear@1
|
112
|
nuclear@1
|
113 glGetDoublev(GL_MODELVIEW_MATRIX, mv);
|
nuclear@1
|
114 glGetDoublev(GL_PROJECTION_MATRIX, proj);
|
nuclear@1
|
115 glGetIntegerv(GL_VIEWPORT, vp);
|
nuclear@1
|
116
|
nuclear@1
|
117 y = vp[3] - y;
|
nuclear@1
|
118
|
nuclear@1
|
119 gluUnProject(x, y, 0, mv, proj, vp, &res_x, &res_y, &res_z);
|
nuclear@1
|
120 pnear.x = res_x;
|
nuclear@1
|
121 pnear.y = res_y;
|
nuclear@1
|
122 pnear.z = res_z;
|
nuclear@1
|
123
|
nuclear@1
|
124 gluUnProject(x, y, 1, mv, proj, vp, &res_x, &res_y, &res_z);
|
nuclear@1
|
125 pfar.x = res_x;
|
nuclear@1
|
126 pfar.y = res_y;
|
nuclear@1
|
127 pfar.z = res_z;
|
nuclear@1
|
128
|
nuclear@1
|
129 t = fabs(pnear.z) / fabs(pfar.z - pnear.z);
|
nuclear@1
|
130 res = v3_add(pnear, v3_scale(v3_sub(pfar, pnear), t));
|
nuclear@1
|
131
|
nuclear@1
|
132 return res;
|
nuclear@1
|
133 }
|