ld33_umonster

view 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 source
1 #include <algorithm>
2 #include "opengl.h"
3 #include "dragon.h"
5 Dragon::Dragon()
6 : pos(0, 0, 0), dir(0, 0, -1), head_pos(0, 0, -1), target(0, 0, -2)
7 {
8 set_head_limits(-1, 1, -1, 1);
9 }
11 Dragon::~Dragon()
12 {
13 }
15 void Dragon::set_position(const Vector3 &p)
16 {
17 pos = p;
18 }
20 void Dragon::set_direction(const Vector3 &dir)
21 {
22 this->dir = dir.normalized();
23 }
25 void Dragon::set_target(const Vector3 &p)
26 {
27 target = p;
28 }
30 void Dragon::set_head_limits(float xmin, float xmax, float ymin, float ymax)
31 {
32 head_xlim[0] = std::min(xmin, xmax);
33 head_xlim[1] = std::max(xmin, xmax);
34 head_ylim[0] = std::min(ymin, ymax);
35 head_ylim[1] = std::max(ymin, ymax);
36 }
38 void Dragon::move_head(const Vector3 &p)
39 {
40 head_pos = p;
41 }
43 static float clamp(float x, float low, float high)
44 {
45 return x < low ? low : (x > high ? high : x);
46 }
48 void Dragon::move_head(float dx, float dy)
49 {
50 float newx = clamp(head_pos.x + dx, head_xlim[0], head_xlim[1]);
51 float newy = clamp(head_pos.y + dy, head_ylim[0], head_ylim[1]);
53 dx = newx - head_pos.x;
54 dy = newy - head_pos.y;
55 head_pos.x = newx;
56 head_pos.y = newy;
58 target.x += dx * 0.7;
59 target.y += dy * 0.5;
60 }
62 const Vector3 &Dragon::head_position() const
63 {
64 return head_pos;
65 }
67 Vector3 Dragon::breath_dir() const
68 {
69 return (target - head_pos).normalized();
70 }
72 void Dragon::update()
73 {
74 }
76 static Vector3 bezier(const Vector3 &a, const Vector3 &b, const Vector3 &c, const Vector3 &d, float t)
77 {
78 float x = bezier(a.x, b.x, c.x, d.x, t);
79 float y = bezier(a.y, b.y, c.y, d.y, t);
80 float z = bezier(a.z, b.z, c.z, d.z, t);
81 return Vector3(x, y, z);
82 }
84 void Dragon::draw() const
85 {
86 Vector3 bdir = breath_dir();
87 Vector3 bezcp[] = { pos, pos + dir * 6.0, head_pos - bdir * 8.0, head_pos };
89 int cur_sdr;
90 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
91 glUseProgram(0);
93 glPushAttrib(GL_ENABLE_BIT);
94 glDisable(GL_LIGHTING);
96 glLineWidth(2.0);
97 glColor3f(0, 0, 1);
99 glBegin(GL_LINE_STRIP);
100 for(int i=0; i<10; i++) {
101 float t = (float)i / 10.0f;
102 Vector3 p = bezier(bezcp[0], bezcp[1], bezcp[2], bezcp[3], t);
103 glVertex3f(p.x, p.y, p.z);
104 }
105 glEnd();
106 glLineWidth(1);
108 glPointSize(5.0);
109 glColor3f(0, 1, 0);
111 glBegin(GL_POINTS);
112 for(int i=0; i<4; i++) {
113 glVertex3f(bezcp[i].x, bezcp[i].y, bezcp[i].z);
114 }
115 glEnd();
117 glPopAttrib();
119 glUseProgram(cur_sdr);
120 }