oculus2_psprite

view src/projectile.c @ 21:dc7af0f549b2

VR point sprite shooting test
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 22 Jan 2015 06:19:52 +0200
parents
children
line source
1 #include <stdlib.h>
2 #include <GL/glew.h>
3 #include "projectile.h"
4 #include "sdr.h"
6 #define MAX_DIST_SQ (100.0 * 100.0)
8 struct projectile {
9 vec3_t pos, vel;
10 struct projectile *next;
11 };
13 static struct projectile *plist, *ptail;
14 static unsigned int prog;
16 int init_shots(void)
17 {
18 if(!(prog = create_program_load("sdr/psprite.v.glsl", "sdr/psprite.p.glsl"))) {
19 return -1;
20 }
21 return 0;
22 }
24 void shoot(vec3_t orig, vec3_t dir)
25 {
26 struct projectile *p;
28 if(!(p = malloc(sizeof *p))) {
29 return;
30 }
31 p->pos = orig;
32 p->vel = dir;
33 p->next = 0;
35 if(plist) {
36 ptail->next = p;
37 ptail = p;
38 } else {
39 plist = ptail = p;
40 }
41 }
43 void update_shots(float dt)
44 {
45 struct projectile dummy, *pp;
47 dummy.next = plist;
48 pp = &dummy;
50 while(pp->next) {
51 struct projectile *p = pp->next;
53 p->pos = v3_add(p->pos, v3_scale(p->vel, dt));
55 if(v3_length_sq(p->pos) > MAX_DIST_SQ) {
56 void *tmp = p;
57 pp->next = p->next;
58 free(tmp);
59 } else {
60 pp = pp->next;
61 }
62 }
64 plist = dummy.next;
65 }
67 void draw_shots(void)
68 {
69 int vp[4];
70 struct projectile *p = plist;
72 glPushAttrib(GL_ENABLE_BIT);
74 glEnable(GL_BLEND);
75 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
77 glDepthMask(0);
79 glEnable(GL_POINT_SPRITE);
80 glEnable(GL_PROGRAM_POINT_SIZE);
81 glUseProgram(prog);
83 glGetIntegerv(GL_VIEWPORT, vp);
84 if(set_uniform_float2(prog, "viewport", vp[2], vp[3]) == -1) {
85 fprintf(stderr, "failed to set viewport\n");
86 }
88 glBegin(GL_POINTS);
90 while(p) {
91 glVertex3f(p->pos.x, p->pos.y, p->pos.z);
92 p = p->next;
93 }
95 glEnd();
97 glDepthMask(1);
99 glUseProgram(0);
100 glPopAttrib();
103 glDisable(GL_LIGHTING);
104 glBegin(GL_LINES);
105 glColor3f(0, 1, 0);
106 p = plist;
107 while(p) {
108 float x0 = p->pos.x - 0.5;
109 float y0 = p->pos.y - 0.5;
110 float x1 = p->pos.x + 0.5;
111 float y1 = p->pos.y + 0.5;
112 glVertex3f(x0, y0, p->pos.z);
113 glVertex3f(x1, y0, p->pos.z);
114 glVertex3f(x1, y0, p->pos.z);
115 glVertex3f(x1, y1, p->pos.z);
116 glVertex3f(x1, y1, p->pos.z);
117 glVertex3f(x0, y1, p->pos.z);
118 glVertex3f(x0, y1, p->pos.z);
119 glVertex3f(x0, y0, p->pos.z);
120 p = p->next;
121 }
122 glEnd();
123 glEnable(GL_LIGHTING);
124 }