diff --git a/src/modules/RenderModule.cc b/src/modules/RenderModule.cc index 7b26ba4..444e020 100644 --- a/src/modules/RenderModule.cc +++ b/src/modules/RenderModule.cc @@ -744,7 +744,7 @@ void Camera::updateMatrices() { -height * 0.5f, height * 0.5f, near, far); } else { - float aspect = width / height; + float aspect = static_cast(width) / height; float mtx_proj[16]; bx::mtxProjRh(mtxProj, fov, aspect, near, far); } @@ -1446,16 +1446,39 @@ void Renderer::paintGL() { for (size_t i = 0; i < entities.size(); i++) { float mtxLightViewProjInv[16]; float light_pos_world[3]; + // shadow map pass - bx::mtxMul(lightMtx, entities[i]->transform.toMatrix().data(), lights[0].mtxShadow); - bgfx::setUniform(lights[0].u_lightMtx, lightMtx); - entities[i]->mesh.submit (&s_renderStates[RenderState::ShadowMap]); + for (uint32_t j = 0; j < entities[i]->mesh.meshes.size(); ++j) { + bx::mtxMul( + lightMtx, + entities[i]->mesh.meshMatrices[j].data(), + lights[0].mtxShadow + ); + bgfx::setUniform(lights[0].u_lightMtx, lightMtx); + bgfxutils::meshSubmit ( + entities[i]->mesh.meshes[j], + &s_renderStates[RenderState::ShadowMap], + 1, + entities[i]->mesh.meshMatrices[j].data() + ); + } // scene pass - bx::mtxMul(lightMtx, entities[i]->transform.toMatrix().data(), lights[0].mtxShadow); - bgfx::setUniform(lights[0].u_lightMtx, lightMtx); - bgfx::setUniform(u_color, entities[i]->color, 4); - entities[i]->mesh.submit (&s_renderStates[RenderState::Scene]); + for (uint32_t j = 0; j < entities[i]->mesh.meshes.size(); ++j) { + bx::mtxMul( + lightMtx, + entities[i]->mesh.meshMatrices[j].data(), + lights[0].mtxShadow + ); + bgfx::setUniform(lights[0].u_lightMtx, lightMtx); + bgfx::setUniform(u_color, entities[i]->color, 4); + bgfxutils::meshSubmit ( + entities[i]->mesh.meshes[j], + &s_renderStates[RenderState::Scene], + 1, + entities[i]->mesh.meshMatrices[j].data() + ); + } } // render debug information @@ -1641,8 +1664,9 @@ void Renderer::paintGL() { float thickness = 0.05f; float miter = 0.0f; + float aspect = static_cast(width) / height; - Vector4f params (thickness, miter, width / height, 0.0f); + Vector4f params (thickness, miter, aspect, 0.0f); Camera &active_camera = cameras[activeCameraIndex]; diff --git a/src/modules/RenderModule.h b/src/modules/RenderModule.h index bb99df6..7619ece 100644 --- a/src/modules/RenderModule.h +++ b/src/modules/RenderModule.h @@ -218,6 +218,27 @@ struct Transform { return Vector3f::Zero(); } + static Transform fromTrans( + const Vector3f &translation + ) { + return Transform ( + translation, + Quaternion(0.0f, 0.0f, 0.0f, 1.0f), + Vector3f(1.0f, 1.0f, 1.0f) + ); + } + + static Transform fromTransRot( + const Vector3f &translation, + const Quaternion &rotation + ) { + return Transform ( + translation, + rotation, + Vector3f(1.0f, 1.0f, 1.0f) + ); + } + static Transform fromTransRotScale( const Vector3f &translation, const Quaternion &rotation, @@ -248,7 +269,7 @@ struct MeshHierarchy { const int parent_idx, const Transform& transform, bgfxutils::Mesh* mesh) { - assert (parent_idx == -1 || parent_idx < parent.size() - 1); + assert (parent_idx == -1 || parent_idx < parent.size()); parent.push_back(parent_idx); localTransforms.push_back(transform); meshes.push_back(mesh); diff --git a/src/modules/RenderUtils.cc b/src/modules/RenderUtils.cc index 9806d9a..bbe761b 100644 --- a/src/modules/RenderUtils.cc +++ b/src/modules/RenderUtils.cc @@ -1101,7 +1101,7 @@ Mesh *createCuboid (float width, float height, float depth) { return createMeshFromStdVectors (vertices, normals, colors); } -Mesh *createUVSphere (int rows, int segments) { +Mesh *createUVSphere (int rows, int segments, float radius) { // work arrays that we fill with data std::vector vertices; std::vector normals; @@ -1115,11 +1115,11 @@ Mesh *createUVSphere (int rows, int segments) { float alpha0 = j * row_d * M_PI; float alpha1 = (j + 1) * row_d * M_PI; - float r0 = sin (alpha0) * 0.5f; - float r1 = sin (alpha1) * 0.5f; + float r0 = sin (alpha0) * 0.5f * radius; + float r1 = sin (alpha1) * 0.5f * radius; - float h0 = cos (alpha0) * 0.5f; - float h1 = cos (alpha1) * 0.5f; + float h0 = cos (alpha0) * 0.5f * radius; + float h1 = cos (alpha1) * 0.5f * radius; for (unsigned int i = 0; i < segments; i++) { Vector3f v0, v1, v2, v3; diff --git a/src/modules/RenderUtils.h b/src/modules/RenderUtils.h index 2ccbe27..f35150e 100644 --- a/src/modules/RenderUtils.h +++ b/src/modules/RenderUtils.h @@ -55,8 +55,10 @@ namespace bgfxutils { // Loads the mesh data from a VBO into a bgfx Mesh // Mesh *createMeshFromVBO (const MeshVBO& mesh_buffer); + void meshTransform (Mesh* mesh, const float *mtx); + Mesh *createCuboid (float width, float height, float depth); - Mesh *createUVSphere (int rows, int segments); + Mesh *createUVSphere (int rows, int segments, float radius = 1.0f); Mesh *createCylinder (int segments); } diff --git a/src/modules/TestModule.cc b/src/modules/TestModule.cc index bec4e67..1fef6f8 100644 --- a/src/modules/TestModule.cc +++ b/src/modules/TestModule.cc @@ -285,6 +285,7 @@ static void module_serialize ( struct module_state *state, Serializer* serializer) { SerializeVec3(*serializer, "protot.TestModule.entity.position", state->character->position); + SerializeVec3(*serializer, "protot.TestModule.entity.velocity", state->character->velocity); SerializeBool(*serializer, "protot.TestModule.character_window.visible", state->character_properties_window_visible); SerializeBool(*serializer, "protot.TestModule.modules_window.visible", state->modules_window_visible); SerializeInt(*serializer, "protot.TestModule.modules_window.selection_index", state->modules_window_selected_index); @@ -309,24 +310,30 @@ static void module_reload(struct module_state *state, void* read_serializer) { cout << "Creating render entity mesh ..." << endl; - Vector3f snowman_offsets (0.45f, 0.35f, 0.25f); - float height_offset = 0.0f; - for (int i = 0; i < 3; i++) { - float radius = cCharacterHeight * snowman_offsets[i]; - Transform transform = Transform::fromTransRotScale( - Vector3f(0.f, height_offset + radius * 0.5f, 0.0f), - Quaternion(0.0f, 0.0f, 0.0f, 1.0f), - Vector3f(radius, radius, radius) - ); + // Build the snowman + state->character->entity->mesh.addMesh( + - 1, + Transform::fromTrans( + Vector3f (0.0f, 0.9 * 0.5f, 0.0f) + ), + bgfxutils::createUVSphere (45, 45, 0.9) + ); - state->character->entity->mesh.addMesh( - -1, - transform, - bgfxutils::createUVSphere (45, 45) - ); + state->character->entity->mesh.addMesh( + 0, + Transform::fromTrans( + Vector3f (0.0f, 0.55f, 0.0f) + ), + bgfxutils::createUVSphere (45, 45, 0.7) + ); - height_offset += radius * 0.8; - } + state->character->entity->mesh.addMesh( + 1, + Transform::fromTrans( + Vector3f (0.0f, 0.4f, 0.0f) + ), + bgfxutils::createUVSphere (45, 45, 0.5) + ); // state->character->entity->mesh = bgfxutils::createCuboid (1.f, 1.f, 1.f); // state->character->entity->mesh = bgfxutils::createCylinder (20);