libpsys
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/examples/simple/simple.c Sat Sep 24 20:44:42 2011 +0300 1.3 @@ -0,0 +1,133 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 + 1.7 +#ifndef __APPLE__ 1.8 +#include <GL/glut.h> 1.9 +#else 1.10 +#include <GLUT/glut.h> 1.11 +#endif 1.12 + 1.13 +#include <vmath.h> 1.14 +#include "psys.h" 1.15 + 1.16 +void disp(void); 1.17 +void idle(void); 1.18 +void reshape(int x, int y); 1.19 +void keyb(unsigned char key, int x, int y); 1.20 +void mouse(int bn, int state, int x, int y); 1.21 +void motion(int x, int y); 1.22 +vec3_t get_mouse_hit(float x, float y); 1.23 + 1.24 +struct psys_emitter *ps; 1.25 + 1.26 +int main(int argc, char **argv) 1.27 +{ 1.28 + glutInitWindowSize(800, 600); 1.29 + glutInit(&argc, argv); 1.30 + glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); 1.31 + glutCreateWindow("libpsys example: simple"); 1.32 + 1.33 + glutDisplayFunc(disp); 1.34 + glutReshapeFunc(reshape); 1.35 + glutKeyboardFunc(keyb); 1.36 + glutMouseFunc(mouse); 1.37 + glutMotionFunc(motion); 1.38 + glutIdleFunc(idle); 1.39 + 1.40 + glEnable(GL_CULL_FACE); 1.41 + 1.42 + if(!(ps = psys_create())) { 1.43 + return 1; 1.44 + } 1.45 + psys_set_grav(ps, v3_cons(0, -1, 0), 0); 1.46 + psys_set_life(ps, 2, 0); 1.47 + 1.48 + glutMainLoop(); 1.49 + return 0; 1.50 +} 1.51 + 1.52 +void disp(void) 1.53 +{ 1.54 + static unsigned int prev_msec; 1.55 + unsigned int msec = glutGet(GLUT_ELAPSED_TIME); 1.56 + 1.57 + glMatrixMode(GL_MODELVIEW); 1.58 + glLoadIdentity(); 1.59 + glTranslatef(0, 0, -10); 1.60 + 1.61 + glClear(GL_COLOR_BUFFER_BIT); 1.62 + 1.63 + psys_update(ps, (msec - prev_msec) / 1000.0); 1.64 + psys_draw(ps); 1.65 + 1.66 + glutSwapBuffers(); 1.67 +} 1.68 + 1.69 +void idle(void) 1.70 +{ 1.71 + glutPostRedisplay(); 1.72 +} 1.73 + 1.74 +void reshape(int x, int y) 1.75 +{ 1.76 + glViewport(0, 0, x, y); 1.77 + 1.78 + glMatrixMode(GL_PROJECTION); 1.79 + glLoadIdentity(); 1.80 + gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0); 1.81 +} 1.82 + 1.83 +void keyb(unsigned char key, int x, int y) 1.84 +{ 1.85 + switch(key) { 1.86 + case 27: 1.87 + exit(0); 1.88 + } 1.89 +} 1.90 + 1.91 +static int bnstate[32]; 1.92 +void mouse(int bn, int state, int x, int y) 1.93 +{ 1.94 + bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN; 1.95 + if(bn == GLUT_LEFT_BUTTON) { 1.96 + psys_set_rate(ps, state == GLUT_DOWN ? 1.0 : 0.0, 0); 1.97 + psys_set_pos(ps, get_mouse_hit(x, y), 0); 1.98 + } 1.99 +} 1.100 + 1.101 +void motion(int x, int y) 1.102 +{ 1.103 + if(bnstate[0]) { 1.104 + psys_set_pos(ps, get_mouse_hit(x, y), 0); 1.105 + } 1.106 +} 1.107 + 1.108 +vec3_t get_mouse_hit(float x, float y) 1.109 +{ 1.110 + double mv[16], proj[16]; 1.111 + int vp[4]; 1.112 + double res_x, res_y, res_z; 1.113 + float t; 1.114 + vec3_t res, pnear, pfar; 1.115 + 1.116 + glGetDoublev(GL_MODELVIEW_MATRIX, mv); 1.117 + glGetDoublev(GL_PROJECTION_MATRIX, proj); 1.118 + glGetIntegerv(GL_VIEWPORT, vp); 1.119 + 1.120 + y = vp[3] - y; 1.121 + 1.122 + gluUnProject(x, y, 0, mv, proj, vp, &res_x, &res_y, &res_z); 1.123 + pnear.x = res_x; 1.124 + pnear.y = res_y; 1.125 + pnear.z = res_z; 1.126 + 1.127 + gluUnProject(x, y, 1, mv, proj, vp, &res_x, &res_y, &res_z); 1.128 + pfar.x = res_x; 1.129 + pfar.y = res_y; 1.130 + pfar.z = res_z; 1.131 + 1.132 + t = fabs(pnear.z) / fabs(pfar.z - pnear.z); 1.133 + res = v3_add(pnear, v3_scale(v3_sub(pfar, pnear), t)); 1.134 + 1.135 + return res; 1.136 +}