libpsys

view examples/simple/simple.c @ 18:66956b1e5e3f

added missing psys_create_attr/psys_free_attr and some checking in load_attr
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 13 Sep 2012 04:10:38 +0300
parents 9c24273f211b
children
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 #define RATE 300.0
30 int main(int argc, char **argv)
31 {
32 glutInitWindowSize(800, 600);
33 glutInit(&argc, argv);
34 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
35 glutCreateWindow("libpsys example: simple");
37 glutDisplayFunc(disp);
38 glutReshapeFunc(reshape);
39 glutKeyboardFunc(keyb);
40 glutMouseFunc(mouse);
41 glutMotionFunc(motion);
42 glutIdleFunc(idle);
44 glClearColor(0.05, 0.05, 0.05, 1);
46 if(!(tex = load_texture("pimg.png"))) {
47 fprintf(stderr, "failed to load the texture\n");
48 return 1;
49 }
51 if(!(ps = psys_create())) {
52 return 1;
53 }
54 if(psys_load_attr(&ps->attr, "simple.psys") == -1) {
55 fprintf(stderr, "failed to load particle system definition\n");
56 psys_free(ps);
57 return 1;
58 }
59 /*ps->attr.tex = tex;
60 ps->attr.drag = 2;
61 psys_set_value3(&ps->attr.grav, 0, v3_cons(0, -4, 0));
62 psys_set_anm_rnd(&ps->attr.life, 0, 2, 0);
63 psys_set_value3(&ps->attr.spawn_range, 0, v3_cons(0.3, 0.3, 0.3));
64 psys_set_anm_rnd3(&ps->attr.dir, 0, v3_cons(0, 0, 0), v3_cons(4, 4, 4));
66 psys_set_value3(&ps->attr.part_attr.color, 0, v3_cons(1.0, 0.6, 0.4));
67 psys_set_value3(&ps->attr.part_attr.color, 1000, v3_cons(0.6, 0.3, 1.0));
68 psys_set_value(&ps->attr.part_attr.alpha, 0, 1);
69 psys_set_value(&ps->attr.part_attr.alpha, 700, 1);
70 psys_set_value(&ps->attr.part_attr.alpha, 1000, 0);*/
72 assert(glGetError() == GL_NO_ERROR);
73 atexit(cleanup);
75 glutMainLoop();
76 return 0;
77 }
79 void cleanup(void)
80 {
81 psys_free(ps);
82 }
84 void disp(void)
85 {
86 static unsigned int prev_msec;
87 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
89 glMatrixMode(GL_MODELVIEW);
90 glLoadIdentity();
91 glTranslatef(0, 0, -10);
93 glClear(GL_COLOR_BUFFER_BIT);
95 psys_update(ps, (msec - prev_msec) / 1000.0);
96 psys_draw(ps);
98 glutSwapBuffers();
99 assert(glGetError() == GL_NO_ERROR);
100 }
102 void idle(void)
103 {
104 glutPostRedisplay();
105 }
107 void reshape(int x, int y)
108 {
109 glViewport(0, 0, x, y);
111 glMatrixMode(GL_PROJECTION);
112 glLoadIdentity();
113 gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0);
114 }
116 void keyb(unsigned char key, int x, int y)
117 {
118 switch(key) {
119 case 27:
120 exit(0);
121 }
122 }
124 static int bnstate[32];
125 void mouse(int bn, int state, int x, int y)
126 {
127 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
128 if(bn == GLUT_LEFT_BUTTON) {
129 psys_set_value(&ps->attr.rate, 0, state == GLUT_DOWN ? RATE : 0.0);
130 psys_set_pos(ps, get_mouse_hit(x, y), 0);
131 }
132 }
134 void motion(int x, int y)
135 {
136 if(bnstate[0]) {
137 psys_set_pos(ps, get_mouse_hit(x, y), 0);
138 }
139 }
141 vec3_t get_mouse_hit(float x, float y)
142 {
143 double mv[16], proj[16];
144 int vp[4];
145 double res_x, res_y, res_z;
146 float t;
147 vec3_t res, pnear, pfar;
149 glGetDoublev(GL_MODELVIEW_MATRIX, mv);
150 glGetDoublev(GL_PROJECTION_MATRIX, proj);
151 glGetIntegerv(GL_VIEWPORT, vp);
153 y = vp[3] - y;
155 gluUnProject(x, y, 0, mv, proj, vp, &res_x, &res_y, &res_z);
156 pnear.x = res_x;
157 pnear.y = res_y;
158 pnear.z = res_z;
160 gluUnProject(x, y, 1, mv, proj, vp, &res_x, &res_y, &res_z);
161 pfar.x = res_x;
162 pfar.y = res_y;
163 pfar.z = res_z;
165 t = fabs(pnear.z) / fabs(pfar.z - pnear.z);
166 res = v3_add(pnear, v3_scale(v3_sub(pfar, pnear), t));
168 return res;
169 }
171 unsigned int load_texture(const char *fname)
172 {
173 void *pixels;
174 int xsz, ysz;
175 unsigned int tex;
177 if(!(pixels = img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32))) {
178 return 0;
179 }
181 glGenTextures(1, &tex);
182 glBindTexture(GL_TEXTURE_2D, tex);
183 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
184 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
185 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
186 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
187 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
188 img_free_pixels(pixels);
190 return tex;
191 }