/* Copyright (c) 2015 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "collision/CollisionBox3D.h" #include "collision/CollisionCapsule.h" #include "collision/CollisionShared.h" #include "collision/CollisionSphere.h" #include "collision/CollisionStaticTrimesh.h" #include "gamemath/Box6f.h" #include "gamemath/Matrix4x4f.h" #include "gamemath/MouseCoordinateTransforms.h" #include "gamemath/Quaternionf.h" #include "gamemath/VectorConversions.h" #include "renderer/VertexTypes.h" #include "shadercollection/ShaderCollection.h" #include "shell/Shell.h" #include "shell/ShellKeyCodes.h" #include "testharness/SingleFrameScreen3D.h" #include "testharness/SharedEvents.h" #include "testharness/TestHarness_globals.h" #include "utilities/printfFormats.h" #include #include #include #include #define stemobject_implementation SingleFrameScreen3D stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_entry(activate); stemobject_vtable_entry(deactivate); stemobject_vtable_end(); #define SPHERE_SUBDIVISIONS 16 #define SPHERE_FACE_VERTEX_COUNT ((SPHERE_SUBDIVISIONS + 2) * (SPHERE_SUBDIVISIONS + 2)) #define SPHERE_VERTEX_COUNT (SPHERE_FACE_VERTEX_COUNT * 6) #define SPHERE_FACE_INDEX_COUNT (6 * (SPHERE_SUBDIVISIONS + 1) * (SPHERE_SUBDIVISIONS + 1)) #define SPHERE_INDEX_COUNT (SPHERE_FACE_INDEX_COUNT * 6) static struct sphereVertex { float position[3]; float normal[3]; } sphereVertexTemplate[SPHERE_VERTEX_COUNT]; static uint32_t sphereIndexTemplate[SPHERE_INDEX_COUNT]; // Improvements: // - Show keyboard and mouse controls // - Show normals static void __attribute__((constructor)) initSphereTemplate() { unsigned int columnIndex, rowIndex, faceIndex, indexIndex; struct sphereVertex vertex; Vector3f vector; Matrix4x4f faceMatrices[5] = { MATRIX4x4f(0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f), // Left MATRIX4x4f(-1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f), // Back MATRIX4x4f(0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f), // Right MATRIX4x4f(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f), // Bottom MATRIX4x4f(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f) // Top }; for (rowIndex = 0; rowIndex < SPHERE_SUBDIVISIONS + 2; rowIndex++) { for (columnIndex = 0; columnIndex < SPHERE_SUBDIVISIONS + 2; columnIndex++) { vector.x = columnIndex * 2.0f / (SPHERE_SUBDIVISIONS + 1) - 1.0f; vector.y = rowIndex * 2.0f / (SPHERE_SUBDIVISIONS + 1) - 1.0f; vector.z = 1.0f; Vector3f_normalize(&vector); vertex.position[0] = vector.x; vertex.position[1] = vector.y; vertex.position[2] = vector.z; vertex.normal[0] = vector.x; vertex.normal[1] = vector.y; vertex.normal[2] = vector.z; sphereVertexTemplate[rowIndex * (SPHERE_SUBDIVISIONS + 2) + columnIndex] = vertex; } } for (faceIndex = 1; faceIndex < 6; faceIndex++) { for (rowIndex = 0; rowIndex < SPHERE_SUBDIVISIONS + 2; rowIndex++) { for (columnIndex = 0; columnIndex < SPHERE_SUBDIVISIONS + 2; columnIndex++) { vertex = sphereVertexTemplate[rowIndex * (SPHERE_SUBDIVISIONS + 2) + columnIndex]; vector = Matrix4x4f_multiplyVector3f(faceMatrices[faceIndex - 1], VECTOR3f(vertex.position[0], vertex.position[1], vertex.position[2])); vertex.position[0] = vector.x; vertex.position[1] = vector.y; vertex.position[2] = vector.z; vertex.normal[0] = vector.x; vertex.normal[1] = vector.y; vertex.normal[2] = vector.z; sphereVertexTemplate[faceIndex * SPHERE_FACE_VERTEX_COUNT + rowIndex * (SPHERE_SUBDIVISIONS + 2) + columnIndex] = vertex; } } } for (faceIndex = 0; faceIndex < 6; faceIndex++) { for (rowIndex = 0; rowIndex < SPHERE_SUBDIVISIONS + 1; rowIndex++) { for (columnIndex = 0; columnIndex < SPHERE_SUBDIVISIONS + 1; columnIndex++) { indexIndex = faceIndex * SPHERE_FACE_INDEX_COUNT + rowIndex * 6 * (SPHERE_SUBDIVISIONS + 1) + columnIndex * 6; sphereIndexTemplate[indexIndex + 0] = faceIndex * SPHERE_FACE_VERTEX_COUNT + rowIndex * (SPHERE_SUBDIVISIONS + 2) + columnIndex; sphereIndexTemplate[indexIndex + 1] = faceIndex * SPHERE_FACE_VERTEX_COUNT + rowIndex * (SPHERE_SUBDIVISIONS + 2) + columnIndex + 1; sphereIndexTemplate[indexIndex + 2] = faceIndex * SPHERE_FACE_VERTEX_COUNT + (rowIndex + 1) * (SPHERE_SUBDIVISIONS + 2) + columnIndex + 1; sphereIndexTemplate[indexIndex + 3] = faceIndex * SPHERE_FACE_VERTEX_COUNT + (rowIndex + 1) * (SPHERE_SUBDIVISIONS + 2) + columnIndex + 1; sphereIndexTemplate[indexIndex + 4] = faceIndex * SPHERE_FACE_VERTEX_COUNT + (rowIndex + 1) * (SPHERE_SUBDIVISIONS + 2) + columnIndex; sphereIndexTemplate[indexIndex + 5] = faceIndex * SPHERE_FACE_VERTEX_COUNT + rowIndex * (SPHERE_SUBDIVISIONS + 2) + columnIndex; } } } } SingleFrameScreen3D * SingleFrameScreen3D_create(Renderer * renderer) { stemobject_create_implementation(init, renderer) } static void writeTextVertices(Renderable * renderable, VertexIO * vertexIO, void * context) { } bool SingleFrameScreen3D_init(SingleFrameScreen3D * self, Renderer * renderer) { call_super(init, self); self->intersectionManager = IntersectionManager_createWithStandardHandlers(); self->renderer = renderer; self->renderLayer3D = RenderLayer_create(RENDER_LAYER_SORT_NONE, NULL, NULL); self->renderLayer2D = RenderLayer_create(RENDER_LAYER_SORT_NONE, NULL, NULL); self->shaderConfiguration3D = ShaderConfiguration3DLit_create(ShaderCollection_get3DLitShader()); call_virtual(setTexture, self->shaderConfiguration3D, 0, g_blankTexture, false); call_virtual(setMaterial, self->shaderConfiguration3D, COLOR4f(1.0f, 1.0f, 1.0f, 1.0f), 0.875f, 32.0f, 0.0f); call_virtual(setLights, self->shaderConfiguration3D, VECTOR3f(0.0f, 8.0f, 8.0f), COLOR3f(1.0f, 1.0f, 0.95f), VECTOR3f(-1.0f, -2.0f, -8.0f), COLOR3f(0.8f, 0.8f, 0.8f), COLOR3f(0.1f, 0.1f, 0.105f)); call_virtual(setProjectionMatrix, self->shaderConfiguration3D, Matrix4x4f_perspective(MATRIX4x4f_IDENTITY, 70.0f, g_viewRatio, 0.5f, 800.0f)); self->shaderConfiguration2D = ShaderConfiguration2DTexture_create(ShaderCollection_get2DTextureShader()); call_virtual(setTexture, self->shaderConfiguration2D, 0, g_fontTexture, false); call_virtual(setProjectionMatrix, self->shaderConfiguration2D, Matrix4x4f_ortho(MATRIX4x4f_IDENTITY, -12.0f * g_viewRatio, 12.0f * g_viewRatio, -12.0f, 12.0f, -1.0f, 1.0f)); self->renderPipelineConfiguration2D = default2DRenderPipelineConfiguration(RENDER_BLEND_ALPHA); self->renderPipelineConfiguration3D = default3DRenderPipelineConfiguration(RENDER_BLEND_ALPHA); self->meshRenderable = Renderable_createWithVertexBuffer(PRIMITIVE_TRIANGLES, &self->renderPipelineConfiguration3D, self->shaderConfiguration3D, VertexBuffer_create(self->shaderConfiguration3D->shader->vertexFormat, NULL, 0, NULL, 0, BUFFER_USAGE_STREAM), true, NULL, NULL); self->meshDirty = true; self->wireframe = false; self->draggingRay = false; self->textRenderable = Renderable_createWithCallback(PRIMITIVE_TRIANGLES, &self->renderPipelineConfiguration2D, self->shaderConfiguration2D, writeTextVertices, NULL, self); RenderLayer_addRenderable(self->renderLayer3D, self->meshRenderable, 0, RECT4i_EMPTY); RenderLayer_addRenderable(self->renderLayer2D, self->textRenderable, 0, RECT4i_EMPTY); return true; } void SingleFrameScreen3D_dispose(SingleFrameScreen3D * self) { IntersectionManager_dispose(self->intersectionManager); Renderable_dispose(self->meshRenderable); Renderable_dispose(self->textRenderable); ShaderConfiguration3DLit_dispose(self->shaderConfiguration3D); ShaderConfiguration2DTexture_dispose(self->shaderConfiguration2D); RenderLayer_dispose(self->renderLayer3D); RenderLayer_dispose(self->renderLayer2D); call_super(dispose, self); } static void updateCollisions(SingleFrameScreen3D * self) { for (unsigned int collisionIndex = 0; collisionIndex < OBJECT_COUNT_3D; collisionIndex++) { if (!CollisionResolver_querySingle(self->resolver, self->objects[collisionIndex], &self->collisions[collisionIndex])) { self->collisions[collisionIndex].time = -1; } } self->meshDirty = true; self->rayIntersection1 = self->rayIntersection2 = FIXED_16_16_INF; CollisionRaycastResult raycastResults[2]; unsigned int raycastHitCount = CollisionResolver_listRaycastHits(self->resolver, self->rayStart, self->rayEnd, ~0, 2, raycastResults); if (raycastHitCount > 0) { self->rayIntersection1 = raycastResults[0].distance; if (raycastHitCount > 1) { self->rayIntersection2 = raycastResults[1].distance; } } } #define COLOR_BOX_LAST_POSITION COLOR4f(0.5f, 0.25f, 0.0f, 1.0f) #define COLOR_BOX_LAST_POSITION_HIGHLIGHT COLOR4f(0.75f, 0.625f, 0.125f, 1.0f) #define COLOR_BOX_POSITION COLOR4f(1.0f, 1.0f, 0.0f, 1.0f) #define COLOR_BOX_POSITION_HIGHLIGHT COLOR4f(1.0f, 1.0f, 0.875f, 1.0f) #define COLOR_BOX_POSITION_COLLIDING COLOR4f(1.0f, 0.0f, 0.0f, 1.0f) #define COLOR_BOX_POSITION_COLLIDING_HIGHLIGHT COLOR4f(1.0f, 0.75f, 0.75f, 1.0f) #define COLOR_SPHERE_LAST_POSITION COLOR4f(0.0f, 0.25f, 0.5f, 1.0f) #define COLOR_SPHERE_LAST_POSITION_HIGHLIGHT COLOR4f(0.125f, 0.625f, 0.75f, 1.0f) #define COLOR_SPHERE_POSITION COLOR4f(0.0f, 1.0f, 1.0f, 1.0f) #define COLOR_SPHERE_POSITION_HIGHLIGHT COLOR4f(0.875f, 1.0f, 1.0f, 1.0f) #define COLOR_SPHERE_POSITION_COLLIDING COLOR4f(1.0f, 0.0f, 0.0f, 1.0f) #define COLOR_SPHERE_POSITION_COLLIDING_HIGHLIGHT COLOR4f(1.0f, 0.75f, 0.75f, 1.0f) #define COLOR_CAPSULE_LAST_POSITION COLOR4f(0.25f, 0.0f, 0.5f, 1.0f) #define COLOR_CAPSULE_LAST_POSITION_HIGHLIGHT COLOR4f(0.5f, 0.25f, 0.75f, 1.0f) #define COLOR_CAPSULE_POSITION COLOR4f(0.5f, 0.0f, 1.0f, 1.0f) #define COLOR_CAPSULE_POSITION_HIGHLIGHT COLOR4f(0.75f, 0.5f, 1.0f, 1.0f) #define COLOR_CAPSULE_POSITION_COLLIDING COLOR4f(1.0f, 0.0f, 0.0f, 1.0f) #define COLOR_CAPSULE_POSITION_COLLIDING_HIGHLIGHT COLOR4f(1.0f, 0.75f, 0.75f, 1.0f) #define COLOR_TRIMESH COLOR4f(0.0f, 1.0f, 0.0f, 1.0f) #define SWEEP_ALPHA 0.5f static void setVertexColor(struct vertex_p3f_t2f_n3f_c4f * vertex, Color4f color) { vertex->color[0] = color.red; vertex->color[1] = color.green; vertex->color[2] = color.blue; vertex->color[3] = color.alpha; } static void writeBoxVertices(Vector3f position, Vector3f size, Color4f color, VertexIO * vertexIO) { struct vertex_p3f_t2f_n3f_c4f vertices[24], vertex = {.color = {color.red, color.green, color.blue, color.alpha}, .texCoords = {0.0f, 0.0f}}; uint32_t indexes[36]; vertex.normal[0] = -1.0f; vertex.normal[1] = 0.0f; vertex.normal[2] = 0.0f; vertex.position[0] = position.x; vertex.position[1] = position.y; vertex.position[2] = position.z; vertices[0] = vertex; vertex.position[2] = position.z + size.z; vertices[1] = vertex; vertex.position[1] = position.y + size.y; vertices[2] = vertex; vertex.position[2] = position.z; vertices[3] = vertex; vertex.normal[0] = 1.0f; vertex.normal[1] = 0.0f; vertex.normal[2] = 0.0f; vertex.position[0] = position.x + size.x; vertex.position[1] = position.y; vertex.position[2] = position.z + size.z; vertices[4] = vertex; vertex.position[2] = position.z; vertices[5] = vertex; vertex.position[1] = position.y + size.y; vertices[6] = vertex; vertex.position[2] = position.z + size.z; vertices[7] = vertex; vertex.normal[0] = 0.0f; vertex.normal[1] = -1.0f; vertex.normal[2] = 0.0f; vertex.position[0] = position.x; vertex.position[1] = position.y; vertex.position[2] = position.z; vertices[8] = vertex; vertex.position[0] = position.x + size.x; vertices[9] = vertex; vertex.position[2] = position.z + size.z; vertices[10] = vertex; vertex.position[0] = position.x; vertices[11] = vertex; vertex.normal[0] = 0.0f; vertex.normal[1] = 1.0f; vertex.normal[2] = 0.0f; vertex.position[0] = position.x; vertex.position[1] = position.y + size.y; vertex.position[2] = position.z + size.z; vertices[12] = vertex; vertex.position[0] = position.x + size.x; vertices[13] = vertex; vertex.position[2] = position.z; vertices[14] = vertex; vertex.position[0] = position.x; vertices[15] = vertex; vertex.normal[0] = 0.0f; vertex.normal[1] = 0.0f; vertex.normal[2] = -1.0f; vertex.position[0] = position.x + size.x; vertex.position[1] = position.y; vertex.position[2] = position.z; vertices[16] = vertex; vertex.position[0] = position.x; vertices[17] = vertex; vertex.position[1] = position.y + size.y; vertices[18] = vertex; vertex.position[0] = position.x + size.x; vertices[19] = vertex; vertex.normal[0] = 0.0f; vertex.normal[1] = 0.0f; vertex.normal[2] = 1.0f; vertex.position[0] = position.x; vertex.position[1] = position.y; vertex.position[2] = position.z + size.z; vertices[20] = vertex; vertex.position[0] = position.x + size.x; vertices[21] = vertex; vertex.position[1] = position.y + size.y; vertices[22] = vertex; vertex.position[0] = position.x; vertices[23] = vertex; for (unsigned int faceIndex = 0; faceIndex < 6; faceIndex++) { indexes[faceIndex * 6 + 0] = faceIndex * 4 + 0; indexes[faceIndex * 6 + 1] = faceIndex * 4 + 1; indexes[faceIndex * 6 + 2] = faceIndex * 4 + 2; indexes[faceIndex * 6 + 3] = faceIndex * 4 + 2; indexes[faceIndex * 6 + 4] = faceIndex * 4 + 3; indexes[faceIndex * 6 + 5] = faceIndex * 4 + 0; } VertexIO_writeIndexedVertices(vertexIO, sizeof(vertices) / sizeof(vertices[0]), vertices, sizeof(indexes) / sizeof(indexes[0]), indexes); } static void writeSphereVertices(Vector3f position, float radius, Color4f color, VertexIO * vertexIO) { struct vertex_p3f_t2f_n3f_c4f vertices[SPHERE_VERTEX_COUNT], vertex = {.color = {color.red, color.green, color.blue, color.alpha}, .texCoords = {0.0f, 0.0f}}; Vector3f vertexPosition; for (unsigned int vertexIndex = 0; vertexIndex < SPHERE_VERTEX_COUNT; vertexIndex++) { vertexPosition.x = sphereVertexTemplate[vertexIndex].position[0]; vertexPosition.y = sphereVertexTemplate[vertexIndex].position[1]; vertexPosition.z = sphereVertexTemplate[vertexIndex].position[2]; vertexPosition = Vector3f_multiplyScalar(vertexPosition, radius); vertex.position[0] = vertexPosition.x + position.x; vertex.position[1] = vertexPosition.y + position.y; vertex.position[2] = vertexPosition.z + position.z; vertex.normal[0] = sphereVertexTemplate[vertexIndex].normal[0]; vertex.normal[1] = sphereVertexTemplate[vertexIndex].normal[1]; vertex.normal[2] = sphereVertexTemplate[vertexIndex].normal[2]; vertices[vertexIndex] = vertex; } VertexIO_writeIndexedVertices(vertexIO, sizeof(vertices) / sizeof(vertices[0]), vertices, sizeof(sphereIndexTemplate) / sizeof(sphereIndexTemplate[0]), sphereIndexTemplate); } static void writeCapsuleVertices(Vector3f position, float radius, float cylinderHeight, Color4f color, VertexIO * vertexIO) { struct vertex_p3f_t2f_n3f_c4f vertices[SPHERE_VERTEX_COUNT], vertex = {.color = {color.red, color.green, color.blue, color.alpha}, .texCoords = {0.0f, 0.0f}}; uint32_t indexes[SPHERE_INDEX_COUNT]; Vector3f vertexPosition; for (unsigned int vertexIndex = 0; vertexIndex < SPHERE_VERTEX_COUNT; vertexIndex++) { vertexPosition.x = sphereVertexTemplate[vertexIndex].position[0]; vertexPosition.y = sphereVertexTemplate[vertexIndex].position[1]; vertexPosition.z = sphereVertexTemplate[vertexIndex].position[2]; vertexPosition = Vector3f_multiplyScalar(vertexPosition, radius); if (vertexPosition.y > 0.0f) { vertexPosition.y += cylinderHeight; } vertexPosition.y += radius; vertex.position[0] = vertexPosition.x + position.x; vertex.position[1] = vertexPosition.y + position.y; vertex.position[2] = vertexPosition.z + position.z; vertex.normal[0] = sphereVertexTemplate[vertexIndex].normal[0]; vertex.normal[1] = sphereVertexTemplate[vertexIndex].normal[1]; vertex.normal[2] = sphereVertexTemplate[vertexIndex].normal[2]; vertices[vertexIndex] = vertex; } for (unsigned int index = 0; index < SPHERE_INDEX_COUNT; index++) { indexes[index] = sphereIndexTemplate[index]; } VertexIO_writeIndexedVertices(vertexIO, sizeof(vertices) / sizeof(vertices[0]), vertices, sizeof(indexes) / sizeof(indexes[0]), indexes); } static void writeTrimeshVertices(CollisionStaticTrimesh * trimesh, Color4f color, VertexIO * vertexIO) { struct vertex_p3f_t2f_n3f_c4f vertices[trimesh->triangleCount * 3], vertex = {.color = {color.red, color.green, color.blue, color.alpha}, .texCoords = {0.0f, 0.0f}}; uint32_t indexes[trimesh->triangleCount * 3]; for (unsigned int triangleIndex = 0; triangleIndex < trimesh->triangleCount; triangleIndex++) { vertex.normal[0] = trimesh->triangles[triangleIndex].normal.x; vertex.normal[1] = trimesh->triangles[triangleIndex].normal.y; vertex.normal[2] = trimesh->triangles[triangleIndex].normal.z; for (unsigned int vertexIndex = 0; vertexIndex < 3; vertexIndex++) { vertex.position[0] = xtof(trimesh->vertices[trimesh->triangles[triangleIndex].vertexIndexes[vertexIndex]].position.x); vertex.position[1] = xtof(trimesh->vertices[trimesh->triangles[triangleIndex].vertexIndexes[vertexIndex]].position.y); vertex.position[2] = xtof(trimesh->vertices[trimesh->triangles[triangleIndex].vertexIndexes[vertexIndex]].position.z); vertices[triangleIndex * 3 + vertexIndex] = vertex; } } for (unsigned int triangleIndex = 0; triangleIndex < trimesh->triangleCount; triangleIndex++) { for (unsigned int vertexIndex = 0; vertexIndex < 3; vertexIndex++) { indexes[triangleIndex * 3 + vertexIndex] = triangleIndex * 3 + vertexIndex; } } VertexIO_writeIndexedVertices(vertexIO, sizeof(vertices) / sizeof(vertices[0]), vertices, sizeof(indexes) / sizeof(indexes[0]), indexes); } static void writeOpaqueCollisionObjectVertices(SingleFrameScreen3D * self, VertexIO * vertexIO) { for (size_t objectIndex = 0; objectIndex < OBJECT_COUNT_3D; objectIndex++) { CollisionObject * object = self->objects[objectIndex]; Color4f color; switch (call_virtual(getShapeType, object)) { case COLLISION_SHAPE_BOX_3D: { CollisionBox3D * box = (CollisionBox3D *) object; if (objectIndex == self->selectedObjectIndex) { color = COLOR_BOX_LAST_POSITION_HIGHLIGHT; } else { color = COLOR_BOX_LAST_POSITION; } writeBoxVertices(Vector3x_toVector3f(box->lastPosition), Vector3x_toVector3f(box->lastSize), color, vertexIO); if (self->collisions[objectIndex].time > -1) { Vector3x collidingPosition = Vector3x_interpolate(box->lastPosition, box->position, self->collisions[objectIndex].time); Vector3x collidingSize = Vector3x_interpolate(box->lastSize, box->size, self->collisions[objectIndex].time); if (objectIndex == self->selectedObjectIndex) { color = COLOR_BOX_POSITION_HIGHLIGHT; } else { color = COLOR_BOX_POSITION; } writeBoxVertices(Vector3x_toVector3f(collidingPosition), Vector3x_toVector3f(collidingSize), color, vertexIO); } if (objectIndex == self->selectedObjectIndex) { if (self->collisions[objectIndex].time > -1) { color = COLOR_BOX_POSITION_COLLIDING_HIGHLIGHT; } else { color = COLOR_BOX_POSITION_HIGHLIGHT; } } else if (self->collisions[objectIndex].time > -1) { color = COLOR_BOX_POSITION_COLLIDING; } else { color = COLOR_BOX_POSITION; } writeBoxVertices(Vector3x_toVector3f(box->position), Vector3x_toVector3f(box->size), color, vertexIO); break; } case COLLISION_SHAPE_SPHERE: { CollisionSphere * sphere = (CollisionSphere *) object; if (objectIndex == self->selectedObjectIndex) { color = COLOR_SPHERE_LAST_POSITION_HIGHLIGHT; } else { color = COLOR_SPHERE_LAST_POSITION; } writeSphereVertices(Vector3x_toVector3f(sphere->lastPosition), xtof(sphere->radius), color, vertexIO); if (self->collisions[objectIndex].time > -1) { Vector3x collidingPosition = Vector3x_interpolate(sphere->lastPosition, sphere->position, self->collisions[objectIndex].time); if (objectIndex == self->selectedObjectIndex) { color = COLOR_SPHERE_POSITION_HIGHLIGHT; } else { color = COLOR_SPHERE_POSITION; } writeSphereVertices(Vector3x_toVector3f(collidingPosition), xtof(sphere->radius), color, vertexIO); } if (objectIndex == self->selectedObjectIndex) { if (self->collisions[objectIndex].time > -1) { color = COLOR_SPHERE_POSITION_COLLIDING_HIGHLIGHT; } else { color = COLOR_SPHERE_POSITION_HIGHLIGHT; } } else if (self->collisions[objectIndex].time > -1) { color = COLOR_SPHERE_POSITION_COLLIDING; } else { color = COLOR_SPHERE_POSITION; } writeSphereVertices(Vector3x_toVector3f(sphere->position), xtof(sphere->radius), color, vertexIO); break; } case COLLISION_SHAPE_CAPSULE: { CollisionCapsule * capsule = (CollisionCapsule *) object; if (objectIndex == self->selectedObjectIndex) { color = COLOR_CAPSULE_LAST_POSITION_HIGHLIGHT; } else { color = COLOR_CAPSULE_LAST_POSITION; } writeCapsuleVertices(Vector3x_toVector3f(capsule->lastPosition), xtof(capsule->radius), xtof(capsule->cylinderHeight), color, vertexIO); if (self->collisions[objectIndex].time > -1) { Vector3x collidingPosition = Vector3x_interpolate(capsule->lastPosition, capsule->position, self->collisions[objectIndex].time); if (objectIndex == self->selectedObjectIndex) { color = COLOR_CAPSULE_POSITION_HIGHLIGHT; } else { color = COLOR_CAPSULE_POSITION; } writeCapsuleVertices(Vector3x_toVector3f(collidingPosition), xtof(capsule->radius), xtof(capsule->cylinderHeight), color, vertexIO); } if (objectIndex == self->selectedObjectIndex) { if (self->collisions[objectIndex].time > -1) { color = COLOR_CAPSULE_POSITION_COLLIDING_HIGHLIGHT; } else { color = COLOR_CAPSULE_POSITION_HIGHLIGHT; } } else if (self->collisions[objectIndex].time > -1) { color = COLOR_CAPSULE_POSITION_COLLIDING; } else { color = COLOR_CAPSULE_POSITION; } writeCapsuleVertices(Vector3x_toVector3f(capsule->position), xtof(capsule->radius), xtof(capsule->cylinderHeight), color, vertexIO); break; } case COLLISION_SHAPE_STATIC_TRIMESH: writeTrimeshVertices((CollisionStaticTrimesh *) object, COLOR_TRIMESH, vertexIO); break; } } } static void writeBoxSweepQuadVertices(Vector3f vertex0, Vector3f vertex1, Vector3f vertex2, Vector3f vertex3, Color4f colorStart, Color4f colorEnd, VertexIO * vertexIO) { struct vertex_p3f_t2f_n3f_c4f vertices[4], vertex = {.texCoords = {0.0f, 0.0f}}; Vector3f normal; setVertexColor(&vertex, colorStart); normal = Vector3f_normalized(Vector3f_cross(Vector3f_subtract(vertex1, vertex0), Vector3f_subtract(vertex3, vertex0))); vertex.normal[0] = normal.x; vertex.normal[1] = normal.y; vertex.normal[2] = normal.z; vertex.position[0] = vertex0.x; vertex.position[1] = vertex0.y; vertex.position[2] = vertex0.z; vertices[0] = vertex; vertex.position[0] = vertex1.x; vertex.position[1] = vertex1.y; vertex.position[2] = vertex1.z; vertices[1] = vertex; setVertexColor(&vertex, colorEnd); vertex.position[0] = vertex2.x; vertex.position[1] = vertex2.y; vertex.position[2] = vertex2.z; vertices[2] = vertex; vertex.position[0] = vertex3.x; vertex.position[1] = vertex3.y; vertex.position[2] = vertex3.z; vertices[3] = vertex; uint32_t indexes[6] = {0, 1, 2, 2, 3, 0}; VertexIO_writeIndexedVertices(vertexIO, sizeof(vertices) / sizeof(vertices[0]), vertices, sizeof(indexes) / sizeof(indexes[0]), indexes); } static void writeBoxSweepVertices(Vector3f lastPosition, Vector3f lastSize, Vector3f position, Vector3f size, Color4f colorStart, Color4f colorEnd, VertexIO * vertexIO) { Box6f lastBounds, bounds; if (lastPosition.x == position.x && lastPosition.y == position.y && lastPosition.z == position.z && lastSize.x == size.x && lastSize.y == size.y && lastSize.z == size.z) { return; } lastBounds = Box6f_fromPositionAndSize(lastPosition, lastSize); bounds = Box6f_fromPositionAndSize(position, size); if (bounds.xMin <= lastBounds.xMin) { if (bounds.yMin >= lastBounds.yMin) { writeBoxSweepQuadVertices(VECTOR3f(lastBounds.xMin, lastBounds.yMin, lastBounds.zMin), VECTOR3f(lastBounds.xMin, lastBounds.yMin, lastBounds.zMax), VECTOR3f(bounds.xMin, bounds.yMin, bounds.zMax), VECTOR3f(bounds.xMin, bounds.yMin, bounds.zMin), colorStart, colorEnd, vertexIO); } else if (bounds.yMax <= lastBounds.yMax) { writeBoxSweepQuadVertices(VECTOR3f(lastBounds.xMin, lastBounds.yMax, lastBounds.zMax), VECTOR3f(lastBounds.xMin, lastBounds.yMax, lastBounds.zMin), VECTOR3f(bounds.xMin, bounds.yMax, bounds.zMin), VECTOR3f(bounds.xMin, bounds.yMax, bounds.zMax), colorStart, colorEnd, vertexIO); } } else if (bounds.xMin >= lastBounds.xMin) { if (bounds.yMax >= lastBounds.yMax) { writeBoxSweepQuadVertices(VECTOR3f(lastBounds.xMin, lastBounds.yMax, lastBounds.zMin), VECTOR3f(lastBounds.xMin, lastBounds.yMax, lastBounds.zMax), VECTOR3f(bounds.xMin, bounds.yMax, bounds.zMax), VECTOR3f(bounds.xMin, bounds.yMax, bounds.zMin), colorStart, colorEnd, vertexIO); } else if (bounds.yMin <= lastBounds.yMin) { writeBoxSweepQuadVertices(VECTOR3f(lastBounds.xMin, lastBounds.yMin, lastBounds.zMax), VECTOR3f(lastBounds.xMin, lastBounds.yMin, lastBounds.zMin), VECTOR3f(bounds.xMin, bounds.yMin, bounds.zMin), VECTOR3f(bounds.xMin, bounds.yMin, bounds.zMax), colorStart, colorEnd, vertexIO); } } if (bounds.xMax >= lastBounds.xMax) { if (bounds.yMin >= lastBounds.yMin) { writeBoxSweepQuadVertices(VECTOR3f(lastBounds.xMax, lastBounds.yMin, lastBounds.zMax), VECTOR3f(lastBounds.xMax, lastBounds.yMin, lastBounds.zMin), VECTOR3f(bounds.xMax, bounds.yMin, bounds.zMin), VECTOR3f(bounds.xMax, bounds.yMin, bounds.zMax), colorStart, colorEnd, vertexIO); } else if (bounds.yMax <= lastBounds.yMax) { writeBoxSweepQuadVertices(VECTOR3f(lastBounds.xMax, lastBounds.yMax, lastBounds.zMin), VECTOR3f(lastBounds.xMax, lastBounds.yMax, lastBounds.zMax), VECTOR3f(bounds.xMax, bounds.yMax, bounds.zMax), VECTOR3f(bounds.xMax, bounds.yMax, bounds.zMin), colorStart, colorEnd, vertexIO); } } else if (bounds.xMax <= lastBounds.xMax) { if (bounds.yMax >= lastBounds.yMax) { writeBoxSweepQuadVertices(VECTOR3f(lastBounds.xMax, lastBounds.yMax, lastBounds.zMax), VECTOR3f(lastBounds.xMax, lastBounds.yMax, lastBounds.zMin), VECTOR3f(bounds.xMax, bounds.yMax, bounds.zMin), VECTOR3f(bounds.xMax, bounds.yMax, bounds.zMax), colorStart, colorEnd, vertexIO); } else if (bounds.yMin <= lastBounds.yMin) { writeBoxSweepQuadVertices(VECTOR3f(lastBounds.xMax, lastBounds.yMin, lastBounds.zMin), VECTOR3f(lastBounds.xMax, lastBounds.yMin, lastBounds.zMax), VECTOR3f(bounds.xMax, bounds.yMin, bounds.zMax), VECTOR3f(bounds.xMax, bounds.yMin, bounds.zMin), colorStart, colorEnd, vertexIO); } } if (bounds.zMin <= lastBounds.zMin) { if (bounds.yMin >= lastBounds.yMin) { writeBoxSweepQuadVertices(VECTOR3f(lastBounds.xMax, lastBounds.yMin, lastBounds.zMin), VECTOR3f(lastBounds.xMin, lastBounds.yMin, lastBounds.zMin), VECTOR3f(bounds.xMin, bounds.yMin, bounds.zMin), VECTOR3f(bounds.xMax, bounds.yMin, bounds.zMin), colorStart, colorEnd, vertexIO); } else if (bounds.yMax <= lastBounds.yMax) { writeBoxSweepQuadVertices(VECTOR3f(lastBounds.xMin, lastBounds.yMax, lastBounds.zMin), VECTOR3f(lastBounds.xMax, lastBounds.yMax, lastBounds.zMin), VECTOR3f(bounds.xMax, bounds.yMax, bounds.zMin), VECTOR3f(bounds.xMin, bounds.yMax, bounds.zMin), colorStart, colorEnd, vertexIO); } } else if (bounds.zMin >= lastBounds.zMin) { if (bounds.yMax >= lastBounds.yMax) { writeBoxSweepQuadVertices(VECTOR3f(lastBounds.xMax, lastBounds.yMax, lastBounds.zMin), VECTOR3f(lastBounds.xMin, lastBounds.yMax, lastBounds.zMin), VECTOR3f(bounds.xMin, bounds.yMax, bounds.zMin), VECTOR3f(bounds.xMax, bounds.yMax, bounds.zMin), colorStart, colorEnd, vertexIO); } else if (bounds.yMin <= lastBounds.yMin) { writeBoxSweepQuadVertices(VECTOR3f(lastBounds.xMin, lastBounds.yMin, lastBounds.zMin), VECTOR3f(lastBounds.xMax, lastBounds.yMin, lastBounds.zMin), VECTOR3f(bounds.xMax, bounds.yMin, bounds.zMin), VECTOR3f(bounds.xMin, bounds.yMin, bounds.zMin), colorStart, colorEnd, vertexIO); } } if (bounds.zMax >= lastBounds.zMax) { if (bounds.yMin >= lastBounds.yMin) { writeBoxSweepQuadVertices(VECTOR3f(lastBounds.xMin, lastBounds.yMin, lastBounds.zMax), VECTOR3f(lastBounds.xMax, lastBounds.yMin, lastBounds.zMax), VECTOR3f(bounds.xMax, bounds.yMin, bounds.zMax), VECTOR3f(bounds.xMin, bounds.yMin, bounds.zMax), colorStart, colorEnd, vertexIO); } else if (bounds.yMax <= lastBounds.yMax) { writeBoxSweepQuadVertices(VECTOR3f(lastBounds.xMax, lastBounds.yMax, lastBounds.zMax), VECTOR3f(lastBounds.xMin, lastBounds.yMax, lastBounds.zMax), VECTOR3f(bounds.xMin, bounds.yMax, bounds.zMax), VECTOR3f(bounds.xMax, bounds.yMax, bounds.zMax), colorStart, colorEnd, vertexIO); } } else if (bounds.zMax <= lastBounds.zMax) { if (bounds.yMax >= lastBounds.yMax) { writeBoxSweepQuadVertices(VECTOR3f(lastBounds.xMin, lastBounds.yMax, lastBounds.zMax), VECTOR3f(lastBounds.xMax, lastBounds.yMax, lastBounds.zMax), VECTOR3f(bounds.xMax, bounds.yMax, bounds.zMax), VECTOR3f(bounds.xMin, bounds.yMax, bounds.zMax), colorStart, colorEnd, vertexIO); } else if (bounds.yMin <= lastBounds.yMin) { writeBoxSweepQuadVertices(VECTOR3f(lastBounds.xMax, lastBounds.yMin, lastBounds.zMax), VECTOR3f(lastBounds.xMin, lastBounds.yMin, lastBounds.zMax), VECTOR3f(bounds.xMin, bounds.yMin, bounds.zMax), VECTOR3f(bounds.xMax, bounds.yMin, bounds.zMax), colorStart, colorEnd, vertexIO); } } if (bounds.zMin <= lastBounds.zMin) { if (bounds.xMin >= lastBounds.xMin) { writeBoxSweepQuadVertices(VECTOR3f(lastBounds.xMin, lastBounds.yMin, lastBounds.zMin), VECTOR3f(lastBounds.xMin, lastBounds.yMax, lastBounds.zMin), VECTOR3f(bounds.xMin, bounds.yMax, bounds.zMin), VECTOR3f(bounds.xMin, bounds.yMin, bounds.zMin), colorStart, colorEnd, vertexIO); } else if (bounds.xMax <= lastBounds.xMax) { writeBoxSweepQuadVertices(VECTOR3f(lastBounds.xMax, lastBounds.yMax, lastBounds.zMin), VECTOR3f(lastBounds.xMax, lastBounds.yMin, lastBounds.zMin), VECTOR3f(bounds.xMax, bounds.yMin, bounds.zMin), VECTOR3f(bounds.xMax, bounds.yMax, bounds.zMin), colorStart, colorEnd, vertexIO); } } else if (bounds.zMin >= lastBounds.zMin) { if (bounds.xMax >= lastBounds.xMax) { writeBoxSweepQuadVertices(VECTOR3f(lastBounds.xMax, lastBounds.yMin, lastBounds.zMin), VECTOR3f(lastBounds.xMax, lastBounds.yMax, lastBounds.zMin), VECTOR3f(bounds.xMax, bounds.yMax, bounds.zMin), VECTOR3f(bounds.xMax, bounds.yMin, bounds.zMin), colorStart, colorEnd, vertexIO); } else if (bounds.xMin <= lastBounds.xMin) { writeBoxSweepQuadVertices(VECTOR3f(lastBounds.xMin, lastBounds.yMax, lastBounds.zMin), VECTOR3f(lastBounds.xMin, lastBounds.yMin, lastBounds.zMin), VECTOR3f(bounds.xMin, bounds.yMin, bounds.zMin), VECTOR3f(bounds.xMin, bounds.yMax, bounds.zMin), colorStart, colorEnd, vertexIO); } } if (bounds.zMax >= lastBounds.zMax) { if (bounds.xMin >= lastBounds.xMin) { writeBoxSweepQuadVertices(VECTOR3f(lastBounds.xMin, lastBounds.yMax, lastBounds.zMax), VECTOR3f(lastBounds.xMin, lastBounds.yMin, lastBounds.zMax), VECTOR3f(bounds.xMin, bounds.yMin, bounds.zMax), VECTOR3f(bounds.xMin, bounds.yMax, bounds.zMax), colorStart, colorEnd, vertexIO); } else if (bounds.xMax <= lastBounds.xMax) { writeBoxSweepQuadVertices(VECTOR3f(lastBounds.xMax, lastBounds.yMin, lastBounds.zMax), VECTOR3f(lastBounds.xMax, lastBounds.yMax, lastBounds.zMax), VECTOR3f(bounds.xMax, bounds.yMax, bounds.zMax), VECTOR3f(bounds.xMax, bounds.yMin, bounds.zMax), colorStart, colorEnd, vertexIO); } } else if (bounds.zMax <= lastBounds.zMax) { if (bounds.xMax >= lastBounds.xMax) { writeBoxSweepQuadVertices(VECTOR3f(lastBounds.xMax, lastBounds.yMax, lastBounds.zMax), VECTOR3f(lastBounds.xMax, lastBounds.yMin, lastBounds.zMax), VECTOR3f(bounds.xMax, bounds.yMin, bounds.zMax), VECTOR3f(bounds.xMax, bounds.yMax, bounds.zMax), colorStart, colorEnd, vertexIO); } else if (bounds.xMin <= lastBounds.xMin) { writeBoxSweepQuadVertices(VECTOR3f(lastBounds.xMin, lastBounds.yMin, lastBounds.zMax), VECTOR3f(lastBounds.xMin, lastBounds.yMax, lastBounds.zMax), VECTOR3f(bounds.xMin, bounds.yMax, bounds.zMax), VECTOR3f(bounds.xMin, bounds.yMin, bounds.zMax), colorStart, colorEnd, vertexIO); } } } #define SWEEP_CYLINDER_SUBDIVISIONS 32 static void writeSphereSweepVertices(Vector3f lastPosition, Vector3f position, float radius, Color4f colorStart, Color4f colorEnd, VertexIO * vertexIO) { if (lastPosition.x == position.x && lastPosition.y == position.y && lastPosition.z == position.z) { return; } Vector3f direction, radialPosition; float length, dot; Quaternionf orientation; struct vertex_p3f_t2f_n3f_c4f vertices[SWEEP_CYLINDER_SUBDIVISIONS * 2], vertex = {.texCoords = {0.0f, 0.0f}}; uint32_t indexes[SWEEP_CYLINDER_SUBDIVISIONS * 6]; direction = Vector3f_subtract(position, lastPosition); length = Vector3f_magnitude(direction); direction = Vector3f_divideScalar(direction, length); dot = Vector3f_dot(direction, VECTOR3f_FRONT); if (dot > 0.9999f) { orientation = QUATERNIONf_IDENTITY; } else if (dot < -0.9999f) { orientation = Quaternionf_fromAxisAngle(VECTOR3f_UP, M_PI); } else { Vector3f axis = Vector3f_cross(direction, VECTOR3f_FRONT); Vector3f_normalize(&axis); orientation = Quaternionf_fromAxisAngle(axis, -acos(dot)); } radius /= cosf(0.5f * M_PI * 2 / SWEEP_CYLINDER_SUBDIVISIONS); for (unsigned int vertexIndex = 0; vertexIndex < SWEEP_CYLINDER_SUBDIVISIONS; vertexIndex++) { radialPosition = VECTOR3f(cos(vertexIndex * M_PI * 2 / SWEEP_CYLINDER_SUBDIVISIONS), sin(vertexIndex * M_PI * 2 / SWEEP_CYLINDER_SUBDIVISIONS), 0.0f); radialPosition = Quaternionf_multiplyVector3f(orientation, radialPosition); vertex.normal[0] = radialPosition.x; vertex.normal[1] = radialPosition.y; vertex.normal[2] = radialPosition.z; vertex.position[0] = radialPosition.x * radius + lastPosition.x; vertex.position[1] = radialPosition.y * radius + lastPosition.y; vertex.position[2] = radialPosition.z * radius + lastPosition.z; setVertexColor(&vertex, colorStart); vertices[vertexIndex] = vertex; vertex.position[0] = radialPosition.x * radius + position.x; vertex.position[1] = radialPosition.y * radius + position.y; vertex.position[2] = radialPosition.z * radius + position.z; setVertexColor(&vertex, colorEnd); vertices[SWEEP_CYLINDER_SUBDIVISIONS + vertexIndex] = vertex; } for (unsigned int vertexIndex = 0; vertexIndex < SWEEP_CYLINDER_SUBDIVISIONS; vertexIndex++) { indexes[vertexIndex * 6 + 0] = vertexIndex; indexes[vertexIndex * 6 + 1] = (vertexIndex + 1) % SWEEP_CYLINDER_SUBDIVISIONS; indexes[vertexIndex * 6 + 2] = (vertexIndex + 1) % SWEEP_CYLINDER_SUBDIVISIONS + SWEEP_CYLINDER_SUBDIVISIONS; indexes[vertexIndex * 6 + 3] = (vertexIndex + 1) % SWEEP_CYLINDER_SUBDIVISIONS + SWEEP_CYLINDER_SUBDIVISIONS; indexes[vertexIndex * 6 + 4] = vertexIndex + SWEEP_CYLINDER_SUBDIVISIONS; indexes[vertexIndex * 6 + 5] = vertexIndex; } VertexIO_writeIndexedVertices(vertexIO, sizeof(vertices) / sizeof(vertices[0]), vertices, sizeof(indexes) / sizeof(indexes[0]), indexes); } static void writeCapsuleSweepVertices(Vector3f lastPosition, Vector3f position, float radius, float cylinderHeight, Color4f colorStart, Color4f colorEnd, VertexIO * vertexIO) { if (lastPosition.x == position.x && lastPosition.y == position.y && lastPosition.z == position.z) { return; } Vector3f direction, radialPosition; float length, dot; Quaternionf orientation; struct vertex_p3f_t2f_n3f_c4f vertices[SWEEP_CYLINDER_SUBDIVISIONS * 2], vertex = {.texCoords = {0.0f, 0.0f}}; uint32_t indexes[SWEEP_CYLINDER_SUBDIVISIONS * 6]; direction = Vector3f_subtract(position, lastPosition); length = Vector3f_magnitude(direction); direction = Vector3f_divideScalar(direction, length); dot = Vector3f_dot(direction, VECTOR3f_FRONT); if (dot > 0.9999f) { orientation = QUATERNIONf_IDENTITY; } else if (dot < -0.9999f) { orientation = Quaternionf_fromAxisAngle(VECTOR3f_UP, M_PI); } else { Vector3f axis = Vector3f_cross(direction, VECTOR3f_FRONT); Vector3f_normalize(&axis); orientation = Quaternionf_fromAxisAngle(axis, -acos(dot)); } radius /= cosf(0.5f * M_PI * 2 / SWEEP_CYLINDER_SUBDIVISIONS); for (unsigned int vertexIndex = 0; vertexIndex < SWEEP_CYLINDER_SUBDIVISIONS; vertexIndex++) { radialPosition = VECTOR3f(cos(vertexIndex * M_PI * 2 / SWEEP_CYLINDER_SUBDIVISIONS), sin(vertexIndex * M_PI * 2 / SWEEP_CYLINDER_SUBDIVISIONS), 0.0f); radialPosition = Quaternionf_multiplyVector3f(orientation, radialPosition); vertex.normal[0] = radialPosition.x; vertex.normal[1] = radialPosition.y; vertex.normal[2] = radialPosition.z; float yOffset = radius; if (radialPosition.y > 0.0f) { yOffset += cylinderHeight; } vertex.position[0] = radialPosition.x * radius + lastPosition.x; vertex.position[1] = radialPosition.y * radius + lastPosition.y + yOffset; vertex.position[2] = radialPosition.z * radius + lastPosition.z; setVertexColor(&vertex, colorStart); vertices[vertexIndex] = vertex; vertex.position[0] = radialPosition.x * radius + position.x; vertex.position[1] = radialPosition.y * radius + position.y + yOffset; vertex.position[2] = radialPosition.z * radius + position.z; setVertexColor(&vertex, colorEnd); vertices[SWEEP_CYLINDER_SUBDIVISIONS + vertexIndex] = vertex; } for (unsigned int vertexIndex = 0; vertexIndex < SWEEP_CYLINDER_SUBDIVISIONS; vertexIndex++) { indexes[vertexIndex * 6 + 0] = vertexIndex; indexes[vertexIndex * 6 + 1] = (vertexIndex + 1) % SWEEP_CYLINDER_SUBDIVISIONS; indexes[vertexIndex * 6 + 2] = (vertexIndex + 1) % SWEEP_CYLINDER_SUBDIVISIONS + SWEEP_CYLINDER_SUBDIVISIONS; indexes[vertexIndex * 6 + 3] = (vertexIndex + 1) % SWEEP_CYLINDER_SUBDIVISIONS + SWEEP_CYLINDER_SUBDIVISIONS; indexes[vertexIndex * 6 + 4] = vertexIndex + SWEEP_CYLINDER_SUBDIVISIONS; indexes[vertexIndex * 6 + 5] = vertexIndex; } VertexIO_writeIndexedVertices(vertexIO, sizeof(vertices) / sizeof(vertices[0]), vertices, sizeof(indexes) / sizeof(indexes[0]), indexes); } static void writeTranslucentCollisionObjectVertices(SingleFrameScreen3D * self, VertexIO * vertexIO) { if (!self->drawSweeps) { return; } for (size_t objectIndex = 0; objectIndex < OBJECT_COUNT_3D; objectIndex++) { CollisionObject * object = self->objects[objectIndex]; Color4f colorStart, colorEnd; switch (call_virtual(getShapeType, object)) { case COLLISION_SHAPE_BOX_3D: { CollisionBox3D * box = (CollisionBox3D *) object; if (objectIndex == self->selectedObjectIndex) { colorStart = COLOR_BOX_LAST_POSITION_HIGHLIGHT; if (self->collisions[objectIndex].time > -1) { colorEnd = COLOR_BOX_POSITION_COLLIDING_HIGHLIGHT; } else { colorEnd = COLOR_BOX_POSITION_HIGHLIGHT; } } else { colorStart = COLOR_BOX_LAST_POSITION; if (self->collisions[objectIndex].time > -1) { colorEnd = COLOR_BOX_POSITION_COLLIDING; } else { colorEnd = COLOR_BOX_POSITION; } } colorStart.alpha = SWEEP_ALPHA; colorEnd.alpha = SWEEP_ALPHA; writeBoxSweepVertices(Vector3x_toVector3f(box->lastPosition), Vector3x_toVector3f(box->lastSize), Vector3x_toVector3f(box->position), Vector3x_toVector3f(box->size), colorStart, colorEnd, vertexIO); break; } case COLLISION_SHAPE_SPHERE: { CollisionSphere * sphere = (CollisionSphere *) object; if (objectIndex == self->selectedObjectIndex) { colorStart = COLOR_SPHERE_LAST_POSITION_HIGHLIGHT; if (self->collisions[objectIndex].time > -1) { colorEnd = COLOR_SPHERE_POSITION_COLLIDING_HIGHLIGHT; } else { colorEnd = COLOR_SPHERE_POSITION_HIGHLIGHT; } } else { colorStart = COLOR_SPHERE_LAST_POSITION; if (self->collisions[objectIndex].time > -1) { colorEnd = COLOR_SPHERE_POSITION_COLLIDING; } else { colorEnd = COLOR_SPHERE_POSITION; } } colorStart.alpha = SWEEP_ALPHA; colorEnd.alpha = SWEEP_ALPHA; writeSphereSweepVertices(Vector3x_toVector3f(sphere->lastPosition), Vector3x_toVector3f(sphere->position), xtof(sphere->radius), colorStart, colorEnd, vertexIO); break; } case COLLISION_SHAPE_CAPSULE: { CollisionCapsule * capsule = (CollisionCapsule *) object; if (objectIndex == self->selectedObjectIndex) { colorStart = COLOR_CAPSULE_LAST_POSITION_HIGHLIGHT; if (self->collisions[objectIndex].time > -1) { colorEnd = COLOR_CAPSULE_POSITION_COLLIDING_HIGHLIGHT; } else { colorEnd = COLOR_CAPSULE_POSITION_HIGHLIGHT; } } else { colorStart = COLOR_CAPSULE_LAST_POSITION; if (self->collisions[objectIndex].time > -1) { colorEnd = COLOR_CAPSULE_POSITION_COLLIDING; } else { colorEnd = COLOR_CAPSULE_POSITION; } } colorStart.alpha = SWEEP_ALPHA; colorEnd.alpha = SWEEP_ALPHA; writeCapsuleSweepVertices(Vector3x_toVector3f(capsule->lastPosition), Vector3x_toVector3f(capsule->position), xtof(capsule->radius), xtof(capsule->cylinderHeight), colorStart, colorEnd, vertexIO); break; } case COLLISION_SHAPE_STATIC_TRIMESH: break; } } } #define RAY_RADIUS 0.05f static void writeRayVertices(SingleFrameScreen3D * self, VertexIO * vertexIO) { Color4f rayColor = COLOR4f(0.5f, 0.75f, 1.0f, 1.0f); if (self->rayIntersection1 != FIXED_16_16_INF) { rayColor = COLOR4f(1.0f, 0.5f, 0.0f, 1.0f); } writeSphereVertices(Vector3x_toVector3f(self->rayStart), RAY_RADIUS, rayColor, vertexIO); writeSphereVertices(Vector3x_toVector3f(self->rayEnd), RAY_RADIUS, rayColor, vertexIO); writeSphereSweepVertices(Vector3x_toVector3f(self->rayStart), Vector3x_toVector3f(self->rayEnd), RAY_RADIUS, rayColor, rayColor, vertexIO); if (self->rayIntersection1 != FIXED_16_16_INF) { Vector3x intersectionPosition = Vector3x_interpolate(self->rayStart, self->rayEnd, self->rayIntersection1); writeSphereVertices(Vector3x_toVector3f(intersectionPosition), RAY_RADIUS * 2, COLOR4f(1.0f, 0.0f, 0.0f, 1.0f), vertexIO); } if (self->rayIntersection2 != FIXED_16_16_INF) { Vector3x intersectionPosition = Vector3x_interpolate(self->rayStart, self->rayEnd, self->rayIntersection2); writeSphereVertices(Vector3x_toVector3f(intersectionPosition), RAY_RADIUS * 2, COLOR4f(1.0f, 0.0f, 1.0f, 1.0f), vertexIO); } } static void writeLightVertices(SingleFrameScreen3D * self, VertexIO * vertexIO) { writeSphereVertices(VECTOR3f(0.0f, 8.0f, 8.0f), 0.25f, COLOR4f(1.0f, 1.0f, 0.95f, 1.0f), vertexIO); writeSphereVertices(VECTOR3f(-1.0f, -2.0f, -8.0f), 0.25f, COLOR4f(0.8f, 0.8f, 0.8f, 1.0f), vertexIO); } static bool draw(Atom eventID, void * eventData, void * context) { SingleFrameScreen3D * self = context; Matrix4x4f matrix = MATRIX4x4f_IDENTITY; if (self->meshDirty) { VertexIO * vertexIO = VertexIO_create(self->shaderConfiguration3D->shader->vertexFormat, 0.0, 0.0); writeOpaqueCollisionObjectVertices(self, vertexIO); writeRayVertices(self, vertexIO); writeLightVertices(self, vertexIO); writeTranslucentCollisionObjectVertices(self, vertexIO); VertexBuffer_bufferData(self->meshRenderable->vertexBuffer, vertexIO->vertices, vertexIO->vertexCount, vertexIO->indexes, vertexIO->indexCount, BUFFER_USAGE_STREAM); VertexIO_dispose(vertexIO); } Renderer_clear(self->renderer, COLOR4f(0.0f, 0.0f, 0.0f, 0.0f)); Matrix4x4f_translate(&matrix, 0.0f, 0.0f, -xtof(self->cameraDistance)); Matrix4x4f_multiply(&matrix, Quaternionf_toMatrix(Quaternionx_toQuaternionf(self->cameraDirection))); Matrix4x4f_translate(&matrix, xtof(self->cameraFocus.x), xtof(self->cameraFocus.y), xtof(self->cameraFocus.z)); call_virtual(setViewMatrix, self->shaderConfiguration3D, matrix); Renderer_drawLayer(self->renderer, self->renderLayer3D, 0.0, 0.0); Renderer_drawLayer(self->renderer, self->renderLayer2D, 0.0, 0.0); return true; } static bool keyDown(Atom eventID, void * eventData, void * context) { SingleFrameScreen3D * self = context; struct keyEvent * event = eventData; switch (event->keyCode) { case KEY_CODE_TAB: { size_t selectedObjectIndex = self->selectedObjectIndex; do { if (event->modifiers & MODIFIER_SHIFT_BIT) { selectedObjectIndex += OBJECT_COUNT_3D - 1; } else { selectedObjectIndex++; } selectedObjectIndex %= OBJECT_COUNT_3D; } while (call_virtual(getShapeType, self->objects[selectedObjectIndex]) == COLLISION_SHAPE_STATIC_TRIMESH && selectedObjectIndex != self->selectedObjectIndex); self->selectedObjectIndex = selectedObjectIndex; self->meshDirty = true; Shell_redisplay(); break; } case KEY_CODE_I: case KEY_CODE_J: case KEY_CODE_K: case KEY_CODE_L: case KEY_CODE_U: case KEY_CODE_M: if (call_virtual(getShapeType, self->objects[self->selectedObjectIndex]) == COLLISION_SHAPE_BOX_3D) { CollisionBox3D * box = (CollisionBox3D *) self->objects[self->selectedObjectIndex]; CollisionBox3D_setSolidity(box, event->keyCode == KEY_CODE_J ? !box->solidLeft : box->solidLeft, event->keyCode == KEY_CODE_L ? !box->solidRight : box->solidRight, event->keyCode == KEY_CODE_K ? !box->solidBottom : box->solidBottom, event->keyCode == KEY_CODE_I ? !box->solidTop : box->solidTop, event->keyCode == KEY_CODE_U ? !box->solidBack : box->solidBack, event->keyCode == KEY_CODE_M ? !box->solidFront : box->solidFront); updateCollisions(self); Shell_redisplay(); } break; case KEY_CODE_S: self->drawSweeps = !self->drawSweeps; Shell_redisplay(); break; case KEY_CODE_W: self->wireframe = !self->wireframe; if (self->wireframe) { self->renderPipelineConfiguration3D.polygonMode = RENDER_POLYGON_LINE; } else { self->renderPipelineConfiguration3D.polygonMode = RENDER_POLYGON_FILL; } Shell_redisplay(); break; case KEY_CODE_P: if (self->collisions[self->selectedObjectIndex].time == -1) { printf("No collision\n"); } else { printf("Collision at 0x%05X; normal = {0x%05X, 0x%05X, 0x%05X}\n", self->collisions[self->selectedObjectIndex].time, self->collisions[self->selectedObjectIndex].normal.x, self->collisions[self->selectedObjectIndex].normal.y, self->collisions[self->selectedObjectIndex].normal.z); } switch (call_virtual(getShapeType, self->objects[self->selectedObjectIndex])) { case COLLISION_SHAPE_BOX_3D: { CollisionBox3D * box = (CollisionBox3D *) self->objects[self->selectedObjectIndex]; printf(" lastPosition: {0x%05X, 0x%05X, 0x%05X}\n", box->lastPosition.x, box->lastPosition.y, box->lastPosition.z); printf(" lastSize: {0x%05X, 0x%05X, 0x%05X}\n", box->lastSize.x, box->lastSize.y, box->lastSize.z); printf(" position: {0x%05X, 0x%05X, 0x%05X}\n", box->position.x, box->position.y, box->position.z); printf(" size: {0x%05X, 0x%05X, 0x%05X}\n", box->size.x, box->size.y, box->size.z); break; } case COLLISION_SHAPE_SPHERE: { CollisionSphere * sphere = (CollisionSphere *) self->objects[self->selectedObjectIndex]; printf(" lastPosition: {0x%05X, 0x%05X, 0x%05X}\n", sphere->lastPosition.x, sphere->lastPosition.y, sphere->lastPosition.z); printf(" position: {0x%05X, 0x%05X, 0x%05X}\n", sphere->position.x, sphere->position.y, sphere->position.z); printf(" radius: 0x%05X\n", sphere->radius); break; } case COLLISION_SHAPE_CAPSULE: { CollisionCapsule * capsule = (CollisionCapsule *) self->objects[self->selectedObjectIndex]; printf(" lastPosition: {0x%05X, 0x%05X, 0x%05X}\n", capsule->lastPosition.x, capsule->lastPosition.y, capsule->lastPosition.z); printf(" position: {0x%05X, 0x%05X, 0x%05X}\n", capsule->position.x, capsule->position.y, capsule->position.z); printf(" radius: 0x%05X\n", capsule->radius); printf(" cylinderHeight: 0x%05X\n", capsule->cylinderHeight); break; } } break; case KEY_CODE_R: self->draggingRay = true; break; } return true; } static bool keyUp(Atom eventID, void * eventData, void * context) { SingleFrameScreen3D * self = context; struct keyEvent * event = eventData; switch (event->keyCode) { case KEY_CODE_R: self->draggingRay = false; return true; } return false; } #define DOUBLE_CLICK_INTERVAL 0.25 #define DOUBLE_CLICK_MAX_DISTANCE 4.0f static bool mouseDown(Atom eventID, void * eventData, void * context) { SingleFrameScreen3D * self = context; struct mouseEvent * event = eventData; static double lastClickTime; static Vector2f lastClickPosition; double clickTime = Shell_getCurrentTime(); if (clickTime - lastClickTime < DOUBLE_CLICK_INTERVAL && fabs(event->position.x - lastClickPosition.x) <= DOUBLE_CLICK_MAX_DISTANCE && fabs(event->position.y - lastClickPosition.y) <= DOUBLE_CLICK_MAX_DISTANCE) { CollisionObject * object = self->objects[self->selectedObjectIndex]; switch (call_virtual(getShapeType, object)) { case COLLISION_SHAPE_BOX_3D: { CollisionBox3D * box = (CollisionBox3D *) object; box->position = box->lastPosition; box->size = box->lastSize; break; } case COLLISION_SHAPE_SPHERE: { CollisionSphere * sphere = (CollisionSphere *) object; sphere->position = sphere->lastPosition; break; } case COLLISION_SHAPE_CAPSULE: { CollisionCapsule * capsule = (CollisionCapsule *) object; capsule->position = capsule->lastPosition; break; } } Shell_redisplay(); } else { self->draggingCamera = event->modifiers & MODIFIER_ALT_BIT; Shell_setMouseDeltaMode(true); } Shell_redisplay(); lastClickTime = clickTime; lastClickPosition = VECTOR2f(event->position.x, event->position.y); return true; } static bool mouseUp(Atom eventID, void * eventData, void * context) { Shell_setMouseDeltaMode(false); Shell_redisplay(); return true; } #define CAMERA_LOOK_SENSITIVITY 0x00100 #define CAMERA_ZOOM_SENSITIVITY 0x00060 #define CAMERA_DRAG_SENSITIVITY 0x00060 #define OBJECT_DRAG_SENSITIVITY 0x00060 static bool mouseDragged(Atom eventID, void * eventData, void * context) { SingleFrameScreen3D * self = context; struct mouseEvent * event = eventData; Vector3x offset; CollisionObject * object = self->objects[self->selectedObjectIndex]; if (self->draggingCamera) { if (event->modifiers & MODIFIER_SHIFT_BIT) { // Pan if (event->modifiers & MODIFIER_CONTROL_BIT) { offset.x = 0x00000; offset.y = 0x00000; offset.z = -xmul(ftox(event->delta.y), xmul(CAMERA_DRAG_SENSITIVITY, self->cameraDistance)); } else { offset.x = xmul(ftox(event->delta.x), xmul(CAMERA_DRAG_SENSITIVITY, self->cameraDistance)); offset.y = -xmul(ftox(event->delta.y), xmul(CAMERA_DRAG_SENSITIVITY, self->cameraDistance)); offset.z = 0x00000; } offset = Quaternionx_multiplyVector3x(Quaternionx_inverted(self->cameraDirection), offset); self->cameraFocus = Vector3x_add(self->cameraFocus, offset); Shell_redisplay(); } else if (event->modifiers & MODIFIER_CONTROL_BIT) { // Zoom self->cameraDistance += xmul(ftox(event->delta.y), xmul(CAMERA_ZOOM_SENSITIVITY, self->cameraDistance)); if (self->cameraDistance < 0x10000) { self->cameraDistance = 0x10000; } Shell_redisplay(); } else { // Rotate Quaternionx_rotate(&self->cameraDirection, VECTOR3x_UP, xmul(ftox(event->delta.x), CAMERA_LOOK_SENSITIVITY)); Quaternionx_rotate(&self->cameraDirection, Quaternionx_multiplyVector3x(Quaternionx_inverted(self->cameraDirection), VECTOR3x_RIGHT), xmul(ftox(event->delta.y), CAMERA_LOOK_SENSITIVITY)); Shell_redisplay(); } } else if (self->draggingRay) { if (event->modifiers & MODIFIER_CONTROL_BIT) { offset.x = 0x00000; offset.y = 0x00000; offset.z = xmul(ftox(event->delta.y), xmul(OBJECT_DRAG_SENSITIVITY, self->cameraDistance)); } else { offset.x = xmul(ftox(event->delta.x), xmul(OBJECT_DRAG_SENSITIVITY, self->cameraDistance)); offset.y = -xmul(ftox(event->delta.y), xmul(OBJECT_DRAG_SENSITIVITY, self->cameraDistance)); offset.z = 0x00000; } offset = Quaternionx_multiplyVector3x(Quaternionx_inverted(self->cameraDirection), offset); if ((event->modifiers & MODIFIER_ALT_BIT) || g_spacebarDown) { self->rayStart = Vector3x_add(self->rayStart, offset); } if (!(event->modifiers & MODIFIER_ALT_BIT) || g_spacebarDown) { self->rayEnd = Vector3x_add(self->rayEnd, offset); } updateCollisions(self); Shell_redisplay(); } else { if (event->modifiers & MODIFIER_CONTROL_BIT) { offset.x = 0x00000; offset.y = 0x00000; offset.z = xmul(ftox(event->delta.y), xmul(OBJECT_DRAG_SENSITIVITY, self->cameraDistance)); } else { offset.x = xmul(ftox(event->delta.x), xmul(OBJECT_DRAG_SENSITIVITY, self->cameraDistance)); offset.y = -xmul(ftox(event->delta.y), xmul(OBJECT_DRAG_SENSITIVITY, self->cameraDistance)); offset.z = 0x00000; } offset = Quaternionx_multiplyVector3x(Quaternionx_inverted(self->cameraDirection), offset); switch (call_virtual(getShapeType, object)) { case COLLISION_SHAPE_BOX_3D: { CollisionBox3D * box = (CollisionBox3D *) object; if (event->modifiers & MODIFIER_ALT_BIT) { if (event->modifiers & MODIFIER_SHIFT_BIT) { box->lastSize = Vector3x_add(box->lastSize, offset); } else { box->lastPosition = Vector3x_add(box->lastPosition, offset); } } else if (event->modifiers & MODIFIER_SHIFT_BIT) { box->size = Vector3x_add(box->size, offset); if (g_spacebarDown) { box->lastSize = Vector3x_add(box->lastSize, offset); } } else { box->position = Vector3x_add(box->position, offset); if (g_spacebarDown) { box->lastPosition = Vector3x_add(box->lastPosition, offset); } } break; } case COLLISION_SHAPE_SPHERE: { CollisionSphere * sphere = (CollisionSphere *) object; if (event->modifiers & MODIFIER_SHIFT_BIT) { sphere->radius += offset.y; } else if (event->modifiers & MODIFIER_ALT_BIT) { sphere->lastPosition = Vector3x_add(sphere->lastPosition, offset); } else { sphere->position = Vector3x_add(sphere->position, offset); if (g_spacebarDown) { sphere->lastPosition = Vector3x_add(sphere->lastPosition, offset); } } break; } case COLLISION_SHAPE_CAPSULE: { CollisionCapsule * capsule = (CollisionCapsule *) object; if (event->modifiers & MODIFIER_SHIFT_BIT) { if (event->modifiers & MODIFIER_ALT_BIT) { capsule->cylinderHeight += offset.y; if (capsule->cylinderHeight < 0x00000) { capsule->cylinderHeight = 0x00000; } } else { capsule->radius += offset.y; } } else if (event->modifiers & MODIFIER_ALT_BIT) { capsule->lastPosition = Vector3x_add(capsule->lastPosition, offset); } else { capsule->position = Vector3x_add(capsule->position, offset); if (g_spacebarDown) { capsule->lastPosition = Vector3x_add(capsule->lastPosition, offset); } } break; } } updateCollisions(self); Shell_redisplay(); } return true; } static bool resized(Atom eventID, void * eventData, void * context) { SingleFrameScreen3D * self = context; call_virtual(setProjectionMatrix, self->shaderConfiguration3D, Matrix4x4f_perspective(MATRIX4x4f_IDENTITY, 70.0f, g_viewRatio, 0.5f, 800.0f)); call_virtual(setProjectionMatrix, self->shaderConfiguration2D, Matrix4x4f_ortho(MATRIX4x4f_IDENTITY, -12.0f * g_viewRatio, 12.0f * g_viewRatio, -12.0f, 12.0f, -1.0f, 1.0f)); Shell_redisplay(); return true; } void SingleFrameScreen3D_activate(SingleFrameScreen3D * self, Screen * lastScreen, const char * transitionName) { Vector3x vertices[] = { {-0x20000, -0x20000, -0x60000}, { 0x20000, -0x20000, -0x20000}, { 0x20000, 0x20000, -0x60000}, { 0x20000, -0x20000, -0x60000} }; unsigned int indexes[] = { 0, 1, 2, 0, 3, 1, 0, 2, 3, 1, 3, 2 }; /* Vector3x vertices2[] = {{-0x80000, -0x60000, 0x00000}, {-0x60000, -0x60000, 0x00000}, {-0x80000, -0x60000, 0x20000}, {-0x80000, -0x60000, 0x00000}, {-0x80000, -0x60000, 0x20000}, {-0x80000, -0x40000, 0x00000}, {-0x80000, -0x60000, 0x00000}, {-0x80000, -0x40000, 0x00000}, {-0x60000, -0x60000, 0x00000}, {-0x80000, -0x40000, 0x00000}, {-0x80000, -0x60000, 0x20000}, {-0x60000, -0x60000, 0x00000}}; */ Vector3x vertices2[] = {{0x01000, 0x01000, 0x01000}, {0x20000, 0x00000, 0x00000}, {0x00000, 0x00000, 0x20000}, {0x01000, 0x01000, 0x01000}, {0x00000, 0x00000, 0x20000}, {0x00000, 0x20000, 0x00000}, {0x01000, 0x01000, 0x01000}, {0x00000, 0x20000, 0x00000}, {0x20000, 0x00000, 0x00000}, {0x00000, 0x20000, 0x00000}, {0x00000, 0x00000, 0x20000}, {0x20000, 0x00000, 0x00000}}; self->objects[0] = (CollisionObject *) CollisionBox3D_create(NULL, NULL, NULL, VECTOR3x(0x30000, 0x10000, 0x00000), VECTOR3x(0x10000, 0x10000, 0x10000), 0x00000); self->objects[1] = (CollisionObject *) CollisionBox3D_create(NULL, NULL, NULL, VECTOR3x(0x20000, -0x40000, 0x00000), VECTOR3x(0x50000, 0x20000, 0x30000), 0x00000); self->objects[2] = (CollisionObject *) CollisionSphere_create(NULL, NULL, NULL, VECTOR3x(-0x30000, 0x00000, 0x00000), 0x10000); self->objects[3] = (CollisionObject *) CollisionSphere_create(NULL, NULL, NULL, VECTOR3x(-0x20000, 0x50000, 0x00000), 0x08000); self->objects[4] = (CollisionObject *) CollisionCapsule_create(NULL, NULL, NULL, VECTOR3x(0x00000, -0x50000, 0x20000), 0x08000, 0x14000); self->objects[5] = (CollisionObject *) CollisionCapsule_create(NULL, NULL, NULL, VECTOR3x(0x20000, 0x60000, 0x20000), 0x0C000, 0x10000); self->objects[6] = (CollisionObject *) CollisionStaticTrimesh_createWithIndexes(NULL, NULL, NULL, vertices, indexes, sizeof(indexes) / sizeof(unsigned int)); self->objects[7] = (CollisionObject *) CollisionStaticTrimesh_create(NULL, NULL, NULL, vertices2, 12); self->resolver = CollisionResolver_create(self->intersectionManager, false, NULL, NULL); CollisionResolver_addObject(self->resolver, self->objects[0]); CollisionResolver_addObject(self->resolver, self->objects[1]); CollisionResolver_addObject(self->resolver, self->objects[2]); CollisionResolver_addObject(self->resolver, self->objects[3]); CollisionResolver_addObject(self->resolver, self->objects[4]); CollisionResolver_addObject(self->resolver, self->objects[5]); CollisionResolver_addObject(self->resolver, self->objects[6]); CollisionResolver_addObject(self->resolver, self->objects[7]); self->selectedObjectIndex = 0; self->cameraFocus = VECTOR3x_ZERO; self->cameraDirection = QUATERNIONx_IDENTITY; self->cameraDistance = 0xF0000; self->rayStart = VECTOR3x(-0x40000, 0x30000, -0x10000); self->rayEnd = VECTOR3x(0x40000, 0x30000, -0x10000); self->drawSweeps = true; updateCollisions(self); EventDispatcher_registerForEvent(self->screenManager->eventDispatcher, ATOM(EVENT_KEY_DOWN), keyDown, self); EventDispatcher_registerForEvent(self->screenManager->eventDispatcher, ATOM(EVENT_KEY_UP), keyUp, self); EventDispatcher_registerForEvent(self->screenManager->eventDispatcher, ATOM(EVENT_MOUSE_DOWN), mouseDown, self); EventDispatcher_registerForEvent(self->screenManager->eventDispatcher, ATOM(EVENT_MOUSE_UP), mouseUp, self); EventDispatcher_registerForEvent(self->screenManager->eventDispatcher, ATOM(EVENT_MOUSE_DRAGGED), mouseDragged, self); EventDispatcher_registerForEvent(self->screenManager->eventDispatcher, ATOM(EVENT_RESIZED), resized, self); EventDispatcher_registerForEvent(self->screenManager->eventDispatcher, ATOM(EVENT_DRAW), draw, self); Shell_redisplay(); } void SingleFrameScreen3D_deactivate(SingleFrameScreen3D * self, Screen * nextScreen, const char * transitionName) { for (size_t objectIndex = 0; objectIndex < OBJECT_COUNT_3D; objectIndex++) { call_virtual(dispose, self->objects[objectIndex]); } CollisionResolver_dispose(self->resolver); EventDispatcher_unregisterForEvent(self->screenManager->eventDispatcher, ATOM(EVENT_KEY_DOWN), keyDown, self); EventDispatcher_unregisterForEvent(self->screenManager->eventDispatcher, ATOM(EVENT_KEY_UP), keyUp, self); EventDispatcher_unregisterForEvent(self->screenManager->eventDispatcher, ATOM(EVENT_MOUSE_DOWN), mouseDown, self); EventDispatcher_unregisterForEvent(self->screenManager->eventDispatcher, ATOM(EVENT_MOUSE_UP), mouseUp, self); EventDispatcher_unregisterForEvent(self->screenManager->eventDispatcher, ATOM(EVENT_MOUSE_DRAGGED), mouseDragged, self); EventDispatcher_unregisterForEvent(self->screenManager->eventDispatcher, ATOM(EVENT_RESIZED), resized, self); EventDispatcher_unregisterForEvent(self->screenManager->eventDispatcher, ATOM(EVENT_DRAW), draw, self); }