comparison render/render.d @ 27:d63faa81a5e4

removed Dog, added derelict and glfw
author zzzzrrr <mason.green@gmail.com>
date Mon, 30 Mar 2009 12:41:29 -0400
parents 88cca12cc8b9
children 1cc6b8c0acd2
comparison
equal deleted inserted replaced
26:88cca12cc8b9 27:d63faa81a5e4
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 module openmelee.render.render; 31 module openmelee.render.render;
32 32
33 import tango.math.Math : PI; 33 import tango.math.Math : PI, cos, sin;
34
35 import xf.dog.Dog;
36 import xf.omg.core.LinearAlgebra;
37 import xf.hybrid.Event;
38 import xf.hybrid.Font;
39 34
40 import blaze.bzWorld : bzWorld; 35 import blaze.bzWorld : bzWorld;
41 import blaze.dynamics.bzBody : bzBody; 36 import blaze.dynamics.bzBody : bzBody;
42 import blaze.collision.shapes.bzShape : bzShape; 37 import blaze.collision.shapes.bzShape : bzShape;
43 import blaze.collision.shapes.bzShapeType; 38 import blaze.collision.shapes.bzShapeType;
49 import blaze.common.bzMath : bzXForm, bzVec2, bzMul, bzClamp; 44 import blaze.common.bzMath : bzXForm, bzVec2, bzMul, bzClamp;
50 import blaze.common.bzConstants : k_toiSlop,k_maxProxies; 45 import blaze.common.bzConstants : k_toiSlop,k_maxProxies;
51 46
52 import openmelee.ships.ship : Ship, State; 47 import openmelee.ships.ship : Ship, State;
53 import openmelee.melee.melee : Settings; 48 import openmelee.melee.melee : Settings;
49 import openmelee.ai.human : Human;
50
51 import derelict.opengl.gl;
52 import derelict.opengl.glu;
53 import openmelee.glfw.glfw;
54 54
55 // Cursor scale factor 55 // Cursor scale factor
56 const CURSORSIZE = 0.05f; 56 const CURSORSIZE = 0.05f;
57 const INIT_SPAWN_SIZE = 0.5f; 57 const INIT_SPAWN_SIZE = 0.5f;
58 // Dragging stuffs 58 // Dragging stuffs
63 const HINGE_RADIUS = 0.05f; 63 const HINGE_RADIUS = 0.05f;
64 // Smallest allowed dimension 64 // Smallest allowed dimension
65 const MIN_DIMENSION = 0.1; 65 const MIN_DIMENSION = 0.1;
66 const MAX_CIRCLE_RES = 32; 66 const MAX_CIRCLE_RES = 32;
67 67
68 Human human;
69
68 /// Color for drawing. Each value has the range [0,1]. 70 /// Color for drawing. Each value has the range [0,1].
69 struct Color { 71 struct Color {
70 static Color opCall(float r, float g, float b) 72 static Color opCall(float r, float g, float b)
71 { 73 {
72 Color u; 74 Color u;
79 float r = 0; 81 float r = 0;
80 float g = 0; 82 float g = 0;
81 float b = 0; 83 float b = 0;
82 } 84 }
83 85
86 void key(int a, int b) {
87 human.onKey(a, b);
88 }
89
84 class Render 90 class Render
85 { 91 {
86 92
87 float zoom = 40; 93 float zoom = 40;
88 vec2 viewCenter; 94 bzVec2 viewCenter;
89 bzWorld world; 95 bzWorld world;
90 vec2i screenSize; 96 bzVec2 screenSize;
91 bool scaling = false; 97 bool scaling = false;
92 bool full = false; 98 bool full = false;
93 Settings settings; 99 Settings settings;
94 Ship ship1, ship2; 100 Ship ship1, ship2;
95 101
96 this(bzWorld world, Ship s1, Ship s2, Settings settings) { 102 this(bzWorld world, Ship s1, Ship s2, Human h, Settings settings) {
103
104 human = h;
97 this.settings = settings; 105 this.settings = settings;
98 ship1 = s1; 106 ship1 = s1;
99 ship2 = s2; 107 ship2 = s2;
100 this.world = world; 108 this.world = world;
101 viewCenter = vec2(10, 10); 109 viewCenter = bzVec2(10, 10);
102 screenSize = vec2i.zero; 110 screenSize = bzVec2(800, 600);
103 } 111
104 112 DerelictGL.load();
105 void drawCircle(GL gl, vec2 center, float radius, bool water = false, float theta = float.nan) 113 DerelictGLU.load();
114 DerelictGLFW.load();
115 glfwInit();
116
117 // Open window
118 int width = cast(int) screenSize.x;
119 int height = cast(int) screenSize.y;
120 int ok = glfwOpenWindow(width, height, 8, 8, 8, 8, 8, 0, GLFW_WINDOW);
121
122 if(!ok) {
123 assert(0, "error loading window");
124 }
125
126 glfwSetWindowTitle("OpenMelee");
127 glfwEnable(GLFW_STICKY_KEYS);
128
129 }
130
131 ~this() {
132 glfwTerminate();
133 }
134
135 void update() {
136 // Limit the fps
137 glfwSwapInterval(1);
138 draw();
139 glfwSwapBuffers();
140 }
141
142 void keys() {
143 GLFWkeyfun cbfun;
144 cbfun = &key;
145 glfwSetKeyCallback(cbfun);
146 }
147
148 void drawCircle(bzVec2 center, float radius, bool water = false, float theta = float.nan)
106 { 149 {
107 int segs = cast(int)(radius) + 20; 150 int segs = cast(int)(radius) + 20;
108 if (segs > MAX_CIRCLE_RES) segs = MAX_CIRCLE_RES; 151 if (segs > MAX_CIRCLE_RES) segs = MAX_CIRCLE_RES;
109 double coef = 2.0 * PI / segs; 152 double coef = 2.0 * PI / segs;
110 153
111 auto realTheta = (theta <>= 0 ? theta : 0); 154 auto realTheta = (theta <>= 0 ? theta : 0);
112 if (water) { 155 if (water) {
113 gl.immediate(GL_TRIANGLE_FAN, 156 glBegin(GL_TRIANGLE_FAN);
114 { 157 {
115 gl.Vertex2fv(center.ptr); 158 glVertex2f(center.x, center.y);
116 for (int n = 0; n <= segs; n++) { 159 for (int n = 0; n <= segs; n++) {
117 double rads = n * coef; 160 double rads = n * coef;
118 gl.Vertex2f(radius * cos(rads + realTheta) + center.x, radius * sin(rads + realTheta) + center.y); 161 glVertex2f(radius * cos(rads + realTheta) + center.x, radius * sin(rads + realTheta) + center.y);
119 } 162 }
120 }); 163 }
121 } 164 glEnd();
122 165 }
123 gl.immediate(GL_LINE_STRIP, 166
167 glBegin(GL_LINE_STRIP);
124 { 168 {
125 for (int n = 0; n <= segs; n++) { 169 for (int n = 0; n <= segs; n++) {
126 double rads = n * coef; 170 double rads = n * coef;
127 gl.Vertex2f(radius * cos(rads + realTheta) + center.x, radius * sin(rads + realTheta) + center.y); 171 glVertex2f(radius * cos(rads + realTheta) + center.x, radius * sin(rads + realTheta) + center.y);
128 } 172 }
129 if (theta <>= 0) 173 if (theta <>= 0)
130 gl.Vertex2fv(center.ptr); 174 glVertex2f(center.x, center.y);
131 }); 175 }
132 } 176 glEnd();
133 177 }
134 void drawSolidCircle(GL gl, vec2 center, float radius, vec2 axis, Color color) 178
179 void drawSolidCircle(bzVec2 center, float radius, bzVec2 axis, Color color)
135 { 180 {
136 const k_segments = 25.0f; 181 const k_segments = 25.0f;
137 const k_increment = 2.0f * PI / k_segments; 182 const k_increment = 2.0f * PI / k_segments;
138 float theta = 0.0f; 183 float theta = 0.0f;
139 gl.Enable(GL_BLEND); 184 glEnable(GL_BLEND);
140 gl.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 185 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
141 gl.Color4f(0.5f * color.r, 0.5f * color.g, 0.5f * color.b, 0.5f); 186 glColor4f(0.5f * color.r, 0.5f * color.g, 0.5f * color.b, 0.5f);
142 gl.Begin(GL_TRIANGLE_FAN); 187 glBegin(GL_TRIANGLE_FAN);
143 for (int i = 0; i < k_segments; ++i) { 188 for (int i = 0; i < k_segments; ++i) {
144 vec2 v = center + radius * vec2(cos(theta), sin(theta)); 189 bzVec2 v = center + radius * bzVec2(cos(theta), sin(theta));
145 gl.Vertex2f(v.x, v.y); 190 glVertex2f(v.x, v.y);
146 theta += k_increment; 191 theta += k_increment;
147 } 192 }
148 gl.End(); 193 glEnd();
149 gl.Disable(GL_BLEND); 194 glDisable(GL_BLEND);
150 195
151 theta = 0.0f; 196 theta = 0.0f;
152 gl.Color4f(color.r, color.g, color.b, 1.0f); 197 glColor4f(color.r, color.g, color.b, 1.0f);
153 gl.Begin(GL_LINE_LOOP); 198 glBegin(GL_LINE_LOOP);
154 for (int i = 0; i < k_segments; ++i) { 199 for (int i = 0; i < k_segments; ++i) {
155 vec2 v = center + radius * vec2(cos(theta), sin(theta)); 200 bzVec2 v = center + radius * bzVec2(cos(theta), sin(theta));
156 gl.Vertex2f(v.x, v.y); 201 glVertex2f(v.x, v.y);
157 theta += k_increment; 202 theta += k_increment;
158 } 203 }
159 gl.End(); 204 glEnd();
160 205
161 vec2 p = center + radius * axis; 206 bzVec2 p = center + radius * axis;
162 gl.Begin(GL_LINES); 207 glBegin(GL_LINES);
163 gl.Vertex2f(center.x, center.y); 208 glVertex2f(center.x, center.y);
164 gl.Vertex2f(p.x, p.y); 209 glVertex2f(p.x, p.y);
165 gl.End(); 210 glEnd();
166 } 211 }
167 212
168 void drawPolygon(GL gl, vec2[] glVerts, Color color) 213 void drawPolygon(bzVec2[] glVerts, Color color)
169 { 214 {
170 gl.Color3f(color.r, color.g, color.b); 215 glColor3f(color.r, color.g, color.b);
171 gl.immediate(GL_LINE_LOOP, 216 glBegin(GL_LINE_LOOP);
172 { 217 {
173 foreach (v; glVerts) 218 foreach (v; glVerts) {
174 gl.Vertex2fv(v.ptr); 219 glVertex2f(v.x, v.y);
175 }); 220 }
176 } 221 }
177 222 glEnd();
178 void drawSolidPolygon(GL gl, vec2[] vertices, Color color) 223 }
179 { 224
180 gl.Enable(GL_BLEND); 225 void drawSolidPolygon(bzVec2[] vertices, Color color)
181 gl.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 226 {
182 gl.Color4f(0.5f * color.r, 0.5f * color.g, 0.5f * color.b, 0.5f); 227 glEnable(GL_BLEND);
183 gl.Begin(GL_TRIANGLE_FAN); 228 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
184 for (int i = 0; i < vertices.length; ++i) { 229 glColor4f(0.5f * color.r, 0.5f * color.g, 0.5f * color.b, 0.5f);
185 gl.Vertex2f(vertices[i].x, vertices[i].y); 230 glBegin(GL_TRIANGLE_FAN);
186 } 231 {
187 gl.End(); 232 for (int i = 0; i < vertices.length; ++i) {
188 gl.Disable(GL_BLEND); 233 glVertex2f(vertices[i].x, vertices[i].y);
189 234 }
190 gl.Color4f(color.r, color.g, color.b, 1.0f); 235 }
191 gl.Begin(GL_LINE_LOOP); 236 glEnd();
192 for (int i = 0; i < vertices.length; ++i) { 237 glDisable(GL_BLEND);
193 gl.Vertex2f(vertices[i].x, vertices[i].y); 238
194 } 239 glColor4f(color.r, color.g, color.b, 1.0f);
195 gl.End(); 240 glBegin(GL_LINE_LOOP);
196 } 241 {
197 242 for (int i = 0; i < vertices.length; ++i) {
198 243 glVertex2f(vertices[i].x, vertices[i].y);
199 void drawPoint(GL gl, vec2 p, float size, Color color) 244 }
200 { 245 }
201 gl.Color3f(color.r, color.g, color.b); 246 glEnd();
202 gl.PointSize(size); 247 }
203 gl.Begin(GL_POINTS); 248
204 gl.Vertex2f(p.x, p.y); 249
205 gl.End(); 250 void drawPoint(bzVec2 p, float size, Color color)
206 gl.PointSize(1.0f); 251 {
207 } 252 glColor3f(color.r, color.g, color.b);
208 253 glPointSize(size);
209 void drawSegment(GL gl, vec2 begin, vec2 end, Color color) 254 glBegin(GL_POINTS);
210 { 255 glVertex2f(p.x, p.y);
211 gl.Color3f(color.r, color.g, color.b); 256 glEnd();
212 gl.immediate(GL_LINES, 257 glPointSize(1.0f);
213 { 258 }
214 gl.Vertex2fv(begin.ptr); 259
215 gl.Vertex2fv(end.ptr); 260 void drawSegment(bzVec2 begin, bzVec2 end, Color color)
216 }); 261 {
262 glColor3f(color.r, color.g, color.b);
263 glBegin(GL_LINES);
264 {
265 glVertex2f(begin.x, begin.y);
266 glVertex2f(end.x, end.y);
267 }
268 glEnd();
217 } 269 }
218 270
219 // TODO: handle inequal radii correctly 271 // TODO: handle inequal radii correctly
220 void connectCircles(GL gl, vec2 center1, float radius1, vec2 center2, float radius2) 272 void connectCircles(bzVec2 center1, float radius1, bzVec2 center2, float radius2)
221 { 273 {
222 auto d = center2 - center1; 274 auto d = center2 - center1;
223 if (!d.length) 275 if (!d.length)
224 return; 276 return;
225 d *= (d.length - radius1) / d.length; 277 d *= (d.length - radius1) / d.length;
226 center1 += d; 278 center1 += d;
227 center2 -= d; 279 center2 -= d;
228 gl.immediate(GL_LINES, 280 glBegin(GL_LINES);
229 { 281 {
230 gl.Vertex2fv(center1.ptr); 282 glVertex2f(center1.x, center1.y);
231 gl.Vertex2fv(center2.ptr); 283 glVertex2f(center2.x, center2.y);
232 }); 284 }
233 } 285 glEnd();
234 286 }
235 void drawXForm(GL gl, bzXForm xf) 287
288 void drawXForm(bzXForm xf)
236 { 289 {
237 bzVec2 p1 = xf.position, p2; 290 bzVec2 p1 = xf.position, p2;
238 const k_axisScale = 0.4f; 291 const k_axisScale = 0.4f;
239 292
240 gl.Begin(GL_LINES); 293 glBegin(GL_LINES);
241 { 294 {
242 gl.Color3f(1.0f, 0.0f, 0.0f); 295 glColor3f(1.0f, 0.0f, 0.0f);
243 gl.Vertex2f(p1.x, p1.y); 296 glVertex2f(p1.x, p1.y);
244 p2 = p1 + k_axisScale * xf.R.col1; 297 p2 = p1 + k_axisScale * xf.R.col1;
245 gl.Vertex2f(p2.x, p2.y); 298 glVertex2f(p2.x, p2.y);
246 299
247 gl.Color3f(0.0f, 1.0f, 0.0f); 300 glColor3f(0.0f, 1.0f, 0.0f);
248 gl.Vertex2f(p1.x, p1.y); 301 glVertex2f(p1.x, p1.y);
249 p2 = p1 + k_axisScale * xf.R.col2; 302 p2 = p1 + k_axisScale * xf.R.col2;
250 gl.Vertex2f(p2.x, p2.y); 303 glVertex2f(p2.x, p2.y);
251 } 304 }
252 gl.End(); 305 glEnd();
253 } 306 }
254 307
255 void drawSpring(GL gl, vec2 a, vec2 b, uint zigs) 308 void drawSpring(bzVec2 a, bzVec2 b, uint zigs)
256 { 309 {
257 zigs++; 310 zigs++;
258 311
259 // Portion of length dedicated to connectors 312 // Portion of length dedicated to connectors
260 const float connPart = 0.2; 313 const float connPart = 0.2;
261 314
262 vec2 inc = (b - a) / (zigs); 315 bzVec2 inc = (b - a) / (zigs);
263 // One step from a to b 316 // One step from a to b
264 vec2 zigLen = inc * (1 - connPart); 317 bzVec2 zigLen = inc * (1 - connPart);
265 // Length of a connector 318 // Length of a connector
266 vec2 connLen = inc * (connPart / 2) * zigs; 319 bzVec2 connLen = inc * (connPart / 2) * zigs;
267 // Width of a zig 320 // Width of a zig
268 vec2 zigWidth = (b - a).rotatedHalfPi.normalized; 321 bzVec2 zigWidth = (b - a).rotate(PI/2);
269 gl.immediate(GL_LINE_STRIP, 322 zigWidth.normalize;
270 { 323 glBegin(GL_LINE_STRIP);
271 gl.Vertex2fv(a.ptr); 324 {
325 glVertex2f(a.x, a.y);
272 326
273 a += connLen; 327 a += connLen;
274 gl.Vertex2fv(a.ptr); 328 glVertex2f(a.x, a.y);
275 329
276 bool dir = true; 330 bool dir = true;
277 a += zigWidth / 2 + zigLen / 2; 331 a += zigWidth / 2 + zigLen / 2;
278 for (int i = 0; i < zigs; i++) { 332 for (int i = 0; i < zigs; i++) {
279 gl.Vertex2fv(a.ptr); 333 glVertex2f(a.x, a.y);
280 a += zigLen; 334 a += zigLen;
281 if (dir) { 335 if (dir) {
282 a -= zigWidth; 336 a -= zigWidth;
283 }else { 337 }else {
284 a += zigWidth; 338 a += zigWidth;
285 } 339 }
286 dir = !dir; 340 dir = !dir;
287 } 341 }
288 342
289 gl.Vertex2fv((b - connLen).ptr); 343 glVertex2f((b - connLen).x, (b - connLen).y);
290 gl.Vertex2fv(b.ptr); 344 glVertex2f(b.x, b.y);
291 }); 345 }
292 } 346 glEnd();
293 347 }
294 void drawShape(GL gl, bzShape shape, bzXForm xf, Color color, bool core) 348
349 void drawShape(bzShape shape, bzXForm xf, Color color, bool core)
295 { 350 {
296 Color coreColor = Color(0.9f, 0.6f, 0.6f); 351 Color coreColor = Color(0.9f, 0.6f, 0.6f);
297 352
298 switch (shape.type) { 353 switch (shape.type) {
299 case bzShapeType.CIRCLE: 354 case bzShapeType.CIRCLE:
300 auto circle = cast(bzCircle)shape; 355 auto circle = cast(bzCircle)shape;
301 356
302 vec2 center = vec2.from(bzMul(xf, circle.localPosition)); 357 bzVec2 center = bzMul(xf, circle.localPosition);
303 float radius = circle.radius; 358 float radius = circle.radius;
304 vec2 axis = vec2.from(xf.R.col1); 359 bzVec2 axis = xf.R.col1;
305 360
306 gl.drawSolidCircle(center, radius, axis, color); 361 drawSolidCircle(center, radius, axis, color);
307 362
308 if (core) { 363 if (core) {
309 gl.Color3f(coreColor.r, coreColor.g, coreColor.b); 364 glColor3f(coreColor.r, coreColor.g, coreColor.b);
310 gl.drawCircle(center, radius - k_toiSlop); 365 drawCircle(center, radius - k_toiSlop);
311 } 366 }
312 break; 367 break;
313 case bzShapeType.POLYGON: 368 case bzShapeType.POLYGON:
314 { 369 {
315 bzPolygon poly = cast(bzPolygon)shape; 370 bzPolygon poly = cast(bzPolygon)shape;
316 bzVec2[] vertices = poly.worldVertices; 371 bzVec2[] vertices = poly.worldVertices;
317 vec2[] verts; 372 drawSolidPolygon(vertices, color);
318 verts.length = vertices.length;
319 foreach (int i, v; vertices) {
320 verts[i] = vec2.from(v);
321 }
322
323 gl.drawSolidPolygon(verts, color);
324 373
325 if (core) { 374 if (core) {
326 bzVec2[] localCoreVertices = poly.coreVertices; 375 bzVec2[] localCoreVertices = poly.coreVertices;
327 verts.length = localCoreVertices.length; 376 vertices.length = localCoreVertices.length;
328 for (int i = 0; i < localCoreVertices.length; ++i) { 377 for (int i = 0; i < localCoreVertices.length; ++i) {
329 verts[i] = vec2.from(bzMul(xf, localCoreVertices[i])); 378 vertices[i] = bzMul(xf, localCoreVertices[i]);
330 } 379 }
331 gl.drawPolygon(verts, coreColor); 380 drawPolygon(vertices, coreColor);
332 } 381 }
333 } 382 }
334 break; 383 break;
335 384
336 case bzShapeType.EDGE: 385 case bzShapeType.EDGE:
337 { 386 {
338 bzEdge edge = cast(bzEdge)shape; 387 bzEdge edge = cast(bzEdge)shape;
339 388
340 vec2 p1 = vec2.from(bzMul(xf, edge.vertex1)); 389 bzVec2 p1 = bzMul(xf, edge.vertex1);
341 vec2 p2 = vec2.from(bzMul(xf, edge.vertex2)); 390 bzVec2 p2 = bzMul(xf, edge.vertex2);
342 gl.drawSegment(p1, p2, color); 391 drawSegment(p1, p2, color);
343 392
344 if (core) { 393 if (core) {
345 p1 = vec2.from(bzMul(xf, edge.coreVertex1)); 394 p1 = bzMul(xf, edge.coreVertex1);
346 p2 = vec2.from(bzMul(xf, edge.coreVertex2)); 395 p2 = bzMul(xf, edge.coreVertex2);
347 gl.drawSegment(p1, p2, coreColor); 396 drawSegment(p1, p2, coreColor);
348 } 397 }
349 } 398 }
350 break; 399 break;
351 } 400 }
352 } 401 }
353 402
354 void draw(vec2i screenSize, GL gl) 403 void draw() {
355 { 404
356 if(ship2) { 405 if(ship2) {
357 vec2 point1 = vec2.from(ship1.rBody.position); 406 bzVec2 point1 = ship1.rBody.position;
358 vec2 point2 = vec2.from(ship2.rBody.position); 407 bzVec2 point2 = ship2.rBody.position;
359 vec2 range = point1 - point2; 408 bzVec2 range = point1 - point2;
360 zoom = bzClamp(1000/range.length, 2, 60); 409 zoom = bzClamp(1000/range.length, 2, 60);
361 viewCenter = point1 - (range * 0.5f); 410 viewCenter = point1 - (range * 0.5f);
362 } else { 411 } else {
363 viewCenter = vec2.from(ship1.rBody.position); 412 viewCenter = ship1.rBody.position;
364 zoom = 10; 413 zoom = 10;
365 } 414 }
366 415
367 this.screenSize = screenSize; 416 glLoadIdentity();
368 417 glMatrixMode(GL_PROJECTION);
369 gl.LoadIdentity(); 418 glLoadIdentity();
370 gl.MatrixMode(GL_PROJECTION);
371 gl.LoadIdentity();
372 419
373 float left = -screenSize.x / zoom; 420 float left = -screenSize.x / zoom;
374 float right = screenSize.x / zoom; 421 float right = screenSize.x / zoom;
375 float bottom = -screenSize.y / zoom; 422 float bottom = -screenSize.y / zoom;
376 float top = screenSize.y / zoom; 423 float top = screenSize.y / zoom;
377 424
378 gl.gluOrtho2D(left, right, bottom, top); 425 gluOrtho2D(left, right, bottom, top);
379 gl.Translatef(-viewCenter.x, -viewCenter.y, 0); 426 glTranslatef(-viewCenter.x, -viewCenter.y, 0);
380 gl.MatrixMode(GL_MODELVIEW); 427 glMatrixMode(GL_MODELVIEW);
381 gl.Disable(GL_DEPTH_TEST); 428 glDisable(GL_DEPTH_TEST);
382 gl.LoadIdentity(); 429 glLoadIdentity();
383 gl.Clear(GL_COLOR_BUFFER_BIT); 430 glClear(GL_COLOR_BUFFER_BIT);
384 431
385 // Draw dynamic bodies 432 // Draw dynamic bodies
386 if (settings.drawShapes) { 433 if (settings.drawShapes) {
387 for (bzBody b = world.bodyList; b; b = b.next) { 434 for (bzBody b = world.bodyList; b; b = b.next) {
388 for (bzShape shape = b.shapeList; shape; shape = shape.next) { 435 for (bzShape shape = b.shapeList; shape; shape = shape.next) {
389 bzShape s = shape; 436 bzShape s = shape;
390 bzXForm xf = b.xf; 437 bzXForm xf = b.xf;
391 if (b.isStatic) { 438 if (b.isStatic) {
392 gl.drawShape(s, xf, Color(0.5f, 0.9f, 0.5f), settings.drawCoreShapes); 439 drawShape(s, xf, Color(0.5f, 0.9f, 0.5f), settings.drawCoreShapes);
393 }else if (b.isSleeping) { 440 }else if (b.isSleeping) {
394 gl.drawShape(s, xf, Color(0.5f, 0.5f, 0.9f), settings.drawCoreShapes); 441 drawShape(s, xf, Color(0.5f, 0.5f, 0.9f), settings.drawCoreShapes);
395 }else { 442 }else {
396 gl.drawShape(s, xf, Color(0.9f, 0.9f, 0.9f), settings.drawCoreShapes); 443 drawShape(s, xf, Color(0.9f, 0.9f, 0.9f), settings.drawCoreShapes);
397 } 444 }
398 445
399 gl.LoadIdentity(); 446 glLoadIdentity();
400 gl.Flush(); 447 glFlush();
401 } 448 }
402 } 449 }
403 } 450 }
404 451
405 // Draw joints 452 // Draw joints
416 auto prismatic = cast(bzPrismaticJoint)joint; 463 auto prismatic = cast(bzPrismaticJoint)joint;
417 auto line = cast(bzLineJoint)joint; 464 auto line = cast(bzLineJoint)joint;
418 if (distance) { 465 if (distance) {
419 color = Color(.5, .5, 0); 466 color = Color(.5, .5, 0);
420 // Endpoints 467 // Endpoints
421 vec2 a = vec2.from(distance.anchor1); 468 bzVec2 a = bzVec2.from(distance.anchor1);
422 vec2 b = vec2.from(distance.anchor2); 469 bzVec2 b = bzVec2.from(distance.anchor2);
423 // Circles 470 // Circles
424 gl.drawCircle(a, HINGE_RADIUS); 471 gl.drawCircle(a, HINGE_RADIUS);
425 gl.drawCircle(b, HINGE_RADIUS); 472 gl.drawCircle(b, HINGE_RADIUS);
426 // Connecting line 473 // Connecting line
427 gl.connectCircles(a, HINGE_RADIUS, b, HINGE_RADIUS); 474 gl.connectCircles(a, HINGE_RADIUS, b, HINGE_RADIUS);
428 }else if (pulley) { 475 }else if (pulley) {
429 auto a = vec2.from(pulley.anchor1); 476 auto a = bzVec2.from(pulley.anchor1);
430 auto b = vec2.from(pulley.groundAnchor1); 477 auto b = bzVec2.from(pulley.groundAnchor1);
431 auto c = vec2.from(pulley.groundAnchor2); 478 auto c = bzVec2.from(pulley.groundAnchor2);
432 auto d = vec2.from(pulley.anchor2); 479 auto d = bzVec2.from(pulley.anchor2);
433 gl.drawCircle(a, HINGE_RADIUS); 480 gl.drawCircle(a, HINGE_RADIUS);
434 gl.drawCircle(b, HINGE_RADIUS); 481 gl.drawCircle(b, HINGE_RADIUS);
435 gl.connectCircles(a, HINGE_RADIUS, b, HINGE_RADIUS); 482 gl.connectCircles(a, HINGE_RADIUS, b, HINGE_RADIUS);
436 gl.drawSegment(b, c, color); 483 gl.drawSegment(b, c, color);
437 gl.drawCircle(c, HINGE_RADIUS); 484 gl.drawCircle(c, HINGE_RADIUS);
438 gl.drawCircle(d, HINGE_RADIUS); 485 gl.drawCircle(d, HINGE_RADIUS);
439 gl.connectCircles(c, HINGE_RADIUS, d, HINGE_RADIUS); 486 gl.connectCircles(c, HINGE_RADIUS, d, HINGE_RADIUS);
440 }else if (revolute) { 487 }else if (revolute) {
441 auto a = vec2.from(revolute.rBody1.position); 488 auto a = bzVec2.from(revolute.rBody1.position);
442 auto b = vec2.from(revolute.anchor1); 489 auto b = bzVec2.from(revolute.anchor1);
443 auto c = vec2.from(revolute.rBody2.position); 490 auto c = bzVec2.from(revolute.rBody2.position);
444 gl.drawCircle(a, HINGE_RADIUS); 491 gl.drawCircle(a, HINGE_RADIUS);
445 gl.drawCircle(b, HINGE_RADIUS); 492 gl.drawCircle(b, HINGE_RADIUS);
446 gl.connectCircles(a, HINGE_RADIUS, b, HINGE_RADIUS); 493 gl.connectCircles(a, HINGE_RADIUS, b, HINGE_RADIUS);
447 gl.drawCircle(c, HINGE_RADIUS); 494 gl.drawCircle(c, HINGE_RADIUS);
448 gl.connectCircles(b, HINGE_RADIUS, c, HINGE_RADIUS); 495 gl.connectCircles(b, HINGE_RADIUS, c, HINGE_RADIUS);
449 }else if (prismatic) { 496 }else if (prismatic) {
450 auto a = vec2.from(prismatic.rBody1.position); 497 auto a = bzVec2.from(prismatic.rBody1.position);
451 auto b = vec2.from(prismatic.anchor1); 498 auto b = bzVec2.from(prismatic.anchor1);
452 auto c = vec2.from(prismatic.rBody2.position); 499 auto c = bzVec2.from(prismatic.rBody2.position);
453 gl.drawCircle(a, HINGE_RADIUS); 500 gl.drawCircle(a, HINGE_RADIUS);
454 gl.drawCircle(b, HINGE_RADIUS); 501 gl.drawCircle(b, HINGE_RADIUS);
455 gl.connectCircles(a, HINGE_RADIUS, b, HINGE_RADIUS); 502 gl.connectCircles(a, HINGE_RADIUS, b, HINGE_RADIUS);
456 gl.drawCircle(c, HINGE_RADIUS); 503 gl.drawCircle(c, HINGE_RADIUS);
457 gl.connectCircles(b, HINGE_RADIUS, c, HINGE_RADIUS); 504 gl.connectCircles(b, HINGE_RADIUS, c, HINGE_RADIUS);
458 }else if (line) { 505 }else if (line) {
459 auto a = vec2.from(line.rBody1.position); 506 auto a = bzVec2.from(line.rBody1.position);
460 auto b = vec2.from(line.anchor1); 507 auto b = bzVec2.from(line.anchor1);
461 auto c = vec2.from(line.rBody2.position); 508 auto c = bzVec2.from(line.rBody2.position);
462 gl.drawCircle(a, HINGE_RADIUS); 509 gl.drawCircle(a, HINGE_RADIUS);
463 gl.drawCircle(b, HINGE_RADIUS); 510 gl.drawCircle(b, HINGE_RADIUS);
464 gl.connectCircles(a, HINGE_RADIUS, b, HINGE_RADIUS); 511 gl.connectCircles(a, HINGE_RADIUS, b, HINGE_RADIUS);
465 gl.drawCircle(c, HINGE_RADIUS); 512 gl.drawCircle(c, HINGE_RADIUS);
466 gl.connectCircles(b, HINGE_RADIUS, c, HINGE_RADIUS); 513 gl.connectCircles(b, HINGE_RADIUS, c, HINGE_RADIUS);
478 if (spring1) { 525 if (spring1) {
479 auto bungee1 = cast(bzBungee1)spring1; 526 auto bungee1 = cast(bzBungee1)spring1;
480 if (bungee1) { 527 if (bungee1) {
481 gl.Color3f(.5, .5, 0); 528 gl.Color3f(.5, .5, 0);
482 // Endpoints 529 // Endpoints
483 vec2 a = vec2.from(bungee1.rBody.position); 530 bzVec2 a = bzVec2.from(bungee1.rBody.position);
484 vec2 b = vec2.from(bungee1.anchor); 531 bzVec2 b = bzVec2.from(bungee1.anchor);
485 // Circles 532 // Circles
486 gl.drawCircle(a, HINGE_RADIUS); 533 gl.drawCircle(a, HINGE_RADIUS);
487 gl.drawCircle(b, HINGE_RADIUS); 534 gl.drawCircle(b, HINGE_RADIUS);
488 // Connecting line 535 // Connecting line
489 gl.connectCircles(a, HINGE_RADIUS, b, HINGE_RADIUS); 536 gl.connectCircles(a, HINGE_RADIUS, b, HINGE_RADIUS);
490 }else { 537 }else {
491 uint zigs = 10; 538 uint zigs = 10;
492 auto anchor1 = vec2.from(spring1.anchor); 539 auto anchor1 = bzVec2.from(spring1.anchor);
493 auto anchor2 = vec2.from(spring1.rBody.position); 540 auto anchor2 = bzVec2.from(spring1.rBody.position);
494 gl.drawSpring(anchor1, anchor2, zigs); 541 gl.drawSpring(anchor1, anchor2, zigs);
495 } 542 }
496 } 543 }
497 544
498 if (spring2) { 545 if (spring2) {
499 auto bungee2 = cast(bzBungee2)spring2; 546 auto bungee2 = cast(bzBungee2)spring2;
500 if (bungee2) { 547 if (bungee2) {
501 gl.Color3f(.5, .5, 0); 548 gl.Color3f(.5, .5, 0);
502 // Endpoints 549 // Endpoints
503 vec2 a = vec2.from(bungee2.rBody.position); 550 bzVec2 a = bzVec2.from(bungee2.rBody.position);
504 vec2 b = vec2.from(bungee2.otherBody.position); 551 bzVec2 b = bzVec2.from(bungee2.otherBody.position);
505 // Circles 552 // Circles
506 gl.drawCircle(a, HINGE_RADIUS); 553 gl.drawCircle(a, HINGE_RADIUS);
507 gl.drawCircle(b, HINGE_RADIUS); 554 gl.drawCircle(b, HINGE_RADIUS);
508 // Connecting line 555 // Connecting line
509 gl.connectCircles(a, HINGE_RADIUS, b, HINGE_RADIUS); 556 gl.connectCircles(a, HINGE_RADIUS, b, HINGE_RADIUS);
510 }else { 557 }else {
511 uint zigs = 10; 558 uint zigs = 10;
512 auto anchor1 = vec2.from(spring2.otherBody.position); 559 auto anchor1 = bzVec2.from(spring2.otherBody.position);
513 auto anchor2 = vec2.from(spring2.rBody.position); 560 auto anchor2 = bzVec2.from(spring2.rBody.position);
514 gl.drawSpring(anchor1, anchor2, zigs); 561 gl.drawSpring(anchor1, anchor2, zigs);
515 } 562 }
516 } 563 }
517 564
518 if(buoyancy) { 565 if(buoyancy) {
519 float plane = buoyancy.planeOffset; 566 float plane = buoyancy.planeOffset;
520 vec2 p1 = vec2(-50, plane); 567 bzVec2 p1 = bzVec2(-50, plane);
521 vec2 p2 = vec2(50, plane); 568 bzVec2 p2 = bzVec2(50, plane);
522 gl.drawSegment(p1, p2, color); 569 gl.drawSegment(p1, p2, color);
523 } 570 }
524 } 571 }
525 } 572 }
526 } 573 }
529 // Draw the world bounds 576 // Draw the world bounds
530 bzBroadPhase bp = world.broadPhase; 577 bzBroadPhase bp = world.broadPhase;
531 bzVec2 worldLower = bp.m_worldAABB.lowerBound; 578 bzVec2 worldLower = bp.m_worldAABB.lowerBound;
532 bzVec2 worldUpper = bp.m_worldAABB.upperBound; 579 bzVec2 worldUpper = bp.m_worldAABB.upperBound;
533 Color color = Color(0.3f, 0.9f, 0.9f); 580 Color color = Color(0.3f, 0.9f, 0.9f);
534 vec2 vs[4]; 581 bzVec2 vs[4];
535 vs[0] = vec2(worldLower.x, worldLower.y); 582 vs[0] = bzVec2(worldLower.x, worldLower.y);
536 vs[1] = vec2(worldUpper.x, worldLower.y); 583 vs[1] = bzVec2(worldUpper.x, worldLower.y);
537 vs[2] = vec2(worldUpper.x, worldUpper.y); 584 vs[2] = bzVec2(worldUpper.x, worldUpper.y);
538 vs[3] = vec2(worldLower.x, worldUpper.y); 585 vs[3] = bzVec2(worldLower.x, worldUpper.y);
539 drawPolygon(gl, vs, color); 586 drawPolygon(vs, color);
540 587
541 // Draw axis aligned bounding boxes (bzAABB) 588 // Draw axis aligned bounding boxes (bzAABB)
542 if (settings.drawAABBs) { 589 if (settings.drawAABBs) {
543 bzVec2 invQ; 590 bzVec2 invQ;
544 invQ.set(1.0f / bp.m_quantizationFactor.x, 1.0f / bp.m_quantizationFactor.y); 591 invQ.set(1.0f / bp.m_quantizationFactor.x, 1.0f / bp.m_quantizationFactor.y);
554 b.lowerBound.x = worldLower.x + invQ.x * bp.m_bounds[0][p.lowerBounds[0]].value; 601 b.lowerBound.x = worldLower.x + invQ.x * bp.m_bounds[0][p.lowerBounds[0]].value;
555 b.lowerBound.y = worldLower.y + invQ.y * bp.m_bounds[1][p.lowerBounds[1]].value; 602 b.lowerBound.y = worldLower.y + invQ.y * bp.m_bounds[1][p.lowerBounds[1]].value;
556 b.upperBound.x = worldLower.x + invQ.x * bp.m_bounds[0][p.upperBounds[0]].value; 603 b.upperBound.x = worldLower.x + invQ.x * bp.m_bounds[0][p.upperBounds[0]].value;
557 b.upperBound.y = worldLower.y + invQ.y * bp.m_bounds[1][p.upperBounds[1]].value; 604 b.upperBound.y = worldLower.y + invQ.y * bp.m_bounds[1][p.upperBounds[1]].value;
558 605
559 vs[0] = vec2(b.lowerBound.x, b.lowerBound.y); 606 vs[0] = bzVec2(b.lowerBound.x, b.lowerBound.y);
560 vs[1] = vec2(b.upperBound.x, b.lowerBound.y); 607 vs[1] = bzVec2(b.upperBound.x, b.lowerBound.y);
561 vs[2] = vec2(b.upperBound.x, b.upperBound.y); 608 vs[2] = bzVec2(b.upperBound.x, b.upperBound.y);
562 vs[3] = vec2(b.lowerBound.x, b.upperBound.y); 609 vs[3] = bzVec2(b.lowerBound.x, b.upperBound.y);
563 610
564 drawPolygon(gl, vs, color); 611 drawPolygon(vs, color);
565 } 612 }
566 } 613 }
567 } 614 }
568 } 615 }