dungeon_crawler
diff prototype/psys/psys_gl.c @ 67:2560a7ab0243
internalized libanim, libimago2, and libpsys
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 07 Oct 2012 02:04:00 +0300 |
parents | |
children | 45172d087ebe |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/prototype/psys/psys_gl.c Sun Oct 07 02:04:00 2012 +0300 1.3 @@ -0,0 +1,106 @@ 1.4 +#include <string.h> 1.5 +#include <errno.h> 1.6 +#include <assert.h> 1.7 + 1.8 +#ifndef __APPLE__ 1.9 +#ifdef WIN32 1.10 +#include <windows.h> 1.11 +#endif 1.12 + 1.13 +#include <GL/gl.h> 1.14 +#else 1.15 +#include <OpenGL/gl.h> 1.16 +#endif 1.17 + 1.18 +#include <imago2.h> 1.19 +#include "psys.h" 1.20 +#include "psys_gl.h" 1.21 + 1.22 +void psys_gl_draw_start(struct psys_emitter *em, void *cls) 1.23 +{ 1.24 + glPushAttrib(GL_ENABLE_BIT); 1.25 + glDisable(GL_LIGHTING); 1.26 + 1.27 + glEnable(GL_BLEND); 1.28 + glBlendFunc(GL_SRC_ALPHA, GL_ONE); 1.29 + 1.30 + if(em->attr.tex) { 1.31 + glEnable(GL_TEXTURE_2D); 1.32 + glBindTexture(GL_TEXTURE_2D, em->attr.tex); 1.33 + } 1.34 + 1.35 + glDepthMask(0); 1.36 +} 1.37 + 1.38 +void psys_gl_draw(struct psys_emitter *em, struct psys_particle *p, void *cls) 1.39 +{ 1.40 + float hsz = p->size / 2.0; 1.41 + 1.42 + float xform[16]; 1.43 + 1.44 + glMatrixMode(GL_MODELVIEW); 1.45 + glPushMatrix(); 1.46 + 1.47 + glTranslatef(p->pos.x, p->pos.y, p->pos.z); 1.48 + 1.49 + glGetFloatv(GL_MODELVIEW_MATRIX, xform); 1.50 + xform[0] = xform[5] = xform[10] = 1.0; 1.51 + xform[1] = xform[2] = xform[4] = xform[6] = xform[8] = xform[9] = 0.0; 1.52 + glLoadMatrixf(xform); 1.53 + 1.54 + glBegin(GL_QUADS); 1.55 + glColor4f(p->color.x, p->color.y, p->color.z, p->alpha); 1.56 + 1.57 + glTexCoord2f(0, 0); 1.58 + glVertex2f(-hsz, -hsz); 1.59 + 1.60 + glTexCoord2f(1, 0); 1.61 + glVertex2f(hsz, -hsz); 1.62 + 1.63 + glTexCoord2f(1, 1); 1.64 + glVertex2f(hsz, hsz); 1.65 + 1.66 + glTexCoord2f(0, 1); 1.67 + glVertex2f(-hsz, hsz); 1.68 + glEnd(); 1.69 + 1.70 + glMatrixMode(GL_MODELVIEW); 1.71 + glPopMatrix(); 1.72 +} 1.73 + 1.74 +void psys_gl_draw_end(struct psys_emitter *em, void *cls) 1.75 +{ 1.76 + glDepthMask(1); 1.77 + glPopAttrib(); 1.78 +} 1.79 + 1.80 + 1.81 +unsigned int psys_gl_load_texture(const char *fname, void *cls) 1.82 +{ 1.83 + unsigned int tex; 1.84 + void *pixels; 1.85 + int xsz, ysz; 1.86 + 1.87 + if(!(pixels = img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32))) { 1.88 + return 0; 1.89 + } 1.90 + printf("%s: creating texture %s (%dx%d)\n", __func__, fname, xsz, ysz); 1.91 + 1.92 + glGenTextures(1, &tex); 1.93 + glBindTexture(GL_TEXTURE_2D, tex); 1.94 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 1.95 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 1.96 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 1.97 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); 1.98 + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); 1.99 + 1.100 + assert(glGetError() == GL_NO_ERROR); 1.101 + 1.102 + img_free_pixels(pixels); 1.103 + return tex; 1.104 +} 1.105 + 1.106 +void psys_gl_unload_texture(unsigned int tex, void *cls) 1.107 +{ 1.108 + glDeleteTextures(1, &tex); 1.109 +}