testbed/common/ConcaveMesh.cpp
void ConcaveMesh::render(openglframework::Shader& shader,
                         const openglframework::Matrix4& worldToCameraMatrix) {

    // Bind the shader
    shader.bind();

    // Set the model to camera matrix
    shader.setMatrix4x4Uniform("localToWorldMatrix", mTransformMatrix);
    shader.setMatrix4x4Uniform("worldToCameraMatrix", worldToCameraMatrix);

    // Set the normal matrix (inverse transpose of the 3x3 upper-left sub matrix of the
    // model-view matrix)
    const openglframework::Matrix4 localToCameraMatrix = worldToCameraMatrix * mTransformMatrix;
    const openglframework::Matrix3 normalMatrix =
                       localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose();
    shader.setMatrix3x3Uniform("normalMatrix", normalMatrix, false);

    // Set the vertex color
    rp3d::RigidBody* rigidBody = dynamic_cast<rp3d::RigidBody*>(mBody);
    openglframework::Color currentColor = rigidBody != nullptr && rigidBody->isSleeping() ? mSleepingColor : mColor;
    openglframework::Vector4 color(currentColor.r, currentColor.g, currentColor.b, currentColor.a);
    shader.setVector4Uniform("globalVertexColor", color, false);

    // Bind the VAO
    mVAO.bind();

    mVBOVertices.bind();

    // Get the location of shader attribute variables
    GLint vertexPositionLoc = shader.getAttribLocation("vertexPosition");
    GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal", false);

    glEnableVertexAttribArray(vertexPositionLoc);
    glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)nullptr);

    mVBONormals.bind();

    if (vertexNormalLoc != -1) glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)nullptr);
    if (vertexNormalLoc != -1) glEnableVertexAttribArray(vertexNormalLoc);

    // For each part of the mesh
    for (unsigned int i=0; i<getNbParts(); i++) {
        glDrawElements(GL_TRIANGLES, getNbFaces(i) * 3, GL_UNSIGNED_INT, (char*)nullptr);
    }

    glDisableVertexAttribArray(vertexPositionLoc);
    if (vertexNormalLoc != -1) glDisableVertexAttribArray(vertexNormalLoc);

    mVBONormals.unbind();
    mVBOVertices.unbind();

    // Unbind the VAO
    mVAO.unbind();

    // Unbind the shader
    shader.unbind();
}
// Render the sphere at the correct position and with the correct orientation
Render the sphere at the correct position and with the correct orientation
Render the sphere at the correct position and with the correct orientation
void ConcaveMesh::render(openglframework::Shader& shader,
                         const openglframework::Matrix4& worldToCameraMatrix) {
    shader.bind();
    shader.setMatrix4x4Uniform("localToWorldMatrix", mTransformMatrix);
    shader.setMatrix4x4Uniform("worldToCameraMatrix", worldToCameraMatrix);
    const openglframework::Matrix4 localToCameraMatrix = worldToCameraMatrix * mTransformMatrix;
    const openglframework::Matrix3 normalMatrix =
                       localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose();
    shader.setMatrix3x3Uniform("normalMatrix", normalMatrix, false);
    rp3d::RigidBody* rigidBody = dynamic_cast<rp3d::RigidBody*>(mBody);
    openglframework::Color currentColor = rigidBody != nullptr && rigidBody->isSleeping() ? mSleepingColor : mColor;
    openglframework::Vector4 color(currentColor.r, currentColor.g, currentColor.b, currentColor.a);
    shader.setVector4Uniform("globalVertexColor", color, false);
    mVAO.bind();
    mVBOVertices.bind();
    GLint vertexPositionLoc = shader.getAttribLocation("vertexPosition");
    GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal", false);
    glEnableVertexAttribArray(vertexPositionLoc);
    glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)nullptr);
    mVBONormals.bind();
    if (vertexNormalLoc != -1) glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)nullptr);
    if (vertexNormalLoc != -1) glEnableVertexAttribArray(vertexNormalLoc);
    for (unsigned int i=0; i<getNbParts(); i++) {
        glDrawElements(GL_TRIANGLES, getNbFaces(i) * 3, GL_UNSIGNED_INT, (char*)nullptr);
    }
    glDisableVertexAttribArray(vertexPositionLoc);
    if (vertexNormalLoc != -1) glDisableVertexAttribArray(vertexNormalLoc);
    mVBONormals.unbind();
    mVBOVertices.unbind();
    mVAO.unbind();
    shader.unbind();
}
void ConcaveMesh :: render ( openglframework :: Shader & shader , const openglframework :: Matrix4 & worldToCameraMatrix ) { shader . bind ( ) ; shader . setMatrix4x4Uniform ( " " , mTransformMatrix ) ; shader . setMatrix4x4Uniform ( " " , worldToCameraMatrix ) ; const openglframework :: Matrix4 localToCameraMatrix = worldToCameraMatrix * mTransformMatrix ; const openglframework :: Matrix3 normalMatrix = localToCameraMatrix . getUpperLeft3x3Matrix ( ) . getInverse ( ) . getTranspose ( ) ; shader . setMatrix3x3Uniform ( " " , normalMatrix , false ) ; rp3d :: RigidBody * rigidBody = dynamic_cast < rp3d :: RigidBody * > ( mBody ) ; openglframework :: Color currentColor = rigidBody != nullptr && rigidBody -> isSleeping ( ) ? mSleepingColor : mColor ; openglframework :: Vector4 color ( currentColor . r , currentColor . g , currentColor . b , currentColor . a ) ; shader . setVector4Uniform ( " " , color , false ) ; mVAO . bind ( ) ; mVBOVertices . bind ( ) ; GLint vertexPositionLoc = shader . getAttribLocation ( " " ) ; GLint vertexNormalLoc = shader . getAttribLocation ( " " , false ) ; glEnableVertexAttribArray ( vertexPositionLoc ) ; glVertexAttribPointer ( vertexPositionLoc , 3 , GL_FLOAT , GL_FALSE , 0 , ( char * ) nullptr ) ; mVBONormals . bind ( ) ; if ( vertexNormalLoc != -1 ) glVertexAttribPointer ( vertexNormalLoc , 3 , GL_FLOAT , GL_FALSE , 0 , ( char * ) nullptr ) ; if ( vertexNormalLoc != -1 ) glEnableVertexAttribArray ( vertexNormalLoc ) ; for ( unsigned int i = 0 ; i < getNbParts ( ) ; i ++ ) { glDrawElements ( GL_TRIANGLES , getNbFaces ( i ) * 3 , GL_UNSIGNED_INT , ( char * ) nullptr ) ; } glDisableVertexAttribArray ( vertexPositionLoc ) ; if ( vertexNormalLoc != -1 ) glDisableVertexAttribArray ( vertexNormalLoc ) ; mVBONormals . unbind ( ) ; mVBOVertices . unbind ( ) ; mVAO . unbind ( ) ; shader . unbind ( ) ; }
Render the sphere at the correct position and with the correct orientation
Render the sphere at the correct position and with the correct orientation
// Bind the shader // Set the model to camera matrix // Set the normal matrix (inverse transpose of the 3x3 upper-left sub matrix of the // model-view matrix) // Set the vertex color // Bind the VAO // Get the location of shader attribute variables // For each part of the mesh // Unbind the VAO // Unbind the shader