Exemple #1
0
GLUSboolean update(GLUSfloat time)
{
    static GLfloat angle = 0.0f;

    GLfloat modelViewMatrix[16];
    GLfloat normalMatrix[9];

    glusMatrix4x4Identityf(modelViewMatrix);

    glusMatrix4x4RotateRyf(modelViewMatrix, angle);

    glusMatrix4x4Multiplyf(modelViewMatrix, g_viewMatrix, modelViewMatrix);

    glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix);

    glUniformMatrix4fv(g_modelViewMatrixLocation, 1, GL_FALSE, modelViewMatrix);
    glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glDrawArrays(GL_TRIANGLES, 0, g_numberVertices);

    angle += 30.0f * time;

    return GLUS_TRUE;
}
Exemple #2
0
GLUSboolean updateWavefront(GLUSfloat time)
{
    static GLfloat angle = 0.0f;

    GLfloat modelViewMatrix[16];
    GLfloat normalMatrix[9];

    glusMatrix4x4Identityf(modelViewMatrix);
    glusMatrix4x4Translatef(modelViewMatrix, 0.0f, -1.5f, 0.0f);
    glusMatrix4x4RotateRyf(modelViewMatrix, angle);
    glusMatrix4x4Multiplyf(modelViewMatrix, g_viewMatrix, modelViewMatrix);

    glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix);

    glUseProgram(g_program.program);

    glUniformMatrix4fv(g_modelViewMatrixLocation, 1, GL_FALSE, modelViewMatrix);
    glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix);

    glBindVertexArray(g_vao);

    glDrawArrays(GL_TRIANGLES, 0, g_numberVertices);

    angle += 30.0f * time;

    return GLUS_TRUE;
}
Exemple #3
0
GLUSboolean update(GLUSfloat time)
{
    static GLfloat angle = 0.0f;

    static GLuint zero = 0;

    GLfloat modelViewMatrix[16];
    GLfloat normalMatrix[9];

    glusMatrix4x4Identityf(modelViewMatrix);

    glusMatrix4x4RotateRyf(modelViewMatrix, angle);

    glusMatrix4x4Multiplyf(modelViewMatrix, g_viewMatrix, modelViewMatrix);

    glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix);

    glUseProgram(g_program.program);
    glBindVertexArray(g_vao);

    glUniformMatrix4fv(g_modelViewMatrixLocation, 1, GL_FALSE, modelViewMatrix);
    glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix);

    //
    // Linked list rendering pass.
    //

    // Reset the atomic counter.
    glBufferSubData(GL_ATOMIC_COUNTER_BUFFER, 0, sizeof(GLuint), &zero);

    // Reset the head pointers by copying the clear buffer into the texture.
    glBindBuffer(GL_PIXEL_UNPACK_BUFFER, g_clearBuffer);

    glBindTexture(GL_TEXTURE_2D, g_headIndexTexture);
    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RED_INTEGER, GL_UNSIGNED_INT, 0);
    glBindTexture(GL_TEXTURE_2D, 0);

    glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);

    //

	glDrawArrays(GL_TRIANGLES, 0, g_numberVertices);

	//
    // Fullscreen quad rendering.
	//

    glUseProgram(g_blendFullscreenProgram.program);
    glBindVertexArray(g_blendFullscreenVAO);

	// Resolving and blending is happening in the shader.
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

    angle += 30.0f * time;

    return GLUS_TRUE;
}
Exemple #4
0
GLUSboolean update(GLUSfloat time)
{
	static GLfloat angle = 0.0f;

	static GLfloat erode = ERODE_START;

	GLfloat modelMatrix[16];
    GLfloat modelViewMatrix[16];
    GLfloat normalMatrix[9];

    glusMatrix4x4Identityf(modelMatrix);
    glusMatrix4x4RotateRyf(modelMatrix, angle);
    glusMatrix4x4Multiplyf(modelViewMatrix, g_viewMatrix, modelMatrix);

    glUniformMatrix4fv(g_modelViewMatrixLocation, 1, GL_FALSE, modelViewMatrix);

    // The calculations are done in camera / view space. So pass the view matrix, which is a rigid body transform.
    glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix);

    glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix);

    //

    glUniform1f(g_erodeLocation, erode);

    // Loop eroding
    if (erode < ERODE_END)
    {
    	erode = ERODE_START;
    }

    //

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glDrawElements(GL_TRIANGLES, g_numberIndicesSphere, GL_UNSIGNED_INT, 0);

    //

    angle += time * 45.0f;

    erode -= time * 0.1f;

    return GLUS_TRUE;
}
Exemple #5
0
GLUSboolean updateWavefront(GLUSfloat time, GLUSfloat scaleMatrix[16])
{
    static GLfloat angle = 0.0f;

    GLfloat modelViewMatrix[16];
    GLfloat normalMatrix[9];
    GLfloat lightMatrix[16];
    GLfloat lightDirection[3];

    // Note that the scale matrix is for flipping the model upside down.
    glusMatrix4x4Identityf(modelViewMatrix);
    glusMatrix4x4Multiplyf(modelViewMatrix, modelViewMatrix, scaleMatrix);
    glusMatrix4x4Translatef(modelViewMatrix, 0.0f, 1.25f, 0.0f);
    glusMatrix4x4RotateRyf(modelViewMatrix, angle);
    glusMatrix4x4Multiplyf(modelViewMatrix, g_viewMatrix, modelViewMatrix);

    glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix);

    // Transform light to camera space, as it is currently in world space. Also, flip light upside down depending on scale.
    glusMatrix4x4Multiplyf(lightMatrix, g_viewMatrix, scaleMatrix);
    glusMatrix4x4MultiplyVector3f(lightDirection, lightMatrix, g_light->direction);

    glUseProgram(g_program.program);

    glUniform3fv(g_lightLocations.directionLocation, 1, lightDirection);

    glUniformMatrix4fv(g_modelViewMatrixLocation, 1, GL_FALSE, modelViewMatrix);
    glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix);

    glBindVertexArray(g_vao);

    glDrawArrays(GL_TRIANGLES, 0, g_numberVertices);

    angle += 30.0f * time;

    return GLUS_TRUE;
}
Exemple #6
0
GLUSboolean update(GLUSfloat time)
{
	static GLfloat angle = 0.0f;

	GLfloat modelMatrix[16];
	GLfloat modelViewMatrix[16];
	GLfloat normalMatrix[9];

	GLfloat planeMatrix[16];

	GLfloat clippingPlane[4] = {0.0f, 0.0f, -1.0f, 0.0f};

	GLfloat planeBecauseOfBug[4] = {0.0f, -1.0f, 0.0f, 100.0f};

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    // Clipping plane

    glusMatrix4x4Identityf(planeMatrix);
    glusMatrix4x4RotateRyf(planeMatrix, angle);
    glusMatrix4x4Translatef(planeMatrix, 0.0f, 0.25, 0.25f);
    glusMatrix4x4RotateRxf(planeMatrix, -30.0f);

    // For planes, we do need the inverse, transposed matrix.
    glusMatrix4x4InverseRigidBodyf(planeMatrix);
    glusMatrix4x4Transposef(planeMatrix);

    glusMatrix4x4MultiplyPlanef(clippingPlane, planeMatrix, clippingPlane);

    glUniform4fv(g_planeLocation, 1, clippingPlane);

    // Sphere

    glusMatrix4x4Identityf(modelMatrix);
    glusMatrix4x4RotateRyf(modelMatrix, angle);

    glusMatrix4x4Multiplyf(modelViewMatrix, g_viewMatrix, modelMatrix);
    glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix);

    glUniformMatrix4fv(g_modelMatrixLocation, 1, GL_FALSE, modelMatrix);
    glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix);

    glBindVertexArray(g_vao);

    // Always pass. The 0s are not used.
    glStencilFunc(GL_ALWAYS, 0, 0);
    // Visible elements gets twice inverted, because of the front and back face. So the value is 0.
    // The clipped area only gets inverted by the back face. The front face is clipped, so no second invert is done. The final value is 255 (assume an 8bit stencil buffer).
    glStencilOp(GL_KEEP, GL_INVERT, GL_INVERT);

    glEnable(GL_CLIP_DISTANCE0);

    glDrawElements(GL_TRIANGLES, g_numberIndicesSphere, GL_UNSIGNED_INT, 0);

    // Plane

    // Use the model matrix from the sphere and add the plane transforms.
    glusMatrix4x4Translatef(modelMatrix, 0.0f, 0.25, 0.25f);
    glusMatrix4x4RotateRxf(modelMatrix, -30.0f);

    glusMatrix4x4Multiplyf(modelViewMatrix, g_viewMatrix, modelMatrix);
    glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix);

    glUniformMatrix4fv(g_modelMatrixLocation, 1, GL_FALSE, modelMatrix);
    glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix);

    glBindVertexArray(g_planeVAO);

    // Render only, where 255 & 1 = 1 is set.
    glStencilFunc(GL_EQUAL, 1, 1);

    glDisable(GL_CLIP_DISTANCE0);
    // Note: Bug in the AMD driver. The above disable is not working: "Values written into gl_ClipDistance for planes that are not enabled have no effect."
    //       For a workaround, I am setting a far plane, which does not clip anything.
    glUniform4fv(g_planeLocation, 1, planeBecauseOfBug);

    glDrawElements(GL_TRIANGLES, g_numberIndicesPlane, GL_UNSIGNED_INT, 0);

    angle += 45.0f * time;

    return GLUS_TRUE;
}
Exemple #7
0
GLUSboolean update(GLUSfloat time)
{
    static GLfloat angle = 0.0f;

    GLuint primitivesWritten;

    // Field of view

    GLfloat rotationMatrix[16];

    GLfloat positionTextureSpace[4];
    GLfloat directionTextureSpace[3];
    GLfloat leftNormalTextureSpace[3];
    GLfloat rightNormalTextureSpace[3];
    GLfloat backNormalTextureSpace[3];

    GLfloat xzPosition2D[4];

    //

    GLfloat tmvpMatrix[16];

    // Animation update

    g_personView.cameraPosition[0] = -cosf(2.0f * GLUS_PI * angle / TURN_DURATION) * TURN_RADIUS * METERS_TO_VIRTUAL_WORLD_SCALE;
    g_personView.cameraPosition[2] = -sinf(2.0f * GLUS_PI * angle / TURN_DURATION) * TURN_RADIUS * METERS_TO_VIRTUAL_WORLD_SCALE;

    g_personView.cameraDirection[0] = sinf(2.0f * GLUS_PI * angle / TURN_DURATION);
    g_personView.cameraDirection[2] = -cosf(2.0f * GLUS_PI * angle / TURN_DURATION);

    if (g_animationOn)
    {
        angle += time;
    }

    glusMatrix4x4LookAtf(g_viewMatrix, g_activeView->cameraPosition[0], g_activeView->cameraPosition[1], g_activeView->cameraPosition[2], g_activeView->cameraPosition[0] + g_activeView->cameraDirection[0], g_activeView->cameraPosition[1] + g_activeView->cameraDirection[1],
            g_activeView->cameraPosition[2] + g_activeView->cameraDirection[2], g_activeView->cameraUp[0], g_activeView->cameraUp[1], g_activeView->cameraUp[2]);

    glusMatrix4x4Identityf(tmvpMatrix);
    glusMatrix4x4Multiplyf(tmvpMatrix, tmvpMatrix, g_projectionMatrix);
    glusMatrix4x4Multiplyf(tmvpMatrix, tmvpMatrix, g_viewMatrix);
    glusMatrix4x4Multiplyf(tmvpMatrix, tmvpMatrix, g_textureToWorldMatrix);

    // Position

    xzPosition2D[0] = g_personView.cameraPosition[0];
    xzPosition2D[1] = 0.0f;
    xzPosition2D[2] = g_personView.cameraPosition[2];
    xzPosition2D[3] = g_personView.cameraPosition[3];

    glusMatrix4x4MultiplyPoint4f(positionTextureSpace, g_worldToTextureMatrix, xzPosition2D);

    // Direction

    glusMatrix4x4MultiplyVector3f(directionTextureSpace, g_worldToTextureMatrix, g_personView.cameraDirection);

    // Left normal of field of view

    glusMatrix4x4Identityf(rotationMatrix);
    glusMatrix4x4RotateRyf(rotationMatrix, g_personView.fov * (g_width / g_height) / 2.0f + 90.0f);
    glusMatrix4x4MultiplyVector3f(leftNormalTextureSpace, rotationMatrix, g_personView.cameraDirection);
    glusMatrix4x4MultiplyVector3f(leftNormalTextureSpace, g_worldToTextureNormalMatrix, leftNormalTextureSpace);

    // Right normal of field of view

    glusMatrix4x4Identityf(rotationMatrix);
    glusMatrix4x4RotateRyf(rotationMatrix, -g_personView.fov * (g_width / g_height) / 2.0f - 90.0f);
    glusMatrix4x4MultiplyVector3f(rightNormalTextureSpace, rotationMatrix, g_personView.cameraDirection);
    glusMatrix4x4MultiplyVector3f(rightNormalTextureSpace, g_worldToTextureNormalMatrix, rightNormalTextureSpace);

    // Back normal of field of view

    glusMatrix4x4Identityf(rotationMatrix);
    glusMatrix4x4RotateRyf(rotationMatrix, 180.0f);
    glusMatrix4x4MultiplyVector3f(backNormalTextureSpace, rotationMatrix, g_personView.cameraDirection);
    glusMatrix4x4MultiplyVector3f(backNormalTextureSpace, g_worldToTextureNormalMatrix, backNormalTextureSpace);

    // OpenGL stuff

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Pass one.

    // Disable any rasterization
    glEnable(GL_RASTERIZER_DISCARD);

    glUseProgram(g_programPassOne.program);

    glUniform4fv(g_positionTextureSpacePassOneLocation, 1, positionTextureSpace);
    glUniform3fv(g_leftNormalTextureSpacePassOneLocation, 1, leftNormalTextureSpace);
    glUniform3fv(g_rightNormalTextureSpacePassOneLocation, 1, rightNormalTextureSpace);
    glUniform3fv(g_backNormalTextureSpacePassOneLocation, 1, backNormalTextureSpace);

    glBindVertexArray(g_vaoPassOne);

    // Bind to vertices used in render pass two. To this buffer is written.
    glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, g_verticesPassTwoVBO);

    // We need to know, how many primitives are written. So start the query.
    glBeginQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN, g_transformFeedbackQuery);

    // Start the operation ...
    glBeginTransformFeedback(GL_POINTS);

    // ... render the elements ...
    glDrawElements(GL_POINTS, g_sNumPoints * g_tNumPoints, GL_UNSIGNED_INT, 0);

    // ... and stop the operation.
    glEndTransformFeedback();

    // Now, we can also stop the query.
    glEndQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN);

    glDisable(GL_RASTERIZER_DISCARD);

    glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);

    glBindVertexArray(0);

    // Pass two

    glUseProgram(g_shaderProgramPassTwo.program);

    glUniformMatrix4fv(g_tmvpPassTwoLocation, 1, GL_FALSE, tmvpMatrix);
    glUniform4fv(g_positionTextureSpacePassTwoLocation, 1, positionTextureSpace);

    glBindVertexArray(g_vaoPassTwo);

    // Now get the number of primitives written in the first render pass.
    glGetQueryObjectuiv(g_transformFeedbackQuery, GL_QUERY_RESULT, &primitivesWritten);

    // No draw the final terrain.
    glDrawArrays(GL_PATCHES, 0, primitivesWritten);

    return GLUS_TRUE;
}
Exemple #8
0
GLUSboolean update(GLUSfloat time)
{
    static GLfloat angle = 0.0f;

    GLint peelLayer, defaultDepth, peelDepth;

    GLfloat modelViewMatrix[16];
    GLfloat normalMatrix[9];

    glusMatrix4x4Identityf(modelViewMatrix);

    glusMatrix4x4RotateRyf(modelViewMatrix, angle);

    glusMatrix4x4Multiplyf(modelViewMatrix, g_viewMatrix, modelViewMatrix);

    glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix);

    glUseProgram(g_program.program);
    glBindVertexArray(g_vao);

    glUniformMatrix4fv(g_modelViewMatrixLocation, 1, GL_FALSE, modelViewMatrix);
    glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix);

    // Depth peeling passes.
	// see http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.18.9286&rep=rep1&type=pdf

    // Disable color texture ...
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D_ARRAY, 0);

	for (peelLayer = 0; peelLayer < LAYERS; peelLayer++)
	{
	    glUniform1i(g_layerLocation, peelLayer);

		peelDepth = peelLayer % 2;
		defaultDepth = (peelLayer + 1) % 2;

		// ... and activate the peeling depth texture.
		glActiveTexture(GL_TEXTURE1);
		glBindTexture(GL_TEXTURE_2D, g_depthTexture[peelDepth]);

		glBindFramebuffer(GL_FRAMEBUFFER, g_blendFullscreenFBO);

		// Now color buffer ...
		glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, g_colorTexture, 0, peelLayer);

		// ... and default depth buffer can be used as the frame buffer.
		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, g_depthTexture[defaultDepth], 0);

		if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
		{
			printf("GL_FRAMEBUFFER_COMPLETE error 0x%x", glCheckFramebufferStatus(GL_FRAMEBUFFER));

			return GLUS_FALSE;
		}

		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		glDrawArrays(GL_TRIANGLES, 0, g_numberVertices);
	}

    // Fullscreen quad rendering.

	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D_ARRAY, g_colorTexture);
	glActiveTexture(GL_TEXTURE1);
	glBindTexture(GL_TEXTURE_2D, 0);

    glBindFramebuffer(GL_FRAMEBUFFER, 0);

    glUseProgram(g_blendFullscreenProgram.program);
    glBindVertexArray(g_blendFullscreenVAO);

	glDisable(GL_DEPTH_TEST);

	// Blending is happening in the shader.
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

	glEnable(GL_DEPTH_TEST);

    angle += 30.0f * time;

    return GLUS_TRUE;
}
Exemple #9
0
GLUSboolean update(GLUSfloat time)
{
	GLenum drawBuffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 };

	GLfloat modelMatrix[16];

	GLint i;

	//
	// Rendering scene to frame buffer.
	//

	glBindFramebuffer(GL_FRAMEBUFFER, g_ssaoFBO);
	glDrawBuffers(2, drawBuffers);

	reshape(TEXTURE_WIDTH, TEXTURE_HEIGHT);

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	for (i = 0; i < 3; i++)
	{
		glusMatrix4x4Identityf(modelMatrix);
		glusMatrix4x4Translatef(modelMatrix, (GLfloat)(i - 1) * 4.1f, 0.0f, 0.0f);
		glusMatrix4x4RotateRyf(modelMatrix, 45.0f);

		if (!updateWavefront(time, modelMatrix))
		{
			return GLUS_FALSE;
		}
	}

	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, g_texture);

	glUseProgram(g_program.program);

	glBindVertexArray(g_vao);

	glDrawElements(GL_TRIANGLES, g_numberIndicesPlane, GL_UNSIGNED_INT, 0);

	//
	// SSAO rendering step.
	//

	glBindFramebuffer(GL_FRAMEBUFFER, g_blurFBO);

	glDrawBuffer(GL_COLOR_ATTACHMENT0);

	glClear(GL_COLOR_BUFFER_BIT);

	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, g_ssaoTexture);
	glActiveTexture(GL_TEXTURE1);
	glBindTexture(GL_TEXTURE_2D, g_ssaoNormalTexture);
	glActiveTexture(GL_TEXTURE2);
	glBindTexture(GL_TEXTURE_2D, g_ssaoDepthTexture);
	glActiveTexture(GL_TEXTURE3);
	glBindTexture(GL_TEXTURE_2D, g_ssaoRotationNoiseTexture);
	glActiveTexture(GL_TEXTURE0);

	glUseProgram(g_ssaoProgram.program);

	glBindVertexArray(g_ssaoVAO);

	glDrawElements(GL_TRIANGLES, g_numberIndicesPostprocessPlane, GL_UNSIGNED_INT, 0);

	glBindTexture(GL_TEXTURE_2D, 0);

	//
	// Blur rendering step.
	//

	glBindFramebuffer(GL_FRAMEBUFFER, 0);

	glDrawBuffer(GL_COLOR_ATTACHMENT0);

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, g_ssaoTexture);
	glActiveTexture(GL_TEXTURE1);
	glBindTexture(GL_TEXTURE_2D, g_blurTexture);
	glActiveTexture(GL_TEXTURE0);

	glUseProgram(g_blurProgram.program);

	glBindVertexArray(g_blurVAO);

	glDrawElements(GL_TRIANGLES, g_numberIndicesPostprocessPlane, GL_UNSIGNED_INT, 0);

	glBindTexture(GL_TEXTURE_2D, 0);

	return GLUS_TRUE;
}