Exemple #1
0
void WorldRenderer::CreateShadowMap(const corgi::CameraInterface& camera,
                                    fplbase::Renderer& renderer, World* world) {
  float shadow_map_resolution = static_cast<float>(
        world->config->rendering_config()->shadow_map_resolution());
  float shadow_map_zoom = world->config->rendering_config()->shadow_map_zoom();
  float shadow_map_offset =
      world->config->rendering_config()->shadow_map_offset();
  vec3 light_position =
      LoadVec3(world->config->rendering_config()->light_position());
  SetLightPosition(light_position);
  light_camera_.set_viewport_angle(kShadowMapViewportAngle / shadow_map_zoom);
  light_camera_.set_viewport_resolution(
      vec2(shadow_map_resolution, shadow_map_resolution));
  vec3 light_camera_focus =
      camera.position() + camera.facing() * shadow_map_offset;
  light_camera_focus.z() = 0;
  vec3 light_facing = light_camera_focus - light_camera_.position();
  light_camera_.set_facing(light_facing.Normalized());

  // Shadow map needs to be cleared to near-white, since that's
  // the maximum (furthest) depth.
  shadow_map_.SetAsRenderTarget();
  renderer.ClearFrameBuffer(kShadowMapClearColor);
  renderer.SetCulling(fplbase::Renderer::kCullBack);

  depth_shader_->Set(renderer);
  // Generate the shadow map:
  // TODO - modify this so that shadowcast is its own render pass
  for (int pass = 0; pass < corgi::RenderPass_Count; pass++) {
    world->render_mesh_component.RenderPass(pass, light_camera_,
                                            renderer, depth_shader_);
  }
}
Exemple #2
0
void WorldRenderer::CreateShadowMap(const corgi::CameraInterface &camera,
                                    fplbase::Renderer &renderer, World *world) {
  PushDebugMarker("CreateShadowMap");

  PushDebugMarker("Setup");
  float shadow_map_resolution = static_cast<float>(
      world->config->rendering_config()->shadow_map_resolution());
  float shadow_map_zoom = world->config->rendering_config()->shadow_map_zoom();
  float shadow_map_offset =
      world->config->rendering_config()->shadow_map_offset();
  LightComponent *light_component =
      world->entity_manager.GetComponent<LightComponent>();

  const EntityRef &main_light_entity = light_component->begin()->entity;
  const TransformData *light_transform =
      world->entity_manager.GetComponentData<TransformData>(main_light_entity);
  vec3 light_position = light_transform->position;
  SetLightPosition(light_position);

  float viewport_angle =
      world->config->rendering_config()->shadow_map_viewport_angle() *
      kDegreesToRadians;
  light_camera_.set_viewport_angle(viewport_angle / shadow_map_zoom);
  light_camera_.set_viewport_resolution(
      vec2(shadow_map_resolution, shadow_map_resolution));
  vec3 light_camera_focus =
      camera.position() + camera.facing() * shadow_map_offset;
  light_camera_focus.z = 0;
  vec3 light_facing = light_camera_focus - light_camera_.position();
  light_camera_.set_facing(light_facing.Normalized());

  // Shadow map needs to be cleared to near-white, since that's
  // the maximum (furthest) depth.
  shadow_map_.SetAsRenderTarget();
  renderer.ClearFrameBuffer(kShadowMapClearColor);
  renderer.SetCulling(fplbase::kCullingModeBack);

  depth_shader_->Set(renderer);
  depth_skinned_shader_->Set(renderer);
  // Generate the shadow map:
  // TODO - modify this so that shadowcast is its own render pass
  PopDebugMarker(); // Setup

  for (int pass = 0; pass < corgi::RenderPass_Count; pass++) {
    PushDebugMarker("RenderPass");
    world->render_mesh_component.RenderPass(pass, light_camera_, renderer,
                                            ShaderIndex_Depth);
    PopDebugMarker();
  }

  fplbase::RenderTarget::ScreenRenderTarget(renderer).SetAsRenderTarget();
  PopDebugMarker(); // CreateShadowMap
}
bool EditorController::GetMouseWorldRay(const corgi::CameraInterface& camera,
                                        const vec2i& screen_size, vec3* near,
                                        vec3* far) const {
  float fov_y_tan = 2 * tan(camera.viewport_angle() * 0.5f);
  float fov_x_tan = fov_y_tan * camera.viewport_resolution().x() /
                    camera.viewport_resolution().y();

  vec2 pointer = vec2(fov_x_tan, -fov_y_tan) *
                 (GetPointer() / vec2(screen_size) - vec2(0.5f, 0.5f));
  // pointer goes from (-tan(FOVx)/2, tan(FOVy)/2) to (tan(FOVx)/2,
  // -tan(FOVy)/2) (upper right to lower left); 0,0 is center of screen.

  vec3 forward = camera.facing().Normalized();
  vec3 up = camera.up().Normalized();
  vec3 right = vec3::CrossProduct(forward, up).Normalized();
  up = vec3::CrossProduct(right, forward).Normalized();

  *near = camera.position();
  *far = camera.position() + forward + up * pointer.y() + right * pointer.x();

  return true;
}