Beispiel #1
0
// 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));
}
Beispiel #2
0
// 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);
    }
  }
}