Example #1
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;
}
Example #2
0
GLUSvoid reshape(GLUSint width, GLUSint height)
{
    GLfloat modelMatrix[16];
    GLfloat modelViewMatrix[16];
    GLfloat normalMatrix[9];
    GLfloat lightDirection[3];

    g_width = width;
    g_height = height;

    reshapeWavefront(width, height);

    //

    glViewport(0, 0, width, height);

    glusPerspectivef(g_viewProjectionMatrix, 40.0f, (GLfloat) width / (GLfloat) height, 1.0f, 100.0f);

    glusMatrix4x4Multiplyf(g_viewProjectionMatrix, g_viewProjectionMatrix, g_viewMatrix);

    glUseProgram(g_program.program);

    glUniformMatrix4fv(g_viewProjectionMatrixLocation, 1, GL_FALSE, g_viewProjectionMatrix);

    glusMatrix4x4Identityf(modelMatrix);
    glusMatrix4x4Translatef(modelMatrix, 0.0f, 0.0f, 3.0f);
    glusMatrix4x4RotateRxf(modelMatrix, -90.0f);
    glUniformMatrix4fv(g_modelMatrixLocation, 1, GL_FALSE, modelMatrix);

    // Calculation is in camera space
    glusMatrix4x4Multiplyf(modelViewMatrix, g_viewMatrix, modelMatrix);
    glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix);
    glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix);

    glusMatrix4x4MultiplyVector3f(lightDirection, g_viewMatrix, g_light.direction);
    // Direction already normalized
    glUniform3fv(g_lightDirectionLocation, 1, lightDirection);
}
Example #3
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;
}
Example #4
0
void Matrix4x4::translate(float x, float y, float z)
{
	glusMatrix4x4Translatef(m, x, y, z);
}
Example #5
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 = glusDegToRadf(angle);

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

    // Circle with the camera around the origin by looking at it.
    glusLookAtf(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;
}
Example #6
0
File: main.c Project: Adon-m/OpenGL
GLUSboolean update(GLUSfloat time)
{
    static GLfloat angle = 0.0f;

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

    // Render the scene.

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    glUseProgram(g_program.program);

    glusLookAtf(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);

    // Draw Color

    glEnable(GL_CULL_FACE);
    glEnable(GL_DEPTH_TEST);

    // 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_vaoPlane);
    glDrawElements(GL_TRIANGLES, g_numberIndicesPlane, 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_ADJACENCY, g_numberIndices, GL_UNSIGNED_INT, 0);

    // Draw Shadow Volume

    // Using zfail see http://joshbeam.com/articles/stenciled_shadow_volumes_in_opengl/
    glEnable(GL_STENCIL_TEST);

    glUseProgram(g_programShadowVolume.program);

    glUniformMatrix4fv(g_viewMatrixShadowVolumeLocation, 1, GL_FALSE, viewMatrix);
    glUniformMatrix4fv(g_modelMatrixShadowVolumeLocation, 1, GL_FALSE, modelMatrix);

    // Only render to the stencil buffer
    glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
    glDepthMask(GL_FALSE);
    // Avoid ugly artifacts
	glEnable(GL_POLYGON_OFFSET_FILL);
	// Needed, as vertices in the back are extruded to infinity
	glEnable(GL_DEPTH_CLAMP);

    glBindVertexArray(g_vaoShadowVolume);

    // Render the back faces ...
	glCullFace(GL_FRONT);
	glStencilFunc(GL_ALWAYS, 0x0, 0xff);
	glStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
    glDrawElements(GL_TRIANGLES_ADJACENCY, g_numberIndices, GL_UNSIGNED_INT, 0);

    // ... and then the front faces
    glCullFace(GL_BACK);
	glStencilFunc(GL_ALWAYS, 0x0, 0xff);
	glStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
    glDrawElements(GL_TRIANGLES_ADJACENCY, g_numberIndices, GL_UNSIGNED_INT, 0);

    // Reset
    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    glDepthMask(GL_TRUE);
	glDisable(GL_POLYGON_OFFSET_FILL);
	glDisable(GL_DEPTH_CLAMP);

    // Draw shadow by blending a black, half transparent plane
    glUseProgram(g_programShadowPlane.program);

    glDisable(GL_DEPTH_TEST);
    glEnable(GL_BLEND);

    // Only render, where the stencil buffer is not 0
    glStencilFunc(GL_NOTEQUAL, 0x0, 0xff);
    glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);

    glBindVertexArray(g_vaoShadowPlane);
    glDrawElements(GL_TRIANGLES, g_numberIndicesShadowPlane, GL_UNSIGNED_INT, 0);

    glDisable(GL_BLEND);
    glDisable(GL_STENCIL_TEST);

    //

    angle += 20.0f * time;

    return GLUS_TRUE;
}
Example #7
0
File: main.c Project: AJ92/OpenGL
GLUSboolean init(GLUSvoid)
{
    GLfloat lightDirection[3] = { 1.0f, 1.0f, 1.0f };
    GLfloat color[4] = { 0.0f, 1.0f, 1.0f, 1.0f };

    GLUStextfile computeSource;

    GLUStextfile vertexSource;
    GLUStextfile fragmentSource;

    GLint i;
    GLfloat matrix[16];

    GLfloat* normals;

    GLfloat distanceRest;
    GLfloat distanceDiagonalRest;

    GLfloat sphereCenter[4] = {0.0f, 0.0f, -0.01f, 1.0f};
    GLfloat sphereRadius = 1.0f;

    glusFileLoadText("../Example40/shader/cloth.comp.glsl", &computeSource);

    glusProgramBuildComputeFromSource(&g_computeProgram, (const GLchar**) &computeSource.text);

    glusFileDestroyText(&computeSource);


    glusFileLoadText("../Example40/shader/cloth.vert.glsl", &vertexSource);
    glusFileLoadText("../Example40/shader/cloth.frag.glsl", &fragmentSource);

    glusProgramBuildFromSource(&g_program, (const GLUSchar**) &vertexSource.text, 0, 0, 0, (const GLUSchar**) &fragmentSource.text);

    glusFileDestroyText(&vertexSource);
    glusFileDestroyText(&fragmentSource);

    //

    g_verticesPerRowLocation = glGetUniformLocation(g_computeProgram.program, "u_verticesPerRow");
    g_deltaTimeLocation = glGetUniformLocation(g_computeProgram.program, "u_deltaTime");
    g_distanceRestLocation = glGetUniformLocation(g_computeProgram.program, "u_distanceRest");
    g_distanceDiagonalRestLocation = glGetUniformLocation(g_computeProgram.program, "u_distanceDiagonalRest");
    g_sphereCenterLocation = glGetUniformLocation(g_computeProgram.program, "u_sphereCenter");
    g_sphereRadiusLocation = glGetUniformLocation(g_computeProgram.program, "u_sphereRadius");


    g_modelViewProjectionMatrixLocation = glGetUniformLocation(g_program.program, "u_modelViewProjectionMatrix");
    g_normalMatrixLocation = glGetUniformLocation(g_program.program, "u_normalMatrix");
    g_lightDirectionLocation = glGetUniformLocation(g_program.program, "u_lightDirection");
    g_colorLocation = glGetUniformLocation(g_program.program, "u_color");

	g_vertexLocation = glGetAttribLocation(g_program.program, "a_vertex");
	g_normalLocation = glGetAttribLocation(g_program.program, "a_normal");

    //

    // Use a helper function to create a grid plane.
    glusShapeCreateRectangularGridPlanef(&g_gridPlane, 2.0f, 2.0f, ROWS, ROWS, GLUS_FALSE);

    // Use x, as only horizontal and vertical springs are used. Adapt this, if diagonal or a non square grid is used.
    distanceRest = g_gridPlane.vertices[4] - g_gridPlane.vertices[0];
    distanceDiagonalRest = sqrtf(2.0f * distanceRest * distanceRest);

    // Rotate by 90 degrees, that the grid is in the x-z-plane.
    glusMatrix4x4Identityf(matrix);
    glusMatrix4x4Translatef(matrix, 0.0f, 1.1f, 0.0f);
    glusMatrix4x4RotateRxf(matrix, -90.0f);
    for (i = 0; i < g_gridPlane.numberVertices; i++)
    {
    	glusMatrix4x4MultiplyPoint4f(&g_gridPlane.vertices[4 * i], matrix, &g_gridPlane.vertices[4 * i]);
    }

    g_numberIndicesPlane = g_gridPlane.numberIndices;

    glGenBuffers(1, &g_indicesVBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, g_gridPlane.numberIndices * sizeof(GLuint), (GLuint*) g_gridPlane.indices, GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    //

    normals = (GLfloat*)malloc(g_gridPlane.numberVertices * 4 * sizeof(GLfloat));

    // Add one more GLfloat channel as padding for std430 layout.
    glusPaddingConvertf(normals, g_gridPlane.normals, 3, 1, g_gridPlane.numberVertices);

    free(g_gridPlane.normals);
    g_gridPlane.normals = normals;

    //

	glGenBuffers(3, g_verticesBuffer);

	glBindBuffer(GL_SHADER_STORAGE_BUFFER, g_verticesBuffer[0]);
	glBufferData(GL_SHADER_STORAGE_BUFFER, g_gridPlane.numberVertices * 4 * sizeof(GLfloat), g_gridPlane.vertices, GL_DYNAMIC_DRAW);

	glBindBuffer(GL_SHADER_STORAGE_BUFFER, g_verticesBuffer[1]);
	glBufferData(GL_SHADER_STORAGE_BUFFER, g_gridPlane.numberVertices * 4 * sizeof(GLfloat), g_gridPlane.vertices, GL_DYNAMIC_DRAW);

	glBindBuffer(GL_SHADER_STORAGE_BUFFER, g_verticesBuffer[2]);
	glBufferData(GL_SHADER_STORAGE_BUFFER, g_gridPlane.numberVertices * 4 * sizeof(GLfloat), 0, GL_DYNAMIC_DRAW);

	glGenBuffers(1, &g_normalsBuffer);

	glBindBuffer(GL_SHADER_STORAGE_BUFFER, g_normalsBuffer);
	glBufferData(GL_SHADER_STORAGE_BUFFER, g_gridPlane.numberVertices * 4 * sizeof(GLfloat), g_gridPlane.normals, GL_DYNAMIC_DRAW);

    //

    glUseProgram(g_program.program);

    glGenVertexArrays(1, &g_vao);
    glBindVertexArray(g_vao);

	glBindBuffer(GL_ARRAY_BUFFER, g_normalsBuffer);
	glVertexAttribPointer(g_normalLocation, 4, GL_FLOAT, GL_FALSE, 0, 0);
	glEnableVertexAttribArray(g_normalLocation);

	glBindBuffer(GL_ARRAY_BUFFER, g_verticesBuffer[2]);
	glVertexAttribPointer(g_vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0);
	glEnableVertexAttribArray(g_vertexLocation);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO);

    glBindVertexArray(0);

    //

    glusVector3Normalizef(lightDirection);
    glUniform3fv(g_lightDirectionLocation, 1, lightDirection);

    glUniform4fv(g_colorLocation, 1, color);

    glUseProgram(0);

    //

    glUseProgram(g_computeProgram.program);

    glUniform1i(g_verticesPerRowLocation, ROWS + 1);

    glUniform1f(g_distanceRestLocation, distanceRest);
    glUniform1f(g_distanceDiagonalRestLocation, distanceDiagonalRest);

    glUniform4fv(g_sphereCenterLocation, 1, sphereCenter);
    glUniform1f(g_sphereRadiusLocation, sphereRadius);

    glUseProgram(0);

    //

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    glClearDepth(1.0f);

    glEnable(GL_DEPTH_TEST);

    //

    if (!initSphere(sphereCenter, sphereRadius, lightDirection))
    {
    	return GLUS_FALSE;
    }

    return GLUS_TRUE;
}
Example #8
0
File: main.c Project: 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;
}
Example #9
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;
}
Example #10
0
GLUSboolean init(GLUSvoid)
{
    GLfloat textureToWorldNormalMatrix[16];

    // The maximum detail level which is 2^s = sMapExtend.
    GLuint sMaxDetailLevel;

    // The maximum detail level which is 2^t = tMapExtend.
    GLuint tMaxDetailLevel;

    // The overall maximum detail level from s and t.
    GLuint overallMaxDetailLevel;

    // Step for s and t direction.
    GLfloat detailStep;

    GLuint s, t;

    GLUStgaimage image;

    GLfloat* map = 0;

    GLuint* indices = 0;

    GLUStextfile vertexSource;
    GLUStextfile controlSource;
    GLUStextfile evaluationSource;
    GLUStextfile geometrySource;
    GLUStextfile fragmentSource;

    GLfloat lightDirection[3] = { 1.0f, 1.0f, 1.0f };

    glusVector3Normalizef(lightDirection);

    g_topView.cameraPosition[0] = 0.0f;
    g_topView.cameraPosition[1] = 30000.0f * METERS_TO_VIRTUAL_WORLD_SCALE;
    g_topView.cameraPosition[2] = 0.0f;
    g_topView.cameraPosition[3] = 1.0;
    g_topView.cameraDirection[0] = 0.0f;
    g_topView.cameraDirection[1] = -1.0f;
    g_topView.cameraDirection[2] = 0.0f;
    g_topView.cameraUp[0] = 0.0f;
    g_topView.cameraUp[1] = 0.0f;
    g_topView.cameraUp[2] = -1.0f;
    g_topView.fov = 40.0f;

    g_personView.cameraPosition[0] = 0.0f;
    g_personView.cameraPosition[1] = 4700.0f * METERS_TO_VIRTUAL_WORLD_SCALE;
    g_personView.cameraPosition[2] = 0.0f;
    g_personView.cameraPosition[3] = 1.0;
    g_personView.cameraDirection[0] = 0.0f;
    g_personView.cameraDirection[1] = 0.0f;
    g_personView.cameraDirection[2] = -1.0f;
    g_personView.cameraUp[0] = 0.0f;
    g_personView.cameraUp[1] = 1.0f;
    g_personView.cameraUp[2] = 0.0f;
    g_personView.fov = 60.0f;

    g_activeView = &g_personView;

    if (!glusImageLoadTga(NORMAL_MAP, &image))
    {
        printf("Could not load normal picture '%s'!\n", NORMAL_MAP);

        return GLUS_FALSE;
    }

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    glGenTextures(1, &g_normalMapTexture);

    glBindTexture(GL_TEXTURE_RECTANGLE, g_normalMapTexture);

    glTexImage2D(GL_TEXTURE_RECTANGLE, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data);

    glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glusImageDestroyTga(&image);

    if (!glusImageLoadTga(HEIGHT_MAP, &image))
    {
        printf("Could not load height picture '%s'!\n", HEIGHT_MAP);

        return GLUS_FALSE;
    }

    glGenTextures(1, &g_heightMapTexture);

    glBindTexture(GL_TEXTURE_RECTANGLE, g_heightMapTexture);

    glTexImage2D(GL_TEXTURE_RECTANGLE, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data);

    glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    g_sMapExtend = (GLfloat) image.width;

    g_tMapExtend = (GLfloat) image.height;

    glusImageDestroyTga(&image);

    // Calculate the detail level for the s and ...
    sMaxDetailLevel = (GLuint) floorf(logf(g_sMapExtend) / logf(2.0f));

    // ... t extend
    tMaxDetailLevel = (GLuint) floorf(logf(g_tMapExtend) / logf(2.0f));

    overallMaxDetailLevel = glusMathMinf(sMaxDetailLevel, tMaxDetailLevel);

    // Do checking of calculated parameters
    if (MINIMUM_DETAIL_LEVEL > overallMaxDetailLevel)
    {
        printf("Detail level to high %d > %d\n", MINIMUM_DETAIL_LEVEL, overallMaxDetailLevel);

        return GLUS_FALSE;
    }

    if (MINIMUM_DETAIL_LEVEL + DETAIL_LEVEL_FIRST_PASS > overallMaxDetailLevel)
    {
        printf("First pass detail level to high %d > %d\n", MINIMUM_DETAIL_LEVEL + DETAIL_LEVEL_FIRST_PASS, overallMaxDetailLevel);

        return GLUS_FALSE;
    }

    if (powf(2.0f, overallMaxDetailLevel - (MINIMUM_DETAIL_LEVEL + DETAIL_LEVEL_FIRST_PASS)) > 32.0f)
    {
        printf("Tessellation level to high %d > 32\n", (GLint) powf(2.0f, overallMaxDetailLevel - (MINIMUM_DETAIL_LEVEL + DETAIL_LEVEL_FIRST_PASS)));

        return GLUS_FALSE;
    }

    detailStep = powf(2.0f, overallMaxDetailLevel - MINIMUM_DETAIL_LEVEL);

    g_sNumPoints = (GLuint) ceilf(g_sMapExtend / detailStep) - 1;

    g_tNumPoints = (GLuint) ceilf(g_tMapExtend / detailStep) - 1;

    //
    // Generate the flat terrain mesh.
    //

    map = (GLUSfloat*) malloc(g_sNumPoints * g_tNumPoints * 2 * sizeof(GLfloat));

    indices = (GLuint*) malloc(g_sNumPoints * g_tNumPoints * sizeof(GLuint));

    for (t = 0; t < g_tNumPoints; t++)
    {
        for (s = 0; s < g_sNumPoints; s++)
        {
            map[t * g_sNumPoints * 2 + s * 2 + 0] = 0.5f + detailStep / 2.0f + (GLfloat) s * detailStep;
            map[t * g_sNumPoints * 2 + s * 2 + 1] = 0.5f + detailStep / 2.0f + (GLfloat) t * detailStep;

            indices[t * g_sNumPoints + s + 0] = (t + 0) * g_sNumPoints + s + 0;
        }
    }

    //
    // Transferring vertices and indices into GPU
    //

    // Pass one

    glGenBuffers(1, &g_verticesPassOneVBO);
    glBindBuffer(GL_ARRAY_BUFFER, g_verticesPassOneVBO);
    glBufferData(GL_ARRAY_BUFFER, g_sNumPoints * g_tNumPoints * 2 * sizeof(GLfloat), map, GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glGenBuffers(1, &g_indicesPassOneVBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesPassOneVBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, g_sNumPoints * g_tNumPoints * sizeof(GLuint), indices, GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    // Pass two.

    glGenBuffers(1, &g_verticesPassTwoVBO);
    glBindBuffer(GL_ARRAY_BUFFER, g_verticesPassTwoVBO);
    // Calculate enough space!
    glBufferData(GL_ARRAY_BUFFER, g_sNumPoints * g_tNumPoints * (GLuint) pow(4, DETAIL_LEVEL_FIRST_PASS + 1) * 2 * sizeof(GLfloat), 0, GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    //

    free(map);
    map = 0;

    free(indices);
    indices = 0;

    //

    if (!glusImageLoadTga(COLOR_MAP, &image))
    {
        printf("Could not load color picture '%s'!\n", COLOR_MAP);

        return GLUS_FALSE;
    }

    glGenTextures(1, &g_colorMapTexture);

    glBindTexture(GL_TEXTURE_2D, g_colorMapTexture);

    glTexImage2D(GL_TEXTURE_2D, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glusImageDestroyTga(&image);

    //

    glGenQueries(1, &g_transformFeedbackQuery);

    //
    // Creating the shader program.
    //

    // Pass one.

    glusFileLoadText("../Example14/shader/PassOne.vert.glsl", &vertexSource);
    glusFileLoadText("../Example14/shader/PassOne.geom.glsl", &geometrySource);
    glusFileLoadText("../Example14/shader/PassOne.frag.glsl", &fragmentSource);

    // Compile and ...
    glusProgramCreateFromSource(&g_programPassOne, (const GLUSchar**) &vertexSource.text, 0, 0, (const GLUSchar**) &geometrySource.text, (const GLUSchar**) &fragmentSource.text);

    // ... add the transform variable ...
    glTransformFeedbackVaryings(g_programPassOne.program, 1, (const GLchar**) &TRANSFORM_VARYING, GL_SEPARATE_ATTRIBS);

    // ... and link the program
    if (!glusProgramLink(&g_programPassOne))
    {
        printf("Could not build program one\n");

        return GLUS_FALSE;
    }

    // Destroy the text resource
    glusFileDestroyText(&vertexSource);
    glusFileDestroyText(&geometrySource);
    glusFileDestroyText(&fragmentSource);

    g_halfDetailStepPassOneLocation = glGetUniformLocation(g_programPassOne.program, "u_halfDetailStep");

    g_detailLevelPassOneLocation = glGetUniformLocation(g_programPassOne.program, "u_detailLevel");

    g_fovRadiusPassOneLocation = glGetUniformLocation(g_programPassOne.program, "u_fovRadius");

    g_positionTextureSpacePassOneLocation = glGetUniformLocation(g_programPassOne.program, "u_positionTextureSpace");
    g_leftNormalTextureSpacePassOneLocation = glGetUniformLocation(g_programPassOne.program, "u_leftNormalTextureSpace");
    g_rightNormalTextureSpacePassOneLocation = glGetUniformLocation(g_programPassOne.program, "u_rightNormalTextureSpace");
    g_backNormalTextureSpacePassOneLocation = glGetUniformLocation(g_programPassOne.program, "u_backNormalTextureSpace");

    // Pass two.

    glusFileLoadText("../Example14/shader/PassTwo.vert.glsl", &vertexSource);
    glusFileLoadText("../Example14/shader/PassTwo.cont.glsl", &controlSource);
    glusFileLoadText("../Example14/shader/PassTwo.eval.glsl", &evaluationSource);
    glusFileLoadText("../Example14/shader/PassTwo.geom.glsl", &geometrySource);
    glusFileLoadText("../Example14/shader/PassTwo.frag.glsl", &fragmentSource);

    if (!glusProgramBuildFromSource(&g_shaderProgramPassTwo, (const GLUSchar**) &vertexSource.text, (const GLUSchar**) &controlSource.text, (const GLUSchar**) &evaluationSource.text, (const GLUSchar**) &geometrySource.text, (const GLUSchar**) &fragmentSource.text))
    {
        printf("Could not build program two\n");

        return GLUS_FALSE;
    }

    glusFileDestroyText(&vertexSource);
    glusFileDestroyText(&controlSource);
    glusFileDestroyText(&evaluationSource);
    glusFileDestroyText(&geometrySource);
    glusFileDestroyText(&fragmentSource);

    g_maxTessellationLevelPassTwoLocation = glGetUniformLocation(g_shaderProgramPassTwo.program, "u_maxTessellationLevel");

    g_quadrantStepPassTwoLocation = glGetUniformLocation(g_shaderProgramPassTwo.program, "u_quadrantStep");

    g_positionTextureSpacePassTwoLocation = glGetUniformLocation(g_shaderProgramPassTwo.program, "u_positionTextureSpace");

    g_heightMapTexturePassTwoLocation = glGetUniformLocation(g_shaderProgramPassTwo.program, "u_heightMapTexture");
    g_normalMapTexturePassTwoLocation = glGetUniformLocation(g_shaderProgramPassTwo.program, "u_normalMapTexture");

    g_tmvpPassTwoLocation = glGetUniformLocation(g_shaderProgramPassTwo.program, "u_tmvpMatrix");

    g_lightDirectionPassTwoLocation = glGetUniformLocation(g_shaderProgramPassTwo.program, "u_lightDirection");

    g_colorMapTexturePassTwoLocation = glGetUniformLocation(g_shaderProgramPassTwo.program, "u_colorMapTexture");

    //

    // One time matrix calculations to convert between texture and world space

    glusMatrix4x4Identityf(g_textureToWorldMatrix);
    glusMatrix4x4Identityf(textureToWorldNormalMatrix);

    glusMatrix4x4Scalef(g_textureToWorldMatrix, HORIZONTAL_PIXEL_SPACING * METERS_TO_VIRTUAL_WORLD_SCALE, VERTICAL_PIXEL_RANGE * METERS_TO_VIRTUAL_WORLD_SCALE, HORIZONTAL_PIXEL_SPACING * METERS_TO_VIRTUAL_WORLD_SCALE);
    // Skip this scale for the normal matrix

    glusMatrix4x4Scalef(g_textureToWorldMatrix, 1.0f, 1.0f, -1.0f);
    glusMatrix4x4Scalef(textureToWorldNormalMatrix, 1.0f, 1.0f, -1.0f);

    glusMatrix4x4Translatef(g_textureToWorldMatrix, -g_sMapExtend / 2.0f, 0.0f, -g_tMapExtend / 2.0f);
    // No need for the translation matrix in the normal matrix

    glusMatrix4x4Copyf(g_worldToTextureMatrix, g_textureToWorldMatrix, GLUS_FALSE);
    glusMatrix4x4Inversef(g_worldToTextureMatrix);

    glusMatrix4x4Copyf(g_worldToTextureNormalMatrix, textureToWorldNormalMatrix, GLUS_FALSE);
    glusMatrix4x4Inversef(g_worldToTextureNormalMatrix);

    // Pass one

    glUseProgram(g_programPassOne.program);

    glUniform1f(g_halfDetailStepPassOneLocation, detailStep / 2.0f);
    glUniform1ui(g_detailLevelPassOneLocation, DETAIL_LEVEL_FIRST_PASS);
    glUniform1f(g_fovRadiusPassOneLocation, FOV_RADIUS / HORIZONTAL_PIXEL_SPACING * METERS_TO_VIRTUAL_WORLD_SCALE);

    glGenVertexArrays(1, &g_vaoPassOne);
    glBindVertexArray(g_vaoPassOne);

    glBindBuffer(GL_ARRAY_BUFFER, g_verticesPassOneVBO);
    // First 0 is the location = 0. See shader source
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
    // Enable location = 0
    glEnableVertexAttribArray(0);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesPassOneVBO);

   // Pass two

    glUseProgram(g_shaderProgramPassTwo.program);

    glUniform3fv(g_lightDirectionPassTwoLocation, 1, lightDirection);
    glUniform1ui(g_maxTessellationLevelPassTwoLocation, overallMaxDetailLevel - (MINIMUM_DETAIL_LEVEL + DETAIL_LEVEL_FIRST_PASS));
    glUniform1i(g_quadrantStepPassTwoLocation, QUADRANT_STEP);

    glGenVertexArrays(1, &g_vaoPassTwo);
    glBindVertexArray(g_vaoPassTwo);

    glBindBuffer(GL_ARRAY_BUFFER, g_verticesPassTwoVBO);
    // First 0 is the location = 0. See shader source
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
    // Enable location = 0
    glEnableVertexAttribArray(0);

    //

    glActiveTexture(GL_TEXTURE0);
    glUniform1i(g_heightMapTexturePassTwoLocation, 0);
    glBindTexture(GL_TEXTURE_RECTANGLE, g_heightMapTexture);

    glActiveTexture(GL_TEXTURE1);
    glUniform1i(g_colorMapTexturePassTwoLocation, 1);
    glBindTexture(GL_TEXTURE_2D, g_colorMapTexture);

    glActiveTexture(GL_TEXTURE2);
    glUniform1i(g_normalMapTexturePassTwoLocation, 2);
    glBindTexture(GL_TEXTURE_RECTANGLE, g_normalMapTexture);

    //

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    glClearDepth(1.0f);

    glEnable(GL_DEPTH_TEST);

    glEnable(GL_CULL_FACE);

    glPatchParameteri(GL_PATCH_VERTICES, 4);

    return GLUS_TRUE;
}
Example #11
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;
}
Example #12
0
File: main.c Project: Adon-m/OpenGL
GLUSboolean update(GLUSfloat time)
{
	static GLfloat biasMatrix[] = { 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 };

	static GLenum drawBuffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 };

	static GLfloat angle = 0.0f;

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

	GLUSgroupList* groupWalker;

	//
	// Do the deferred shading by rendering into the geometry buffers.
	//

	glBindFramebuffer(GL_FRAMEBUFFER, g_dsFBO);
	glDrawBuffers(4, drawBuffers);

	reshape(TEXTURE_WIDTH, TEXTURE_HEIGHT);

	glUseProgram(g_programDeferredShading.program);

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	//

	// Just pass the projection matrix. The final matrix is calculated in the shader.
	glUniformMatrix4fv(g_projectionMatrixLocation, 1, GL_FALSE, g_projectionMatrix);

	// Orbit camera around models
	glusLookAtf(viewMatrix, sinf(angle) * 10.0f, 4.0f, cosf(angle) * 10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

	glUniformMatrix4fv(g_viewMatrixLocation, 1, GL_FALSE, viewMatrix);

	//

	glusMatrix4x4Identityf(modelMatrix);

	glusMatrix4x4Translatef(modelMatrix, -(float)OBJECTS_COLUMNS / 2.0f + 0.5f, 0.0f, (float)OBJECTS_COLUMNS / 2.0f - 0.5f);

	// Scale the model up
	glusMatrix4x4Scalef(modelMatrix, 10.0f, 10.0f, 10.0f);

	// Uniform scale, so extracting is sufficient
	glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelMatrix);

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

	//

	glActiveTexture(GL_TEXTURE0);

	groupWalker = g_wavefront.groups;
	while (groupWalker)
	{
		// Set up material values.
		glUniform4fv(g_material.diffuseColorLocation, 1, groupWalker->group.material->diffuse);
		glUniform4fv(g_material.specularColorLocation, 1, groupWalker->group.material->specular);
		glUniform1f(g_material.specularExponentLocation, groupWalker->group.material->shininess);

		// Enable only texturing, if the material has a texture
		if (groupWalker->group.material->diffuseTextureName)
		{
			glUniform1i(g_useTextureLocation, 1);
			glUniform1i(g_material.diffuseTextureLocation, 0);
			glBindTexture(GL_TEXTURE_2D, groupWalker->group.material->diffuseTextureName);
		}
		else
		{
			glUniform1i(g_useTextureLocation, 0);
			glUniform1i(g_material.diffuseTextureLocation, 0);
			glBindTexture(GL_TEXTURE_2D, 0);
		}

		glBindVertexArray(groupWalker->group.vao);

		glDrawElementsInstanced(GL_TRIANGLES, groupWalker->group.numberIndices, GL_UNSIGNED_INT, 0, OBJECTS_COLUMNS * OBJECTS_COLUMNS);

		groupWalker = groupWalker->next;
	}

	//
	// Render now to display framebuffer.
	//

	glBindFramebuffer(GL_FRAMEBUFFER, 0);
	glDrawBuffer(GL_COLOR_ATTACHMENT0);

	reshape(TEXTURE_WIDTH, TEXTURE_HEIGHT);

	glUseProgram(g_programPointLight.program);

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glUniformMatrix4fv(g_projectionMatrixPointLightLocation, 1, GL_FALSE, g_projectionMatrix);

	glUniformMatrix4fv(g_viewMatrixPointLightLocation, 1, GL_FALSE, viewMatrix);

	//

	glusMatrix4x4Identityf(modelMatrix);

	glusMatrix4x4Translatef(modelMatrix, -7.0f, 0.0f, 7.0f);

	glUniformMatrix4fv(g_modelMatrixPointLightLocation, 1, GL_FALSE, modelMatrix);

	//

	// Update position matrix of the point lights. Each matrix filed is used to calculate the final position.
	for (GLint i = 0; i < POINT_LIGHT_COUNT; i++)
	{
		g_positionMatrix[i] += g_directionMatrix[i] * time * 5.0f;

		if (g_positionMatrix[i] > (float)POINT_LIGHT_COUNT - 2.0f)
		{
			g_positionMatrix[i] = ((float)POINT_LIGHT_COUNT - 2.0f) - (g_positionMatrix[i] - ((float)POINT_LIGHT_COUNT - 2.0f));
			g_directionMatrix[i] *= -1.0f;
		}
		else if (g_positionMatrix[i] < 0.0f)
		{
			g_positionMatrix[i] = -g_positionMatrix[i];
			g_directionMatrix[i] *= -1.0f;
		}
	}

	glUniformMatrix4fv(g_positionMatrixPointLightLocation, 1, GL_FALSE, g_positionMatrix);

	glUniformMatrix4fv(g_biasMatrixPointLightLocation, 1, GL_FALSE, biasMatrix);

	glUniform1f(g_radiusPointLightLocation, POINT_LIGHT_RADIUS);

	// Blend, as color is accumulated for each point light.
	glEnable(GL_BLEND);
	// No depth test, as the complete sphere of the point light should be processed.
	glDisable(GL_DEPTH_TEST);

	// Enable all the geometry buffer textures.

	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, g_dsDiffuseTexture);
	glUniform1i(g_diffusePointLightLocation, 0);

	glActiveTexture(GL_TEXTURE1);
	glBindTexture(GL_TEXTURE_2D, g_dsSpecularTexture);
	glUniform1i(g_specularPointLightLocation, 1);

	glActiveTexture(GL_TEXTURE2);
	glBindTexture(GL_TEXTURE_2D, g_dsPositionTexture);
	glUniform1i(g_positionPointLightLocation, 2);

	glActiveTexture(GL_TEXTURE3);
	glBindTexture(GL_TEXTURE_2D, g_dsNormalTexture);
	glUniform1i(g_normalPointLightLocation, 3);

	glBindVertexArray(g_vaoPointLight);

	glDrawElementsInstanced(GL_TRIANGLES, g_numberIndicesPointLight, GL_UNSIGNED_INT, 0, POINT_LIGHT_COUNT);

	glEnable(GL_DEPTH_TEST);
	glDisable(GL_BLEND);

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

	glActiveTexture(GL_TEXTURE1);
	glBindTexture(GL_TEXTURE_2D, 0);

	glActiveTexture(GL_TEXTURE2);
	glBindTexture(GL_TEXTURE_2D, 0);

	glActiveTexture(GL_TEXTURE3);
	glBindTexture(GL_TEXTURE_2D, 0);

	//

	// 20 seconds for one turn
	angle += 2.0f * GLUS_PI * time / 20.0f;

	return GLUS_TRUE;
}
Example #13
0
File: main.c Project: Adon-m/OpenGL
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;
}