Beispiel #1
0
//
// Main drawing function. Renders a completed scene to the screen.
//
void Renderer::drawScene(Scene& scene, Camera& camera)
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | 
      GL_STENCIL_BUFFER_BIT);

  // Load the viewing translations.
  resize(global.winWidth, global.winHeight);
  glLoadMatrix(camera.getWorldToCamMatrix());

  // Unlit scene + Depth Buffer info.
  ambientPass(scene, camera);

  // Only enter this loop if ambient only is not enabled.
  if (!global.drawAmbientOnly)
  {
    // The rest of the rendering is done on a 'per-light' basis, shadows are
    // determined for each light and the scene is additively illuminated.
  	for (int i = 0; i < scene.lights.size(); ++i)
  	{
  	  if (i >= global.maxVisibleLights) break;

  	  Light& light = scene.lights[i];
  	  
      // Setup the light for drawing and draw it.
      setupLight(light);
      if (global.drawPointLights)
        drawLight(light);

      // Determine shadows and light the scene.
      if (global.drawShadows)
      {
        determineShadows(scene.casters, light, camera);
      }
      
      // Iluminate the scene fro this light.
      illuminationPass(scene, camera);

	glClear(GL_STENCIL_BUFFER_BIT);
  	}
  	
    scene.dirtyAllCasters();
	}

	// Check for OpenGL errors.
	int er = glGetError();
	if (er) printf("%s\n", gluErrorString(er));
}
void MyView::
windowViewRender(std::shared_ptr<tygra::Window> window)
{
	assert(scene_ != nullptr);

	updateBuffers(); //update positions of meshes and light sources

	geometryPass(); //must be done first to write to g-buffer

	glUseProgram(shadowMap_program_);
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, shadowMap_fbo_);
	glClear(GL_DEPTH_BUFFER_BIT);
	glEnable(GL_DEPTH_TEST);

	GLuint lightPVLocation = glGetUniformLocation(shadowMap_program_, "lightPV");

	glViewport(0, 0, 1024, 1024);

	int i;
	for (i = 0; i < spotLightCount_; i++){
		//generate shadow maps for spot

		float far = scene_->getCamera().getFarPlaneDistance();
		float near = scene_->getCamera().getNearPlaneDistance();

		glm::vec3 lightPos = scene_->getAllSpotLights()[i].getPosition();
		glm::vec3 lightLookAt = scene_->getAllSpotLights()[i].getDirection() - lightPos;
		float fov = scene_->getAllSpotLights()[i].getConeAngleDegrees();
		glm::mat4 view_xform = glm::lookAt(lightPos, lightLookAt, upDir_);
		glm::mat4 projection = glm::perspective(fov, 1.0f, near, far);

		glm::mat4 lightPV = projection * view_xform;

		glUniformMatrix4fv(lightPVLocation, 1, GL_FALSE, glm::value_ptr(lightPV));

		glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, shadowMap_tex_, 0, i);

		Mesh mesh;
		for (int i = 0; i < meshes_.size(); i++)
		{
			mesh = meshes_[i];
			glBindVertexArray(mesh.vao);
			glDrawElementsInstanced(GL_TRIANGLES, mesh.element_count, GL_UNSIGNED_INT, 0, mesh.instanceIDs.size());
		}
	}

	glViewport(0, 0, width_, height_);

	ambientPass();
	directionalLightPass(); //must be done after ambient and before point, as the quad vao is bound in ambient pass

	//pointlightPass();

	glCullFace(GL_FRONT);

	//point lights

	//glEnable(GL_DEPTH_TEST);
	//glDepthFunc(GL_GREATER);

	//bind the sphere mesh, used by point and spot lights
	glBindVertexArray(light_sphere_mesh_.vao);
	spotlightPass(); //must be done after point lights, as the sphere vao is bound in point light pass

	//apply SSMAA to the image
	antiAliasingPasses();

	//blit l-buffer to screen
	glBindFramebuffer(GL_READ_FRAMEBUFFER, lbuffer_fbo_);
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
	glBlitFramebuffer(0, 0, width_, height_, 0, 0, width_, height_, GL_COLOR_BUFFER_BIT, GL_LINEAR);

}