libpsys

annotate examples/simple/simple.c @ 2:6e5342a2529a

more stuff done
author John Tsiombikas <nuclear@mutantstargoat.com>
date Sun, 25 Sep 2011 04:26:51 +0300
parents 874a942853ad
children a10f19674147
rev   line source
nuclear@1 1 #include <stdio.h>
nuclear@1 2 #include <stdlib.h>
nuclear@2 3 #include <assert.h>
nuclear@1 4
nuclear@1 5 #ifndef __APPLE__
nuclear@1 6 #include <GL/glut.h>
nuclear@1 7 #else
nuclear@1 8 #include <GLUT/glut.h>
nuclear@1 9 #endif
nuclear@1 10
nuclear@1 11 #include <vmath.h>
nuclear@2 12 #include <imago2.h>
nuclear@1 13 #include "psys.h"
nuclear@1 14
nuclear@1 15 void disp(void);
nuclear@1 16 void idle(void);
nuclear@1 17 void reshape(int x, int y);
nuclear@1 18 void keyb(unsigned char key, int x, int y);
nuclear@1 19 void mouse(int bn, int state, int x, int y);
nuclear@1 20 void motion(int x, int y);
nuclear@1 21 vec3_t get_mouse_hit(float x, float y);
nuclear@2 22 unsigned int load_texture(const char *fname);
nuclear@1 23
nuclear@1 24 struct psys_emitter *ps;
nuclear@2 25 unsigned int tex;
nuclear@1 26
nuclear@1 27 int main(int argc, char **argv)
nuclear@1 28 {
nuclear@1 29 glutInitWindowSize(800, 600);
nuclear@1 30 glutInit(&argc, argv);
nuclear@1 31 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
nuclear@1 32 glutCreateWindow("libpsys example: simple");
nuclear@1 33
nuclear@1 34 glutDisplayFunc(disp);
nuclear@1 35 glutReshapeFunc(reshape);
nuclear@1 36 glutKeyboardFunc(keyb);
nuclear@1 37 glutMouseFunc(mouse);
nuclear@1 38 glutMotionFunc(motion);
nuclear@1 39 glutIdleFunc(idle);
nuclear@1 40
nuclear@2 41 glClearColor(0.05, 0.05, 0.05, 1);
nuclear@2 42
nuclear@2 43 if(!(tex = load_texture("pimg.png"))) {
nuclear@2 44 fprintf(stderr, "failed to load the FUCKING TEXTURE GOD DAMN IT\n");
nuclear@2 45 return 1;
nuclear@2 46 }
nuclear@1 47
nuclear@1 48 if(!(ps = psys_create())) {
nuclear@1 49 return 1;
nuclear@1 50 }
nuclear@2 51 psys_set_grav(ps, v3_cons(0, -9, 0), 0);
nuclear@1 52 psys_set_life(ps, 2, 0);
nuclear@2 53 psys_set_texture(ps, tex);
nuclear@1 54
nuclear@1 55 glutMainLoop();
nuclear@1 56 return 0;
nuclear@1 57 }
nuclear@1 58
nuclear@1 59 void disp(void)
nuclear@1 60 {
nuclear@1 61 static unsigned int prev_msec;
nuclear@1 62 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
nuclear@1 63
nuclear@1 64 glMatrixMode(GL_MODELVIEW);
nuclear@1 65 glLoadIdentity();
nuclear@1 66 glTranslatef(0, 0, -10);
nuclear@1 67
nuclear@1 68 glClear(GL_COLOR_BUFFER_BIT);
nuclear@1 69
nuclear@1 70 psys_update(ps, (msec - prev_msec) / 1000.0);
nuclear@1 71 psys_draw(ps);
nuclear@1 72
nuclear@1 73 glutSwapBuffers();
nuclear@2 74 assert(glGetError() == GL_NO_ERROR);
nuclear@1 75 }
nuclear@1 76
nuclear@1 77 void idle(void)
nuclear@1 78 {
nuclear@1 79 glutPostRedisplay();
nuclear@1 80 }
nuclear@1 81
nuclear@1 82 void reshape(int x, int y)
nuclear@1 83 {
nuclear@1 84 glViewport(0, 0, x, y);
nuclear@1 85
nuclear@1 86 glMatrixMode(GL_PROJECTION);
nuclear@1 87 glLoadIdentity();
nuclear@1 88 gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0);
nuclear@1 89 }
nuclear@1 90
nuclear@1 91 void keyb(unsigned char key, int x, int y)
nuclear@1 92 {
nuclear@1 93 switch(key) {
nuclear@1 94 case 27:
nuclear@1 95 exit(0);
nuclear@1 96 }
nuclear@1 97 }
nuclear@1 98
nuclear@1 99 static int bnstate[32];
nuclear@1 100 void mouse(int bn, int state, int x, int y)
nuclear@1 101 {
nuclear@1 102 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
nuclear@1 103 if(bn == GLUT_LEFT_BUTTON) {
nuclear@2 104 psys_set_rate(ps, state == GLUT_DOWN ? 30.0 : 0.0, 0);
nuclear@1 105 psys_set_pos(ps, get_mouse_hit(x, y), 0);
nuclear@1 106 }
nuclear@1 107 }
nuclear@1 108
nuclear@1 109 void motion(int x, int y)
nuclear@1 110 {
nuclear@1 111 if(bnstate[0]) {
nuclear@1 112 psys_set_pos(ps, get_mouse_hit(x, y), 0);
nuclear@1 113 }
nuclear@1 114 }
nuclear@1 115
nuclear@1 116 vec3_t get_mouse_hit(float x, float y)
nuclear@1 117 {
nuclear@1 118 double mv[16], proj[16];
nuclear@1 119 int vp[4];
nuclear@1 120 double res_x, res_y, res_z;
nuclear@1 121 float t;
nuclear@1 122 vec3_t res, pnear, pfar;
nuclear@1 123
nuclear@1 124 glGetDoublev(GL_MODELVIEW_MATRIX, mv);
nuclear@1 125 glGetDoublev(GL_PROJECTION_MATRIX, proj);
nuclear@1 126 glGetIntegerv(GL_VIEWPORT, vp);
nuclear@1 127
nuclear@1 128 y = vp[3] - y;
nuclear@1 129
nuclear@1 130 gluUnProject(x, y, 0, mv, proj, vp, &res_x, &res_y, &res_z);
nuclear@1 131 pnear.x = res_x;
nuclear@1 132 pnear.y = res_y;
nuclear@1 133 pnear.z = res_z;
nuclear@1 134
nuclear@1 135 gluUnProject(x, y, 1, mv, proj, vp, &res_x, &res_y, &res_z);
nuclear@1 136 pfar.x = res_x;
nuclear@1 137 pfar.y = res_y;
nuclear@1 138 pfar.z = res_z;
nuclear@1 139
nuclear@1 140 t = fabs(pnear.z) / fabs(pfar.z - pnear.z);
nuclear@1 141 res = v3_add(pnear, v3_scale(v3_sub(pfar, pnear), t));
nuclear@1 142
nuclear@1 143 return res;
nuclear@1 144 }
nuclear@2 145
nuclear@2 146 unsigned int load_texture(const char *fname)
nuclear@2 147 {
nuclear@2 148 void *pixels;
nuclear@2 149 int xsz, ysz;
nuclear@2 150 unsigned int tex;
nuclear@2 151
nuclear@2 152 if(!(pixels = img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32))) {
nuclear@2 153 return 0;
nuclear@2 154 }
nuclear@2 155
nuclear@2 156 glGenTextures(1, &tex);
nuclear@2 157 glBindTexture(GL_TEXTURE_2D, tex);
nuclear@2 158 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@2 159 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@2 160 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
nuclear@2 161 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
nuclear@2 162 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
nuclear@2 163 img_free_pixels(pixels);
nuclear@2 164
nuclear@2 165 return tex;
nuclear@2 166 }