libpsys

view examples/simple/simple.c @ 8:a10f19674147

ha!
author John Tsiombikas <nuclear@mutantstargoat.com>
date Tue, 27 Sep 2011 21:47:27 +0300
parents 6e5342a2529a
children 9c24273f211b
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
5 #ifndef __APPLE__
6 #include <GL/glut.h>
7 #else
8 #include <GLUT/glut.h>
9 #endif
11 #include <vmath.h>
12 #include <imago2.h>
13 #include "psys.h"
15 void cleanup(void);
16 void disp(void);
17 void idle(void);
18 void reshape(int x, int y);
19 void keyb(unsigned char key, int x, int y);
20 void mouse(int bn, int state, int x, int y);
21 void motion(int x, int y);
22 vec3_t get_mouse_hit(float x, float y);
23 unsigned int load_texture(const char *fname);
25 struct psys_emitter *ps;
26 unsigned int tex;
28 int main(int argc, char **argv)
29 {
30 glutInitWindowSize(800, 600);
31 glutInit(&argc, argv);
32 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
33 glutCreateWindow("libpsys example: simple");
35 glutDisplayFunc(disp);
36 glutReshapeFunc(reshape);
37 glutKeyboardFunc(keyb);
38 glutMouseFunc(mouse);
39 glutMotionFunc(motion);
40 glutIdleFunc(idle);
42 glClearColor(0.05, 0.05, 0.05, 1);
44 if(!(tex = load_texture("pimg.png"))) {
45 fprintf(stderr, "failed to load the texture\n");
46 return 1;
47 }
49 if(!(ps = psys_create())) {
50 return 1;
51 }
52 psys_set_value3(&ps->attr.grav, 0, v3_cons(0, -9, 0));
53 psys_set_anm_rnd(&ps->attr.life, 0, 2, 0);
54 psys_set_value3(&ps->attr.spawn_range, 0, v3_cons(0.2, 0.2, 0.2));
55 psys_set_anm_rnd3(&ps->attr.dir, 0, v3_cons(0, 0, 0), v3_cons(2, 2, 2));
56 ps->attr.tex = tex;
58 atexit(cleanup);
60 glutMainLoop();
61 return 0;
62 }
64 void cleanup(void)
65 {
66 psys_free(ps);
67 }
69 void disp(void)
70 {
71 static unsigned int prev_msec;
72 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
74 glMatrixMode(GL_MODELVIEW);
75 glLoadIdentity();
76 glTranslatef(0, 0, -10);
78 glClear(GL_COLOR_BUFFER_BIT);
80 psys_update(ps, (msec - prev_msec) / 1000.0);
81 psys_draw(ps);
83 glutSwapBuffers();
84 assert(glGetError() == GL_NO_ERROR);
85 }
87 void idle(void)
88 {
89 glutPostRedisplay();
90 }
92 void reshape(int x, int y)
93 {
94 glViewport(0, 0, x, y);
96 glMatrixMode(GL_PROJECTION);
97 glLoadIdentity();
98 gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0);
99 }
101 void keyb(unsigned char key, int x, int y)
102 {
103 switch(key) {
104 case 27:
105 exit(0);
106 }
107 }
109 static int bnstate[32];
110 void mouse(int bn, int state, int x, int y)
111 {
112 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
113 if(bn == GLUT_LEFT_BUTTON) {
114 psys_set_value(&ps->attr.rate, 0, state == GLUT_DOWN ? 30.0 : 0.0);
115 psys_set_pos(ps, get_mouse_hit(x, y), 0);
116 }
117 }
119 void motion(int x, int y)
120 {
121 if(bnstate[0]) {
122 psys_set_pos(ps, get_mouse_hit(x, y), 0);
123 }
124 }
126 vec3_t get_mouse_hit(float x, float y)
127 {
128 double mv[16], proj[16];
129 int vp[4];
130 double res_x, res_y, res_z;
131 float t;
132 vec3_t res, pnear, pfar;
134 glGetDoublev(GL_MODELVIEW_MATRIX, mv);
135 glGetDoublev(GL_PROJECTION_MATRIX, proj);
136 glGetIntegerv(GL_VIEWPORT, vp);
138 y = vp[3] - y;
140 gluUnProject(x, y, 0, mv, proj, vp, &res_x, &res_y, &res_z);
141 pnear.x = res_x;
142 pnear.y = res_y;
143 pnear.z = res_z;
145 gluUnProject(x, y, 1, mv, proj, vp, &res_x, &res_y, &res_z);
146 pfar.x = res_x;
147 pfar.y = res_y;
148 pfar.z = res_z;
150 t = fabs(pnear.z) / fabs(pfar.z - pnear.z);
151 res = v3_add(pnear, v3_scale(v3_sub(pfar, pnear), t));
153 return res;
154 }
156 unsigned int load_texture(const char *fname)
157 {
158 void *pixels;
159 int xsz, ysz;
160 unsigned int tex;
162 if(!(pixels = img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32))) {
163 return 0;
164 }
166 glGenTextures(1, &tex);
167 glBindTexture(GL_TEXTURE_2D, tex);
168 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
169 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
170 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
171 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
172 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
173 img_free_pixels(pixels);
175 return tex;
176 }