예제 #1
0
void MyView::
windowViewDidReset(std::shared_ptr<tygra::Window> window,
int width,
int height)
{

	//anything that needs updated when the screen size changes

	glViewport(0, 0, width, height);

	width_ = width;
	height_ = height;

	upDir_ = scene_->getUpDirection();

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

	GLint viewport_size[4];
	glGetIntegerv(GL_VIEWPORT, viewport_size);
	aspect_ratio_ = viewport_size[2] / (float)viewport_size[3];
	projection_xform_ = glm::perspective(fov, aspect_ratio_, near, far);

	//deffered stuff
	//g-buffer
	glBindFramebuffer(GL_FRAMEBUFFER, gbuffer_fbo_);
	//position
	//store full-fat position 
	glBindTexture(GL_TEXTURE_RECTANGLE, gbuffer_position_tex_);
	glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGB32F, width, height, 0, GL_RGB, GL_FLOAT, 0);
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE, gbuffer_position_tex_, 0);

	//normal. Only store x and y, reconstruct z in the fragment shader
	glBindTexture(GL_TEXTURE_RECTANGLE, gbuffer_normal_tex_);
	glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RG32F, width, height, 0, GL_RG, GL_FLOAT, 0);
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_RECTANGLE, gbuffer_normal_tex_, 0);

	//mat colour. Store an index to an array of materials stored in a ubo
	glBindTexture(GL_TEXTURE_RECTANGLE, gbuffer_matColour_tex_);
	glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_R16F, width, height, 0, GL_RED, GL_FLOAT, 0);
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_RECTANGLE, gbuffer_matColour_tex_, 0);

	//total storage per texel = 96 + 64 + 16 bits = 22 bytes
	//full HD G-buffer sohuld use 45.6MB of GPU memory

	glBindRenderbuffer(GL_RENDERBUFFER, gbuffer_depth_rbo_);
	glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
	glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, gbuffer_depth_rbo_);

	GLuint framebuffer_status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
	if (framebuffer_status != GL_FRAMEBUFFER_COMPLETE) {
		tglDebugMessage(GL_DEBUG_SEVERITY_HIGH_ARB, "framebuffer not complete");
	}

	GLenum buffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 };
	glDrawBuffers(3, buffers);

	glBindFramebuffer(GL_FRAMEBUFFER, 0);

	//l-buffer

	GLenum modes[] = { GL_COLOR_ATTACHMENT0 };

	glBindFramebuffer(GL_FRAMEBUFFER, lbuffer_fbo_);
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, albedo_tex, 0);

	glDrawBuffers(1, modes);

	//glBindRenderbuffer(GL_RENDERBUFFER, lbuffer_colour_rbo_);
	//glRenderbufferStorage(GL_RENDERBUFFER, GL_RGB32F, width, height);
	//glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, lbuffer_colour_rbo_);
	glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, gbuffer_depth_rbo_);

	framebuffer_status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
	if (framebuffer_status != GL_FRAMEBUFFER_COMPLETE) {
		tglDebugMessage(GL_DEBUG_SEVERITY_HIGH_ARB, "framebuffer not complete");
	}

	glBindFramebuffer(GL_FRAMEBUFFER, 0);

	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_RECTANGLE, gbuffer_position_tex_);

	glActiveTexture(GL_TEXTURE1);
	glBindTexture(GL_TEXTURE_RECTANGLE, gbuffer_normal_tex_);

	glActiveTexture(GL_TEXTURE2);
	glBindTexture(GL_TEXTURE_RECTANGLE, gbuffer_matColour_tex_);


	for (int i = 1; i < POST_SHADER; i++){

		GLuint program = shaderPrograms_[i];

		glUseProgram(program);

		GLint posTex = glGetUniformLocation(program, "sampler_world_position");
		glUniform1i(posTex, 0);

		GLint normTex = glGetUniformLocation(program, "sampler_world_normal");
		glUniform1i(normTex, 1);

		GLint matColTex = glGetUniformLocation(program, "sampler_world_matColour");
		glUniform1i(matColTex, 2);
	}


	glActiveTexture(GL_TEXTURE3);
	glBindTexture(GL_TEXTURE_2D_ARRAY, shadowMap_tex_);
	GLuint program = shaderPrograms_[SPOT_LIGHT];
	glUseProgram(program);
	GLint smapLoc = glGetUniformLocation(program, "sampler_shadowMap");
	glUniform1i(smapLoc, 3);
}
예제 #2
0
void Window::
onError(int error_code,
        const char* description)
{
    tglDebugMessage(GL_DEBUG_SEVERITY_HIGH_ARB, description);
}