libpsys

view examples/simple/simple.c @ 1:874a942853ad

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