libpsys
view src/psys_gl.c @ 14:4f36bbbcc82f
really started writting the parser... god I hate ad-hoc parsing...
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Mon, 10 Sep 2012 05:58:03 +0300 |
parents | 613d2bf3ea1f |
children | 3871a45a4e4b |
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 glColor4f(p->color.x, p->color.y, p->color.z, p->alpha);
54 glTexCoord2f(0, 0);
55 glVertex3f(p->pos.x - hsz, p->pos.y - hsz, p->pos.z);
57 glTexCoord2f(1, 0);
58 glVertex3f(p->pos.x + hsz, p->pos.y - hsz, p->pos.z);
60 glTexCoord2f(1, 1);
61 glVertex3f(p->pos.x + hsz, p->pos.y + hsz, p->pos.z);
63 glTexCoord2f(0, 1);
64 glVertex3f(p->pos.x - hsz, p->pos.y + hsz, p->pos.z);
65 }
67 void psys_gl_draw_end(struct psys_emitter *em, void *cls)
68 {
69 glEnd();
71 glDepthMask(1);
72 glPopAttrib();
74 glMatrixMode(GL_MODELVIEW);
75 glPopMatrix();
76 }
79 unsigned int psys_gl_load_texture(const char *fname, void *cls)
80 {
81 unsigned int tex;
82 void *pixels;
83 int xsz, ysz;
85 if(!(pixels = img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32))) {
86 return 0;
87 }
89 glGenTextures(1, &tex);
90 glBindTexture(GL_TEXTURE_2D, tex);
91 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
92 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
93 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
94 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
95 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, xsz, ysz, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
97 img_free_pixels(pixels);
98 return tex;
99 }
101 void psys_gl_unload_texture(unsigned int tex, void *cls)
102 {
103 glDeleteTextures(1, &tex);
104 }