Modify scenes to use the SceneDome class

This commit is contained in:
Daniel Chappuis 2015-07-29 18:15:20 +02:00
parent 6884adf0c2
commit a35340a930
32 changed files with 332 additions and 365 deletions

View File

@ -36,7 +36,7 @@ int Capsule::totalNbCapsules = 0;
// Constructor // Constructor
Capsule::Capsule(float radius, float height, const openglframework::Vector3& position, Capsule::Capsule(float radius, float height, const openglframework::Vector3& position,
reactphysics3d::CollisionWorld* world, reactphysics3d::CollisionWorld* world,
const std::string& meshFolderPath, openglframework::Shader& shader) const std::string& meshFolderPath)
: openglframework::Mesh(), mRadius(radius), mHeight(height) { : openglframework::Mesh(), mRadius(radius), mHeight(height) {
// Load the mesh from a file // Load the mesh from a file
@ -76,7 +76,7 @@ Capsule::Capsule(float radius, float height, const openglframework::Vector3& pos
// Create the VBOs and VAO // Create the VBOs and VAO
if (totalNbCapsules == 0) { if (totalNbCapsules == 0) {
createVBOAndVAO(shader); createVBOAndVAO();
} }
totalNbCapsules++; totalNbCapsules++;
@ -85,7 +85,7 @@ Capsule::Capsule(float radius, float height, const openglframework::Vector3& pos
// Constructor // Constructor
Capsule::Capsule(float radius, float height, const openglframework::Vector3& position, Capsule::Capsule(float radius, float height, const openglframework::Vector3& position,
float mass, reactphysics3d::DynamicsWorld* dynamicsWorld, float mass, reactphysics3d::DynamicsWorld* dynamicsWorld,
const std::string& meshFolderPath, openglframework::Shader &shader) const std::string& meshFolderPath)
: openglframework::Mesh(), mRadius(radius), mHeight(height), : openglframework::Mesh(), mRadius(radius), mHeight(height),
mColor(0.5f, 0.5f, 0.5f, 1.0f) { mColor(0.5f, 0.5f, 0.5f, 1.0f) {
@ -126,7 +126,7 @@ Capsule::Capsule(float radius, float height, const openglframework::Vector3& pos
// Create the VBOs and VAO // Create the VBOs and VAO
if (totalNbCapsules == 0) { if (totalNbCapsules == 0) {
createVBOAndVAO(shader); createVBOAndVAO();
} }
totalNbCapsules++; totalNbCapsules++;
@ -159,27 +159,45 @@ void Capsule::render(openglframework::Shader& shader,
shader.bind(); shader.bind();
// Set the model to camera matrix // Set the model to camera matrix
const openglframework::Matrix4 localToCameraMatrix = worldToCameraMatrix * mTransformMatrix; shader.setMatrix4x4Uniform("localToWorldMatrix", mTransformMatrix);
shader.setMatrix4x4Uniform("localToCameraMatrix", localToCameraMatrix); shader.setMatrix4x4Uniform("worldToCameraMatrix", worldToCameraMatrix);
// Set the normal matrix (inverse transpose of the 3x3 upper-left sub matrix of the // Set the normal matrix (inverse transpose of the 3x3 upper-left sub matrix of the
// model-view matrix) // model-view matrix)
const openglframework::Matrix4 localToCameraMatrix = worldToCameraMatrix * mTransformMatrix;
const openglframework::Matrix3 normalMatrix = const openglframework::Matrix3 normalMatrix =
localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose(); localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose();
shader.setMatrix3x3Uniform("normalMatrix", normalMatrix); shader.setMatrix3x3Uniform("normalMatrix", normalMatrix, false);
// Set the vertex color // Set the vertex color
openglframework::Vector4 color(mColor.r, mColor.g, mColor.b, mColor.a); openglframework::Vector4 color(mColor.r, mColor.g, mColor.b, mColor.a);
shader.setVector4Uniform("vertexColor", color); shader.setVector4Uniform("vertexColor", color, false);
// Bind the VAO // Bind the VAO
mVAO.bind(); mVAO.bind();
mVBOVertices.bind();
// Get the location of shader attribute variables
GLint vertexPositionLoc = shader.getAttribLocation("vertexPosition");
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal", false);
glEnableVertexAttribArray(vertexPositionLoc);
if (vertexNormalLoc != -1) glEnableVertexAttribArray(vertexNormalLoc);
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
if (vertexNormalLoc != -1) glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
// For each part of the mesh // For each part of the mesh
for (unsigned int i=0; i<getNbParts(); i++) { for (unsigned int i=0; i<getNbParts(); i++) {
glDrawElements(GL_TRIANGLES, getNbFaces(i) * 3, GL_UNSIGNED_INT, (char*)NULL); glDrawElements(GL_TRIANGLES, getNbFaces(i) * 3, GL_UNSIGNED_INT, (char*)NULL);
} }
glDisableVertexAttribArray(vertexPositionLoc);
if (vertexNormalLoc != -1) glDisableVertexAttribArray(vertexNormalLoc);
mVBOVertices.unbind();
// Unbind the VAO // Unbind the VAO
mVAO.unbind(); mVAO.unbind();
@ -213,15 +231,7 @@ void Capsule::updateTransform(float interpolationFactor) {
// Create the Vertex Buffer Objects used to render with OpenGL. // Create the Vertex Buffer Objects used to render with OpenGL.
/// We create two VBOs (one for vertices and one for indices) /// We create two VBOs (one for vertices and one for indices)
void Capsule::createVBOAndVAO(openglframework::Shader& shader) { void Capsule::createVBOAndVAO() {
// Bind the shader
shader.bind();
// Get the location of shader attribute variables
GLint vertexPositionLoc = shader.getAttribLocation("vertexPosition");
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal");
GLint vertexTexCoordLoc = shader.getAttribLocation("textureCoords");
// Create the VBO for the vertices data // Create the VBO for the vertices data
mVBOVertices.create(); mVBOVertices.create();
@ -259,19 +269,13 @@ void Capsule::createVBOAndVAO(openglframework::Shader& shader) {
// Bind the VBO of vertices // Bind the VBO of vertices
mVBOVertices.bind(); mVBOVertices.bind();
glEnableVertexAttribArray(vertexPositionLoc);
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
// Bind the VBO of normals // Bind the VBO of normals
mVBONormals.bind(); mVBONormals.bind();
glEnableVertexAttribArray(vertexNormalLoc);
glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
if (hasTexture()) { if (hasTexture()) {
// Bind the VBO of texture coords // Bind the VBO of texture coords
mVBOTextureCoords.bind(); mVBOTextureCoords.bind();
glEnableVertexAttribArray(vertexTexCoordLoc);
glVertexAttribPointer(vertexTexCoordLoc, 2, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
} }
// Bind the VBO of indices // Bind the VBO of indices
@ -279,9 +283,6 @@ void Capsule::createVBOAndVAO(openglframework::Shader& shader) {
// Unbind the VAO // Unbind the VAO
mVAO.unbind(); mVAO.unbind();
// Unbind the shader
shader.unbind();
} }
// Reset the transform // Reset the transform

View File

@ -76,7 +76,7 @@ class Capsule : public openglframework::Mesh {
// -------------------- Methods -------------------- // // -------------------- Methods -------------------- //
// Create the Vertex Buffer Objects used to render with OpenGL. // Create the Vertex Buffer Objects used to render with OpenGL.
void createVBOAndVAO(openglframework::Shader& shader); void createVBOAndVAO();
public : public :
@ -84,13 +84,12 @@ class Capsule : public openglframework::Mesh {
/// Constructor /// Constructor
Capsule(float radius, float height, const openglframework::Vector3& position, Capsule(float radius, float height, const openglframework::Vector3& position,
reactphysics3d::CollisionWorld* world, const std::string& meshFolderPath, reactphysics3d::CollisionWorld* world, const std::string& meshFolderPath);
openglframework::Shader& shader);
/// Constructor /// Constructor
Capsule(float radius, float height, const openglframework::Vector3& position, Capsule(float radius, float height, const openglframework::Vector3& position,
float mass, reactphysics3d::DynamicsWorld* dynamicsWorld, float mass, reactphysics3d::DynamicsWorld* dynamicsWorld,
const std::string& meshFolderPath, openglframework::Shader& shader); const std::string& meshFolderPath);
/// Destructor /// Destructor
~Capsule(); ~Capsule();

View File

@ -36,7 +36,7 @@ int Cone::totalNbCones = 0;
// Constructor // Constructor
Cone::Cone(float radius, float height, const openglframework::Vector3 &position, Cone::Cone(float radius, float height, const openglframework::Vector3 &position,
reactphysics3d::CollisionWorld* world, reactphysics3d::CollisionWorld* world,
const std::string& meshFolderPath, openglframework::Shader& shader) const std::string& meshFolderPath)
: openglframework::Mesh(), mRadius(radius), mHeight(height), : openglframework::Mesh(), mRadius(radius), mHeight(height),
mColor(0.5f, 0.5f, 0.5f, 1.0f) { mColor(0.5f, 0.5f, 0.5f, 1.0f) {
@ -77,7 +77,7 @@ Cone::Cone(float radius, float height, const openglframework::Vector3 &position,
// Create the VBOs and VAO // Create the VBOs and VAO
if (totalNbCones == 0) { if (totalNbCones == 0) {
createVBOAndVAO(shader); createVBOAndVAO();
} }
totalNbCones++; totalNbCones++;
@ -86,7 +86,7 @@ Cone::Cone(float radius, float height, const openglframework::Vector3 &position,
// Constructor // Constructor
Cone::Cone(float radius, float height, const openglframework::Vector3 &position, Cone::Cone(float radius, float height, const openglframework::Vector3 &position,
float mass, reactphysics3d::DynamicsWorld* dynamicsWorld, float mass, reactphysics3d::DynamicsWorld* dynamicsWorld,
const std::string& meshFolderPath, openglframework::Shader &shader) const std::string& meshFolderPath)
: openglframework::Mesh(), mRadius(radius), mHeight(height), : openglframework::Mesh(), mRadius(radius), mHeight(height),
mColor(0.5f, 0.5f, 0.5f, 1.0f) { mColor(0.5f, 0.5f, 0.5f, 1.0f) {
@ -127,7 +127,7 @@ Cone::Cone(float radius, float height, const openglframework::Vector3 &position,
// Create the VBOs and VAO // Create the VBOs and VAO
if (totalNbCones == 0) { if (totalNbCones == 0) {
createVBOAndVAO(shader); createVBOAndVAO();
} }
totalNbCones++; totalNbCones++;
@ -159,27 +159,45 @@ void Cone::render(openglframework::Shader& shader,
shader.bind(); shader.bind();
// Set the model to camera matrix // Set the model to camera matrix
const openglframework::Matrix4 localToCameraMatrix = worldToCameraMatrix * mTransformMatrix; shader.setMatrix4x4Uniform("localToWorldMatrix", mTransformMatrix);
shader.setMatrix4x4Uniform("localToCameraMatrix", localToCameraMatrix); shader.setMatrix4x4Uniform("worldToCameraMatrix", worldToCameraMatrix);
// Set the normal matrix (inverse transpose of the 3x3 upper-left sub matrix of the // Set the normal matrix (inverse transpose of the 3x3 upper-left sub matrix of the
// model-view matrix) // model-view matrix)
const openglframework::Matrix4 localToCameraMatrix = worldToCameraMatrix * mTransformMatrix;
const openglframework::Matrix3 normalMatrix = const openglframework::Matrix3 normalMatrix =
localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose(); localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose();
shader.setMatrix3x3Uniform("normalMatrix", normalMatrix); shader.setMatrix3x3Uniform("normalMatrix", normalMatrix, false);
// Set the vertex color // Set the vertex color
openglframework::Vector4 color(mColor.r, mColor.g, mColor.b, mColor.a); openglframework::Vector4 color(mColor.r, mColor.g, mColor.b, mColor.a);
shader.setVector4Uniform("vertexColor", color); shader.setVector4Uniform("vertexColor", color, false);
// Bind the VAO // Bind the VAO
mVAO.bind(); mVAO.bind();
mVBOVertices.bind();
// Get the location of shader attribute variables
GLint vertexPositionLoc = shader.getAttribLocation("vertexPosition");
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal", false);
glEnableVertexAttribArray(vertexPositionLoc);
if (vertexNormalLoc != -1) glEnableVertexAttribArray(vertexNormalLoc);
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
if (vertexNormalLoc != -1) glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
// For each part of the mesh // For each part of the mesh
for (unsigned int i=0; i<getNbParts(); i++) { for (unsigned int i=0; i<getNbParts(); i++) {
glDrawElements(GL_TRIANGLES, getNbFaces(i) * 3, GL_UNSIGNED_INT, (char*)NULL); glDrawElements(GL_TRIANGLES, getNbFaces(i) * 3, GL_UNSIGNED_INT, (char*)NULL);
} }
glDisableVertexAttribArray(vertexPositionLoc);
if (vertexNormalLoc != -1) glDisableVertexAttribArray(vertexNormalLoc);
mVBOVertices.unbind();
// Unbind the VAO // Unbind the VAO
mVAO.unbind(); mVAO.unbind();
@ -214,15 +232,7 @@ void Cone::updateTransform(float interpolationFactor) {
// Create the Vertex Buffer Objects used to render with OpenGL. // Create the Vertex Buffer Objects used to render with OpenGL.
/// We create two VBOs (one for vertices and one for indices) /// We create two VBOs (one for vertices and one for indices)
void Cone::createVBOAndVAO(openglframework::Shader& shader) { void Cone::createVBOAndVAO() {
// Bind the shader
shader.bind();
// Get the location of shader attribute variables
GLint vertexPositionLoc = shader.getAttribLocation("vertexPosition");
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal");
GLint vertexTexCoordLoc = shader.getAttribLocation("textureCoords");
// Create the VBO for the vertices data // Create the VBO for the vertices data
mVBOVertices.create(); mVBOVertices.create();
@ -260,19 +270,13 @@ void Cone::createVBOAndVAO(openglframework::Shader& shader) {
// Bind the VBO of vertices // Bind the VBO of vertices
mVBOVertices.bind(); mVBOVertices.bind();
glEnableVertexAttribArray(vertexPositionLoc);
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
// Bind the VBO of normals // Bind the VBO of normals
mVBONormals.bind(); mVBONormals.bind();
glEnableVertexAttribArray(vertexNormalLoc);
glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
if (hasTexture()) { if (hasTexture()) {
// Bind the VBO of texture coords // Bind the VBO of texture coords
mVBOTextureCoords.bind(); mVBOTextureCoords.bind();
glEnableVertexAttribArray(vertexTexCoordLoc);
glVertexAttribPointer(vertexTexCoordLoc, 2, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
} }
// Bind the VBO of indices // Bind the VBO of indices
@ -280,9 +284,6 @@ void Cone::createVBOAndVAO(openglframework::Shader& shader) {
// Unbind the VAO // Unbind the VAO
mVAO.unbind(); mVAO.unbind();
// Unbind the shader
shader.unbind();
} }
// Reset the transform // Reset the transform

View File

@ -76,7 +76,7 @@ class Cone : public openglframework::Mesh {
// -------------------- Methods -------------------- // // -------------------- Methods -------------------- //
// Create the Vertex Buffer Objects used to render with OpenGL. // Create the Vertex Buffer Objects used to render with OpenGL.
void createVBOAndVAO(openglframework::Shader& shader); void createVBOAndVAO();
public : public :
@ -84,12 +84,11 @@ class Cone : public openglframework::Mesh {
/// Constructor /// Constructor
Cone(float radius, float height, const openglframework::Vector3& position, Cone(float radius, float height, const openglframework::Vector3& position,
rp3d::CollisionWorld* world, const std::string& meshFolderPath, openglframework::Shader &shader); rp3d::CollisionWorld* world, const std::string& meshFolderPath);
/// Constructor /// Constructor
Cone(float radius, float height, const openglframework::Vector3& position, Cone(float radius, float height, const openglframework::Vector3& position,
float mass, rp3d::DynamicsWorld* dynamicsWorld, const std::string& meshFolderPath, float mass, rp3d::DynamicsWorld* dynamicsWorld, const std::string& meshFolderPath);
openglframework::Shader& shader);
/// Destructor /// Destructor
~Cone(); ~Cone();

View File

@ -29,8 +29,7 @@
// Constructor // Constructor
ConvexMesh::ConvexMesh(const openglframework::Vector3 &position, ConvexMesh::ConvexMesh(const openglframework::Vector3 &position,
reactphysics3d::CollisionWorld* world, reactphysics3d::CollisionWorld* world,
const std::string& meshFolderPath, const std::string& meshFolderPath)
openglframework::Shader& shader)
: openglframework::Mesh(), mVBOVertices(GL_ARRAY_BUFFER), : openglframework::Mesh(), mVBOVertices(GL_ARRAY_BUFFER),
mVBONormals(GL_ARRAY_BUFFER), mVBOTextureCoords(GL_ARRAY_BUFFER), mVBONormals(GL_ARRAY_BUFFER), mVBOTextureCoords(GL_ARRAY_BUFFER),
mVBOIndices(GL_ELEMENT_ARRAY_BUFFER), mColor(0.5f, 0.5f, 0.5f, 1.0f) { mVBOIndices(GL_ELEMENT_ARRAY_BUFFER), mColor(0.5f, 0.5f, 0.5f, 1.0f) {
@ -91,13 +90,13 @@ ConvexMesh::ConvexMesh(const openglframework::Vector3 &position,
mRigidBody->addCollisionShape(collisionShape, rp3d::Transform::identity()); mRigidBody->addCollisionShape(collisionShape, rp3d::Transform::identity());
// Create the VBOs and VAO // Create the VBOs and VAO
createVBOAndVAO(shader); createVBOAndVAO();
} }
// Constructor // Constructor
ConvexMesh::ConvexMesh(const openglframework::Vector3 &position, float mass, ConvexMesh::ConvexMesh(const openglframework::Vector3 &position, float mass,
reactphysics3d::DynamicsWorld* dynamicsWorld, reactphysics3d::DynamicsWorld* dynamicsWorld,
const std::string& meshFolderPath, openglframework::Shader &shader) const std::string& meshFolderPath)
: openglframework::Mesh(), mVBOVertices(GL_ARRAY_BUFFER), : openglframework::Mesh(), mVBOVertices(GL_ARRAY_BUFFER),
mVBONormals(GL_ARRAY_BUFFER), mVBOTextureCoords(GL_ARRAY_BUFFER), mVBONormals(GL_ARRAY_BUFFER), mVBOTextureCoords(GL_ARRAY_BUFFER),
mVBOIndices(GL_ELEMENT_ARRAY_BUFFER), mColor(0.5f, 0.5f, 0.5f, 1.0f) { mVBOIndices(GL_ELEMENT_ARRAY_BUFFER), mColor(0.5f, 0.5f, 0.5f, 1.0f) {
@ -157,7 +156,7 @@ ConvexMesh::ConvexMesh(const openglframework::Vector3 &position, float mass,
mRigidBody = body; mRigidBody = body;
// Create the VBOs and VAO // Create the VBOs and VAO
createVBOAndVAO(shader); createVBOAndVAO();
} }
// Destructor // Destructor
@ -182,27 +181,45 @@ void ConvexMesh::render(openglframework::Shader& shader,
shader.bind(); shader.bind();
// Set the model to camera matrix // Set the model to camera matrix
const openglframework::Matrix4 localToCameraMatrix = worldToCameraMatrix * mTransformMatrix; shader.setMatrix4x4Uniform("localToWorldMatrix", mTransformMatrix);
shader.setMatrix4x4Uniform("localToCameraMatrix", localToCameraMatrix); shader.setMatrix4x4Uniform("worldToCameraMatrix", worldToCameraMatrix);
// Set the normal matrix (inverse transpose of the 3x3 upper-left sub matrix of the // Set the normal matrix (inverse transpose of the 3x3 upper-left sub matrix of the
// model-view matrix) // model-view matrix)
const openglframework::Matrix4 localToCameraMatrix = worldToCameraMatrix * mTransformMatrix;
const openglframework::Matrix3 normalMatrix = const openglframework::Matrix3 normalMatrix =
localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose(); localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose();
shader.setMatrix3x3Uniform("normalMatrix", normalMatrix); shader.setMatrix3x3Uniform("normalMatrix", normalMatrix, false);
// Set the vertex color // Set the vertex color
openglframework::Vector4 color(mColor.r, mColor.g, mColor.b, mColor.a); openglframework::Vector4 color(mColor.r, mColor.g, mColor.b, mColor.a);
shader.setVector4Uniform("vertexColor", color); shader.setVector4Uniform("vertexColor", color, false);
// Bind the VAO // Bind the VAO
mVAO.bind(); mVAO.bind();
mVBOVertices.bind();
// Get the location of shader attribute variables
GLint vertexPositionLoc = shader.getAttribLocation("vertexPosition");
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal", false);
glEnableVertexAttribArray(vertexPositionLoc);
if (vertexNormalLoc != -1) glEnableVertexAttribArray(vertexNormalLoc);
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
if (vertexNormalLoc != -1) glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
// For each part of the mesh // For each part of the mesh
for (unsigned int i=0; i<getNbParts(); i++) { for (unsigned int i=0; i<getNbParts(); i++) {
glDrawElements(GL_TRIANGLES, getNbFaces(i) * 3, GL_UNSIGNED_INT, (char*)NULL); glDrawElements(GL_TRIANGLES, getNbFaces(i) * 3, GL_UNSIGNED_INT, (char*)NULL);
} }
glDisableVertexAttribArray(vertexPositionLoc);
if (vertexNormalLoc != -1) glDisableVertexAttribArray(vertexNormalLoc);
mVBOVertices.unbind();
// Unbind the VAO // Unbind the VAO
mVAO.unbind(); mVAO.unbind();
@ -236,15 +253,7 @@ void ConvexMesh::updateTransform(float interpolationFactor) {
// Create the Vertex Buffer Objects used to render with OpenGL. // Create the Vertex Buffer Objects used to render with OpenGL.
/// We create two VBOs (one for vertices and one for indices) /// We create two VBOs (one for vertices and one for indices)
void ConvexMesh::createVBOAndVAO(openglframework::Shader& shader) { void ConvexMesh::createVBOAndVAO() {
// Bind the shader
shader.bind();
// Get the location of shader attribute variables
GLint vertexPositionLoc = shader.getAttribLocation("vertexPosition");
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal");
GLint vertexTexCoordLoc = shader.getAttribLocation("textureCoords");
// Create the VBO for the vertices data // Create the VBO for the vertices data
mVBOVertices.create(); mVBOVertices.create();
@ -282,19 +291,13 @@ void ConvexMesh::createVBOAndVAO(openglframework::Shader& shader) {
// Bind the VBO of vertices // Bind the VBO of vertices
mVBOVertices.bind(); mVBOVertices.bind();
glEnableVertexAttribArray(vertexPositionLoc);
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
// Bind the VBO of normals // Bind the VBO of normals
mVBONormals.bind(); mVBONormals.bind();
glEnableVertexAttribArray(vertexNormalLoc);
glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
if (hasTexture()) { if (hasTexture()) {
// Bind the VBO of texture coords // Bind the VBO of texture coords
mVBOTextureCoords.bind(); mVBOTextureCoords.bind();
glEnableVertexAttribArray(vertexTexCoordLoc);
glVertexAttribPointer(vertexTexCoordLoc, 2, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
} }
// Bind the VBO of indices // Bind the VBO of indices
@ -302,9 +305,6 @@ void ConvexMesh::createVBOAndVAO(openglframework::Shader& shader) {
// Unbind the VAO // Unbind the VAO
mVAO.unbind(); mVAO.unbind();
// Unbind the shader
shader.unbind();
} }
// Reset the transform // Reset the transform

View File

@ -64,7 +64,7 @@ class ConvexMesh : public openglframework::Mesh {
// -------------------- Methods -------------------- // // -------------------- Methods -------------------- //
// Create the Vertex Buffer Objects used to render with OpenGL. // Create the Vertex Buffer Objects used to render with OpenGL.
void createVBOAndVAO(openglframework::Shader& shader); void createVBOAndVAO();
public : public :
@ -72,12 +72,11 @@ class ConvexMesh : public openglframework::Mesh {
/// Constructor /// Constructor
ConvexMesh(const openglframework::Vector3& position, ConvexMesh(const openglframework::Vector3& position,
rp3d::CollisionWorld* world, const std::string& meshFolderPath, openglframework::Shader &shader); rp3d::CollisionWorld* world, const std::string& meshFolderPath);
/// Constructor /// Constructor
ConvexMesh(const openglframework::Vector3& position, float mass, ConvexMesh(const openglframework::Vector3& position, float mass,
rp3d::DynamicsWorld* dynamicsWorld, const std::string& meshFolderPath, rp3d::DynamicsWorld* dynamicsWorld, const std::string& meshFolderPath);
openglframework::Shader& shader);
/// Destructor /// Destructor
~ConvexMesh(); ~ConvexMesh();

View File

@ -36,8 +36,7 @@ int Cylinder::totalNbCylinders = 0;
// Constructor // Constructor
Cylinder::Cylinder(float radius, float height, const openglframework::Vector3& position, Cylinder::Cylinder(float radius, float height, const openglframework::Vector3& position,
reactphysics3d::CollisionWorld* world, reactphysics3d::CollisionWorld* world,
const std::string& meshFolderPath, const std::string& meshFolderPath)
openglframework::Shader& shader)
: openglframework::Mesh(), mRadius(radius), mHeight(height), : openglframework::Mesh(), mRadius(radius), mHeight(height),
mColor(0.5f, 0.5f, 0.5f, 1.0f) { mColor(0.5f, 0.5f, 0.5f, 1.0f) {
@ -78,7 +77,7 @@ Cylinder::Cylinder(float radius, float height, const openglframework::Vector3& p
// Create the VBOs and VAO // Create the VBOs and VAO
if (totalNbCylinders == 0) { if (totalNbCylinders == 0) {
createVBOAndVAO(shader); createVBOAndVAO();
} }
totalNbCylinders++; totalNbCylinders++;
@ -87,7 +86,7 @@ Cylinder::Cylinder(float radius, float height, const openglframework::Vector3& p
// Constructor // Constructor
Cylinder::Cylinder(float radius, float height, const openglframework::Vector3& position, Cylinder::Cylinder(float radius, float height, const openglframework::Vector3& position,
float mass, reactphysics3d::DynamicsWorld* dynamicsWorld, float mass, reactphysics3d::DynamicsWorld* dynamicsWorld,
const std::string& meshFolderPath, openglframework::Shader& shader) const std::string& meshFolderPath)
: openglframework::Mesh(), mRadius(radius), mHeight(height), : openglframework::Mesh(), mRadius(radius), mHeight(height),
mColor(0.5f, 0.5f, 0.5f, 1.0f) { mColor(0.5f, 0.5f, 0.5f, 1.0f) {
@ -128,7 +127,7 @@ Cylinder::Cylinder(float radius, float height, const openglframework::Vector3& p
// Create the VBOs and VAO // Create the VBOs and VAO
if (totalNbCylinders == 0) { if (totalNbCylinders == 0) {
createVBOAndVAO(shader); createVBOAndVAO();
} }
totalNbCylinders++; totalNbCylinders++;
@ -161,27 +160,45 @@ void Cylinder::render(openglframework::Shader& shader,
shader.bind(); shader.bind();
// Set the model to camera matrix // Set the model to camera matrix
const openglframework::Matrix4 localToCameraMatrix = worldToCameraMatrix * mTransformMatrix; shader.setMatrix4x4Uniform("localToWorldMatrix", mTransformMatrix);
shader.setMatrix4x4Uniform("localToCameraMatrix", localToCameraMatrix); shader.setMatrix4x4Uniform("worldToCameraMatrix", worldToCameraMatrix);
// Set the normal matrix (inverse transpose of the 3x3 upper-left sub matrix of the // Set the normal matrix (inverse transpose of the 3x3 upper-left sub matrix of the
// model-view matrix) // model-view matrix)
const openglframework::Matrix4 localToCameraMatrix = worldToCameraMatrix * mTransformMatrix;
const openglframework::Matrix3 normalMatrix = const openglframework::Matrix3 normalMatrix =
localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose(); localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose();
shader.setMatrix3x3Uniform("normalMatrix", normalMatrix); shader.setMatrix3x3Uniform("normalMatrix", normalMatrix, false);
// Set the vertex color // Set the vertex color
openglframework::Vector4 color(mColor.r, mColor.g, mColor.b, mColor.a); openglframework::Vector4 color(mColor.r, mColor.g, mColor.b, mColor.a);
shader.setVector4Uniform("vertexColor", color); shader.setVector4Uniform("vertexColor", color, false);
// Bind the VAO // Bind the VAO
mVAO.bind(); mVAO.bind();
mVBOVertices.bind();
// Get the location of shader attribute variables
GLint vertexPositionLoc = shader.getAttribLocation("vertexPosition");
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal", false);
glEnableVertexAttribArray(vertexPositionLoc);
if (vertexNormalLoc != -1) glEnableVertexAttribArray(vertexNormalLoc);
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
if (vertexNormalLoc != -1) glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
// For each part of the mesh // For each part of the mesh
for (unsigned int i=0; i<getNbParts(); i++) { for (unsigned int i=0; i<getNbParts(); i++) {
glDrawElements(GL_TRIANGLES, getNbFaces(i) * 3, GL_UNSIGNED_INT, (char*)NULL); glDrawElements(GL_TRIANGLES, getNbFaces(i) * 3, GL_UNSIGNED_INT, (char*)NULL);
} }
glDisableVertexAttribArray(vertexPositionLoc);
if (vertexNormalLoc != -1) glDisableVertexAttribArray(vertexNormalLoc);
mVBOVertices.unbind();
// Unbind the VAO // Unbind the VAO
mVAO.unbind(); mVAO.unbind();
@ -215,15 +232,7 @@ void Cylinder::updateTransform(float interpolationFactor) {
// Create the Vertex Buffer Objects used to render with OpenGL. // Create the Vertex Buffer Objects used to render with OpenGL.
/// We create two VBOs (one for vertices and one for indices) /// We create two VBOs (one for vertices and one for indices)
void Cylinder::createVBOAndVAO(openglframework::Shader& shader) { void Cylinder::createVBOAndVAO() {
// Bind the shader
shader.bind();
// Get the location of shader attribute variables
GLint vertexPositionLoc = shader.getAttribLocation("vertexPosition");
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal");
GLint vertexTexCoordLoc = shader.getAttribLocation("textureCoords");
// Create the VBO for the vertices data // Create the VBO for the vertices data
mVBOVertices.create(); mVBOVertices.create();
@ -261,19 +270,13 @@ void Cylinder::createVBOAndVAO(openglframework::Shader& shader) {
// Bind the VBO of vertices // Bind the VBO of vertices
mVBOVertices.bind(); mVBOVertices.bind();
glEnableVertexAttribArray(vertexPositionLoc);
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
// Bind the VBO of normals // Bind the VBO of normals
mVBONormals.bind(); mVBONormals.bind();
glEnableVertexAttribArray(vertexNormalLoc);
glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
if (hasTexture()) { if (hasTexture()) {
// Bind the VBO of texture coords // Bind the VBO of texture coords
mVBOTextureCoords.bind(); mVBOTextureCoords.bind();
glEnableVertexAttribArray(vertexTexCoordLoc);
glVertexAttribPointer(vertexTexCoordLoc, 2, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
} }
// Bind the VBO of indices // Bind the VBO of indices
@ -281,9 +284,6 @@ void Cylinder::createVBOAndVAO(openglframework::Shader& shader) {
// Unbind the VAO // Unbind the VAO
mVAO.unbind(); mVAO.unbind();
// Unbind the shader
shader.unbind();
} }
// Reset the transform // Reset the transform

View File

@ -76,7 +76,7 @@ class Cylinder : public openglframework::Mesh {
// -------------------- Methods -------------------- // // -------------------- Methods -------------------- //
// Create the Vertex Buffer Objects used to render with OpenGL. // Create the Vertex Buffer Objects used to render with OpenGL.
void createVBOAndVAO(openglframework::Shader& shader); void createVBOAndVAO();
public : public :
@ -84,13 +84,11 @@ class Cylinder : public openglframework::Mesh {
/// Constructor /// Constructor
Cylinder(float radius, float height, const openglframework::Vector3& position, Cylinder(float radius, float height, const openglframework::Vector3& position,
rp3d::CollisionWorld* world, const std::string &meshFolderPath, rp3d::CollisionWorld* world, const std::string &meshFolderPath);
openglframework::Shader& shader);
/// Constructor /// Constructor
Cylinder(float radius, float height, const openglframework::Vector3& position, Cylinder(float radius, float height, const openglframework::Vector3& position,
float mass, rp3d::DynamicsWorld* dynamicsWorld, const std::string &meshFolderPath, float mass, rp3d::DynamicsWorld* dynamicsWorld, const std::string &meshFolderPath);
openglframework::Shader &shader);
/// Destructor /// Destructor
~Cylinder(); ~Cylinder();

View File

@ -35,8 +35,7 @@ int Dumbbell::totalNbDumbbells = 0;
// Constructor // Constructor
Dumbbell::Dumbbell(const openglframework::Vector3 &position, Dumbbell::Dumbbell(const openglframework::Vector3 &position,
reactphysics3d::DynamicsWorld* dynamicsWorld, const std::string& meshFolderPath, reactphysics3d::DynamicsWorld* dynamicsWorld, const std::string& meshFolderPath)
openglframework::Shader& shader)
: openglframework::Mesh(), mColor(0.5f, 0.5f, 0.5f, 1.0f) { : openglframework::Mesh(), mColor(0.5f, 0.5f, 0.5f, 1.0f) {
// Load the mesh from a file // Load the mesh from a file
@ -97,7 +96,7 @@ Dumbbell::Dumbbell(const openglframework::Vector3 &position,
// Create the VBOs and VAO // Create the VBOs and VAO
if (totalNbDumbbells == 0) { if (totalNbDumbbells == 0) {
createVBOAndVAO(shader); createVBOAndVAO();
} }
totalNbDumbbells++; totalNbDumbbells++;
@ -105,8 +104,7 @@ Dumbbell::Dumbbell(const openglframework::Vector3 &position,
// Constructor // Constructor
Dumbbell::Dumbbell(const openglframework::Vector3 &position, Dumbbell::Dumbbell(const openglframework::Vector3 &position,
reactphysics3d::CollisionWorld* world, const std::string& meshFolderPath, reactphysics3d::CollisionWorld* world, const std::string& meshFolderPath)
openglframework::Shader& shader)
: openglframework::Mesh(), mColor(0.5f, 0.5f, 0.5f, 1.0f) { : openglframework::Mesh(), mColor(0.5f, 0.5f, 0.5f, 1.0f) {
// Load the mesh from a file // Load the mesh from a file
@ -163,7 +161,7 @@ Dumbbell::Dumbbell(const openglframework::Vector3 &position,
// Create the VBOs and VAO // Create the VBOs and VAO
if (totalNbDumbbells == 0) { if (totalNbDumbbells == 0) {
createVBOAndVAO(shader); createVBOAndVAO();
} }
totalNbDumbbells++; totalNbDumbbells++;
@ -196,27 +194,45 @@ void Dumbbell::render(openglframework::Shader& shader,
shader.bind(); shader.bind();
// Set the model to camera matrix // Set the model to camera matrix
const openglframework::Matrix4 localToCameraMatrix = worldToCameraMatrix * mTransformMatrix; shader.setMatrix4x4Uniform("localToWorldMatrix", mTransformMatrix);
shader.setMatrix4x4Uniform("localToCameraMatrix", localToCameraMatrix); shader.setMatrix4x4Uniform("worldToCameraMatrix", worldToCameraMatrix);
// Set the normal matrix (inverse transpose of the 3x3 upper-left sub matrix of the // Set the normal matrix (inverse transpose of the 3x3 upper-left sub matrix of the
// model-view matrix) // model-view matrix)
const openglframework::Matrix4 localToCameraMatrix = worldToCameraMatrix * mTransformMatrix;
const openglframework::Matrix3 normalMatrix = const openglframework::Matrix3 normalMatrix =
localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose(); localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose();
shader.setMatrix3x3Uniform("normalMatrix", normalMatrix); shader.setMatrix3x3Uniform("normalMatrix", normalMatrix, false);
// Set the vertex color // Set the vertex color
openglframework::Vector4 color(mColor.r, mColor.g, mColor.b, mColor.a); openglframework::Vector4 color(mColor.r, mColor.g, mColor.b, mColor.a);
shader.setVector4Uniform("vertexColor", color); shader.setVector4Uniform("vertexColor", color, false);
// Bind the VAO // Bind the VAO
mVAO.bind(); mVAO.bind();
mVBOVertices.bind();
// Get the location of shader attribute variables
GLint vertexPositionLoc = shader.getAttribLocation("vertexPosition");
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal", false);
glEnableVertexAttribArray(vertexPositionLoc);
if (vertexNormalLoc != -1) glEnableVertexAttribArray(vertexNormalLoc);
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
if (vertexNormalLoc != -1) glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
// For each part of the mesh // For each part of the mesh
for (unsigned int i=0; i<getNbParts(); i++) { for (unsigned int i=0; i<getNbParts(); i++) {
glDrawElements(GL_TRIANGLES, getNbFaces(i) * 3, GL_UNSIGNED_INT, (char*)NULL); glDrawElements(GL_TRIANGLES, getNbFaces(i) * 3, GL_UNSIGNED_INT, (char*)NULL);
} }
glDisableVertexAttribArray(vertexPositionLoc);
if (vertexNormalLoc != -1) glDisableVertexAttribArray(vertexNormalLoc);
mVBOVertices.unbind();
// Unbind the VAO // Unbind the VAO
mVAO.unbind(); mVAO.unbind();
@ -250,15 +266,7 @@ void Dumbbell::updateTransform(float interpolationFactor) {
// Create the Vertex Buffer Objects used to render with OpenGL. // Create the Vertex Buffer Objects used to render with OpenGL.
/// We create two VBOs (one for vertices and one for indices) /// We create two VBOs (one for vertices and one for indices)
void Dumbbell::createVBOAndVAO(openglframework::Shader& shader) { void Dumbbell::createVBOAndVAO() {
// Bind the shader
shader.bind();
// Get the location of shader attribute variables
GLint vertexPositionLoc = shader.getAttribLocation("vertexPosition");
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal");
GLint vertexTexCoordLoc = shader.getAttribLocation("textureCoords");
// Create the VBO for the vertices data // Create the VBO for the vertices data
mVBOVertices.create(); mVBOVertices.create();
@ -296,19 +304,13 @@ void Dumbbell::createVBOAndVAO(openglframework::Shader& shader) {
// Bind the VBO of vertices // Bind the VBO of vertices
mVBOVertices.bind(); mVBOVertices.bind();
glEnableVertexAttribArray(vertexPositionLoc);
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
// Bind the VBO of normals // Bind the VBO of normals
mVBONormals.bind(); mVBONormals.bind();
glEnableVertexAttribArray(vertexNormalLoc);
glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
if (hasTexture()) { if (hasTexture()) {
// Bind the VBO of texture coords // Bind the VBO of texture coords
mVBOTextureCoords.bind(); mVBOTextureCoords.bind();
glEnableVertexAttribArray(vertexTexCoordLoc);
glVertexAttribPointer(vertexTexCoordLoc, 2, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
} }
// Bind the VBO of indices // Bind the VBO of indices
@ -316,9 +318,6 @@ void Dumbbell::createVBOAndVAO(openglframework::Shader& shader) {
// Unbind the VAO // Unbind the VAO
mVAO.unbind(); mVAO.unbind();
// Unbind the shader
shader.unbind();
} }
// Reset the transform // Reset the transform

View File

@ -73,7 +73,7 @@ class Dumbbell : public openglframework::Mesh {
// -------------------- Methods -------------------- // // -------------------- Methods -------------------- //
// Create the Vertex Buffer Objects used to render with OpenGL. // Create the Vertex Buffer Objects used to render with OpenGL.
void createVBOAndVAO(openglframework::Shader& shader); void createVBOAndVAO();
public : public :
@ -81,11 +81,11 @@ class Dumbbell : public openglframework::Mesh {
/// Constructor /// Constructor
Dumbbell(const openglframework::Vector3& position, rp3d::DynamicsWorld* dynamicsWorld, Dumbbell(const openglframework::Vector3& position, rp3d::DynamicsWorld* dynamicsWorld,
const std::string& meshFolderPath, openglframework::Shader &shader); const std::string& meshFolderPath);
/// Constructor /// Constructor
Dumbbell(const openglframework::Vector3& position, rp3d::CollisionWorld* world, Dumbbell(const openglframework::Vector3& position, rp3d::CollisionWorld* world,
const std::string& meshFolderPath, openglframework::Shader& shader); const std::string& meshFolderPath);
/// Destructor /// Destructor

View File

@ -47,11 +47,12 @@ void Line::render(openglframework::Shader& shader,
shader.bind(); shader.bind();
// Set the model to camera matrix // Set the model to camera matrix
shader.setMatrix4x4Uniform("localToCameraMatrix", worldToCameraMatrix); shader.setMatrix4x4Uniform("localToWorldMatrix", openglframework::Matrix4::identity());
shader.setMatrix4x4Uniform("worldToCameraMatrix", worldToCameraMatrix);
// Set the vertex color // Set the vertex color
openglframework::Vector4 color(1, 0, 0, 1); openglframework::Vector4 color(1, 0, 0, 1);
shader.setVector4Uniform("vertexColor", color); shader.setVector4Uniform("vertexColor", color, false);
glBegin(GL_LINES); glBegin(GL_LINES);
glVertex3f(mWorldPoint1.x, mWorldPoint1.y, mWorldPoint1.z); glVertex3f(mWorldPoint1.x, mWorldPoint1.y, mWorldPoint1.z);

View File

@ -36,7 +36,7 @@ int Sphere::totalNbSpheres = 0;
// Constructor // Constructor
Sphere::Sphere(float radius, const openglframework::Vector3 &position, Sphere::Sphere(float radius, const openglframework::Vector3 &position,
reactphysics3d::CollisionWorld* world, reactphysics3d::CollisionWorld* world,
const std::string& meshFolderPath, openglframework::Shader& shader) const std::string& meshFolderPath)
: openglframework::Mesh(), mRadius(radius), mColor(0.5f, 0.5f, 0.5f, 1.0f) { : openglframework::Mesh(), mRadius(radius), mColor(0.5f, 0.5f, 0.5f, 1.0f) {
// Load the mesh from a file // Load the mesh from a file
@ -76,7 +76,7 @@ Sphere::Sphere(float radius, const openglframework::Vector3 &position,
// Create the VBOs and VAO // Create the VBOs and VAO
if (totalNbSpheres == 0) { if (totalNbSpheres == 0) {
createVBOAndVAO(shader); createVBOAndVAO();
} }
totalNbSpheres++; totalNbSpheres++;
@ -85,7 +85,7 @@ Sphere::Sphere(float radius, const openglframework::Vector3 &position,
// Constructor // Constructor
Sphere::Sphere(float radius, const openglframework::Vector3 &position, Sphere::Sphere(float radius, const openglframework::Vector3 &position,
float mass, reactphysics3d::DynamicsWorld* world, float mass, reactphysics3d::DynamicsWorld* world,
const std::string& meshFolderPath, openglframework::Shader& shader) const std::string& meshFolderPath)
: openglframework::Mesh(), mRadius(radius), mColor(0.5f, 0.5f, 0.5f, 1.0f) { : openglframework::Mesh(), mRadius(radius), mColor(0.5f, 0.5f, 0.5f, 1.0f) {
// Load the mesh from a file // Load the mesh from a file
@ -125,7 +125,7 @@ Sphere::Sphere(float radius, const openglframework::Vector3 &position,
// Create the VBOs and VAO // Create the VBOs and VAO
if (totalNbSpheres == 0) { if (totalNbSpheres == 0) {
createVBOAndVAO(shader); createVBOAndVAO();
} }
totalNbSpheres++; totalNbSpheres++;
@ -157,27 +157,45 @@ void Sphere::render(openglframework::Shader& shader,
shader.bind(); shader.bind();
// Set the model to camera matrix // Set the model to camera matrix
const openglframework::Matrix4 localToCameraMatrix = worldToCameraMatrix * mTransformMatrix; shader.setMatrix4x4Uniform("localToWorldMatrix", mTransformMatrix);
shader.setMatrix4x4Uniform("localToCameraMatrix", localToCameraMatrix); shader.setMatrix4x4Uniform("worldToCameraMatrix", worldToCameraMatrix);
// Set the normal matrix (inverse transpose of the 3x3 upper-left sub matrix of the // Set the normal matrix (inverse transpose of the 3x3 upper-left sub matrix of the
// model-view matrix) // model-view matrix)
const openglframework::Matrix4 localToCameraMatrix = worldToCameraMatrix * mTransformMatrix;
const openglframework::Matrix3 normalMatrix = const openglframework::Matrix3 normalMatrix =
localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose(); localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose();
shader.setMatrix3x3Uniform("normalMatrix", normalMatrix); shader.setMatrix3x3Uniform("normalMatrix", normalMatrix, false);
// Set the vertex color // Set the vertex color
openglframework::Vector4 color(mColor.r, mColor.g, mColor.b, mColor.a); openglframework::Vector4 color(mColor.r, mColor.g, mColor.b, mColor.a);
shader.setVector4Uniform("vertexColor", color); shader.setVector4Uniform("vertexColor", color, false);
// Bind the VAO // Bind the VAO
mVAO.bind(); mVAO.bind();
mVBOVertices.bind();
// Get the location of shader attribute variables
GLint vertexPositionLoc = shader.getAttribLocation("vertexPosition");
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal", false);
glEnableVertexAttribArray(vertexPositionLoc);
if (vertexNormalLoc != -1) glEnableVertexAttribArray(vertexNormalLoc);
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
if (vertexNormalLoc != -1) glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
// For each part of the mesh // For each part of the mesh
for (unsigned int i=0; i<getNbParts(); i++) { for (unsigned int i=0; i<getNbParts(); i++) {
glDrawElements(GL_TRIANGLES, getNbFaces(i) * 3, GL_UNSIGNED_INT, (char*)NULL); glDrawElements(GL_TRIANGLES, getNbFaces(i) * 3, GL_UNSIGNED_INT, (char*)NULL);
} }
glDisableVertexAttribArray(vertexPositionLoc);
if (vertexNormalLoc != -1) glDisableVertexAttribArray(vertexNormalLoc);
mVBOVertices.unbind();
// Unbind the VAO // Unbind the VAO
mVAO.unbind(); mVAO.unbind();
@ -211,15 +229,7 @@ void Sphere::updateTransform(float interpolationFactor) {
// Create the Vertex Buffer Objects used to render with OpenGL. // Create the Vertex Buffer Objects used to render with OpenGL.
/// We create two VBOs (one for vertices and one for indices) /// We create two VBOs (one for vertices and one for indices)
void Sphere::createVBOAndVAO(openglframework::Shader& shader) { void Sphere::createVBOAndVAO() {
// Bind the shader
shader.bind();
// Get the location of shader attribute variables
GLint vertexPositionLoc = shader.getAttribLocation("vertexPosition");
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal");
GLint vertexTexCoordLoc = shader.getAttribLocation("textureCoords");
// Create the VBO for the vertices data // Create the VBO for the vertices data
mVBOVertices.create(); mVBOVertices.create();
@ -257,19 +267,13 @@ void Sphere::createVBOAndVAO(openglframework::Shader& shader) {
// Bind the VBO of vertices // Bind the VBO of vertices
mVBOVertices.bind(); mVBOVertices.bind();
glEnableVertexAttribArray(vertexPositionLoc);
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
// Bind the VBO of normals // Bind the VBO of normals
mVBONormals.bind(); mVBONormals.bind();
glEnableVertexAttribArray(vertexNormalLoc);
glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
if (hasTexture()) { if (hasTexture()) {
// Bind the VBO of texture coords // Bind the VBO of texture coords
mVBOTextureCoords.bind(); mVBOTextureCoords.bind();
glEnableVertexAttribArray(vertexTexCoordLoc);
glVertexAttribPointer(vertexTexCoordLoc, 2, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
} }
// Bind the VBO of indices // Bind the VBO of indices
@ -277,9 +281,6 @@ void Sphere::createVBOAndVAO(openglframework::Shader& shader) {
// Unbind the VAO // Unbind the VAO
mVAO.unbind(); mVAO.unbind();
// Unbind the shader
shader.unbind();
} }
// Reset the transform // Reset the transform

View File

@ -73,7 +73,7 @@ class Sphere : public openglframework::Mesh {
// -------------------- Methods -------------------- // // -------------------- Methods -------------------- //
// Create the Vertex Buffer Objects used to render with OpenGL. // Create the Vertex Buffer Objects used to render with OpenGL.
void createVBOAndVAO(openglframework::Shader& shader); void createVBOAndVAO();
public : public :
@ -81,13 +81,11 @@ class Sphere : public openglframework::Mesh {
/// Constructor /// Constructor
Sphere(float radius, const openglframework::Vector3& position, Sphere(float radius, const openglframework::Vector3& position,
rp3d::CollisionWorld* world, const std::string& meshFolderPath, rp3d::CollisionWorld* world, const std::string& meshFolderPath);
openglframework::Shader& shader);
/// Constructor /// Constructor
Sphere(float radius, const openglframework::Vector3& position, Sphere(float radius, const openglframework::Vector3& position,
float mass, rp3d::DynamicsWorld* dynamicsWorld, const std::string& meshFolderPath, float mass, rp3d::DynamicsWorld* dynamicsWorld, const std::string& meshFolderPath);
openglframework::Shader& shader);
/// Destructor /// Destructor
~Sphere(); ~Sphere();

View File

@ -37,7 +37,6 @@ int VisualContactPoint::totalNbBoxes = 0;
// Constructor // Constructor
VisualContactPoint::VisualContactPoint(const openglframework::Vector3& position, VisualContactPoint::VisualContactPoint(const openglframework::Vector3& position,
openglframework::Shader& shader,
const std::string& meshFolderPath) const std::string& meshFolderPath)
: mColor(1.0f, 0.0f, 0.0f, 1.0f) { : mColor(1.0f, 0.0f, 0.0f, 1.0f) {
@ -47,7 +46,7 @@ VisualContactPoint::VisualContactPoint(const openglframework::Vector3& position,
// Create the VBOs and VAO // Create the VBOs and VAO
if (totalNbBoxes == 0) { if (totalNbBoxes == 0) {
createStaticData(meshFolderPath); createStaticData(meshFolderPath);
createVBOAndVAO(shader); createVBOAndVAO();
} }
totalNbBoxes++; totalNbBoxes++;
@ -96,27 +95,45 @@ void VisualContactPoint::render(openglframework::Shader& shader,
shader.bind(); shader.bind();
// Set the model to camera matrix // Set the model to camera matrix
const openglframework::Matrix4 localToCameraMatrix = worldToCameraMatrix * mTransformMatrix; shader.setMatrix4x4Uniform("localToWorldMatrix", mTransformMatrix);
shader.setMatrix4x4Uniform("localToCameraMatrix", localToCameraMatrix); shader.setMatrix4x4Uniform("worldToCameraMatrix", worldToCameraMatrix);
// Set the normal matrix (inverse transpose of the 3x3 upper-left sub matrix of the // Set the normal matrix (inverse transpose of the 3x3 upper-left sub matrix of the
// model-view matrix) // model-view matrix)
const openglframework::Matrix4 localToCameraMatrix = worldToCameraMatrix * mTransformMatrix;
const openglframework::Matrix3 normalMatrix = const openglframework::Matrix3 normalMatrix =
localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose(); localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose();
shader.setMatrix3x3Uniform("normalMatrix", normalMatrix); shader.setMatrix3x3Uniform("normalMatrix", normalMatrix, false);
// Set the vertex color // Set the vertex color
openglframework::Vector4 color(mColor.r, mColor.g, mColor.b, mColor.a); openglframework::Vector4 color(mColor.r, mColor.g, mColor.b, mColor.a);
shader.setVector4Uniform("vertexColor", color); shader.setVector4Uniform("vertexColor", color, false);
// Bind the VAO // Bind the VAO
mVAO.bind(); mVAO.bind();
mVBOVertices.bind();
// Get the location of shader attribute variables
GLint vertexPositionLoc = shader.getAttribLocation("vertexPosition");
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal", false);
glEnableVertexAttribArray(vertexPositionLoc);
if (vertexNormalLoc != -1) glEnableVertexAttribArray(vertexNormalLoc);
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
if (vertexNormalLoc != -1) glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
// For each part of the mesh // For each part of the mesh
for (unsigned int i=0; i<mMesh.getNbParts(); i++) { for (unsigned int i=0; i<mMesh.getNbParts(); i++) {
glDrawElements(GL_TRIANGLES, mMesh.getNbFaces(i) * 3, GL_UNSIGNED_INT, (char*)NULL); glDrawElements(GL_TRIANGLES, mMesh.getNbFaces(i) * 3, GL_UNSIGNED_INT, (char*)NULL);
} }
glDisableVertexAttribArray(vertexPositionLoc);
if (vertexNormalLoc != -1) glDisableVertexAttribArray(vertexNormalLoc);
mVBOVertices.unbind();
// Unbind the VAO // Unbind the VAO
mVAO.unbind(); mVAO.unbind();
@ -126,15 +143,7 @@ void VisualContactPoint::render(openglframework::Shader& shader,
// Create the Vertex Buffer Objects used to render with OpenGL. // Create the Vertex Buffer Objects used to render with OpenGL.
/// We create two VBOs (one for vertices and one for indices) /// We create two VBOs (one for vertices and one for indices)
void VisualContactPoint::createVBOAndVAO(openglframework::Shader& shader) { void VisualContactPoint::createVBOAndVAO() {
// Bind the shader
shader.bind();
// Get the location of shader attribute variables
GLint vertexPositionLoc = shader.getAttribLocation("vertexPosition");
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal");
GLint vertexTexCoordLoc = shader.getAttribLocation("textureCoords");
// Create the VBO for the vertices data // Create the VBO for the vertices data
mVBOVertices.create(); mVBOVertices.create();
@ -163,20 +172,13 @@ void VisualContactPoint::createVBOAndVAO(openglframework::Shader& shader) {
// Bind the VBO of vertices // Bind the VBO of vertices
mVBOVertices.bind(); mVBOVertices.bind();
glEnableVertexAttribArray(vertexPositionLoc);
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
// Bind the VBO of normals // Bind the VBO of normals
mVBONormals.bind(); mVBONormals.bind();
glEnableVertexAttribArray(vertexNormalLoc);
glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
// Bind the VBO of indices // Bind the VBO of indices
mVBOIndices.bind(); mVBOIndices.bind();
// Unbind the VAO // Unbind the VAO
mVAO.unbind(); mVAO.unbind();
// Unbind the shader
shader.unbind();
} }

View File

@ -63,7 +63,7 @@ class VisualContactPoint : public openglframework::Object3D {
openglframework::Color mColor; openglframework::Color mColor;
// Create the Vertex Buffer Objects used to render with OpenGL. // Create the Vertex Buffer Objects used to render with OpenGL.
static void createVBOAndVAO(openglframework::Shader& shader); static void createVBOAndVAO();
// -------------------- Methods -------------------- // // -------------------- Methods -------------------- //
@ -73,7 +73,6 @@ class VisualContactPoint : public openglframework::Object3D {
/// Constructor /// Constructor
VisualContactPoint(const openglframework::Vector3& position, VisualContactPoint(const openglframework::Vector3& position,
openglframework::Shader &shader,
const std::string &meshFolderPath); const std::string &meshFolderPath);
/// Destructor /// Destructor

View File

@ -37,7 +37,7 @@ FrameBufferObject::FrameBufferObject() : mFrameBufferID(0), mRenderBufferID (0)
// Destructor // Destructor
FrameBufferObject::~FrameBufferObject() { FrameBufferObject::~FrameBufferObject() {
destroy();
} }
// Create the frame buffer object // Create the frame buffer object

View File

@ -48,7 +48,7 @@ Shader::Shader(const std::string vertexShaderFilename, const std::string fragmen
// Destructor // Destructor
Shader::~Shader() { Shader::~Shader() {
destroy();
} }
// Create the shader // Create the shader

View File

@ -180,8 +180,10 @@ inline GLint Shader::getAttribLocation(const std::string& variableName, bool err
// Clear the shader // Clear the shader
inline void Shader::destroy() { inline void Shader::destroy() {
glDeleteProgram(mProgramObjectID); if (mProgramObjectID != 0) {
mProgramObjectID = 0; glDeleteProgram(mProgramObjectID);
mProgramObjectID = 0;
}
} }
// Set a float uniform value to this shader (be careful if the uniform is not // Set a float uniform value to this shader (be careful if the uniform is not

View File

@ -47,7 +47,7 @@ Texture2D::Texture2D(uint width, uint height, uint internalFormat, uint format,
// Destructor // Destructor
Texture2D::~Texture2D() { Texture2D::~Texture2D() {
destroy();
} }
// Create the texture // Create the texture

View File

@ -35,7 +35,7 @@ VertexArrayObject::VertexArrayObject() : mVertexArrayID(0) {
// Destructor // Destructor
VertexArrayObject::~VertexArrayObject() { VertexArrayObject::~VertexArrayObject() {
destroy();
} }
// Create the vertex buffer object // Create the vertex buffer object
@ -63,7 +63,7 @@ bool VertexArrayObject::create() {
void VertexArrayObject::destroy() { void VertexArrayObject::destroy() {
// Delete the vertex buffer object // Delete the vertex buffer object
if (mVertexArrayID) { if (mVertexArrayID != 0) {
glDeleteVertexArrays(1, &mVertexArrayID); glDeleteVertexArrays(1, &mVertexArrayID);
mVertexArrayID = 0; mVertexArrayID = 0;
} }

View File

@ -36,7 +36,7 @@ VertexBufferObject::VertexBufferObject(GLenum targetData)
// Destructor // Destructor
VertexBufferObject::~VertexBufferObject() { VertexBufferObject::~VertexBufferObject() {
destroy();
} }
// Create the vertex buffer object // Create the vertex buffer object
@ -71,7 +71,7 @@ void VertexBufferObject::copyDataIntoVBO(GLsizei size, const void* data, GLenum
void VertexBufferObject::destroy() { void VertexBufferObject::destroy() {
// Delete the vertex buffer object // Delete the vertex buffer object
if (mVertexBufferID) { if (mVertexBufferID != 0) {
glDeleteFramebuffers(1, &mVertexBufferID); glDeleteFramebuffers(1, &mVertexBufferID);
mVertexBufferID = 0; mVertexBufferID = 0;
} }

View File

@ -272,6 +272,13 @@ class Matrix3 {
m[2][0] *= nb; m[2][1] *= nb; m[2][2] *= nb; m[2][0] *= nb; m[2][1] *= nb; m[2][2] *= nb;
return *this; return *this;
} }
// Return the identity matrix
static Matrix3 identity() {
return Matrix3(1, 0, 0,
0, 1, 0,
0, 0, 1);
}
}; };
} }

View File

@ -385,6 +385,9 @@ class Matrix4 {
// Return a 4x4 orthographic projection matrix // Return a 4x4 orthographic projection matrix
static Matrix4 orthoProjectionMatrix(float near, float far, int width, static Matrix4 orthoProjectionMatrix(float near, float far, int width,
int height); int height);
// Return the identity matrix
static Matrix4 identity();
}; };
// * operator // * operator
@ -478,6 +481,14 @@ inline Matrix4 Matrix4::orthoProjectionMatrix(float near, float far, int width,
0, 0, 0, 1); 0, 0, 0, 1);
} }
// Return the identity matrix
inline Matrix4 Matrix4::identity() {
return Matrix4(1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
}
} }
#endif //_MATRIX4_H #endif //_MATRIX4_H

View File

@ -32,19 +32,15 @@ using namespace collisionshapesscene;
// Constructor // Constructor
CollisionShapesScene::CollisionShapesScene(const std::string& name) CollisionShapesScene::CollisionShapesScene(const std::string& name)
: Scene(name), mLight0(0), mPhongShader("shaders/phong.vert", "shaders/phong.frag") { : SceneDemo(name, SCENE_RADIUS) {
std::string meshFolderPath("meshes/"); std::string meshFolderPath("meshes/");
// Move the light 0
mLight0.translateWorld(Vector3(50, 50, 50));
// Compute the radius and the center of the scene // Compute the radius and the center of the scene
float radiusScene = 30.0f;
openglframework::Vector3 center(0, 5, 0); openglframework::Vector3 center(0, 5, 0);
// Set the center of the scene // Set the center of the scene
setScenePosition(center, radiusScene); setScenePosition(center, SCENE_RADIUS);
// Gravity vector in the dynamics world // Gravity vector in the dynamics world
rp3d::Vector3 gravity(0, -9.81, 0); rp3d::Vector3 gravity(0, -9.81, 0);
@ -69,8 +65,7 @@ CollisionShapesScene::CollisionShapesScene(const std::string& name)
radius * sin(angle)); radius * sin(angle));
// Create a convex mesh and a corresponding rigid in the dynamics world // Create a convex mesh and a corresponding rigid in the dynamics world
Dumbbell* dumbbell = new Dumbbell(position, mDynamicsWorld, meshFolderPath, Dumbbell* dumbbell = new Dumbbell(position, mDynamicsWorld, meshFolderPath);
mPhongShader);
// Change the material properties of the rigid body // Change the material properties of the rigid body
rp3d::Material& material = dumbbell->getRigidBody()->getMaterial(); rp3d::Material& material = dumbbell->getRigidBody()->getMaterial();
@ -111,7 +106,7 @@ CollisionShapesScene::CollisionShapesScene(const std::string& name)
// Create a sphere and a corresponding rigid in the dynamics world // Create a sphere and a corresponding rigid in the dynamics world
Sphere* sphere = new Sphere(SPHERE_RADIUS, position , BOX_MASS, mDynamicsWorld, Sphere* sphere = new Sphere(SPHERE_RADIUS, position , BOX_MASS, mDynamicsWorld,
meshFolderPath, mPhongShader); meshFolderPath);
// Change the material properties of the rigid body // Change the material properties of the rigid body
rp3d::Material& material = sphere->getRigidBody()->getMaterial(); rp3d::Material& material = sphere->getRigidBody()->getMaterial();
@ -132,7 +127,7 @@ CollisionShapesScene::CollisionShapesScene(const std::string& name)
// Create a cone and a corresponding rigid in the dynamics world // Create a cone and a corresponding rigid in the dynamics world
Cone* cone = new Cone(CONE_RADIUS, CONE_HEIGHT, position, CONE_MASS, mDynamicsWorld, Cone* cone = new Cone(CONE_RADIUS, CONE_HEIGHT, position, CONE_MASS, mDynamicsWorld,
meshFolderPath, mPhongShader); meshFolderPath);
// Change the material properties of the rigid body // Change the material properties of the rigid body
rp3d::Material& material = cone->getRigidBody()->getMaterial(); rp3d::Material& material = cone->getRigidBody()->getMaterial();
@ -153,7 +148,7 @@ CollisionShapesScene::CollisionShapesScene(const std::string& name)
// Create a cylinder and a corresponding rigid in the dynamics world // Create a cylinder and a corresponding rigid in the dynamics world
Cylinder* cylinder = new Cylinder(CYLINDER_RADIUS, CYLINDER_HEIGHT, position , Cylinder* cylinder = new Cylinder(CYLINDER_RADIUS, CYLINDER_HEIGHT, position ,
CYLINDER_MASS, mDynamicsWorld, meshFolderPath, mPhongShader); CYLINDER_MASS, mDynamicsWorld, meshFolderPath);
// Change the material properties of the rigid body // Change the material properties of the rigid body
rp3d::Material& material = cylinder->getRigidBody()->getMaterial(); rp3d::Material& material = cylinder->getRigidBody()->getMaterial();
@ -174,8 +169,7 @@ CollisionShapesScene::CollisionShapesScene(const std::string& name)
// Create a cylinder and a corresponding rigid in the dynamics world // Create a cylinder and a corresponding rigid in the dynamics world
Capsule* capsule = new Capsule(CAPSULE_RADIUS, CAPSULE_HEIGHT, position , Capsule* capsule = new Capsule(CAPSULE_RADIUS, CAPSULE_HEIGHT, position ,
CAPSULE_MASS, mDynamicsWorld, meshFolderPath, CAPSULE_MASS, mDynamicsWorld, meshFolderPath);
mPhongShader);
// Change the material properties of the rigid body // Change the material properties of the rigid body
rp3d::Material& material = capsule->getRigidBody()->getMaterial(); rp3d::Material& material = capsule->getRigidBody()->getMaterial();
@ -195,8 +189,7 @@ CollisionShapesScene::CollisionShapesScene(const std::string& name)
radius * sin(angle)); radius * sin(angle));
// Create a convex mesh and a corresponding rigid in the dynamics world // Create a convex mesh and a corresponding rigid in the dynamics world
ConvexMesh* mesh = new ConvexMesh(position, MESH_MASS, mDynamicsWorld, meshFolderPath, ConvexMesh* mesh = new ConvexMesh(position, MESH_MASS, mDynamicsWorld, meshFolderPath);
mPhongShader);
// Change the material properties of the rigid body // Change the material properties of the rigid body
rp3d::Material& material = mesh->getRigidBody()->getMaterial(); rp3d::Material& material = mesh->getRigidBody()->getMaterial();
@ -394,70 +387,54 @@ void CollisionShapesScene::update() {
} }
// Render the scene // Render the scene
void CollisionShapesScene::render() { void CollisionShapesScene::renderSinglePass(openglframework::Shader& shader,
const openglframework::Matrix4& worldToCameraMatrix) {
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_CULL_FACE);
// Get the world-space to camera-space matrix
const openglframework::Matrix4 worldToCameraMatrix = mCamera.getTransformMatrix().getInverse();
// Bind the shader // Bind the shader
mPhongShader.bind(); shader.bind();
// Set the variables of the shader
mPhongShader.setMatrix4x4Uniform("projectionMatrix", mCamera.getProjectionMatrix());
mPhongShader.setVector3Uniform("light0PosCameraSpace", worldToCameraMatrix * mLight0.getOrigin());
mPhongShader.setVector3Uniform("lightAmbientColor", Vector3(0.3f, 0.3f, 0.3f));
const Color& diffColLight0 = mLight0.getDiffuseColor();
const Color& specColLight0 = mLight0.getSpecularColor();
mPhongShader.setVector3Uniform("light0DiffuseColor", Vector3(diffColLight0.r, diffColLight0.g, diffColLight0.b));
mPhongShader.setVector3Uniform("light0SpecularColor", Vector3(specColLight0.r, specColLight0.g, specColLight0.b));
mPhongShader.setFloatUniform("shininess", 200.0f);
// Render all the boxes of the scene // Render all the boxes of the scene
for (std::vector<Box*>::iterator it = mBoxes.begin(); it != mBoxes.end(); ++it) { for (std::vector<Box*>::iterator it = mBoxes.begin(); it != mBoxes.end(); ++it) {
(*it)->render(mPhongShader, worldToCameraMatrix); (*it)->render(shader, worldToCameraMatrix);
} }
// Render all the sphere of the scene // Render all the sphere of the scene
for (std::vector<Sphere*>::iterator it = mSpheres.begin(); it != mSpheres.end(); ++it) { for (std::vector<Sphere*>::iterator it = mSpheres.begin(); it != mSpheres.end(); ++it) {
(*it)->render(mPhongShader, worldToCameraMatrix); (*it)->render(shader, worldToCameraMatrix);
} }
// Render all the cones of the scene // Render all the cones of the scene
for (std::vector<Cone*>::iterator it = mCones.begin(); it != mCones.end(); ++it) { for (std::vector<Cone*>::iterator it = mCones.begin(); it != mCones.end(); ++it) {
(*it)->render(mPhongShader, worldToCameraMatrix); (*it)->render(shader, worldToCameraMatrix);
} }
// Render all the cylinders of the scene // Render all the cylinders of the scene
for (std::vector<Cylinder*>::iterator it = mCylinders.begin(); it != mCylinders.end(); ++it) { for (std::vector<Cylinder*>::iterator it = mCylinders.begin(); it != mCylinders.end(); ++it) {
(*it)->render(mPhongShader, worldToCameraMatrix); (*it)->render(shader, worldToCameraMatrix);
} }
// Render all the capsules of the scene // Render all the capsules of the scene
for (std::vector<Capsule*>::iterator it = mCapsules.begin(); it != mCapsules.end(); ++it) { for (std::vector<Capsule*>::iterator it = mCapsules.begin(); it != mCapsules.end(); ++it) {
(*it)->render(mPhongShader, worldToCameraMatrix); (*it)->render(shader, worldToCameraMatrix);
} }
// Render all the convex meshes of the scene // Render all the convex meshes of the scene
for (std::vector<ConvexMesh*>::iterator it = mConvexMeshes.begin(); for (std::vector<ConvexMesh*>::iterator it = mConvexMeshes.begin();
it != mConvexMeshes.end(); ++it) { it != mConvexMeshes.end(); ++it) {
(*it)->render(mPhongShader, worldToCameraMatrix); (*it)->render(shader, worldToCameraMatrix);
} }
// Render all the dumbbells of the scene // Render all the dumbbells of the scene
for (std::vector<Dumbbell*>::iterator it = mDumbbells.begin(); for (std::vector<Dumbbell*>::iterator it = mDumbbells.begin();
it != mDumbbells.end(); ++it) { it != mDumbbells.end(); ++it) {
(*it)->render(mPhongShader, worldToCameraMatrix); (*it)->render(shader, worldToCameraMatrix);
} }
// Render the floor // Render the floor
mFloor->render(mPhongShader, worldToCameraMatrix); mFloor->render(shader, worldToCameraMatrix);
// Unbind the shader // Unbind the shader
mPhongShader.unbind(); shader.unbind();
} }
/// Reset the scene /// Reset the scene

View File

@ -29,7 +29,7 @@
// Libraries // Libraries
#include "openglframework.h" #include "openglframework.h"
#include "reactphysics3d.h" #include "reactphysics3d.h"
#include "Scene.h" #include "SceneDemo.h"
#include "Sphere.h" #include "Sphere.h"
#include "Box.h" #include "Box.h"
#include "Cone.h" #include "Cone.h"
@ -44,6 +44,7 @@ namespace collisionshapesscene {
// Constants // Constants
const float SCENE_RADIUS = 30.0f;
const int NB_BOXES = 3; const int NB_BOXES = 3;
const int NB_CUBES = 3; const int NB_CUBES = 3;
const int NB_CONES = 3; const int NB_CONES = 3;
@ -69,15 +70,12 @@ const float MESH_MASS = 1.0f;
const float FLOOR_MASS = 100.0f; // Floor mass in kilograms const float FLOOR_MASS = 100.0f; // Floor mass in kilograms
// Class CollisionShapesScene // Class CollisionShapesScene
class CollisionShapesScene : public Scene{ class CollisionShapesScene : public SceneDemo {
private : private :
// -------------------- Attributes -------------------- // // -------------------- Attributes -------------------- //
/// Light 0
openglframework::Light mLight0;
/// Phong shader /// Phong shader
openglframework::Shader mPhongShader; openglframework::Shader mPhongShader;
@ -121,8 +119,9 @@ class CollisionShapesScene : public Scene{
/// Take a step for the simulation /// Take a step for the simulation
virtual void update(); virtual void update();
/// Render the scene /// Render the scene in a single pass
virtual void render(); virtual void renderSinglePass(openglframework::Shader& shader,
const openglframework::Matrix4& worldToCameraMatrix);
/// Reset the scene /// Reset the scene
virtual void reset(); virtual void reset();

View File

@ -33,18 +33,17 @@ using namespace jointsscene;
// Constructor // Constructor
JointsScene::JointsScene(const std::string& name) JointsScene::JointsScene(const std::string& name)
: Scene(name), mLight0(0), mPhongShader("shaders/phong.vert", : SceneDemo(name, SCENE_RADIUS), mLight0(0), mPhongShader("shaders/phong.vert",
"shaders/phong.frag") { "shaders/phong.frag") {
// Move the light 0 // Move the light 0
mLight0.translateWorld(Vector3(7, 15, 15)); mLight0.translateWorld(Vector3(7, 15, 15));
// Compute the radius and the center of the scene // Compute the radius and the center of the scene
float radiusScene = 30.0f;
openglframework::Vector3 center(0, 5, 0); openglframework::Vector3 center(0, 5, 0);
// Set the center of the scene // Set the center of the scene
setScenePosition(center, radiusScene); setScenePosition(center, SCENE_RADIUS);
// Gravity vector in the dynamics world // Gravity vector in the dynamics world
rp3d::Vector3 gravity(0, rp3d::decimal(-9.81), 0); rp3d::Vector3 gravity(0, rp3d::decimal(-9.81), 0);
@ -168,43 +167,27 @@ void JointsScene::update() {
} }
// Render the scene // Render the scene
void JointsScene::render() { void JointsScene::renderSinglePass(openglframework::Shader& shader,
const openglframework::Matrix4& worldToCameraMatrix) {
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_CULL_FACE);
// Get the world-space to camera-space matrix
const openglframework::Matrix4 worldToCameraMatrix = mCamera.getTransformMatrix().getInverse();
// Bind the shader // Bind the shader
mPhongShader.bind(); shader.bind();
// Set the variables of the shader
mPhongShader.setVector3Uniform("light0PosCameraSpace",worldToCameraMatrix * mLight0.getOrigin());
mPhongShader.setMatrix4x4Uniform("projectionMatrix", mCamera.getProjectionMatrix());
mPhongShader.setVector3Uniform("lightAmbientColor", Vector3(0.3f, 0.3f, 0.3f));
const Color& diffCol = mLight0.getDiffuseColor();
const Color& specCol = mLight0.getSpecularColor();
mPhongShader.setVector3Uniform("light0DiffuseColor", Vector3(diffCol.r, diffCol.g, diffCol.b));
mPhongShader.setVector3Uniform("light0SpecularColor", Vector3(specCol.r, specCol.g, specCol.b));
mPhongShader.setFloatUniform("shininess", 60.0f);
// Render all the boxes // Render all the boxes
mSliderJointBottomBox->render(mPhongShader, worldToCameraMatrix); mSliderJointBottomBox->render(shader, worldToCameraMatrix);
mSliderJointTopBox->render(mPhongShader, worldToCameraMatrix); mSliderJointTopBox->render(shader, worldToCameraMatrix);
mPropellerBox->render(mPhongShader, worldToCameraMatrix); mPropellerBox->render(shader, worldToCameraMatrix);
mFixedJointBox1->render(mPhongShader, worldToCameraMatrix); mFixedJointBox1->render(shader, worldToCameraMatrix);
mFixedJointBox2->render(mPhongShader, worldToCameraMatrix); mFixedJointBox2->render(shader, worldToCameraMatrix);
for (int i=0; i<NB_BALLSOCKETJOINT_BOXES; i++) { for (int i=0; i<NB_BALLSOCKETJOINT_BOXES; i++) {
mBallAndSocketJointChainBoxes[i]->render(mPhongShader, worldToCameraMatrix); mBallAndSocketJointChainBoxes[i]->render(shader, worldToCameraMatrix);
} }
// Render the floor // Render the floor
mFloor->render(mPhongShader, worldToCameraMatrix); mFloor->render(shader, worldToCameraMatrix);
// Unbind the shader // Unbind the shader
mPhongShader.unbind(); shader.unbind();
} }
// Reset the scene // Reset the scene

View File

@ -30,11 +30,12 @@
#include "openglframework.h" #include "openglframework.h"
#include "reactphysics3d.h" #include "reactphysics3d.h"
#include "Box.h" #include "Box.h"
#include "Scene.h" #include "SceneDemo.h"
namespace jointsscene { namespace jointsscene {
// Constants // Constants
const float SCENE_RADIUS = 30.0f;
const openglframework::Vector3 BOX_SIZE(2, 2, 2); // Box dimensions in meters const openglframework::Vector3 BOX_SIZE(2, 2, 2); // Box dimensions in meters
const openglframework::Vector3 FLOOR_SIZE(20, 0.5f, 20); // Floor dimensions in meters const openglframework::Vector3 FLOOR_SIZE(20, 0.5f, 20); // Floor dimensions in meters
const float BOX_MASS = 1.0f; // Box mass in kilograms const float BOX_MASS = 1.0f; // Box mass in kilograms
@ -43,7 +44,7 @@ const int NB_BALLSOCKETJOINT_BOXES = 7; // Number of Ball-An
const int NB_HINGE_BOXES = 7; // Number of Hinge chain boxes const int NB_HINGE_BOXES = 7; // Number of Hinge chain boxes
// Class JointsScene // Class JointsScene
class JointsScene : public Scene { class JointsScene : public SceneDemo {
protected : protected :
@ -134,8 +135,9 @@ class JointsScene : public Scene {
/// Take a step for the simulation /// Take a step for the simulation
virtual void update(); virtual void update();
/// Render the scene /// Render the scene in a single pass
virtual void render(); virtual void renderSinglePass(openglframework::Shader& shader,
const openglframework::Matrix4& worldToCameraMatrix);
/// Reset the scene /// Reset the scene
virtual void reset(); virtual void reset();

View File

@ -32,7 +32,7 @@ using namespace raycastscene;
// Constructor // Constructor
RaycastScene::RaycastScene(const std::string& name) RaycastScene::RaycastScene(const std::string& name)
: Scene(name), mLight0(0), mCurrentBodyIndex(-1), mAreNormalsDisplayed(false), : SceneDemo(name, SCENE_RADIUS), mLight0(0), mCurrentBodyIndex(-1), mAreNormalsDisplayed(false),
mPhongShader("shaders/phong.vert", "shaders/phong.frag"), mPhongShader("shaders/phong.vert", "shaders/phong.frag"),
mMeshFolderPath("meshes/"), mRaycastManager(mPhongShader, mMeshFolderPath), mMeshFolderPath("meshes/"), mRaycastManager(mPhongShader, mMeshFolderPath),
mVBOVertices(GL_ARRAY_BUFFER) { mVBOVertices(GL_ARRAY_BUFFER) {
@ -41,11 +41,10 @@ RaycastScene::RaycastScene(const std::string& name)
mLight0.translateWorld(Vector3(50, 50, 50)); mLight0.translateWorld(Vector3(50, 50, 50));
// Compute the radius and the center of the scene // Compute the radius and the center of the scene
float radiusScene = 30.0f;
openglframework::Vector3 center(0, 0, 0); openglframework::Vector3 center(0, 0, 0);
// Set the center of the scene // Set the center of the scene
setScenePosition(center, radiusScene); setScenePosition(center, SCENE_RADIUS);
// Create the dynamics world for the physics simulation // Create the dynamics world for the physics simulation
mCollisionWorld = new rp3d::CollisionWorld(); mCollisionWorld = new rp3d::CollisionWorld();
@ -54,7 +53,7 @@ RaycastScene::RaycastScene(const std::string& name)
openglframework::Vector3 position1(0, 0, 0); openglframework::Vector3 position1(0, 0, 0);
// Create a convex mesh and a corresponding collision body in the dynamics world // Create a convex mesh and a corresponding collision body in the dynamics world
mDumbbell = new Dumbbell(position1, mCollisionWorld, mMeshFolderPath, mPhongShader); mDumbbell = new Dumbbell(position1, mCollisionWorld, mMeshFolderPath);
// ---------- Box ---------- // // ---------- Box ---------- //
openglframework::Vector3 position2(0, 0, 0); openglframework::Vector3 position2(0, 0, 0);
@ -68,34 +67,34 @@ RaycastScene::RaycastScene(const std::string& name)
// Create a sphere and a corresponding collision body in the dynamics world // Create a sphere and a corresponding collision body in the dynamics world
mSphere = new Sphere(SPHERE_RADIUS, position3, mCollisionWorld, mSphere = new Sphere(SPHERE_RADIUS, position3, mCollisionWorld,
mMeshFolderPath, mPhongShader); mMeshFolderPath);
// ---------- Cone ---------- // // ---------- Cone ---------- //
openglframework::Vector3 position4(0, 0, 0); openglframework::Vector3 position4(0, 0, 0);
// Create a cone and a corresponding collision body in the dynamics world // Create a cone and a corresponding collision body in the dynamics world
mCone = new Cone(CONE_RADIUS, CONE_HEIGHT, position4, mCollisionWorld, mCone = new Cone(CONE_RADIUS, CONE_HEIGHT, position4, mCollisionWorld,
mMeshFolderPath, mPhongShader); mMeshFolderPath);
// ---------- Cylinder ---------- // // ---------- Cylinder ---------- //
openglframework::Vector3 position5(0, 0, 0); openglframework::Vector3 position5(0, 0, 0);
// Create a cylinder and a corresponding collision body in the dynamics world // Create a cylinder and a corresponding collision body in the dynamics world
mCylinder = new Cylinder(CYLINDER_RADIUS, CYLINDER_HEIGHT, position5, mCylinder = new Cylinder(CYLINDER_RADIUS, CYLINDER_HEIGHT, position5,
mCollisionWorld, mMeshFolderPath, mPhongShader); mCollisionWorld, mMeshFolderPath);
// ---------- Capsule ---------- // // ---------- Capsule ---------- //
openglframework::Vector3 position6(0, 0, 0); openglframework::Vector3 position6(0, 0, 0);
// Create a cylinder and a corresponding collision body in the dynamics world // Create a cylinder and a corresponding collision body in the dynamics world
mCapsule = new Capsule(CAPSULE_RADIUS, CAPSULE_HEIGHT, position6 , mCapsule = new Capsule(CAPSULE_RADIUS, CAPSULE_HEIGHT, position6 ,
mCollisionWorld, mMeshFolderPath, mPhongShader); mCollisionWorld, mMeshFolderPath);
// ---------- Convex Mesh ---------- // // ---------- Convex Mesh ---------- //
openglframework::Vector3 position7(0, 0, 0); openglframework::Vector3 position7(0, 0, 0);
// Create a convex mesh and a corresponding collision body in the dynamics world // Create a convex mesh and a corresponding collision body in the dynamics world
mConvexMesh = new ConvexMesh(position7, mCollisionWorld, mMeshFolderPath, mPhongShader); mConvexMesh = new ConvexMesh(position7, mCollisionWorld, mMeshFolderPath);
// Create the lines that will be used for raycasting // Create the lines that will be used for raycasting
createLines(); createLines();
@ -266,41 +265,26 @@ void RaycastScene::update() {
} }
// Render the scene // Render the scene
void RaycastScene::render() { void RaycastScene::renderSinglePass(openglframework::Shader& shader,
const openglframework::Matrix4& worldToCameraMatrix) {
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_CULL_FACE);
// Bind the shader // Bind the shader
mPhongShader.bind(); shader.bind();
// Get the world-space to camera-space matrix
const openglframework::Matrix4 worldToCameraMatrix = mCamera.getTransformMatrix().getInverse();
// Set the variables of the shader
mPhongShader.setMatrix4x4Uniform("projectionMatrix", mCamera.getProjectionMatrix());
mPhongShader.setVector3Uniform("light0PosCameraSpace",worldToCameraMatrix * mLight0.getOrigin());
mPhongShader.setVector3Uniform("lightAmbientColor", Vector3(0.3f, 0.3f, 0.3f));
const Color& diffCol = mLight0.getDiffuseColor();
const Color& specCol = mLight0.getSpecularColor();
mPhongShader.setVector3Uniform("light0DiffuseColor", Vector3(diffCol.r, diffCol.g, diffCol.b));
mPhongShader.setVector3Uniform("light0SpecularColor", Vector3(specCol.r, specCol.g, specCol.b));
mPhongShader.setFloatUniform("shininess", 60.0f);
// Set the model to camera matrix // Set the model to camera matrix
const openglframework::Matrix4 localToCameraMatrix = worldToCameraMatrix; const Matrix4 localToCameraMatrix = Matrix4::identity();
mPhongShader.setMatrix4x4Uniform("localToCameraMatrix", localToCameraMatrix); shader.setMatrix4x4Uniform("localToWorldMatrix", localToCameraMatrix);
shader.setMatrix4x4Uniform("worldToCameraMatrix", worldToCameraMatrix);
// Set the normal matrix (inverse transpose of the 3x3 upper-left sub matrix of the // Set the normal matrix (inverse transpose of the 3x3 upper-left sub matrix of the
// model-view matrix) // model-view matrix)
const openglframework::Matrix3 normalMatrix = const openglframework::Matrix3 normalMatrix =
localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose(); localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose();
mPhongShader.setMatrix3x3Uniform("normalMatrix", normalMatrix); shader.setMatrix3x3Uniform("normalMatrix", normalMatrix, false);
// Set the vertex color // Set the vertex color
openglframework::Vector4 color(1, 0, 0, 1); openglframework::Vector4 color(1, 0, 0, 1);
mPhongShader.setVector4Uniform("vertexColor", color); shader.setVector4Uniform("vertexColor", color, false);
// Bind the VAO // Bind the VAO
mVAO.bind(); mVAO.bind();
@ -311,41 +295,40 @@ void RaycastScene::render() {
// Unbind the VAO // Unbind the VAO
mVAO.unbind(); mVAO.unbind();
mPhongShader.unbind(); shader.unbind();
// Render the shapes // Render the shapes
if (mBox->getCollisionBody()->isActive()) mBox->render(mPhongShader, worldToCameraMatrix); if (mBox->getCollisionBody()->isActive()) mBox->render(shader, worldToCameraMatrix);
if (mSphere->getCollisionBody()->isActive()) mSphere->render(mPhongShader, worldToCameraMatrix); if (mSphere->getCollisionBody()->isActive()) mSphere->render(shader, worldToCameraMatrix);
if (mCone->getCollisionBody()->isActive()) mCone->render(mPhongShader, worldToCameraMatrix); if (mCone->getCollisionBody()->isActive()) mCone->render(shader, worldToCameraMatrix);
if (mCylinder->getCollisionBody()->isActive()) mCylinder->render(mPhongShader, worldToCameraMatrix); if (mCylinder->getCollisionBody()->isActive()) mCylinder->render(shader, worldToCameraMatrix);
if (mCapsule->getCollisionBody()->isActive()) mCapsule->render(mPhongShader, worldToCameraMatrix); if (mCapsule->getCollisionBody()->isActive()) mCapsule->render(shader, worldToCameraMatrix);
if (mConvexMesh->getCollisionBody()->isActive()) mConvexMesh->render(mPhongShader, worldToCameraMatrix); if (mConvexMesh->getCollisionBody()->isActive()) mConvexMesh->render(shader, worldToCameraMatrix);
if (mDumbbell->getCollisionBody()->isActive()) mDumbbell->render(mPhongShader, worldToCameraMatrix); if (mDumbbell->getCollisionBody()->isActive()) mDumbbell->render(shader, worldToCameraMatrix);
//mPhongShader.unbind(); //mPhongShader.unbind();
mPhongShader.bind(); shader.bind();
mPhongShader.setVector3Uniform("light0SpecularColor", Vector3(0, 0, 0));
openglframework::Vector4 redColor(1, 0, 0, 1); openglframework::Vector4 redColor(1, 0, 0, 1);
mPhongShader.setVector4Uniform("vertexColor", redColor); shader.setVector4Uniform("vertexColor", redColor, false);
// Render all the raycast hit points // Render all the raycast hit points
mRaycastManager.render(worldToCameraMatrix, mAreNormalsDisplayed); mRaycastManager.render(worldToCameraMatrix, mAreNormalsDisplayed);
mPhongShader.unbind(); shader.unbind();
mPhongShader.bind(); shader.bind();
openglframework::Vector4 blueColor(0, 0.62, 0.92, 1); openglframework::Vector4 blueColor(0, 0.62, 0.92, 1);
mPhongShader.setVector4Uniform("vertexColor", blueColor); shader.setVector4Uniform("vertexColor", blueColor, false);
// Render the lines // Render the lines
for (std::vector<Line*>::iterator it = mLines.begin(); it != mLines.end(); for (std::vector<Line*>::iterator it = mLines.begin(); it != mLines.end();
++it) { ++it) {
(*it)->render(mPhongShader, worldToCameraMatrix); (*it)->render(shader, worldToCameraMatrix);
} }
// Unbind the shader // Unbind the shader
mPhongShader.unbind(); shader.unbind();
} }
// Create the Vertex Buffer Objects used to render with OpenGL. // Create the Vertex Buffer Objects used to render with OpenGL.

View File

@ -31,7 +31,7 @@
#include <cmath> #include <cmath>
#include "openglframework.h" #include "openglframework.h"
#include "reactphysics3d.h" #include "reactphysics3d.h"
#include "Scene.h" #include "SceneDemo.h"
#include "Sphere.h" #include "Sphere.h"
#include "Box.h" #include "Box.h"
#include "Cone.h" #include "Cone.h"
@ -46,6 +46,7 @@
namespace raycastscene { namespace raycastscene {
// Constants // Constants
const float SCENE_RADIUS = 30.0f;
const openglframework::Vector3 BOX_SIZE(4, 2, 1); const openglframework::Vector3 BOX_SIZE(4, 2, 1);
const float SPHERE_RADIUS = 3.0f; const float SPHERE_RADIUS = 3.0f;
const float CONE_RADIUS = 3.0f; const float CONE_RADIUS = 3.0f;
@ -87,7 +88,7 @@ class RaycastManager : public rp3d::RaycastCallback {
virtual rp3d::decimal notifyRaycastHit(const rp3d::RaycastInfo& raycastInfo) { virtual rp3d::decimal notifyRaycastHit(const rp3d::RaycastInfo& raycastInfo) {
rp3d::Vector3 hitPos = raycastInfo.worldPoint; rp3d::Vector3 hitPos = raycastInfo.worldPoint;
openglframework::Vector3 position(hitPos.x, hitPos.y, hitPos.z); openglframework::Vector3 position(hitPos.x, hitPos.y, hitPos.z);
VisualContactPoint* point = new VisualContactPoint(position, mShader, mMeshFolderPath); VisualContactPoint* point = new VisualContactPoint(position, mMeshFolderPath);
mHitPoints.push_back(point); mHitPoints.push_back(point);
// Create a line to display the normal at hit point // Create a line to display the normal at hit point
@ -137,7 +138,7 @@ class RaycastManager : public rp3d::RaycastCallback {
}; };
// Class RaycastScene // Class RaycastScene
class RaycastScene : public Scene { class RaycastScene : public SceneDemo {
private : private :
@ -211,8 +212,9 @@ class RaycastScene : public Scene {
/// Take a step for the simulation /// Take a step for the simulation
virtual void update(); virtual void update();
/// Render the scene /// Render the scene in a single pass
virtual void render(); virtual void renderSinglePass(openglframework::Shader& shader,
const openglframework::Matrix4& worldToCameraMatrix);
/// Reset the scene /// Reset the scene
virtual void reset(); virtual void reset();

View File

@ -70,12 +70,14 @@ void main() {
vec3 specular = light0SpecularColor * specularFactor; vec3 specular = light0SpecularColor * specularFactor;
// Compute shadow factor // Compute shadow factor
vec4 shadowMapCoordsOverW = shadowMapCoords / shadowMapCoords.w ; float shadowBias = 0.005;
//shadowMapCoordsOverW += 0.0005; vec4 shadowMapUV = shadowMapCoords;
shadowMapUV.z -= shadowBias;
vec4 shadowMapCoordsOverW = shadowMapUV / shadowMapUV.w ;
float distanceInShadowMap = texture(shadowMapSampler, shadowMapCoordsOverW.xy).r; float distanceInShadowMap = texture(shadowMapSampler, shadowMapCoordsOverW.xy).r;
float shadow = 0.0; float shadow = 0.0;
if (shadowMapCoords.w > 0) { if (shadowMapCoords.w > 0) {
shadow = distanceInShadowMap < shadowMapCoordsOverW.z ? 0.0 : 1.0; shadow = distanceInShadowMap < shadowMapCoordsOverW.z ? 0.5 : 1.0;
} }
// Compute the final color // Compute the final color

View File

@ -37,11 +37,11 @@ SceneDemo::SceneDemo(const std::string& name, float sceneRadius) : Scene(name),
mVBOQuad(GL_ARRAY_BUFFER) { mVBOQuad(GL_ARRAY_BUFFER) {
// Move the light0 // Move the light0
mLight0.translateWorld(Vector3(0, 60, 00)); mLight0.translateWorld(Vector3(0, 40, 40));
// Camera at light0 postion for the shadow map // Camera at light0 postion for the shadow map
mShadowMapLightCamera.translateWorld(mLight0.getOrigin()); mShadowMapLightCamera.translateWorld(mLight0.getOrigin());
mShadowMapLightCamera.rotateLocal(Vector3(1, 0, 0), -PI / 2.0f); mShadowMapLightCamera.rotateLocal(Vector3(1, 0, 0), -PI / 4.0f);
mShadowMapLightCamera.setDimensions(SHADOWMAP_WIDTH, SHADOWMAP_HEIGHT); mShadowMapLightCamera.setDimensions(SHADOWMAP_WIDTH, SHADOWMAP_HEIGHT);
mShadowMapLightCamera.setFieldOfView(70.0f); mShadowMapLightCamera.setFieldOfView(70.0f);
mShadowMapLightCamera.setSceneRadius(200); mShadowMapLightCamera.setSceneRadius(200);
@ -64,6 +64,8 @@ SceneDemo::~SceneDemo() {
mShadowMapTexture.destroy(); mShadowMapTexture.destroy();
mFBOShadowMap.destroy(); mFBOShadowMap.destroy();
mVBOQuad.destroy();
} }
// Render the scene (in multiple passes for shadow mapping) // Render the scene (in multiple passes for shadow mapping)
@ -75,7 +77,7 @@ void SceneDemo::render() {
// ---------- Render the scene to generate the shadow map (first pass) ----------- // // ---------- Render the scene to generate the shadow map (first pass) ----------- //
// Culling switching, rendering only backface, this is done to avoid self-shadowing // Culling switching, rendering only backface, this is done to avoid self-shadowing
glCullFace(GL_FRONT); glCullFace(GL_BACK);
Matrix4 shadowMapProjMatrix = mShadowMapLightCamera.getProjectionMatrix(); Matrix4 shadowMapProjMatrix = mShadowMapLightCamera.getProjectionMatrix();

View File

@ -30,8 +30,8 @@
#include "Scene.h" #include "Scene.h"
// Constants // Constants
const int SHADOWMAP_WIDTH = 1024; const int SHADOWMAP_WIDTH = 2048;
const int SHADOWMAP_HEIGHT = 1024; const int SHADOWMAP_HEIGHT = 2048;
// Class SceneDemo // Class SceneDemo
// Abstract class that represents a 3D scene for the ReactPhysics3D examples. // Abstract class that represents a 3D scene for the ReactPhysics3D examples.