oculus2_psprite

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/projectile.c	Thu Jan 22 06:19:52 2015 +0200
     1.3 @@ -0,0 +1,124 @@
     1.4 +#include <stdlib.h>
     1.5 +#include <GL/glew.h>
     1.6 +#include "projectile.h"
     1.7 +#include "sdr.h"
     1.8 +
     1.9 +#define MAX_DIST_SQ		(100.0 * 100.0)
    1.10 +
    1.11 +struct projectile {
    1.12 +	vec3_t pos, vel;
    1.13 +	struct projectile *next;
    1.14 +};
    1.15 +
    1.16 +static struct projectile *plist, *ptail;
    1.17 +static unsigned int prog;
    1.18 +
    1.19 +int init_shots(void)
    1.20 +{
    1.21 +	if(!(prog = create_program_load("sdr/psprite.v.glsl", "sdr/psprite.p.glsl"))) {
    1.22 +		return -1;
    1.23 +	}
    1.24 +	return 0;
    1.25 +}
    1.26 +
    1.27 +void shoot(vec3_t orig, vec3_t dir)
    1.28 +{
    1.29 +	struct projectile *p;
    1.30 +
    1.31 +	if(!(p = malloc(sizeof *p))) {
    1.32 +		return;
    1.33 +	}
    1.34 +	p->pos = orig;
    1.35 +	p->vel = dir;
    1.36 +	p->next = 0;
    1.37 +
    1.38 +	if(plist) {
    1.39 +		ptail->next = p;
    1.40 +		ptail = p;
    1.41 +	} else {
    1.42 +		plist = ptail = p;
    1.43 +	}
    1.44 +}
    1.45 +
    1.46 +void update_shots(float dt)
    1.47 +{
    1.48 +	struct projectile dummy, *pp;
    1.49 +
    1.50 +	dummy.next = plist;
    1.51 +	pp = &dummy;
    1.52 +
    1.53 +	while(pp->next) {
    1.54 +		struct projectile *p = pp->next;
    1.55 +
    1.56 +		p->pos = v3_add(p->pos, v3_scale(p->vel, dt));
    1.57 +
    1.58 +		if(v3_length_sq(p->pos) > MAX_DIST_SQ) {
    1.59 +			void *tmp = p;
    1.60 +			pp->next = p->next;
    1.61 +			free(tmp);
    1.62 +		} else {
    1.63 +			pp = pp->next;
    1.64 +		}
    1.65 +	}
    1.66 +
    1.67 +	plist = dummy.next;
    1.68 +}
    1.69 +
    1.70 +void draw_shots(void)
    1.71 +{
    1.72 +	int vp[4];
    1.73 +	struct projectile *p = plist;
    1.74 +
    1.75 +	glPushAttrib(GL_ENABLE_BIT);
    1.76 +
    1.77 +	glEnable(GL_BLEND);
    1.78 +	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    1.79 +
    1.80 +	glDepthMask(0);
    1.81 +
    1.82 +	glEnable(GL_POINT_SPRITE);
    1.83 +	glEnable(GL_PROGRAM_POINT_SIZE);
    1.84 +	glUseProgram(prog);
    1.85 +
    1.86 +	glGetIntegerv(GL_VIEWPORT, vp);
    1.87 +	if(set_uniform_float2(prog, "viewport", vp[2], vp[3]) == -1) {
    1.88 +		fprintf(stderr, "failed to set viewport\n");
    1.89 +	}
    1.90 +
    1.91 +	glBegin(GL_POINTS);
    1.92 +
    1.93 +	while(p) {
    1.94 +		glVertex3f(p->pos.x, p->pos.y, p->pos.z);
    1.95 +		p = p->next;
    1.96 +	}
    1.97 +
    1.98 +	glEnd();
    1.99 +
   1.100 +	glDepthMask(1);
   1.101 +
   1.102 +	glUseProgram(0);
   1.103 +	glPopAttrib();
   1.104 +
   1.105 +
   1.106 +	glDisable(GL_LIGHTING);
   1.107 +	glBegin(GL_LINES);
   1.108 +	glColor3f(0, 1, 0);
   1.109 +	p = plist;
   1.110 +	while(p) {
   1.111 +		float x0 = p->pos.x - 0.5;
   1.112 +		float y0 = p->pos.y - 0.5;
   1.113 +		float x1 = p->pos.x + 0.5;
   1.114 +		float y1 = p->pos.y + 0.5;
   1.115 +		glVertex3f(x0, y0, p->pos.z);
   1.116 +		glVertex3f(x1, y0, p->pos.z);
   1.117 +		glVertex3f(x1, y0, p->pos.z);
   1.118 +		glVertex3f(x1, y1, p->pos.z);
   1.119 +		glVertex3f(x1, y1, p->pos.z);
   1.120 +		glVertex3f(x0, y1, p->pos.z);
   1.121 +		glVertex3f(x0, y1, p->pos.z);
   1.122 +		glVertex3f(x0, y0, p->pos.z);
   1.123 +		p = p->next;
   1.124 +	}
   1.125 +	glEnd();
   1.126 +	glEnable(GL_LIGHTING);
   1.127 +}