ld33_umonster

diff src/dragon.cc @ 6:3b4460b34d43

progress
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 23 Aug 2015 05:37:09 +0300
parents
children 92d662deb66e
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/dragon.cc	Sun Aug 23 05:37:09 2015 +0300
     1.3 @@ -0,0 +1,120 @@
     1.4 +#include <algorithm>
     1.5 +#include "opengl.h"
     1.6 +#include "dragon.h"
     1.7 +
     1.8 +Dragon::Dragon()
     1.9 +	: pos(0, 0, 0), dir(0, 0, -1), head_pos(0, 0, -1), target(0, 0, -2)
    1.10 +{
    1.11 +	set_head_limits(-1, 1, -1, 1);
    1.12 +}
    1.13 +
    1.14 +Dragon::~Dragon()
    1.15 +{
    1.16 +}
    1.17 +
    1.18 +void Dragon::set_position(const Vector3 &p)
    1.19 +{
    1.20 +	pos = p;
    1.21 +}
    1.22 +
    1.23 +void Dragon::set_direction(const Vector3 &dir)
    1.24 +{
    1.25 +	this->dir = dir.normalized();
    1.26 +}
    1.27 +
    1.28 +void Dragon::set_target(const Vector3 &p)
    1.29 +{
    1.30 +	target = p;
    1.31 +}
    1.32 +
    1.33 +void Dragon::set_head_limits(float xmin, float xmax, float ymin, float ymax)
    1.34 +{
    1.35 +	head_xlim[0] = std::min(xmin, xmax);
    1.36 +	head_xlim[1] = std::max(xmin, xmax);
    1.37 +	head_ylim[0] = std::min(ymin, ymax);
    1.38 +	head_ylim[1] = std::max(ymin, ymax);
    1.39 +}
    1.40 +
    1.41 +void Dragon::move_head(const Vector3 &p)
    1.42 +{
    1.43 +	head_pos = p;
    1.44 +}
    1.45 +
    1.46 +static float clamp(float x, float low, float high)
    1.47 +{
    1.48 +	return x < low ? low : (x > high ? high : x);
    1.49 +}
    1.50 +
    1.51 +void Dragon::move_head(float dx, float dy)
    1.52 +{
    1.53 +	float newx = clamp(head_pos.x + dx, head_xlim[0], head_xlim[1]);
    1.54 +	float newy = clamp(head_pos.y + dy, head_ylim[0], head_ylim[1]);
    1.55 +
    1.56 +	dx = newx - head_pos.x;
    1.57 +	dy = newy - head_pos.y;
    1.58 +	head_pos.x = newx;
    1.59 +	head_pos.y = newy;
    1.60 +
    1.61 +	target.x += dx * 0.7;
    1.62 +	target.y += dy * 0.5;
    1.63 +}
    1.64 +
    1.65 +const Vector3 &Dragon::head_position() const
    1.66 +{
    1.67 +	return head_pos;
    1.68 +}
    1.69 +
    1.70 +Vector3 Dragon::breath_dir() const
    1.71 +{
    1.72 +	return (target - head_pos).normalized();
    1.73 +}
    1.74 +
    1.75 +void Dragon::update()
    1.76 +{
    1.77 +}
    1.78 +
    1.79 +static Vector3 bezier(const Vector3 &a, const Vector3 &b, const Vector3 &c, const Vector3 &d, float t)
    1.80 +{
    1.81 +	float x = bezier(a.x, b.x, c.x, d.x, t);
    1.82 +	float y = bezier(a.y, b.y, c.y, d.y, t);
    1.83 +	float z = bezier(a.z, b.z, c.z, d.z, t);
    1.84 +	return Vector3(x, y, z);
    1.85 +}
    1.86 +
    1.87 +void Dragon::draw() const
    1.88 +{
    1.89 +	Vector3 bdir = breath_dir();
    1.90 +	Vector3 bezcp[] = { pos, pos + dir * 6.0, head_pos - bdir * 8.0, head_pos };
    1.91 +
    1.92 +	int cur_sdr;
    1.93 +	glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
    1.94 +	glUseProgram(0);
    1.95 +
    1.96 +	glPushAttrib(GL_ENABLE_BIT);
    1.97 +	glDisable(GL_LIGHTING);
    1.98 +
    1.99 +	glLineWidth(2.0);
   1.100 +	glColor3f(0, 0, 1);
   1.101 +
   1.102 +	glBegin(GL_LINE_STRIP);
   1.103 +	for(int i=0; i<10; i++) {
   1.104 +		float t = (float)i / 10.0f;
   1.105 +		Vector3 p = bezier(bezcp[0], bezcp[1], bezcp[2], bezcp[3], t);
   1.106 +		glVertex3f(p.x, p.y, p.z);
   1.107 +	}
   1.108 +	glEnd();
   1.109 +	glLineWidth(1);
   1.110 +
   1.111 +	glPointSize(5.0);
   1.112 +	glColor3f(0, 1, 0);
   1.113 +
   1.114 +	glBegin(GL_POINTS);
   1.115 +	for(int i=0; i<4; i++) {
   1.116 +		glVertex3f(bezcp[i].x, bezcp[i].y, bezcp[i].z);
   1.117 +	}
   1.118 +	glEnd();
   1.119 +
   1.120 +	glPopAttrib();
   1.121 +
   1.122 +	glUseProgram(cur_sdr);
   1.123 +}