void WorldRenderer::RenderWorld(const corgi::CameraInterface& camera, fplbase::Renderer& renderer, World* world) { mat4 camera_transform = camera.GetTransformMatrix(); renderer.set_color(mathfu::kOnes4f); renderer.DepthTest(true); renderer.set_model_view_projection(camera_transform); assert(textured_shadowed_shader_); textured_shadowed_shader_->SetUniform("view_projection", camera_transform); textured_shadowed_shader_->SetUniform("light_view_projection", light_camera_.GetTransformMatrix()); textured_shadowed_shader_->SetUniform( "shadow_intensity", world->config->rendering_config()->shadow_intensity()); float shadow_intensity = world->config->rendering_config()->shadow_intensity(); float ambient_intensity = 1.0f - shadow_intensity; vec4 ambient = vec4(ambient_intensity, ambient_intensity, ambient_intensity, 1.0f); float diffuse_intensity = shadow_intensity; vec4 diffuse = vec4(diffuse_intensity, diffuse_intensity, diffuse_intensity, 1.0f); textured_lit_cutout_shader_->SetUniform("ambient_material", ambient); textured_lit_cutout_shader_->SetUniform("diffuse_material", diffuse); textured_lit_shader_->SetUniform("ambient_material", ambient); textured_lit_shader_->SetUniform("diffuse_material", diffuse); textured_lit_bank_shader_->SetUniform("ambient_material", ambient); textured_lit_bank_shader_->SetUniform("diffuse_material", diffuse); textured_skinned_lit_shader_->SetUniform("ambient_material", ambient); textured_skinned_lit_shader_->SetUniform("diffuse_material", diffuse); float texture_repeats = world->config->river_config()->texture_repeats(); float river_offset = world->river_component.river_offset(); river_shader_->SetUniform("river_offset", river_offset); river_shader_->SetUniform("texture_repeats", texture_repeats); SetFogUniforms(textured_shadowed_shader_, world); SetFogUniforms(textured_lit_shader_, world); SetFogUniforms(textured_lit_bank_shader_, world); SetFogUniforms(textured_skinned_lit_shader_, world); shadow_map_.BindAsTexture(kShadowMapTextureID); if (!world->skip_rendermesh_rendering) { for (int pass = 0; pass < corgi::RenderPass_Count; pass++) { world->render_mesh_component.RenderPass(pass, camera, renderer); } } if (world->draw_debug_physics) { world->physics_component.DebugDrawWorld(&renderer, camera_transform); } }
static void RenderSettingsGear(fplbase::Renderer& renderer, World* world) { vec2i res = renderer.window_size(); renderer.set_model_view_projection(mathfu::mat4::Ortho( 0.0f, static_cast<float>(res.x), static_cast<float>(res.y), 0.0f, -1.0f, 1.0f)); renderer.set_color(mathfu::kOnes4f); auto shader = world->asset_manager->LoadShader("shaders/textured"); world->cardboard_settings_gear->Set(renderer); shader->Set(renderer); fplbase::Mesh::RenderAAQuadAlongX( vec3((res.x - kGearSize) / 2.0f, res.y - kGearSize, 0.0f), vec3((res.x + kGearSize) / 2.0f, res.y, 0.0f)); }
// Draw the shadow map in the world, so we can see it. void WorldRenderer::DebugShowShadowMap(const corgi::CameraInterface& camera, fplbase::Renderer& renderer) { fplbase::RenderTarget::ScreenRenderTarget(renderer).SetAsRenderTarget(); static const mat4 kDebugTextureWorldTransform = mat4::FromScaleVector(mathfu::vec3(10.0f, 10.0f, 10.0f)); const mat4 mvp = camera.GetTransformMatrix() * kDebugTextureWorldTransform; const mat4 world_matrix_inverse = kDebugTextureWorldTransform.Inverse(); renderer.set_camera_pos(world_matrix_inverse * camera.position()); renderer.set_light_pos(world_matrix_inverse * light_camera_.position()); renderer.set_model_view_projection(mvp); renderer.set_color(vec4(1.0f, 1.0f, 1.0f, 1.0f)); shadow_map_.BindAsTexture(0); textured_shader_->Set(renderer); // Render a large quad in the world, with the shadowmap texture on it: fplbase::Mesh::RenderAAQuadAlongX(vec3(0.0f, 0.0f, 0.0f), vec3(10.0f, 0.0f, 10.0f), vec2(1.0f, 0.0f), vec2(0.0f, 1.0f)); }
void WorldRenderer::RenderWorld(const corgi::CameraInterface &camera, fplbase::Renderer &renderer, World *world) { PushDebugMarker("Render World"); PushDebugMarker("Scene Setup"); if (world->RenderingOptionsDirty()) { RefreshGlobalShaderDefines(world); } mat4 camera_transform = camera.GetTransformMatrix(); renderer.set_color(mathfu::kOnes4f); renderer.SetDepthFunction(fplbase::kDepthFunctionLess); renderer.set_model_view_projection(camera_transform); float texture_repeats = world->CurrentLevel()->river_config()->texture_repeats(); float river_offset = world->river_component.river_offset(); if (world->RenderingOptionEnabled(kShadowEffect)) { world->asset_manager->ForEachShaderWithDefine( kDefinesText[kShadowEffect], [&](fplbase::Shader *shader) { shader->SetUniform("view_projection", camera_transform); shader->SetUniform("light_view_projection", light_camera_.GetTransformMatrix()); }); } world->asset_manager->ForEachShaderWithDefine( "WATER", [&](fplbase::Shader *shader) { shader->SetUniform("river_offset", river_offset); shader->SetUniform("texture_repeats", texture_repeats); }); world->asset_manager->ForEachShaderWithDefine( kDefinesText[kPhongShading], [&](fplbase::Shader *shader) { SetLightingUniforms(shader, world); }); world->asset_manager->ForEachShaderWithDefine( "FOG_EFFECT", [&](fplbase::Shader *shader) { SetFogUniforms(shader, world); }); shadow_map_.BindAsTexture(kShadowMapTextureID); PopDebugMarker(); // Scene Setup if (!world->skip_rendermesh_rendering) { for (int pass = 0; pass < corgi::RenderPass_Count; pass++) { PushDebugMarker("RenderPass"); world->render_mesh_component.RenderPass(pass, camera, renderer); PopDebugMarker(); } } if (world->draw_debug_physics) { PushDebugMarker("Debug Draw World"); world->physics_component.DebugDrawWorld(&renderer, camera_transform); PopDebugMarker(); } PushDebugMarker("Text"); world->render_3d_text_component.RenderAllEntities(camera); PopDebugMarker(); PopDebugMarker(); // Render World }
// Render a single render-pass, by ID. void RenderMeshComponent::RenderPass(int pass_id, const CameraInterface& camera, fplbase::Renderer& renderer, const fplbase::Shader* shader_override) { mat4 camera_vp = camera.GetTransformMatrix(); for (size_t i = 0; i < pass_render_list_[pass_id].size(); i++) { EntityRef& entity = pass_render_list_[pass_id][i].entity; RenderMeshData* rendermesh_data = Data<RenderMeshData>(entity); TransformData* transform_data = Data<TransformData>(entity); AnimationData* anim_data = Data<AnimationData>(entity); // TODO: anim_data will set uniforms for an array of matricies. Each // matrix represents one bone position. const bool has_anim = anim_data != nullptr && anim_data->motivator.Valid(); const int num_mesh_bones = rendermesh_data->mesh->num_bones(); const int num_anim_bones = has_anim ? anim_data->motivator.DefiningAnim()->NumBones() : 0; const bool has_one_bone_anim = has_anim && (num_mesh_bones <= 1 || num_anim_bones == 1); const mat4 world_transform = has_one_bone_anim ? transform_data->world_transform * mat4::FromAffineTransform( anim_data->motivator.GlobalTransforms()[0]) : transform_data->world_transform; const mat4 mvp = camera_vp * world_transform; const mat4 world_matrix_inverse = world_transform.Inverse(); renderer.set_light_pos(world_matrix_inverse * light_position_); renderer.set_color(rendermesh_data->tint); renderer.set_model(world_transform); // If the mesh has a skeleton, we need to update the bone positions. // The positions are normally supplied by the animation, but if they are // not, use the default pose in the RenderMesh. if (num_mesh_bones > 1) { const bool use_default_pose = num_anim_bones != num_mesh_bones || rendermesh_data->default_pose; const mathfu::AffineTransform* bone_transforms = use_default_pose ? rendermesh_data->mesh->bone_global_transforms() : anim_data->motivator.GlobalTransforms(); rendermesh_data->mesh->GatherShaderTransforms( bone_transforms, rendermesh_data->shader_transforms); renderer.SetBoneTransforms(rendermesh_data->shader_transforms, rendermesh_data->num_shader_transforms); } if (!camera.IsStereo()) { renderer.set_camera_pos(world_matrix_inverse * camera.position()); renderer.set_model_view_projection(mvp); if (!shader_override && rendermesh_data->shader) { rendermesh_data->shader->Set(renderer); } else { shader_override->Set(renderer); } rendermesh_data->mesh->Render(renderer); } else { const fplbase::Shader* shader = nullptr; if (!shader_override && rendermesh_data->shader) { shader = rendermesh_data->shader; } else { shader = shader_override; } mathfu::vec4i viewport[2] = {camera.viewport(0), camera.viewport(1)}; mat4 camera_vp_stereo = camera.GetTransformMatrix(1); mat4 mvp_matrices[2] = {mvp, camera_vp_stereo * world_transform}; vec3 camera_positions[2] = {world_matrix_inverse * camera.position(0), world_matrix_inverse * camera.position(1)}; rendermesh_data->mesh->RenderStereo(renderer, shader, viewport, mvp_matrices, camera_positions); } } }