Fix rendering issue in RaycastScene

This commit is contained in:
Daniel Chappuis 2015-08-11 21:40:07 +02:00
parent ce80426a2c
commit 06e68baa39
5 changed files with 32 additions and 62 deletions

View File

@ -33,6 +33,7 @@ openglframework::VertexBufferObject VisualContactPoint::mVBOIndices(GL_ELEMENT_A
openglframework::VertexArrayObject VisualContactPoint::mVAO;
int VisualContactPoint::mNbTotalPoints = 0;
openglframework::Mesh VisualContactPoint::mMesh;
bool VisualContactPoint::mStaticDataCreated = false;
// Constructor
VisualContactPoint::VisualContactPoint(const openglframework::Vector3& position,
@ -51,6 +52,8 @@ VisualContactPoint::~VisualContactPoint() {
// Load and initialize the mesh for all the contact points
void VisualContactPoint::createStaticData(const std::string& meshFolderPath) {
if (mStaticDataCreated) return;
// Load the mesh from a file
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "sphere.obj", mMesh);
@ -60,11 +63,15 @@ void VisualContactPoint::createStaticData(const std::string& meshFolderPath) {
mMesh.scaleVertices(VISUAL_CONTACT_POINT_RADIUS);
createVBOAndVAO();
mStaticDataCreated = true;
}
// Destroy the mesh for the contact points
void VisualContactPoint::destroyStaticData() {
if (!mStaticDataCreated) return;
// Destroy the VBOs and VAO
mVBOIndices.destroy();
mVBOVertices.destroy();
@ -72,15 +79,22 @@ void VisualContactPoint::destroyStaticData() {
mVAO.destroy();
mMesh.destroy();
mStaticDataCreated = false;
}
// Render the sphere at the correct position and with the correct orientation
void VisualContactPoint::render(openglframework::Shader& shader,
const openglframework::Matrix4& worldToCameraMatrix) {
// Bind the VAO
mVAO.bind();
// Bind the shader
shader.bind();
mVBOVertices.bind();
// Set the model to camera matrix
shader.setMatrix4x4Uniform("localToWorldMatrix", mTransformMatrix);
shader.setMatrix4x4Uniform("worldToCameraMatrix", worldToCameraMatrix);
@ -96,11 +110,6 @@ void VisualContactPoint::render(openglframework::Shader& shader,
openglframework::Vector4 color(mColor.r, mColor.g, mColor.b, mColor.a);
shader.setVector4Uniform("vertexColor", 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);

View File

@ -56,6 +56,9 @@ class VisualContactPoint : public openglframework::Object3D {
/// Vertex Array Object for the vertex data
static openglframework::VertexArrayObject mVAO;
/// True if static data (VBO, VAO) has been created already
static bool mStaticDataCreated;
/// Color
openglframework::Color mColor;

View File

@ -296,9 +296,14 @@ void RaycastScene::update() {
void RaycastScene::renderSinglePass(openglframework::Shader& shader,
const openglframework::Matrix4& worldToCameraMatrix) {
// Bind the VAO
mVAO.bind();
// Bind the shader
shader.bind();
mVBOVertices.bind();
// Set the model to camera matrix
const Matrix4 localToCameraMatrix = Matrix4::identity();
shader.setMatrix4x4Uniform("localToWorldMatrix", localToCameraMatrix);
@ -314,12 +319,19 @@ void RaycastScene::renderSinglePass(openglframework::Shader& shader,
openglframework::Vector4 color(1, 0, 0, 1);
shader.setVector4Uniform("vertexColor", color, false);
// Bind the VAO
mVAO.bind();
// Get the location of shader attribute variables
GLint vertexPositionLoc = shader.getAttribLocation("vertexPosition");
glEnableVertexAttribArray(vertexPositionLoc);
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
// Draw the lines
glDrawArrays(GL_LINES, 0, NB_RAYS);
glDisableVertexAttribArray(vertexPositionLoc);
mVBOVertices.unbind();
// Unbind the VAO
mVAO.unbind();
@ -335,27 +347,6 @@ void RaycastScene::renderSinglePass(openglframework::Shader& shader,
if (mDumbbell->getCollisionBody()->isActive()) mDumbbell->render(shader, worldToCameraMatrix);
//mPhongShader.unbind();
shader.bind();
openglframework::Vector4 redColor(1, 0, 0, 1);
shader.setVector4Uniform("vertexColor", redColor, false);
// Render all the raycast hit points
mRaycastManager.render(worldToCameraMatrix, mAreNormalsDisplayed);
shader.unbind();
shader.bind();
openglframework::Vector4 blueColor(0, 0.62, 0.92, 1);
shader.setVector4Uniform("vertexColor", blueColor, false);
// Render the lines
for (std::vector<Line*>::iterator it = mLines.begin(); it != mLines.end();
++it) {
(*it)->render(shader, worldToCameraMatrix);
}
// Unbind the shader
shader.unbind();
}
@ -366,9 +357,6 @@ void RaycastScene::createVBOAndVAO(openglframework::Shader& shader) {
// Bind the shader
shader.bind();
// Get the location of shader attribute variables
GLint vertexPositionLoc = shader.getAttribLocation("vertexPosition");
// Create the VBO for the vertices data
mVBOVertices.create();
mVBOVertices.bind();
@ -382,8 +370,6 @@ void RaycastScene::createVBOAndVAO(openglframework::Shader& shader) {
// Bind the VBO of vertices
mVBOVertices.bind();
glEnableVertexAttribArray(vertexPositionLoc);
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
// Unbind the VAO
mVAO.unbind();

View File

@ -99,36 +99,8 @@ class RaycastManager : public rp3d::RaycastCallback {
return raycastInfo.hitFraction;
}
void render(const openglframework::Matrix4& worldToCameraMatrix,
bool showNormals) {
/*
// Render all the raycast hit points
for (std::vector<ContactPoint>::iterator it = mHitPoints.begin();
it != mHitPoints.end(); ++it) {
(*it)->render(mShader, worldToCameraMatrix);
}
*/
if (showNormals) {
// Render all the normals at hit points
for (std::vector<Line*>::iterator it = mNormals.begin();
it != mNormals.end(); ++it) {
(*it)->render(mShader, worldToCameraMatrix);
}
}
}
void resetPoints() {
/*
// Destroy all the visual contact points
for (std::vector<VisualContactPoint*>::iterator it = mHitPoints.begin();
it != mHitPoints.end(); ++it) {
delete (*it);
}
*/
mHitPoints.clear();
// Destroy all the normals

View File

@ -188,7 +188,7 @@ void SceneDemo::render() {
if (mIsShadowMappingEnabled) mShadowMapTexture.unbind();
mPhongShader.unbind();
drawTextureQuad();
//drawTextureQuad();
}
// Create the Shadow map FBO and texture