libpsys

view examples/simple/simple.c @ 2:6e5342a2529a

more stuff done
author John Tsiombikas <nuclear@mutantstargoat.com>
date Sun, 25 Sep 2011 04:26:51 +0300
parents 874a942853ad
children a10f19674147
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 disp(void);
16 void idle(void);
17 void reshape(int x, int y);
18 void keyb(unsigned char key, int x, int y);
19 void mouse(int bn, int state, int x, int y);
20 void motion(int x, int y);
21 vec3_t get_mouse_hit(float x, float y);
22 unsigned int load_texture(const char *fname);
24 struct psys_emitter *ps;
25 unsigned int tex;
27 int main(int argc, char **argv)
28 {
29 glutInitWindowSize(800, 600);
30 glutInit(&argc, argv);
31 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
32 glutCreateWindow("libpsys example: simple");
34 glutDisplayFunc(disp);
35 glutReshapeFunc(reshape);
36 glutKeyboardFunc(keyb);
37 glutMouseFunc(mouse);
38 glutMotionFunc(motion);
39 glutIdleFunc(idle);
41 glClearColor(0.05, 0.05, 0.05, 1);
43 if(!(tex = load_texture("pimg.png"))) {
44 fprintf(stderr, "failed to load the FUCKING TEXTURE GOD DAMN IT\n");
45 return 1;
46 }
48 if(!(ps = psys_create())) {
49 return 1;
50 }
51 psys_set_grav(ps, v3_cons(0, -9, 0), 0);
52 psys_set_life(ps, 2, 0);
53 psys_set_texture(ps, tex);
55 glutMainLoop();
56 return 0;
57 }
59 void disp(void)
60 {
61 static unsigned int prev_msec;
62 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
64 glMatrixMode(GL_MODELVIEW);
65 glLoadIdentity();
66 glTranslatef(0, 0, -10);
68 glClear(GL_COLOR_BUFFER_BIT);
70 psys_update(ps, (msec - prev_msec) / 1000.0);
71 psys_draw(ps);
73 glutSwapBuffers();
74 assert(glGetError() == GL_NO_ERROR);
75 }
77 void idle(void)
78 {
79 glutPostRedisplay();
80 }
82 void reshape(int x, int y)
83 {
84 glViewport(0, 0, x, y);
86 glMatrixMode(GL_PROJECTION);
87 glLoadIdentity();
88 gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0);
89 }
91 void keyb(unsigned char key, int x, int y)
92 {
93 switch(key) {
94 case 27:
95 exit(0);
96 }
97 }
99 static int bnstate[32];
100 void mouse(int bn, int state, int x, int y)
101 {
102 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
103 if(bn == GLUT_LEFT_BUTTON) {
104 psys_set_rate(ps, state == GLUT_DOWN ? 30.0 : 0.0, 0);
105 psys_set_pos(ps, get_mouse_hit(x, y), 0);
106 }
107 }
109 void motion(int x, int y)
110 {
111 if(bnstate[0]) {
112 psys_set_pos(ps, get_mouse_hit(x, y), 0);
113 }
114 }
116 vec3_t get_mouse_hit(float x, float y)
117 {
118 double mv[16], proj[16];
119 int vp[4];
120 double res_x, res_y, res_z;
121 float t;
122 vec3_t res, pnear, pfar;
124 glGetDoublev(GL_MODELVIEW_MATRIX, mv);
125 glGetDoublev(GL_PROJECTION_MATRIX, proj);
126 glGetIntegerv(GL_VIEWPORT, vp);
128 y = vp[3] - y;
130 gluUnProject(x, y, 0, mv, proj, vp, &res_x, &res_y, &res_z);
131 pnear.x = res_x;
132 pnear.y = res_y;
133 pnear.z = res_z;
135 gluUnProject(x, y, 1, mv, proj, vp, &res_x, &res_y, &res_z);
136 pfar.x = res_x;
137 pfar.y = res_y;
138 pfar.z = res_z;
140 t = fabs(pnear.z) / fabs(pfar.z - pnear.z);
141 res = v3_add(pnear, v3_scale(v3_sub(pfar, pnear), t));
143 return res;
144 }
146 unsigned int load_texture(const char *fname)
147 {
148 void *pixels;
149 int xsz, ysz;
150 unsigned int tex;
152 if(!(pixels = img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32))) {
153 return 0;
154 }
156 glGenTextures(1, &tex);
157 glBindTexture(GL_TEXTURE_2D, tex);
158 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
159 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
160 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
161 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
162 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
163 img_free_pixels(pixels);
165 return tex;
166 }