Esempio n. 1
0
File: main.c Progetto: Adon-m/OpenGL
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;
}
Esempio n. 2
0
GLUSboolean update(GLUSfloat time)
{
	// Bias needed to convert the from [-1;1] to [0;1]
	GLfloat biasMatrix[16] = {0.5f, 0.0f, 0.0f, 0.0f,
								0.0f, 0.5f, 0.0f, 0.0f,
								0.0f, 0.0f, 0.5f, 0.0f,
								0.5f, 0.5f, 0.5f, 1.0f};
	// Frame buffer has another view port and so perspective projection. Needed for projected texturing of the mirror texture.
	GLfloat viewProjectionBiasTextureMatrix[16];

	// This matrix is used to flip the rendered object upside down.
	GLfloat scaleMatrix[16];

	// Store current width and height for later reseting.
	GLuint width = g_width;
	GLuint height = g_height;

	//
    // Upside down rendering to frame buffer.
	//
    glBindFramebuffer(GL_FRAMEBUFFER, g_fboMirrorTexture);

	reshape(TEXTURE_WIDTH, TEXTURE_HEIGHT);

    glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glusMatrix4x4Identityf(scaleMatrix);
    glusMatrix4x4Scalef(scaleMatrix, 1.0f, -1.0f, 1.0f);

    glFrontFace(GL_CW);

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

    glBindFramebuffer(GL_FRAMEBUFFER, 0);

    // Save the current projection matrix for later usage.

    glusMatrix4x4Copyf(viewProjectionBiasTextureMatrix, g_viewProjectionMatrix, GLUS_FALSE);

    glusMatrix4x4Multiplyf(viewProjectionBiasTextureMatrix, biasMatrix, viewProjectionBiasTextureMatrix);

    //
    // Scene rendering
    //

    reshape(width, height);

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Normal rendering
    glusMatrix4x4Identityf(scaleMatrix);

    glFrontFace(GL_CCW);

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

    glUseProgram(g_program.program);

    // This matrix is needed to calculate the vertices into the frame buffer render pass.
    glUniformMatrix4fv(g_viewProjectionBiasTextureMatrixLocation, 1, GL_FALSE, viewProjectionBiasTextureMatrix);

    glBindVertexArray(g_vao);

    glDrawElements(GL_TRIANGLES, g_numberIndicesSphere, GL_UNSIGNED_INT, 0);

    return GLUS_TRUE;
}
Esempio n. 3
0
GLUSboolean update(GLUSfloat time)
{
    static GLfloat angle = 0.0f;

    GLfloat modelViewMatrix[16];

    GLfloat viewMatrix[16];
    GLfloat shadowProjectionMatrix[16];
    GLfloat modelMatrix[16];
    GLfloat normalMatrix[9];

    // This shadow plane represents mathematically the background plane
    GLfloat shadowPlane[4] = {0.0f, 0.0f, 1.0f, 5.0f};

    glusMatrix4x4LookAtf(viewMatrix, g_cameraPosition[0], g_cameraPosition[1], g_cameraPosition[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    //
    // Render the scene.
    //

    glUseProgram(g_program.program);

    glUniformMatrix4fv(g_viewMatrixLocation, 1, GL_FALSE, viewMatrix);

    // Background Plane

    glusMatrix4x4Identityf(modelMatrix);
    glusMatrix4x4Translatef(modelMatrix, 0.0f, 0.0f, -5.0f);
    glusMatrix4x4Multiplyf(modelViewMatrix, viewMatrix, modelMatrix);
    glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix);

    glUniformMatrix4fv(g_modelMatrixLocation, 1, GL_FALSE, modelMatrix);
    glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix);
    glUniform4f(g_colorLocation, 0.0f, 0.5f, 0.0f, 1.0f);

    glBindVertexArray(g_vaoBackground);
    glDrawElements(GL_TRIANGLES, g_numberIndicesBackground, GL_UNSIGNED_INT, 0);

    //
    // Render the planar shadow
    //

    glUseProgram(g_programShadow.program);

    glUniformMatrix4fv(g_viewMatrixShadowLocation, 1, GL_FALSE, viewMatrix);

    // Torus projected as a shadow

    glusMatrix4x4PlanarShadowDirectionalLightf(shadowProjectionMatrix, shadowPlane, g_lightDirection);
    glUniformMatrix4fv(g_shadowProjectionMatrixShadowLocation, 1, GL_FALSE, shadowProjectionMatrix);

    glusMatrix4x4Identityf(modelMatrix);
    glusMatrix4x4RotateRzRxRyf(modelMatrix, 0.0f, 0.0f, angle);
    glUniformMatrix4fv(g_modelMatrixShadowLocation, 1, GL_FALSE, modelMatrix);

    glBindVertexArray(g_vaoShadow);

    // Overwrite the background plane
    glDisable(GL_DEPTH_TEST);

    glDrawElements(GL_TRIANGLES, g_numberIndicesTorus, GL_UNSIGNED_INT, 0);

    glEnable(GL_DEPTH_TEST);

    // Torus with color

    glUseProgram(g_program.program);

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

    glUniformMatrix4fv(g_modelMatrixLocation, 1, GL_FALSE, modelMatrix);
    glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix);
    glUniform4f(g_colorLocation, 0.33f, 0.0f, 0.5f, 1.0f);

    glBindVertexArray(g_vao);
    glDrawElements(GL_TRIANGLES, g_numberIndicesTorus, GL_UNSIGNED_INT, 0);

    //

    angle += 20.0f * time;

    return GLUS_TRUE;
}
Esempio n. 4
0
GLUSboolean update(GLUSfloat time)
{
    static GLfloat angle = 0.0f;

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

    // Rendering into the shadow texture.

    glBindTexture(GL_TEXTURE_2D, 0);

    // Setup for the framebuffer.
    glBindFramebuffer(GL_FRAMEBUFFER, g_fbo);
    glViewport(0, 0, g_shadowTextureSize, g_shadowTextureSize);
    glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);

    glusMatrix4x4LookAtf(viewMatrix, g_lightPosition[0], g_lightPosition[1], g_lightPosition[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

    glusMatrix4x4Multiplyf(shadowMatrix, g_shadowMatrix, viewMatrix);

    glClear(GL_DEPTH_BUFFER_BIT);

    glUseProgram(g_programShadow.program);

    // Render the torus.

    glusMatrix4x4Identityf(modelMatrix);
    glusMatrix4x4RotateRzRxRyf(modelMatrix, 0.0f, 0.0f, angle);
    glusMatrix4x4Multiplyf(modelViewMatrix, viewMatrix, modelMatrix);

    glUniformMatrix4fv(g_modelViewMatrixShadowLocation, 1, GL_FALSE, modelViewMatrix);

    glBindVertexArray(g_vaoShadow);

    glEnable(GL_POLYGON_OFFSET_FILL);
    glFrontFace(GL_CW);

    glDrawElements(GL_TRIANGLES, g_numberIndicesSphere, GL_UNSIGNED_INT, 0);

    glDisable(GL_POLYGON_OFFSET_FILL);
    glFrontFace(GL_CCW);

    // Revert for the scene.
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    glViewport(0, 0, g_width, g_height);

    glBindTexture(GL_TEXTURE_2D, g_shadowTexture);

    //

    // Render the scene.

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glUseProgram(g_program.program);

    glusMatrix4x4LookAtf(viewMatrix, g_cameraPosition[0], g_cameraPosition[1], g_cameraPosition[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

    glUniformMatrix4fv(g_viewMatrixLocation, 1, GL_FALSE, viewMatrix);
    glUniformMatrix4fv(g_shadowMatrixLocation, 1, GL_FALSE, shadowMatrix);

    // Plane
    glusMatrix4x4Identityf(modelMatrix);
    glusMatrix4x4Translatef(modelMatrix, 0.0f, 0.0f, -5.0f);
    glusMatrix4x4Multiplyf(modelViewMatrix, viewMatrix, modelMatrix);
    glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix);

    glUniformMatrix4fv(g_modelMatrixLocation, 1, GL_FALSE, modelMatrix);
    glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix);
    glUniform4f(g_colorLocation, 0.0f, 0.5f, 0.0f, 1.0f);

    glBindVertexArray(g_vaoBackground);
    glDrawElements(GL_TRIANGLES, g_numberIndicesBackground, GL_UNSIGNED_INT, 0);

    // Torus
    glusMatrix4x4Identityf(modelMatrix);
    glusMatrix4x4RotateRzRxRyf(modelMatrix, 0.0f, 0.0f, angle);
    glusMatrix4x4Multiplyf(modelViewMatrix, viewMatrix, modelMatrix);
    glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix);

    glUniformMatrix4fv(g_modelMatrixLocation, 1, GL_FALSE, modelMatrix);
    glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix);
    glUniform4f(g_colorLocation, 0.33f, 0.0f, 0.5f, 1.0f);

    glBindVertexArray(g_vao);
    glDrawElements(GL_TRIANGLES, g_numberIndicesSphere, GL_UNSIGNED_INT, 0);

    //

    angle += 20.0f * time;

    return GLUS_TRUE;
}
Esempio n. 5
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;
}
Esempio n. 6
0
GLUSboolean update(GLUSfloat time)
{
    static GLfloat angle = 0.0f;

    GLfloat angleRadians;

    GLfloat viewProjectionMatrix[16];
    GLfloat viewMatrix[16];
    GLfloat modelMatrix[16];
    GLfloat normalMatrix[9];

    GLfloat camera[4] = {0.0, 0.0, 0.0, 1.0};

    angleRadians = glusMathDegToRadf(angle);

    camera[0] = g_circleRadius * -sinf(angleRadians);
    camera[2] = g_circleRadius * cosf(angleRadians);

    // Circle with the camera around the origin by looking at it.
    glusMatrix4x4LookAtf(viewMatrix, camera[0], 0.0f, camera[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

    glusMatrix4x4Multiplyf(viewProjectionMatrix, g_projectionMatrix, viewMatrix);

    glusMatrix4x4Identityf(modelMatrix);

    glusMatrix4x4Translatef(modelMatrix, 0.0f, -0.5f, 0.0f);
    glusMatrix4x4RotateRxf(modelMatrix, 45.0f);

    glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelMatrix);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    //

    // First render the background.

    glUseProgram(g_programBackground.program);

    glUniformMatrix4fv(g_viewProjectionMatrixBackgroundLocation, 1, GL_FALSE, viewProjectionMatrix);

    glUniformMatrix4fv(g_modelMatrixBackgroundLocation, 1, GL_FALSE, modelMatrix);

    glBindVertexArray(g_vaoBackground);

    glFrontFace(GL_CW);

    glDrawElements(GL_TRIANGLES, g_numberIndicesBackground, GL_UNSIGNED_INT, 0);

    // Now render the sphere.

    glUseProgram(g_program.program);

    glUniformMatrix4fv(g_viewProjectionMatrixLocation, 1, GL_FALSE, viewProjectionMatrix);

    glUniformMatrix4fv(g_modelMatrixLocation, 1, GL_FALSE, modelMatrix);

    glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix);

    glUniform4fv(g_cameraLocation, 1, camera);

    glBindVertexArray(g_vao);

    glFrontFace(GL_CCW);

    glDrawElements(GL_TRIANGLES, g_numberIndicesSphere, GL_UNSIGNED_INT, 0);

    // Increase the angle 30 degree per second.
    angle += 30.0f * time;

    return GLUS_TRUE;
}
Esempio n. 7
0
File: main.c Progetto: AJ92/OpenGL
GLUSboolean update(GLUSfloat time)
{
    static GLfloat angle = 0.0f;

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

    // Rendering into the depth pass texture.

    glBindTexture(GL_TEXTURE_2D, 0);

    // Setup for the framebuffer.
    glBindFramebuffer(GL_FRAMEBUFFER, g_fbo);
    glViewport(0, 0, g_depthPassTextureSize, g_depthPassTextureSize);
    glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);

    glusMatrix4x4LookAtf(viewMatrix, g_lightPosition[0], g_lightPosition[1], g_lightPosition[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

    glusMatrix4x4Multiplyf(depthPassMatrix, g_depthPassMatrix, viewMatrix);

    glClear(GL_DEPTH_BUFFER_BIT);

    glUseProgram(g_programDepthPass.program);

    // Render the Dragon.

    glusMatrix4x4Identityf(modelMatrix);
    glusMatrix4x4RotateRzRxRyf(modelMatrix, 0.0f, 0.0f, angle);
    // Upscaling a little bit avoids artifacts.
    glusMatrix4x4Scalef(modelMatrix, 1.05f, 1.05f, 1.05f);
    glusMatrix4x4Multiplyf(modelViewMatrix, viewMatrix, modelMatrix);

    glUniformMatrix4fv(g_modelViewMatrixDepthPassLocation, 1, GL_FALSE, modelViewMatrix);

    glBindVertexArray(g_vaoDepthPass);

    glDrawArrays(GL_TRIANGLES, 0, g_numberVertices);

    // Revert for the scene.
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    glViewport(0, 0, g_width, g_height);

    glBindTexture(GL_TEXTURE_2D, g_depthPassTexture);

    //

    // Render the scene.

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glUseProgram(g_program.program);

    glusMatrix4x4LookAtf(viewMatrix, g_cameraPosition[0], g_cameraPosition[1], g_cameraPosition[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

    glUniformMatrix4fv(g_viewMatrixLocation, 1, GL_FALSE, viewMatrix);
    glUniformMatrix4fv(g_depthPassMatrixLocation, 1, GL_FALSE, depthPassMatrix);

    // Dragon
    glusMatrix4x4Identityf(modelMatrix);
    glusMatrix4x4RotateRzRxRyf(modelMatrix, 0.0f, 0.0f, angle);
    glusMatrix4x4Multiplyf(modelViewMatrix, viewMatrix, modelMatrix);
    glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix);

    glUniformMatrix4fv(g_modelMatrixLocation, 1, GL_FALSE, modelMatrix);
    glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix);
    glUniform4f(g_diffuseColorLocation, 0.8f, 0.0f, 0.0f, 1.0f);
    glUniform4f(g_scatterColorLocation, 0.8f, 0.8f, 0.0f, 1.0f);

    glUniform2f(g_nearFarLocation, g_near, g_far);
    glUniform1f(g_wrapLocation, g_wrap);
    glUniform1f(g_scatterWidthLocation, g_scatterWidth);
    glUniform1f(g_scatterFalloffLocation, g_scatterFalloff);

    glBindVertexArray(g_vao);
    glDrawArrays(GL_TRIANGLES, 0, g_numberVertices);

    //

    angle += 20.0f * time;

    return GLUS_TRUE;
}
Esempio n. 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;
}
Esempio n. 9
0
Matrix4x4& Matrix4x4::operator*=(const Matrix4x4& other)
{
	glusMatrix4x4Multiplyf(m, m, other.m);

	return *this;
}
Esempio n. 10
0
void Matrix4x4::multiply(const Matrix4x4& other)
{
	glusMatrix4x4Multiplyf(m, m, other.m);
}