libpsys

view src/psys_gl.c @ 20:0a53b22f7caf

the billboarding was wrong ...
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 18 Sep 2012 09:38:48 +0300
parents 3871a45a4e4b
children
line source
1 #include <string.h>
2 #include <errno.h>
3 #include <assert.h>
5 #ifndef __APPLE__
6 #ifdef WIN32
7 #include <windows.h>
8 #endif
10 #include <GL/gl.h>
11 #else
12 #include <OpenGL/gl.h>
13 #endif
15 #include <imago2.h>
16 #include "psys.h"
17 #include "psys_gl.h"
19 void psys_gl_draw_start(struct psys_emitter *em, void *cls)
20 {
21 glPushAttrib(GL_ENABLE_BIT);
22 glDisable(GL_LIGHTING);
24 glEnable(GL_BLEND);
25 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
27 if(em->attr.tex) {
28 glEnable(GL_TEXTURE_2D);
29 glBindTexture(GL_TEXTURE_2D, em->attr.tex);
30 }
32 glDepthMask(0);
33 }
35 void psys_gl_draw(struct psys_emitter *em, struct psys_particle *p, void *cls)
36 {
37 float hsz = p->size / 2.0;
39 float xform[16];
41 glMatrixMode(GL_MODELVIEW);
42 glPushMatrix();
44 glTranslatef(p->pos.x, p->pos.y, p->pos.z);
46 glGetFloatv(GL_MODELVIEW_MATRIX, xform);
47 xform[0] = xform[5] = xform[10] = 1.0;
48 xform[1] = xform[2] = xform[4] = xform[6] = xform[8] = xform[9] = 0.0;
49 glLoadMatrixf(xform);
51 glBegin(GL_QUADS);
52 glColor4f(p->color.x, p->color.y, p->color.z, p->alpha);
54 glTexCoord2f(0, 0);
55 glVertex2f(-hsz, -hsz);
57 glTexCoord2f(1, 0);
58 glVertex2f(hsz, -hsz);
60 glTexCoord2f(1, 1);
61 glVertex2f(hsz, hsz);
63 glTexCoord2f(0, 1);
64 glVertex2f(-hsz, hsz);
65 glEnd();
67 glMatrixMode(GL_MODELVIEW);
68 glPopMatrix();
69 }
71 void psys_gl_draw_end(struct psys_emitter *em, void *cls)
72 {
73 glDepthMask(1);
74 glPopAttrib();
75 }
78 unsigned int psys_gl_load_texture(const char *fname, void *cls)
79 {
80 unsigned int tex;
81 void *pixels;
82 int xsz, ysz;
84 if(!(pixels = img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32))) {
85 return 0;
86 }
87 printf("%s: creating texture %s (%dx%d)\n", __func__, fname, xsz, ysz);
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, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
97 assert(glGetError() == GL_NO_ERROR);
99 img_free_pixels(pixels);
100 return tex;
101 }
103 void psys_gl_unload_texture(unsigned int tex, void *cls)
104 {
105 glDeleteTextures(1, &tex);
106 }