ld33_umonster

view src/dragon.cc @ 8:bed39534d471

fell back to the equi-radii capsule which works
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 25 Aug 2015 02:26:01 +0300
parents 92d662deb66e
children 4f6168f3ca82
line source
1 #include <algorithm>
2 #include "opengl.h"
3 #include "dragon.h"
4 #include "metasurf.h"
5 #include "geom.h"
6 #include "game.h"
7 #include "shadow.h"
9 #define VOXEL_PAD 1.0f
10 #define DYN_FCOUNT 64
11 #define DYN_VCOUNT (DYN_FCOUNT * 3)
13 #define NUM_NECK_SEG 10
14 static const float nseg_sizes[NUM_NECK_SEG][2] = {
15 {2.0, 2.0}, {1.0, 1.0}, {1.0, 1.0}, {1.25, 1.25}, {1.3, 1.3}, {1.5, 1.5}, {1.6, 1.6}, {1.75, 1.75}, {2.0, 2.0}, {2.1, 2.1}
16 };
17 #define NSEG_SZ_SCALE 0.5f
19 static Vector3 bezier(const Vector3 &a, const Vector3 &b, const Vector3 &c, const Vector3 &d, float t);
20 static float mseval(struct metasurface *ms, float x, float y, float z);
21 static void msvertex(struct metasurface *ms, float x, float y, float z);
24 Dragon::Dragon()
25 : pos(0, 0, 0), dir(0, 0, -1), head_pos(0, 0, -1), target(0, 0, -2)
26 {
27 set_head_limits(-1, 1, -1, 1);
29 glGenBuffers(1, &dyn_vbo);
30 glBindBuffer(GL_ARRAY_BUFFER, dyn_vbo);
31 glBufferData(GL_ARRAY_BUFFER, DYN_VCOUNT * sizeof(DynVertex), 0, GL_STREAM_DRAW);
32 glBindBuffer(GL_ARRAY_BUFFER, 0);
34 dyn_varr = new DynVertex[DYN_VCOUNT];
36 neck_seg_count = NUM_NECK_SEG;
37 neck_seg = new Capsule[neck_seg_count];
39 for(int i=0; i<neck_seg_count; i++) {
40 int idx = neck_seg_count - i - 1;
41 neck_seg[i].w[0] = nseg_sizes[idx][0] * NSEG_SZ_SCALE;
42 neck_seg[i].w[1] = nseg_sizes[idx][1] * NSEG_SZ_SCALE;
43 }
45 msurf = msurf_create();
46 msurf_set_user_data(msurf, this);
47 msurf_set_resolution(msurf, 36, 36, 36);
48 msurf_set_threshold(msurf, 1.0);
49 msurf_eval_func(msurf, mseval);
50 msurf_vertex_func(msurf, msvertex);
51 }
53 Dragon::~Dragon()
54 {
55 delete [] neck_seg;
56 msurf_free(msurf);
58 delete [] dyn_varr;
59 glDeleteBuffers(1, &dyn_vbo);
60 }
62 void Dragon::set_position(const Vector3 &p)
63 {
64 pos = p;
65 }
67 void Dragon::set_direction(const Vector3 &dir)
68 {
69 this->dir = dir.normalized();
70 }
72 void Dragon::set_target(const Vector3 &p)
73 {
74 target = p;
75 }
77 void Dragon::set_head_limits(float xmin, float xmax, float ymin, float ymax)
78 {
79 head_xlim[0] = std::min(xmin, xmax);
80 head_xlim[1] = std::max(xmin, xmax);
81 head_ylim[0] = std::min(ymin, ymax);
82 head_ylim[1] = std::max(ymin, ymax);
83 }
85 void Dragon::move_head(const Vector3 &p)
86 {
87 head_pos = p;
88 }
90 static float clamp(float x, float low, float high)
91 {
92 return x < low ? low : (x > high ? high : x);
93 }
95 void Dragon::move_head(float dx, float dy)
96 {
97 float newx = clamp(head_pos.x + dx, head_xlim[0], head_xlim[1]);
98 float newy = clamp(head_pos.y + dy, head_ylim[0], head_ylim[1]);
100 dx = newx - head_pos.x;
101 dy = newy - head_pos.y;
102 head_pos.x = newx;
103 head_pos.y = newy;
105 target.x += dx * 0.7;
106 target.y += dy * 0.5;
107 }
109 const Vector3 &Dragon::head_position() const
110 {
111 return head_pos;
112 }
114 Vector3 Dragon::breath_dir() const
115 {
116 return (target - head_pos).normalized();
117 }
119 void Dragon::update()
120 {
121 Vector3 bdir = breath_dir();
122 Vector3 bezcp[] = { pos, pos + dir * 6.0, head_pos - bdir * 8.0, head_pos };
124 float t = 0.0, dt = 1.0 / (float)(neck_seg_count + 1);
125 Vector3 p = bezier(bezcp[0], bezcp[1], bezcp[2], bezcp[3], t);
127 for(int i=0; i<neck_seg_count; i++) {
128 t += dt;
129 Vector3 pnext = bezier(bezcp[0], bezcp[1], bezcp[2], bezcp[3], t);
131 neck_seg[i].p[0] = p;
132 neck_seg[i].p[1] = pnext;
134 p = pnext;
135 }
136 }
138 void Dragon::draw() const
139 {
140 static float bmin[] = { head_xlim[0] - VOXEL_PAD * 1.2f, head_ylim[0] - VOXEL_PAD, head_pos.z };
141 static float bmax[] = { head_xlim[1] + VOXEL_PAD * 1.2f, head_ylim[1] + VOXEL_PAD * 2.1f, pos.z + VOXEL_PAD };
142 msurf_set_bounds(msurf, bmin[0], bmin[1], bmin[2], bmax[0], bmax[1], bmax[2]);
145 if(!shadow_pass) {
147 if(dbg_wireframe) {
148 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
149 }
151 dyn_vidx = 0;
152 msurf_polygonize(msurf);
153 flush_dynvbo();
155 if(dbg_wireframe) {
156 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
157 }
159 int cur_sdr;
160 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
161 glUseProgram(0);
163 glPushAttrib(GL_ENABLE_BIT);
164 glDisable(GL_LIGHTING);
166 // bounds
167 glColor3f(1, 0, 0);
168 glBegin(GL_LINE_LOOP);
169 glVertex3f(bmin[0], bmin[1], bmin[2]);
170 glVertex3f(bmax[0], bmin[1], bmin[2]);
171 glVertex3f(bmax[0], bmax[1], bmin[2]);
172 glVertex3f(bmin[0], bmax[1], bmin[2]);
173 glEnd();
174 glBegin(GL_LINE_LOOP);
175 glVertex3f(bmin[0], bmin[1], bmax[2]);
176 glVertex3f(bmax[0], bmin[1], bmax[2]);
177 glVertex3f(bmax[0], bmax[1], bmax[2]);
178 glVertex3f(bmin[0], bmax[1], bmax[2]);
179 glEnd();
180 glBegin(GL_LINE_LOOP);
181 glVertex3f(bmin[0], bmax[1], bmin[2]);
182 glVertex3f(bmax[0], bmax[1], bmin[2]);
183 glVertex3f(bmax[0], bmax[1], bmax[2]);
184 glVertex3f(bmin[0], bmax[1], bmax[2]);
185 glEnd();
186 glBegin(GL_LINE_LOOP);
187 glVertex3f(bmin[0], bmin[1], bmin[2]);
188 glVertex3f(bmax[0], bmin[1], bmin[2]);
189 glVertex3f(bmax[0], bmin[1], bmax[2]);
190 glVertex3f(bmin[0], bmin[1], bmax[2]);
191 glEnd();
193 // foo
194 glDisable(GL_DEPTH_TEST);
195 glEnable(GL_BLEND);
196 glBlendFunc(GL_ONE, GL_ONE);
197 glLineWidth(2.0);
198 glColor3f(0, 0, 1);
200 glBegin(GL_LINES);
201 for(int i=0; i<neck_seg_count; i++) {
202 glVertex3f(neck_seg[i].p[0].x, neck_seg[i].p[0].y, neck_seg[i].p[0].z);
203 glVertex3f(neck_seg[i].p[1].x, neck_seg[i].p[1].y, neck_seg[i].p[1].z);
204 }
205 glEnd();
206 glLineWidth(1);
208 // done debug drawing
209 glPopAttrib();
210 if(cur_sdr) glUseProgram(cur_sdr);
211 }
212 }
214 void Dragon::flush_dynvbo() const
215 {
216 if(!dyn_vidx) return;
218 glBindBuffer(GL_ARRAY_BUFFER, dyn_vbo);
219 glBufferSubData(GL_ARRAY_BUFFER, 0, dyn_vidx * sizeof(DynVertex), dyn_varr);
221 glEnableClientState(GL_VERTEX_ARRAY);
222 glVertexPointer(3, GL_FLOAT, sizeof(DynVertex), (void*)offsetof(DynVertex, x));
223 glEnableClientState(GL_NORMAL_ARRAY);
224 glNormalPointer(GL_FLOAT, sizeof(DynVertex), (void*)offsetof(DynVertex, nx));
225 glBindBuffer(GL_ARRAY_BUFFER, 0);
227 glDrawArrays(GL_TRIANGLES, 0, dyn_vidx);
229 glDisableClientState(GL_VERTEX_ARRAY);
230 glDisableClientState(GL_NORMAL_ARRAY);
231 dyn_vidx = 0;
232 }
235 static Vector3 bezier(const Vector3 &a, const Vector3 &b, const Vector3 &c, const Vector3 &d, float t)
236 {
237 float x = bezier(a.x, b.x, c.x, d.x, t);
238 float y = bezier(a.y, b.y, c.y, d.y, t);
239 float z = bezier(a.z, b.z, c.z, d.z, t);
240 return Vector3(x, y, z);
241 }
243 static float mseval(struct metasurface *ms, float x, float y, float z)
244 {
245 Dragon *dragon = (Dragon*)msurf_get_user_data(ms);
247 Vector3 pt = Vector3(x, y, z);
248 Capsule *seg = dragon->neck_seg;
250 //printf("eval(%g %g %g)\n", x, y, z);
252 float sum = 0.0f;
253 for(int i=0; i<dragon->neck_seg_count; i++) {
254 float dist = capsule_distance(seg[i].p[0], seg[i].w[0], seg[i].p[1], seg[i].w[1], pt);
255 //float dist = sphere_distance(seg[i].p[0], 1.0, pt);
256 if(dist < 1e-4) dist = 1e-4;
257 float energy = 0.0001 / (dist * dist);
258 /*float dx = x - seg[i].p[0].x;
259 float dy = y - seg[i].p[0].y;
260 float dz = z - seg[i].p[0].z;
261 float energy = 0.5 / (dx * dx + dy * dy + dz * dz);*/
262 sum += energy;
263 }
264 return sum;
265 }
267 static void msvertex(struct metasurface *ms, float x, float y, float z)
268 {
269 Dragon *dragon = (Dragon*)msurf_get_user_data(ms);
271 const float dt = 0.001;
272 float dfdx = mseval(ms, x - dt, y, z) - mseval(ms, x + dt, y, z);
273 float dfdy = mseval(ms, x, y - dt, z) - mseval(ms, x, y + dt, z);
274 float dfdz = mseval(ms, x, y, z - dt) - mseval(ms, x, y, z + dt);
276 DynVertex *vptr = dragon->dyn_varr + dragon->dyn_vidx++;
277 vptr->x = x;
278 vptr->y = y;
279 vptr->z = z;
280 vptr->nx = dfdx;
281 vptr->ny = dfdy;
282 vptr->nz = dfdz;
284 if(dragon->dyn_vidx >= DYN_VCOUNT) {
285 dragon->flush_dynvbo();
286 }
287 }