Example #1
0
Plane Matrix4x4::operator*(const Plane& plane) const
{
	Plane result;

	glusMatrix4x4MultiplyPlanef(result.plane, m, plane.plane);

	return result;
}
Example #2
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;
}