libpsys

view src/psys_gl.c @ 16:3871a45a4e4b

Almost there
author John Tsiombikas <nuclear@mutantstargoat.com>
date Tue, 11 Sep 2012 02:13:12 +0300
parents 9c24273f211b
children 0a53b22f7caf
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 float xform[16];
23 glMatrixMode(GL_MODELVIEW);
24 glPushMatrix();
26 glGetFloatv(GL_MODELVIEW_MATRIX, xform);
27 xform[0] = xform[5] = xform[10] = 1.0;
28 xform[1] = xform[2] = xform[4] = xform[6] = xform[8] = xform[9] = 0.0;
30 glLoadMatrixf(xform);
32 glPushAttrib(GL_ENABLE_BIT);
33 glDisable(GL_LIGHTING);
35 glEnable(GL_BLEND);
36 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
38 if(em->attr.tex) {
39 glEnable(GL_TEXTURE_2D);
40 glBindTexture(GL_TEXTURE_2D, em->attr.tex);
41 }
43 glDepthMask(0);
45 glBegin(GL_QUADS);
46 glColor3f(1, 1, 1);
47 }
49 void psys_gl_draw(struct psys_emitter *em, struct psys_particle *p, void *cls)
50 {
51 float hsz = p->size / 2.0;
53 glColor4f(p->color.x, p->color.y, p->color.z, p->alpha);
55 glTexCoord2f(0, 0);
56 glVertex3f(p->pos.x - hsz, p->pos.y - hsz, p->pos.z);
58 glTexCoord2f(1, 0);
59 glVertex3f(p->pos.x + hsz, p->pos.y - hsz, p->pos.z);
61 glTexCoord2f(1, 1);
62 glVertex3f(p->pos.x + hsz, p->pos.y + hsz, p->pos.z);
64 glTexCoord2f(0, 1);
65 glVertex3f(p->pos.x - hsz, p->pos.y + hsz, p->pos.z);
66 }
68 void psys_gl_draw_end(struct psys_emitter *em, void *cls)
69 {
70 glEnd();
72 glDepthMask(1);
73 glPopAttrib();
75 glMatrixMode(GL_MODELVIEW);
76 glPopMatrix();
77 }
80 unsigned int psys_gl_load_texture(const char *fname, void *cls)
81 {
82 unsigned int tex;
83 void *pixels;
84 int xsz, ysz;
86 if(!(pixels = img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32))) {
87 return 0;
88 }
89 printf("%s: creating texture %s (%dx%d)\n", __func__, fname, xsz, ysz);
91 glGenTextures(1, &tex);
92 glBindTexture(GL_TEXTURE_2D, tex);
93 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
94 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
95 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
96 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
97 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
99 assert(glGetError() == GL_NO_ERROR);
101 img_free_pixels(pixels);
102 return tex;
103 }
105 void psys_gl_unload_texture(unsigned int tex, void *cls)
106 {
107 glDeleteTextures(1, &tex);
108 }