libpsys

view examples/simple/simple.c @ 13:1b0db53d5b5b

started writting config file for the particle system attributes
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 06 Sep 2012 02:32:58 +0300
parents a10f19674147
children 3871a45a4e4b
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 ps->attr.tex = tex;
55 ps->attr.drag = 2;
56 psys_set_value3(&ps->attr.grav, 0, v3_cons(0, -4, 0));
57 psys_set_anm_rnd(&ps->attr.life, 0, 2, 0);
58 psys_set_value3(&ps->attr.spawn_range, 0, v3_cons(0.3, 0.3, 0.3));
59 psys_set_anm_rnd3(&ps->attr.dir, 0, v3_cons(0, 0, 0), v3_cons(4, 4, 4));
61 psys_set_value3(&ps->attr.part_attr.color, 0, v3_cons(1.0, 0.6, 0.4));
62 psys_set_value3(&ps->attr.part_attr.color, 1000, v3_cons(0.6, 0.3, 1.0));
63 psys_set_value(&ps->attr.part_attr.alpha, 0, 1);
64 psys_set_value(&ps->attr.part_attr.alpha, 700, 1);
65 psys_set_value(&ps->attr.part_attr.alpha, 1000, 0);
67 atexit(cleanup);
69 glutMainLoop();
70 return 0;
71 }
73 void cleanup(void)
74 {
75 psys_free(ps);
76 }
78 void disp(void)
79 {
80 static unsigned int prev_msec;
81 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
83 glMatrixMode(GL_MODELVIEW);
84 glLoadIdentity();
85 glTranslatef(0, 0, -10);
87 glClear(GL_COLOR_BUFFER_BIT);
89 psys_update(ps, (msec - prev_msec) / 1000.0);
90 psys_draw(ps);
92 glutSwapBuffers();
93 assert(glGetError() == GL_NO_ERROR);
94 }
96 void idle(void)
97 {
98 glutPostRedisplay();
99 }
101 void reshape(int x, int y)
102 {
103 glViewport(0, 0, x, y);
105 glMatrixMode(GL_PROJECTION);
106 glLoadIdentity();
107 gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0);
108 }
110 void keyb(unsigned char key, int x, int y)
111 {
112 switch(key) {
113 case 27:
114 exit(0);
115 }
116 }
118 static int bnstate[32];
119 void mouse(int bn, int state, int x, int y)
120 {
121 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
122 if(bn == GLUT_LEFT_BUTTON) {
123 psys_set_value(&ps->attr.rate, 0, state == GLUT_DOWN ? RATE : 0.0);
124 psys_set_pos(ps, get_mouse_hit(x, y), 0);
125 }
126 }
128 void motion(int x, int y)
129 {
130 if(bnstate[0]) {
131 psys_set_pos(ps, get_mouse_hit(x, y), 0);
132 }
133 }
135 vec3_t get_mouse_hit(float x, float y)
136 {
137 double mv[16], proj[16];
138 int vp[4];
139 double res_x, res_y, res_z;
140 float t;
141 vec3_t res, pnear, pfar;
143 glGetDoublev(GL_MODELVIEW_MATRIX, mv);
144 glGetDoublev(GL_PROJECTION_MATRIX, proj);
145 glGetIntegerv(GL_VIEWPORT, vp);
147 y = vp[3] - y;
149 gluUnProject(x, y, 0, mv, proj, vp, &res_x, &res_y, &res_z);
150 pnear.x = res_x;
151 pnear.y = res_y;
152 pnear.z = res_z;
154 gluUnProject(x, y, 1, mv, proj, vp, &res_x, &res_y, &res_z);
155 pfar.x = res_x;
156 pfar.y = res_y;
157 pfar.z = res_z;
159 t = fabs(pnear.z) / fabs(pfar.z - pnear.z);
160 res = v3_add(pnear, v3_scale(v3_sub(pfar, pnear), t));
162 return res;
163 }
165 unsigned int load_texture(const char *fname)
166 {
167 void *pixels;
168 int xsz, ysz;
169 unsigned int tex;
171 if(!(pixels = img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32))) {
172 return 0;
173 }
175 glGenTextures(1, &tex);
176 glBindTexture(GL_TEXTURE_2D, tex);
177 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
178 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
179 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
180 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
181 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
182 img_free_pixels(pixels);
184 return tex;
185 }