libpsys

annotate src/psys_gl.c @ 9:9c24273f211b

first test is done
author John Tsiombikas <nuclear@mutantstargoat.com>
date Wed, 28 Sep 2011 03:42:01 +0300
parents 613d2bf3ea1f
children 3871a45a4e4b
rev   line source
nuclear@5 1 #include <string.h>
nuclear@5 2 #include <errno.h>
nuclear@5 3
nuclear@0 4 #ifndef __APPLE__
nuclear@5 5 #ifdef WIN32
nuclear@5 6 #include <windows.h>
nuclear@5 7 #endif
nuclear@5 8
nuclear@0 9 #include <GL/gl.h>
nuclear@0 10 #else
nuclear@0 11 #include <OpenGL/gl.h>
nuclear@0 12 #endif
nuclear@0 13
nuclear@5 14 #include <imago2.h>
nuclear@5 15 #include "psys.h"
nuclear@5 16 #include "psys_gl.h"
nuclear@0 17
nuclear@0 18 void psys_gl_draw_start(struct psys_emitter *em, void *cls)
nuclear@0 19 {
nuclear@0 20 float xform[16];
nuclear@0 21
nuclear@0 22 glMatrixMode(GL_MODELVIEW);
nuclear@0 23 glPushMatrix();
nuclear@0 24
nuclear@0 25 glGetFloatv(GL_MODELVIEW_MATRIX, xform);
nuclear@2 26 xform[0] = xform[5] = xform[10] = 1.0;
nuclear@2 27 xform[1] = xform[2] = xform[4] = xform[6] = xform[8] = xform[9] = 0.0;
nuclear@0 28
nuclear@0 29 glLoadMatrixf(xform);
nuclear@0 30
nuclear@0 31 glPushAttrib(GL_ENABLE_BIT);
nuclear@0 32 glDisable(GL_LIGHTING);
nuclear@0 33
nuclear@2 34 glEnable(GL_BLEND);
nuclear@3 35 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
nuclear@2 36
nuclear@5 37 if(em->attr.tex) {
nuclear@2 38 glEnable(GL_TEXTURE_2D);
nuclear@5 39 glBindTexture(GL_TEXTURE_2D, em->attr.tex);
nuclear@2 40 }
nuclear@2 41
nuclear@0 42 glDepthMask(0);
nuclear@0 43
nuclear@0 44 glBegin(GL_QUADS);
nuclear@0 45 glColor3f(1, 1, 1);
nuclear@0 46 }
nuclear@0 47
nuclear@0 48 void psys_gl_draw(struct psys_emitter *em, struct psys_particle *p, void *cls)
nuclear@0 49 {
nuclear@0 50 float hsz = p->size / 2.0;
nuclear@0 51
nuclear@9 52 glColor4f(p->color.x, p->color.y, p->color.z, p->alpha);
nuclear@9 53
nuclear@0 54 glTexCoord2f(0, 0);
nuclear@0 55 glVertex3f(p->pos.x - hsz, p->pos.y - hsz, p->pos.z);
nuclear@0 56
nuclear@0 57 glTexCoord2f(1, 0);
nuclear@0 58 glVertex3f(p->pos.x + hsz, p->pos.y - hsz, p->pos.z);
nuclear@0 59
nuclear@0 60 glTexCoord2f(1, 1);
nuclear@0 61 glVertex3f(p->pos.x + hsz, p->pos.y + hsz, p->pos.z);
nuclear@0 62
nuclear@0 63 glTexCoord2f(0, 1);
nuclear@0 64 glVertex3f(p->pos.x - hsz, p->pos.y + hsz, p->pos.z);
nuclear@0 65 }
nuclear@0 66
nuclear@0 67 void psys_gl_draw_end(struct psys_emitter *em, void *cls)
nuclear@0 68 {
nuclear@0 69 glEnd();
nuclear@0 70
nuclear@0 71 glDepthMask(1);
nuclear@0 72 glPopAttrib();
nuclear@0 73
nuclear@0 74 glMatrixMode(GL_MODELVIEW);
nuclear@0 75 glPopMatrix();
nuclear@0 76 }
nuclear@5 77
nuclear@5 78
nuclear@5 79 unsigned int psys_gl_load_texture(const char *fname, void *cls)
nuclear@5 80 {
nuclear@5 81 unsigned int tex;
nuclear@5 82 void *pixels;
nuclear@5 83 int xsz, ysz;
nuclear@5 84
nuclear@5 85 if(!(pixels = img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32))) {
nuclear@5 86 return 0;
nuclear@5 87 }
nuclear@5 88
nuclear@5 89 glGenTextures(1, &tex);
nuclear@5 90 glBindTexture(GL_TEXTURE_2D, tex);
nuclear@5 91 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@5 92 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@5 93 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
nuclear@5 94 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
nuclear@5 95 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, xsz, ysz, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
nuclear@5 96
nuclear@5 97 img_free_pixels(pixels);
nuclear@5 98 return tex;
nuclear@5 99 }
nuclear@5 100
nuclear@5 101 void psys_gl_unload_texture(unsigned int tex, void *cls)
nuclear@5 102 {
nuclear@5 103 glDeleteTextures(1, &tex);
nuclear@5 104 }