libpsys

view src/psys_gl.c @ 5:613d2bf3ea1f

almost finished with the reorg
author John Tsiombikas <nuclear@mutantstargoat.com>
date Tue, 27 Sep 2011 07:42:32 +0300
parents 133094e2f5a5
children 9c24273f211b
line source
1 #include <string.h>
2 #include <errno.h>
4 #ifndef __APPLE__
5 #ifdef WIN32
6 #include <windows.h>
7 #endif
9 #include <GL/gl.h>
10 #else
11 #include <OpenGL/gl.h>
12 #endif
14 #include <imago2.h>
15 #include "psys.h"
16 #include "psys_gl.h"
18 void psys_gl_draw_start(struct psys_emitter *em, void *cls)
19 {
20 float xform[16];
22 glMatrixMode(GL_MODELVIEW);
23 glPushMatrix();
25 glGetFloatv(GL_MODELVIEW_MATRIX, xform);
26 xform[0] = xform[5] = xform[10] = 1.0;
27 xform[1] = xform[2] = xform[4] = xform[6] = xform[8] = xform[9] = 0.0;
29 glLoadMatrixf(xform);
31 glPushAttrib(GL_ENABLE_BIT);
32 glDisable(GL_LIGHTING);
34 glEnable(GL_BLEND);
35 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
37 if(em->attr.tex) {
38 glEnable(GL_TEXTURE_2D);
39 glBindTexture(GL_TEXTURE_2D, em->attr.tex);
40 }
42 glDepthMask(0);
44 glBegin(GL_QUADS);
45 glColor3f(1, 1, 1);
46 }
48 void psys_gl_draw(struct psys_emitter *em, struct psys_particle *p, void *cls)
49 {
50 float hsz = p->size / 2.0;
52 glTexCoord2f(0, 0);
53 glVertex3f(p->pos.x - hsz, p->pos.y - hsz, p->pos.z);
55 glTexCoord2f(1, 0);
56 glVertex3f(p->pos.x + hsz, p->pos.y - hsz, p->pos.z);
58 glTexCoord2f(1, 1);
59 glVertex3f(p->pos.x + hsz, p->pos.y + hsz, p->pos.z);
61 glTexCoord2f(0, 1);
62 glVertex3f(p->pos.x - hsz, p->pos.y + hsz, p->pos.z);
63 }
65 void psys_gl_draw_end(struct psys_emitter *em, void *cls)
66 {
67 glEnd();
69 glDepthMask(1);
70 glPopAttrib();
72 glMatrixMode(GL_MODELVIEW);
73 glPopMatrix();
74 }
77 unsigned int psys_gl_load_texture(const char *fname, void *cls)
78 {
79 unsigned int tex;
80 void *pixels;
81 int xsz, ysz;
83 if(!(pixels = img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32))) {
84 return 0;
85 }
87 glGenTextures(1, &tex);
88 glBindTexture(GL_TEXTURE_2D, tex);
89 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
90 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
91 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
92 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
93 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, xsz, ysz, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
95 img_free_pixels(pixels);
96 return tex;
97 }
99 void psys_gl_unload_texture(unsigned int tex, void *cls)
100 {
101 glDeleteTextures(1, &tex);
102 }