From fadedbdb3097b01168a3a14e4bb7886006098ba3 Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Sun, 24 May 2015 17:23:55 +0200 Subject: [PATCH] Modify rendering to use OpenGL 3.3 --- testbed/common/Box.cpp | 90 ++++++++----- testbed/common/Box.h | 11 +- testbed/common/Capsule.cpp | 124 ++++++++++++++--- testbed/common/Capsule.h | 26 +++- testbed/common/Cone.cpp | 126 +++++++++++++++--- testbed/common/Cone.h | 26 +++- testbed/common/ConvexMesh.cpp | 124 ++++++++++++++--- testbed/common/ConvexMesh.h | 26 +++- testbed/common/Cylinder.cpp | 126 +++++++++++++++--- testbed/common/Cylinder.h | 27 +++- testbed/common/Dumbbell.cpp | 124 ++++++++++++++--- testbed/common/Dumbbell.h | 25 +++- testbed/common/Sphere.cpp | 123 ++++++++++++++--- testbed/common/Sphere.h | 27 +++- testbed/opengl-framework/CMakeLists.txt | 2 + testbed/opengl-framework/src/Shader.cpp | 4 +- testbed/opengl-framework/src/Shader.h | 30 ++++- .../src/VertexBufferObject.cpp | 6 - .../opengl-framework/src/openglframework.h | 1 + .../collisionshapes/CollisionShapesScene.cpp | 19 +-- .../collisionshapes/CollisionShapesScene.h | 8 +- testbed/scenes/cubes/CubesScene.cpp | 36 ++++- testbed/scenes/cubes/CubesScene.h | 3 + testbed/scenes/joints/JointsScene.cpp | 14 +- testbed/scenes/raycast/RaycastScene.cpp | 14 +- testbed/shaders/depth.frag | 2 +- testbed/shaders/depth.vert | 2 +- testbed/shaders/phong.frag | 21 +-- testbed/shaders/phong.vert | 23 ++-- testbed/src/Gui.cpp | 120 +++++------------ testbed/src/Gui.h | 7 +- testbed/src/TestbedApplication.cpp | 27 +++- 32 files changed, 1019 insertions(+), 325 deletions(-) diff --git a/testbed/common/Box.cpp b/testbed/common/Box.cpp index 68d213e7..29149488 100644 --- a/testbed/common/Box.cpp +++ b/testbed/common/Box.cpp @@ -32,6 +32,7 @@ // Initialize static variables openglframework::VertexBufferObject Box::mVBOVertices(GL_ARRAY_BUFFER); openglframework::VertexBufferObject Box::mVBOIndices(GL_ELEMENT_ARRAY_BUFFER); +openglframework::VertexArrayObject Box::mVAO; bool Box::areVBOsCreated = false; VertexData Box::mCubeVertices[8] = { {openglframework::Vector3(1,1,1),openglframework::Vector3(1,1,1),openglframework::Color(1,0,0,1)}, @@ -58,7 +59,7 @@ GLuint Box::mCubeIndices[36] = { 0, 1, 2, // Constructor Box::Box(const openglframework::Vector3& size, const openglframework::Vector3 &position, - reactphysics3d::CollisionWorld* world) + reactphysics3d::CollisionWorld* world, openglframework::Shader& shader) : openglframework::Object3D(), mColor(0.5f, 0.5f, 0.5f, 1.0f) { // Initialize the size of the box @@ -95,8 +96,9 @@ Box::Box(const openglframework::Vector3& size, const openglframework::Vector3 &p // If the Vertex Buffer object has not been created yet if (!areVBOsCreated) { + // Create the Vertex Buffer - createVBO(); + createVBOAndVAO(shader); } mTransformMatrix = mTransformMatrix * mScalingMatrix; @@ -104,7 +106,7 @@ Box::Box(const openglframework::Vector3& size, const openglframework::Vector3 &p // Constructor Box::Box(const openglframework::Vector3& size, const openglframework::Vector3 &position, - float mass, reactphysics3d::DynamicsWorld* world) + float mass, reactphysics3d::DynamicsWorld* world, openglframework::Shader& shader) : openglframework::Object3D(), mColor(0.5f, 0.5f, 0.5f, 1.0f) { // Initialize the size of the box @@ -142,7 +144,7 @@ Box::Box(const openglframework::Vector3& size, const openglframework::Vector3 &p // If the Vertex Buffer object has not been created yet if (!areVBOsCreated) { // Create the Vertex Buffer - createVBO(); + createVBOAndVAO(shader); } mTransformMatrix = mTransformMatrix * mScalingMatrix; @@ -151,6 +153,15 @@ Box::Box(const openglframework::Vector3& size, const openglframework::Vector3 &p // Destructor Box::~Box() { + if (areVBOsCreated) { + + // Destroy the VBOs and VAO + mVBOIndices.destroy(); + mVBOVertices.destroy(); + mVAO.destroy(); + + areVBOsCreated = false; + } } // Render the cube at the correct position and with the correct orientation @@ -174,33 +185,14 @@ void Box::render(openglframework::Shader& shader, openglframework::Vector4 color(mColor.r, mColor.g, mColor.b, mColor.a); shader.setVector4Uniform("vertexColor", color); - // Bind the vertices VBO - mVBOVertices.bind(); - - // Enable the vertex, normal and color arrays - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_COLOR_ARRAY); - glEnableClientState(GL_NORMAL_ARRAY); - - // Set the arrays pointers - glVertexPointer(3, GL_FLOAT, sizeof(VertexData), MEMBER_OFFSET(VertexData, position)); - glNormalPointer(GL_FLOAT, sizeof(VertexData), MEMBER_OFFSET(VertexData, normal)); - glColorPointer(3, GL_FLOAT, sizeof(VertexData), MEMBER_OFFSET(VertexData, color)); - - // Bind the indices VBO - mVBOIndices.bind(); + // Bind the VAO + mVAO.bind(); // Draw the geometry of the box glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, (char*)NULL); - // Unbind the VBOs - mVBOVertices.unbind(); - mVBOIndices.unbind(); - - // Disable the arrays - glDisableClientState(GL_VERTEX_ARRAY); - glDisableClientState(GL_COLOR_ARRAY); - glDisableClientState(GL_NORMAL_ARRAY); + // Unbind the VAO + mVAO.unbind(); // Unbind the shader shader.unbind(); @@ -233,15 +225,49 @@ void Box::updateTransform(float interpolationFactor) { // Create the Vertex Buffer Objects used to render to box with OpenGL. /// We create two VBOs (one for vertices and one for indices) to render all the boxes /// in the simulation. -void Box::createVBO() { +void Box::createVBOAndVAO(openglframework::Shader& shader) { - // Create the VBOs + // Bind the shader + shader.bind(); + + // Get the location of shader attribute variables + GLint vertexPositionLoc = shader.getAttribLocation("vertexPosition"); + GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal"); + + // Create the VBO for the vertices data mVBOVertices.create(); - mVBOIndices.create(); - - // Copy the data into the VBOs + mVBOVertices.bind(); mVBOVertices.copyDataIntoVBO(sizeof(mCubeVertices), mCubeVertices, GL_STATIC_DRAW); + mVBOVertices.unbind(); + size_t test = sizeof(mCubeVertices); + + // Create th VBO for the indices data + mVBOIndices.create(); + mVBOIndices.bind(); mVBOIndices.copyDataIntoVBO(sizeof(mCubeIndices), mCubeIndices, GL_STATIC_DRAW); + mVBOIndices.unbind(); + + // Create the VAO for both VBOs + mVAO.create(); + mVAO.bind(); + + // Bind the VBO of vertices + mVBOVertices.bind(); + + glEnableVertexAttribArray(vertexPositionLoc); + glEnableVertexAttribArray(vertexNormalLoc); + + glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), MEMBER_OFFSET(VertexData, position)); + glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), MEMBER_OFFSET(VertexData, normal)); + + // Bind the VBO of indices + mVBOIndices.bind(); + + // Unbind the VAO + mVAO.unbind(); + + // Unbind the shader + shader.unbind(); areVBOsCreated = true; } diff --git a/testbed/common/Box.h b/testbed/common/Box.h index 97c86e5b..7d0b2721 100644 --- a/testbed/common/Box.h +++ b/testbed/common/Box.h @@ -68,6 +68,9 @@ class Box : public openglframework::Object3D { /// Vertex Buffer Object for the indices used to render the box with OpenGL static openglframework::VertexBufferObject mVBOIndices; + /// Vertex Array Object for the vertex data + static openglframework::VertexArrayObject mVAO; + /// Vertex data for each vertex of the cube (used to render the box) static VertexData mCubeVertices[8]; @@ -82,8 +85,8 @@ class Box : public openglframework::Object3D { // -------------------- Methods -------------------- // - /// Create a Vertex Buffer Object to render to box with OpenGL - static void createVBO(); + /// Create a the VAO and VBOs to render to box with OpenGL + static void createVBOAndVAO(openglframework::Shader &shader); public : @@ -91,11 +94,11 @@ class Box : public openglframework::Object3D { /// Constructor Box(const openglframework::Vector3& size, const openglframework::Vector3& position, - reactphysics3d::CollisionWorld* world); + reactphysics3d::CollisionWorld* world, openglframework::Shader &shader); /// Constructor Box(const openglframework::Vector3& size, const openglframework::Vector3& position, - float mass, reactphysics3d::DynamicsWorld *world); + float mass, reactphysics3d::DynamicsWorld *world, openglframework::Shader& shader); /// Destructor ~Box(); diff --git a/testbed/common/Capsule.cpp b/testbed/common/Capsule.cpp index 81b6fcbe..2a925a99 100644 --- a/testbed/common/Capsule.cpp +++ b/testbed/common/Capsule.cpp @@ -29,8 +29,10 @@ // Constructor Capsule::Capsule(float radius, float height, const openglframework::Vector3& position, reactphysics3d::CollisionWorld* world, - const std::string& meshFolderPath) - : openglframework::Mesh(), mRadius(radius), mHeight(height) { + const std::string& meshFolderPath, openglframework::Shader& shader) + : openglframework::Mesh(), mRadius(radius), mHeight(height), mVBOVertices(GL_ARRAY_BUFFER), + mVBONormals(GL_ARRAY_BUFFER), mVBOTextureCoords(GL_ARRAY_BUFFER), + mVBOIndices(GL_ELEMENT_ARRAY_BUFFER), mColor(1.0f, 0.5f, 0.5f, 1.0f) { // Load the mesh from a file openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "capsule.obj", *this); @@ -66,13 +68,19 @@ Capsule::Capsule(float radius, float height, const openglframework::Vector3& pos mRigidBody->addCollisionShape(collisionShape, rp3d::Transform::identity()); mTransformMatrix = mTransformMatrix * mScalingMatrix; + + // Create the VBOs and VAO + createVBOAndVAO(shader); } // Constructor Capsule::Capsule(float radius, float height, const openglframework::Vector3& position, float mass, reactphysics3d::DynamicsWorld* dynamicsWorld, - const std::string& meshFolderPath) - : openglframework::Mesh(), mRadius(radius), mHeight(height) { + const std::string& meshFolderPath, openglframework::Shader &shader) + : openglframework::Mesh(), mRadius(radius), mHeight(height), + mVBOVertices(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) { // Load the mesh from a file openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "capsule.obj", *this); @@ -108,6 +116,9 @@ Capsule::Capsule(float radius, float height, const openglframework::Vector3& pos mRigidBody = body; mTransformMatrix = mTransformMatrix * mScalingMatrix; + + // Create the VBOs and VAO + createVBOAndVAO(shader); } // Destructor @@ -115,6 +126,13 @@ Capsule::~Capsule() { // Destroy the mesh destroy(); + + // Destroy the VBOs and VAO + mVBOIndices.destroy(); + mVBOVertices.destroy(); + mVBONormals.destroy(); + mVBOTextureCoords.destroy(); + mVAO.destroy(); } // Render the sphere at the correct position and with the correct orientation @@ -134,29 +152,20 @@ void Capsule::render(openglframework::Shader& shader, localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose(); shader.setMatrix3x3Uniform("normalMatrix", normalMatrix); - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_NORMAL_ARRAY); - if (hasTexture()) { - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - } + // Set the vertex color + openglframework::Vector4 color(mColor.r, mColor.g, mColor.b, mColor.a); + shader.setVector4Uniform("vertexColor", color); - glVertexPointer(3, GL_FLOAT, 0, getVerticesPointer()); - glNormalPointer(GL_FLOAT, 0, getNormalsPointer()); - if(hasTexture()) { - glTexCoordPointer(2, GL_FLOAT, 0, getUVTextureCoordinatesPointer()); - } + // Bind the VAO + mVAO.bind(); // For each part of the mesh for (unsigned int i=0; iaddCollisionShape(collisionShape, rp3d::Transform::identity()); mTransformMatrix = mTransformMatrix * mScalingMatrix; + + // Create the VBOs and VAO + createVBOAndVAO(shader); } // Constructor Cone::Cone(float radius, float height, const openglframework::Vector3 &position, float mass, reactphysics3d::DynamicsWorld* dynamicsWorld, - const std::string& meshFolderPath) - : openglframework::Mesh(), mRadius(radius), mHeight(height) { + const std::string& meshFolderPath, openglframework::Shader &shader) + : openglframework::Mesh(), mRadius(radius), mHeight(height), + mVBOVertices(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) { // Load the mesh from a file openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "cone.obj", *this); @@ -108,6 +117,9 @@ Cone::Cone(float radius, float height, const openglframework::Vector3 &position, mRigidBody = body; mTransformMatrix = mTransformMatrix * mScalingMatrix; + + // Create the VBOs and VAO + createVBOAndVAO(shader); } // Destructor @@ -115,6 +127,13 @@ Cone::~Cone() { // Destroy the mesh destroy(); + + // Destroy the VBOs and VAO + mVBOIndices.destroy(); + mVBOVertices.destroy(); + mVBONormals.destroy(); + mVBOTextureCoords.destroy(); + mVAO.destroy(); } // Render the cone at the correct position and with the correct orientation @@ -134,29 +153,20 @@ void Cone::render(openglframework::Shader& shader, localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose(); shader.setMatrix3x3Uniform("normalMatrix", normalMatrix); - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_NORMAL_ARRAY); - if (hasTexture()) { - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - } + // Set the vertex color + openglframework::Vector4 color(mColor.r, mColor.g, mColor.b, mColor.a); + shader.setVector4Uniform("vertexColor", color); - glVertexPointer(3, GL_FLOAT, 0, getVerticesPointer()); - glNormalPointer(GL_FLOAT, 0, getNormalsPointer()); - if(hasTexture()) { - glTexCoordPointer(2, GL_FLOAT, 0, getUVTextureCoordinatesPointer()); - } + // Bind the VAO + mVAO.bind(); // For each part of the mesh for (unsigned int i=0; iaddCollisionShape(collisionShape, rp3d::Transform::identity()); + + // Create the VBOs and VAO + createVBOAndVAO(shader); } // Constructor ConvexMesh::ConvexMesh(const openglframework::Vector3 &position, float mass, reactphysics3d::DynamicsWorld* dynamicsWorld, - const std::string& meshFolderPath) - : openglframework::Mesh() { + const std::string& meshFolderPath, openglframework::Shader &shader) + : openglframework::Mesh(), mVBOVertices(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) { // Load the mesh from a file openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "convexmesh.obj", *this); @@ -147,6 +155,9 @@ ConvexMesh::ConvexMesh(const openglframework::Vector3 &position, float mass, body->addCollisionShape(collisionShape, rp3d::Transform::identity(), mass); mRigidBody = body; + + // Create the VBOs and VAO + createVBOAndVAO(shader); } // Destructor @@ -154,6 +165,13 @@ ConvexMesh::~ConvexMesh() { // Destroy the mesh destroy(); + + // Destroy the VBOs and VAO + mVBOIndices.destroy(); + mVBOVertices.destroy(); + mVBONormals.destroy(); + mVBOTextureCoords.destroy(); + mVAO.destroy(); } // Render the sphere at the correct position and with the correct orientation @@ -173,29 +191,20 @@ void ConvexMesh::render(openglframework::Shader& shader, localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose(); shader.setMatrix3x3Uniform("normalMatrix", normalMatrix); - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_NORMAL_ARRAY); - if (hasTexture()) { - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - } + // Set the vertex color + openglframework::Vector4 color(mColor.r, mColor.g, mColor.b, mColor.a); + shader.setVector4Uniform("vertexColor", color); - glVertexPointer(3, GL_FLOAT, 0, getVerticesPointer()); - glNormalPointer(GL_FLOAT, 0, getNormalsPointer()); - if(hasTexture()) { - glTexCoordPointer(2, GL_FLOAT, 0, getUVTextureCoordinatesPointer()); - } + // Bind the VAO + mVAO.bind(); // For each part of the mesh for (unsigned int i=0; iaddCollisionShape(collisionShape, rp3d::Transform::identity()); mTransformMatrix = mTransformMatrix * mScalingMatrix; + + // Create the VBOs and VAO + createVBOAndVAO(shader); } // Constructor Cylinder::Cylinder(float radius, float height, const openglframework::Vector3& position, float mass, reactphysics3d::DynamicsWorld* dynamicsWorld, - const std::string& meshFolderPath) - : openglframework::Mesh(), mRadius(radius), mHeight(height) { + const std::string& meshFolderPath, openglframework::Shader& shader) + : openglframework::Mesh(), mRadius(radius), mHeight(height), + mVBOVertices(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) { // Load the mesh from a file openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "cylinder.obj", *this); @@ -108,6 +118,9 @@ Cylinder::Cylinder(float radius, float height, const openglframework::Vector3& p mTransformMatrix = mTransformMatrix * mScalingMatrix; mRigidBody = body; + + // Create the VBOs and VAO + createVBOAndVAO(shader); } // Destructor @@ -115,6 +128,13 @@ Cylinder::~Cylinder() { // Destroy the mesh destroy(); + + // Destroy the VBOs and VAO + mVBOIndices.destroy(); + mVBOVertices.destroy(); + mVBONormals.destroy(); + mVBOTextureCoords.destroy(); + mVAO.destroy(); } // Render the cylinder at the correct position and with the correct orientation @@ -134,29 +154,20 @@ void Cylinder::render(openglframework::Shader& shader, localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose(); shader.setMatrix3x3Uniform("normalMatrix", normalMatrix); - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_NORMAL_ARRAY); - if (hasTexture()) { - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - } + // Set the vertex color + openglframework::Vector4 color(mColor.r, mColor.g, mColor.b, mColor.a); + shader.setVector4Uniform("vertexColor", color); - glVertexPointer(3, GL_FLOAT, 0, getVerticesPointer()); - glNormalPointer(GL_FLOAT, 0, getNormalsPointer()); - if(hasTexture()) { - glTexCoordPointer(2, GL_FLOAT, 0, getUVTextureCoordinatesPointer()); - } + // Bind the VAO + mVAO.bind(); // For each part of the mesh for (unsigned int i=0; iaddCollisionShape(cylinderCollisionShape, transformCylinderShape); mTransformMatrix = mTransformMatrix * mScalingMatrix; + + // Create the VBOs and VAO + createVBOAndVAO(shader); } // Destructor @@ -151,6 +163,13 @@ Dumbbell::~Dumbbell() { // Destroy the mesh destroy(); + + // Destroy the VBOs and VAO + mVBOIndices.destroy(); + mVBOVertices.destroy(); + mVBONormals.destroy(); + mVBOTextureCoords.destroy(); + mVAO.destroy(); } // Render the sphere at the correct position and with the correct orientation @@ -170,29 +189,20 @@ void Dumbbell::render(openglframework::Shader& shader, localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose(); shader.setMatrix3x3Uniform("normalMatrix", normalMatrix); - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_NORMAL_ARRAY); - if (hasTexture()) { - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - } + // Set the vertex color + openglframework::Vector4 color(mColor.r, mColor.g, mColor.b, mColor.a); + shader.setVector4Uniform("vertexColor", color); - glVertexPointer(3, GL_FLOAT, 0, getVerticesPointer()); - glNormalPointer(GL_FLOAT, 0, getNormalsPointer()); - if(hasTexture()) { - glTexCoordPointer(2, GL_FLOAT, 0, getUVTextureCoordinatesPointer()); - } + // Bind the VAO + mVAO.bind(); // For each part of the mesh for (unsigned int i=0; iaddCollisionShape(collisionShape, rp3d::Transform::identity()); mTransformMatrix = mTransformMatrix * mScalingMatrix; + + // Create the VBOs and VAO + createVBOAndVAO(shader); } // Constructor Sphere::Sphere(float radius, const openglframework::Vector3 &position, float mass, reactphysics3d::DynamicsWorld* world, - const std::string& meshFolderPath) - : openglframework::Mesh(), mRadius(radius) { + const std::string& meshFolderPath, openglframework::Shader& shader) + : openglframework::Mesh(), mRadius(radius), mVBOVertices(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) { // Load the mesh from a file openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "sphere.obj", *this); @@ -108,6 +115,9 @@ Sphere::Sphere(float radius, const openglframework::Vector3 &position, mRigidBody = body; mTransformMatrix = mTransformMatrix * mScalingMatrix; + + // Create the VBOs and VAO + createVBOAndVAO(shader); } // Destructor @@ -115,6 +125,13 @@ Sphere::~Sphere() { // Destroy the mesh destroy(); + + // Destroy the VBOs and VAO + mVBOIndices.destroy(); + mVBOVertices.destroy(); + mVBONormals.destroy(); + mVBOTextureCoords.destroy(); + mVAO.destroy(); } // Render the sphere at the correct position and with the correct orientation @@ -134,29 +151,20 @@ void Sphere::render(openglframework::Shader& shader, localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose(); shader.setMatrix3x3Uniform("normalMatrix", normalMatrix); - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_NORMAL_ARRAY); - if (hasTexture()) { - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - } + // Set the vertex color + openglframework::Vector4 color(mColor.r, mColor.g, mColor.b, mColor.a); + shader.setVector4Uniform("vertexColor", color); - glVertexPointer(3, GL_FLOAT, 0, getVerticesPointer()); - glNormalPointer(GL_FLOAT, 0, getNormalsPointer()); - if(hasTexture()) { - glTexCoordPointer(2, GL_FLOAT, 0, getUVTextureCoordinatesPointer()); - } + // Bind the VAO + mVAO.bind(); // For each part of the mesh for (unsigned int i=0; igetRigidBody()->getMaterial(); @@ -92,7 +93,7 @@ CollisionShapesScene::CollisionShapesScene(const std::string& name) radius * sin(angle)); // Create a sphere and a corresponding rigid in the dynamics world - Box* box = new Box(BOX_SIZE, position , BOX_MASS, mDynamicsWorld); + Box* box = new Box(BOX_SIZE, position , BOX_MASS, mDynamicsWorld, mPhongShader); // Change the material properties of the rigid body rp3d::Material& material = box->getRigidBody()->getMaterial(); @@ -113,7 +114,7 @@ CollisionShapesScene::CollisionShapesScene(const std::string& name) // Create a sphere and a corresponding rigid in the dynamics world Sphere* sphere = new Sphere(SPHERE_RADIUS, position , BOX_MASS, mDynamicsWorld, - meshFolderPath); + meshFolderPath, mPhongShader); // Change the material properties of the rigid body rp3d::Material& material = sphere->getRigidBody()->getMaterial(); @@ -134,7 +135,7 @@ CollisionShapesScene::CollisionShapesScene(const std::string& name) // Create a cone and a corresponding rigid in the dynamics world Cone* cone = new Cone(CONE_RADIUS, CONE_HEIGHT, position, CONE_MASS, mDynamicsWorld, - meshFolderPath); + meshFolderPath, mPhongShader); // Change the material properties of the rigid body rp3d::Material& material = cone->getRigidBody()->getMaterial(); @@ -155,7 +156,7 @@ CollisionShapesScene::CollisionShapesScene(const std::string& name) // Create a cylinder and a corresponding rigid in the dynamics world Cylinder* cylinder = new Cylinder(CYLINDER_RADIUS, CYLINDER_HEIGHT, position , - CYLINDER_MASS, mDynamicsWorld, meshFolderPath); + CYLINDER_MASS, mDynamicsWorld, meshFolderPath, mPhongShader); // Change the material properties of the rigid body rp3d::Material& material = cylinder->getRigidBody()->getMaterial(); @@ -176,7 +177,8 @@ CollisionShapesScene::CollisionShapesScene(const std::string& name) // Create a cylinder and a corresponding rigid in the dynamics world 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 rp3d::Material& material = capsule->getRigidBody()->getMaterial(); @@ -196,7 +198,8 @@ CollisionShapesScene::CollisionShapesScene(const std::string& name) radius * sin(angle)); // 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 rp3d::Material& material = mesh->getRigidBody()->getMaterial(); @@ -208,7 +211,7 @@ CollisionShapesScene::CollisionShapesScene(const std::string& name) // Create the floor openglframework::Vector3 floorPosition(0, 0, 0); - mFloor = new Box(FLOOR_SIZE, floorPosition, FLOOR_MASS, mDynamicsWorld); + mFloor = new Box(FLOOR_SIZE, floorPosition, FLOOR_MASS, mDynamicsWorld, mPhongShader); // The floor must be a static rigid body mFloor->getRigidBody()->setType(rp3d::STATIC); diff --git a/testbed/scenes/collisionshapes/CollisionShapesScene.h b/testbed/scenes/collisionshapes/CollisionShapesScene.h index 4a4e49b3..a12d3fa3 100644 --- a/testbed/scenes/collisionshapes/CollisionShapesScene.h +++ b/testbed/scenes/collisionshapes/CollisionShapesScene.h @@ -44,11 +44,11 @@ namespace collisionshapesscene { // Constants -const int NB_BOXES = 2; -const int NB_CUBES = 1; +const int NB_BOXES = 3; +const int NB_CUBES = 3; const int NB_CONES = 3; -const int NB_CYLINDERS = 2; -const int NB_CAPSULES = 1; +const int NB_CYLINDERS = 3; +const int NB_CAPSULES = 3; const int NB_MESHES = 2; const int NB_COMPOUND_SHAPES = 2; const openglframework::Vector3 BOX_SIZE(2, 2, 2); diff --git a/testbed/scenes/cubes/CubesScene.cpp b/testbed/scenes/cubes/CubesScene.cpp index e44d10e1..7e029977 100644 --- a/testbed/scenes/cubes/CubesScene.cpp +++ b/testbed/scenes/cubes/CubesScene.cpp @@ -69,7 +69,7 @@ CubesScene::CubesScene(const std::string& name) 0); // Create a cube and a corresponding rigid in the dynamics world - Box* cube = new Box(BOX_SIZE, position , BOX_MASS, mDynamicsWorld); + Box* cube = new Box(BOX_SIZE, position , BOX_MASS, mDynamicsWorld, mPhongShader); // Change the material properties of the rigid body rp3d::Material& material = cube->getRigidBody()->getMaterial(); @@ -81,7 +81,7 @@ CubesScene::CubesScene(const std::string& name) // Create the floor openglframework::Vector3 floorPosition(0, 0, 0); - mFloor = new Box(FLOOR_SIZE, floorPosition, FLOOR_MASS, mDynamicsWorld); + mFloor = new Box(FLOOR_SIZE, floorPosition, FLOOR_MASS, mDynamicsWorld, mPhongShader); // The floor must be a static rigid body mFloor->getRigidBody()->setType(rp3d::STATIC); @@ -150,6 +150,8 @@ void CubesScene::update() { // Render the scene void CubesScene::render() { + checkOpenGLErrors(); + glEnable(GL_DEPTH_TEST); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_CULL_FACE); @@ -170,19 +172,49 @@ void CubesScene::render() { mPhongShader.setVector3Uniform("light0SpecularColor", Vector3(specCol.r, specCol.g, specCol.b)); mPhongShader.setFloatUniform("shininess", 60.0f); + checkOpenGLErrors(); + // Render all the cubes of the scene for (std::vector::iterator it = mBoxes.begin(); it != mBoxes.end(); ++it) { (*it)->render(mPhongShader, worldToCameraMatrix); } + checkOpenGLErrors(); + // Render the floor mFloor->render(mPhongShader, worldToCameraMatrix); // Unbind the shader mPhongShader.unbind(); + + checkOpenGLErrors(); } // Reset the scene void CubesScene::reset() { } + +// Check the OpenGL errors +void CubesScene::checkOpenGLErrors() { + GLenum glError; + + // Get the OpenGL errors + glError = glGetError(); + + // While there are errors + while (glError != GL_NO_ERROR) { + + // Get the error string + const GLubyte* stringError = gluErrorString(glError); + + // Display the error + if (stringError) + std::cerr << "OpenGL Error #" << glError << "(" << gluErrorString(glError) << ")" << std::endl; + else + std::cerr << "OpenGL Error #" << glError << " (no message available)" << std::endl; + + // Get the next error + glError = glGetError(); + } +} diff --git a/testbed/scenes/cubes/CubesScene.h b/testbed/scenes/cubes/CubesScene.h index 9acc5a62..50ebb001 100644 --- a/testbed/scenes/cubes/CubesScene.h +++ b/testbed/scenes/cubes/CubesScene.h @@ -85,6 +85,9 @@ class CubesScene : public Scene { /// Reset the scene virtual void reset(); + + // Check the OpenGL errors + static void checkOpenGLErrors(); }; } diff --git a/testbed/scenes/joints/JointsScene.cpp b/testbed/scenes/joints/JointsScene.cpp index fd3943df..a8688873 100644 --- a/testbed/scenes/joints/JointsScene.cpp +++ b/testbed/scenes/joints/JointsScene.cpp @@ -202,7 +202,7 @@ void JointsScene::createBallAndSocketJoints() { // Create a box and a corresponding rigid in the dynamics world mBallAndSocketJointChainBoxes[i] = new Box(boxDimension, positionBox , boxMass, - mDynamicsWorld); + mDynamicsWorld, mPhongShader); // The fist box cannot move (static body) if (i == 0) { @@ -247,7 +247,7 @@ void JointsScene::createSliderJoint() { // Create a box and a corresponding rigid in the dynamics world openglframework::Vector3 box1Dimension(2, 4, 2); - mSliderJointBottomBox = new Box(box1Dimension, positionBox1 , BOX_MASS, mDynamicsWorld); + mSliderJointBottomBox = new Box(box1Dimension, positionBox1 , BOX_MASS, mDynamicsWorld, mPhongShader); // The fist box cannot move mSliderJointBottomBox->getRigidBody()->setType(rp3d::STATIC); @@ -263,7 +263,7 @@ void JointsScene::createSliderJoint() { // Create a box and a corresponding rigid in the dynamics world openglframework::Vector3 box2Dimension(1.5f, 4, 1.5f); - mSliderJointTopBox = new Box(box2Dimension, positionBox2 , BOX_MASS, mDynamicsWorld); + mSliderJointTopBox = new Box(box2Dimension, positionBox2, BOX_MASS, mDynamicsWorld, mPhongShader); // Change the material properties of the rigid body rp3d::Material& material2 = mSliderJointTopBox->getRigidBody()->getMaterial(); @@ -299,7 +299,7 @@ void JointsScene::createPropellerHingeJoint() { // Create a box and a corresponding rigid in the dynamics world openglframework::Vector3 boxDimension(10, 1, 1); - mPropellerBox = new Box(boxDimension, positionBox1 , BOX_MASS, mDynamicsWorld); + mPropellerBox = new Box(boxDimension, positionBox1 , BOX_MASS, mDynamicsWorld, mPhongShader); // Change the material properties of the rigid body rp3d::Material& material = mPropellerBox->getRigidBody()->getMaterial(); @@ -334,7 +334,7 @@ void JointsScene::createFixedJoints() { // Create a box and a corresponding rigid in the dynamics world openglframework::Vector3 boxDimension(1.5, 1.5, 1.5); - mFixedJointBox1 = new Box(boxDimension, positionBox1 , BOX_MASS, mDynamicsWorld); + mFixedJointBox1 = new Box(boxDimension, positionBox1 , BOX_MASS, mDynamicsWorld, mPhongShader); // Change the material properties of the rigid body rp3d::Material& material1 = mFixedJointBox1->getRigidBody()->getMaterial(); @@ -346,7 +346,7 @@ void JointsScene::createFixedJoints() { openglframework::Vector3 positionBox2(-5, 7, 0); // Create a box and a corresponding rigid in the dynamics world - mFixedJointBox2 = new Box(boxDimension, positionBox2 , BOX_MASS, mDynamicsWorld); + mFixedJointBox2 = new Box(boxDimension, positionBox2 , BOX_MASS, mDynamicsWorld, mPhongShader); // Change the material properties of the rigid body rp3d::Material& material2 = mFixedJointBox2->getRigidBody()->getMaterial(); @@ -381,7 +381,7 @@ void JointsScene::createFloor() { // Create the floor openglframework::Vector3 floorPosition(0, 0, 0); - mFloor = new Box(FLOOR_SIZE, floorPosition, FLOOR_MASS, mDynamicsWorld); + mFloor = new Box(FLOOR_SIZE, floorPosition, FLOOR_MASS, mDynamicsWorld, mPhongShader); // The floor must be a static rigid body mFloor->getRigidBody()->setType(rp3d::STATIC); diff --git a/testbed/scenes/raycast/RaycastScene.cpp b/testbed/scenes/raycast/RaycastScene.cpp index d3d42eb4..fcf86c15 100644 --- a/testbed/scenes/raycast/RaycastScene.cpp +++ b/testbed/scenes/raycast/RaycastScene.cpp @@ -57,13 +57,13 @@ RaycastScene::RaycastScene(const std::string& name) openglframework::Vector3 position1(0, 0, 0); // Create a convex mesh and a corresponding collision body in the dynamics world - mDumbbell = new Dumbbell(position1, mCollisionWorld, meshFolderPath); + mDumbbell = new Dumbbell(position1, mCollisionWorld, meshFolderPath, mPhongShader); // ---------- Box ---------- // openglframework::Vector3 position2(0, 0, 0); // Create a box and a corresponding collision body in the dynamics world - mBox = new Box(BOX_SIZE, position2, mCollisionWorld); + mBox = new Box(BOX_SIZE, position2, mCollisionWorld, mPhongShader); mBox->getCollisionBody()->setIsActive(false); // ---------- Sphere ---------- // @@ -71,34 +71,34 @@ RaycastScene::RaycastScene(const std::string& name) // Create a sphere and a corresponding collision body in the dynamics world mSphere = new Sphere(SPHERE_RADIUS, position3, mCollisionWorld, - meshFolderPath); + meshFolderPath, mPhongShader); // ---------- Cone ---------- // openglframework::Vector3 position4(0, 0, 0); // Create a cone and a corresponding collision body in the dynamics world mCone = new Cone(CONE_RADIUS, CONE_HEIGHT, position4, mCollisionWorld, - meshFolderPath); + meshFolderPath, mPhongShader); // ---------- Cylinder ---------- // openglframework::Vector3 position5(0, 0, 0); // Create a cylinder and a corresponding collision body in the dynamics world mCylinder = new Cylinder(CYLINDER_RADIUS, CYLINDER_HEIGHT, position5, - mCollisionWorld, meshFolderPath); + mCollisionWorld, meshFolderPath, mPhongShader); // ---------- Capsule ---------- // openglframework::Vector3 position6(0, 0, 0); // Create a cylinder and a corresponding collision body in the dynamics world mCapsule = new Capsule(CAPSULE_RADIUS, CAPSULE_HEIGHT, position6 , - mCollisionWorld, meshFolderPath); + mCollisionWorld, meshFolderPath, mPhongShader); // ---------- Convex Mesh ---------- // openglframework::Vector3 position7(0, 0, 0); // Create a convex mesh and a corresponding collision body in the dynamics world - mConvexMesh = new ConvexMesh(position7, mCollisionWorld, meshFolderPath); + mConvexMesh = new ConvexMesh(position7, mCollisionWorld, meshFolderPath, mPhongShader); // Create the lines that will be used for raycasting createLines(); diff --git a/testbed/shaders/depth.frag b/testbed/shaders/depth.frag index 4ef97b32..9b32019e 100644 --- a/testbed/shaders/depth.frag +++ b/testbed/shaders/depth.frag @@ -1,6 +1,6 @@ /******************************************************************************** * OpenGL-Framework * -* Copyright (c) 2013 Daniel Chappuis * +* Copyright (c) 2015 Daniel Chappuis * ********************************************************************************* * * * This software is provided 'as-is', without any express or implied warranty. * diff --git a/testbed/shaders/depth.vert b/testbed/shaders/depth.vert index f320cc72..09b9ff9a 100644 --- a/testbed/shaders/depth.vert +++ b/testbed/shaders/depth.vert @@ -1,6 +1,6 @@ /******************************************************************************** * OpenGL-Framework * -* Copyright (c) 2013 Daniel Chappuis * +* Copyright (c) 2015 Daniel Chappuis * ********************************************************************************* * * * This software is provided 'as-is', without any express or implied warranty. * diff --git a/testbed/shaders/phong.frag b/testbed/shaders/phong.frag index f09777da..2f5777f1 100644 --- a/testbed/shaders/phong.frag +++ b/testbed/shaders/phong.frag @@ -1,6 +1,8 @@ +#version 330 + /******************************************************************************** * OpenGL-Framework * -* Copyright (c) 2013 Daniel Chappuis * +* Copyright (c) 2015 Daniel Chappuis * ********************************************************************************* * * * This software is provided 'as-is', without any express or implied warranty. * @@ -29,14 +31,17 @@ uniform vec3 light0PosCameraSpace; // Camera-space position of the ligh uniform vec3 light0DiffuseColor; // Light 0 diffuse color uniform vec3 light0SpecularColor; // Light 0 specular color uniform float shininess; // Shininess -uniform sampler2D texture; // Texture +uniform sampler2D textureSampler; // Texture uniform bool isTexture; // True if we need to use the texture uniform vec4 vertexColor; // Vertex color -// Varying variables -varying vec3 vertexPosCameraSpace; // Camera-space position of the vertex -varying vec3 vertexNormalCameraSpace; // Vertex normal in camera-space -varying vec2 texCoords; // Texture coordinates +// In variables +in vec3 vertexPosCameraSpace; // Camera-space position of the vertex +in vec3 vertexNormalCameraSpace; // Vertex normal in camera-space +in vec2 texCoords; // Texture coordinates + +// Out variable +out vec4 color; // Output color void main() { @@ -45,7 +50,7 @@ void main() { // Get the texture color vec3 textureColor = vertexColor.rgb; - if (isTexture) textureColor = texture2D(texture, texCoords).rgb; + if (isTexture) textureColor = texture(textureSampler, texCoords).rgb; // Compute the surface normal vector vec3 N = normalize(vertexNormalCameraSpace); @@ -63,5 +68,5 @@ void main() { vec3 specular = light0SpecularColor * specularFactor; // Compute the final color - gl_FragColor = vec4(ambient + diffuse + specular, 1.0); + color = vec4(ambient + diffuse + specular, 1.0); } diff --git a/testbed/shaders/phong.vert b/testbed/shaders/phong.vert index 620344a9..3e9a5398 100644 --- a/testbed/shaders/phong.vert +++ b/testbed/shaders/phong.vert @@ -1,6 +1,8 @@ +#version 330 + /******************************************************************************** * OpenGL-Framework * -* Copyright (c) 2013 Daniel Chappuis * +* Copyright (c) 2015 Daniel Chappuis * ********************************************************************************* * * * This software is provided 'as-is', without any express or implied warranty. * @@ -28,22 +30,27 @@ uniform mat4 localToCameraMatrix; // Local-space to camera-space matrix uniform mat4 projectionMatrix; // Projection matrix uniform mat3 normalMatrix; // Normal matrix -// Varying variables -varying vec3 vertexPosCameraSpace; // Camera-space position of the vertex -varying vec3 vertexNormalCameraSpace; // Vertex normal in camera-space -varying vec2 texCoords; // Texture coordinates +// In variables +in vec4 vertexPosition; +in vec3 vertexNormal; +in vec2 textureCoords; + +// Out variables +out vec3 vertexPosCameraSpace; // Camera-space position of the vertex +out vec3 vertexNormalCameraSpace; // Vertex normal in camera-space +out vec2 texCoords; // Texture coordinates void main() { // Compute the vertex position - vec4 positionCameraSpace = localToCameraMatrix * gl_Vertex; + vec4 positionCameraSpace = localToCameraMatrix * vertexPosition; vertexPosCameraSpace = positionCameraSpace.xyz; // Compute the world surface normal - vertexNormalCameraSpace = normalMatrix * gl_Normal; + vertexNormalCameraSpace = normalMatrix * vertexNormal; // Get the texture coordinates - texCoords = gl_MultiTexCoord0.xy; + texCoords = textureCoords; // Compute the clip-space vertex coordinates gl_Position = projectionMatrix * positionCameraSpace; diff --git a/testbed/src/Gui.cpp b/testbed/src/Gui.cpp index d826ae8b..6c78006c 100644 --- a/testbed/src/Gui.cpp +++ b/testbed/src/Gui.cpp @@ -32,17 +32,12 @@ double Gui::g_Time = 0.0f; bool Gui::g_MousePressed[3] = {false, false, false}; float Gui::g_MouseWheel = 0.0f; GLuint Gui::g_FontTexture = 0; -int Gui::g_ShaderHandle = 0; -int Gui::g_VertHandle = 0; -int Gui::g_FragHandle = 0; -int Gui::g_AttribLocationTex = 0; -int Gui::g_AttribLocationProjMtx = 0; -int Gui::g_AttribLocationPosition = 0; -int Gui::g_AttribLocationUV = 0; -int Gui::g_AttribLocationColor = 0; size_t Gui::g_VboSize = 0; unsigned int Gui::g_VboHandle = 0; unsigned int Gui::g_VaoHandle = 0; +int Gui::g_AttribLocationTex = 0, Gui::g_AttribLocationProjMtx = 0; +int Gui::g_AttribLocationPosition = 0, Gui::g_AttribLocationUV = 0, Gui::g_AttribLocationColor = 0; +Shader Gui::mShader; // Constructor Gui::Gui() { @@ -52,9 +47,6 @@ Gui::Gui() { g_MousePressed[2] = false; g_MouseWheel = 0.0f; g_FontTexture = 0; - g_ShaderHandle = 0, g_VertHandle = 0, g_FragHandle = 0; - g_AttribLocationTex = 0, g_AttribLocationProjMtx = 0; - g_AttribLocationPosition = 0, g_AttribLocationUV = 0, g_AttribLocationColor = 0; g_VboSize = 0; g_VboHandle = 0, g_VaoHandle = 0; } @@ -67,16 +59,7 @@ Gui::~Gui() { g_VaoHandle = 0; g_VboHandle = 0; - glDetachShader(g_ShaderHandle, g_VertHandle); - glDeleteShader(g_VertHandle); - g_VertHandle = 0; - - glDetachShader(g_ShaderHandle, g_FragHandle); - glDeleteShader(g_FragHandle); - g_FragHandle = 0; - - glDeleteProgram(g_ShaderHandle); - g_ShaderHandle = 0; + mShader.destroy(); if (g_FontTexture) { @@ -122,48 +105,15 @@ void Gui::init() { void Gui::createDeviceObjects() { - const GLchar *vertex_shader = - "#version 330\n" - "uniform mat4 ProjMtx;\n" - "in vec2 Position;\n" - "in vec2 UV;\n" - "in vec4 Color;\n" - "out vec2 Frag_UV;\n" - "out vec4 Frag_Color;\n" - "void main()\n" - "{\n" - " Frag_UV = UV;\n" - " Frag_Color = Color;\n" - " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n" - "}\n"; + /* + mShader.create("shaders/gui.vert", "shaders/gui.frag"); - const GLchar* fragment_shader = - "#version 330\n" - "uniform sampler2D Texture;\n" - "in vec2 Frag_UV;\n" - "in vec4 Frag_Color;\n" - "out vec4 Out_Color;\n" - "void main()\n" - "{\n" - " Out_Color = Frag_Color * texture( Texture, Frag_UV.st);\n" - "}\n"; - - g_ShaderHandle = glCreateProgram(); - g_VertHandle = glCreateShader(GL_VERTEX_SHADER); - g_FragHandle = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(g_VertHandle, 1, &vertex_shader, 0); - glShaderSource(g_FragHandle, 1, &fragment_shader, 0); - glCompileShader(g_VertHandle); - glCompileShader(g_FragHandle); - glAttachShader(g_ShaderHandle, g_VertHandle); - glAttachShader(g_ShaderHandle, g_FragHandle); - glLinkProgram(g_ShaderHandle); - - g_AttribLocationTex = glGetUniformLocation(g_ShaderHandle, "Texture"); - g_AttribLocationProjMtx = glGetUniformLocation(g_ShaderHandle, "ProjMtx"); - g_AttribLocationPosition = glGetAttribLocation(g_ShaderHandle, "Position"); - g_AttribLocationUV = glGetAttribLocation(g_ShaderHandle, "UV"); - g_AttribLocationColor = glGetAttribLocation(g_ShaderHandle, "Color"); + GLuint shaderID = mShader.getProgramObjectId(); + g_AttribLocationTex = glGetUniformLocation(shaderID, "Texture"); + g_AttribLocationProjMtx = glGetUniformLocation(shaderID, "ProjMtx"); + g_AttribLocationPosition = glGetAttribLocation(shaderID, "Position"); + g_AttribLocationUV = glGetAttribLocation(shaderID, "UV"); + g_AttribLocationColor = glGetAttribLocation(shaderID, "Color"); glGenBuffers(1, &g_VboHandle); @@ -173,7 +123,6 @@ void Gui::createDeviceObjects() { glEnableVertexAttribArray(g_AttribLocationPosition); glEnableVertexAttribArray(g_AttribLocationUV); glEnableVertexAttribArray(g_AttribLocationColor); - #define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT)) glVertexAttribPointer(g_AttribLocationPosition, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, pos)); glVertexAttribPointer(g_AttribLocationUV, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, uv)); @@ -183,56 +132,41 @@ void Gui::createDeviceObjects() { glBindBuffer(GL_ARRAY_BUFFER, 0); createFontTextures(); + */ } // Display the GUI void Gui::render() { +/* ImGuiIO& io = ImGui::GetIO(); //glfwPollEvents(); beginNewFrame(); + bool show_test_window = true; bool show_another_window = false; - ImVec4 clear_color = ImColor(114, 144, 154); + ImVec4 clear_color = ImColor(255, 255, 255); // 1. Show a simple window // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug" { - static float f = 0.0f; ImGui::Text("Hello, world!"); - ImGui::SliderFloat("float", &f, 0.0f, 1.0f); - ImGui::ColorEdit3("clear color", (float*)&clear_color); - if (ImGui::Button("Test Window")) int test = 1; - if (ImGui::Button("Another Window")) int test = 1; - ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); } - // 2. Show another simple window, this time using an explicit Begin/End pair - //if (show_another_window) - { - ImGui::SetNextWindowSize(ImVec2(200,100), ImGuiSetCond_FirstUseEver); - ImGui::Begin("Another Window", &show_another_window); - ImGui::Text("Hello"); - ImGui::End(); - } - - // 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow() - //if (show_test_window) - { - ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiSetCond_FirstUseEver); - ImGui::ShowTestWindow(&show_test_window); - } // Render the GUI ImGui::Render(); + */ } void Gui::beginNewFrame() { + /* if (!g_FontTexture) createDeviceObjects(); + ImGuiIO& io = ImGui::GetIO(); // Setup display size (every frame to accommodate for window resizing) @@ -276,6 +210,7 @@ void Gui::beginNewFrame() { // Start the frame ImGui::NewFrame(); + */ } void Gui::createFontTextures() @@ -301,6 +236,7 @@ void Gui::createFontTextures() // - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f) void Gui::renderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count) { + std::cout << "OpenGLVersion : " << glGetString(GL_VERSION) << std::endl; if (cmd_lists_count == 0) return; @@ -316,9 +252,14 @@ void Gui::renderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count) glEnable(GL_SCISSOR_TEST); glActiveTexture(GL_TEXTURE0); + /* // Setup orthographic projection matrix const float width = ImGui::GetIO().DisplaySize.x; const float height = ImGui::GetIO().DisplaySize.y; + Matrix4 orthoProj(2.0f / width, 0.0f, 0.0f, 0.0f, + 0.0f, 2.0/-height, 0.0f, 0.0f, + 0.0f, 0.0f, -1.0f, 0.0f, + -1.0f, 1.0f, 0.0f, 1.0f); const float ortho_projection[4][4] = { { 2.0f/width, 0.0f, 0.0f, 0.0f }, @@ -326,7 +267,11 @@ void Gui::renderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count) { 0.0f, 0.0f, -1.0f, 0.0f }, { -1.0f, 1.0f, 0.0f, 1.0f }, }; - glUseProgram(g_ShaderHandle); + + mShader.bind(); + //mShader.setIntUniform("Texture", 0); + //mShader.setMatrix4x4Uniform("ProjMtx", orthoProj); + ///glUseProgram(g_ShaderHandle); glUniform1i(g_AttribLocationTex, 0); glUniformMatrix4fv(g_AttribLocationProjMtx, 1, GL_FALSE, &ortho_projection[0][0]); @@ -379,11 +324,14 @@ void Gui::renderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count) cmd_offset = vtx_offset; } + + // Restore modified state glBindVertexArray(0); glUseProgram(last_program); glDisable(GL_SCISSOR_TEST); glBindTexture(GL_TEXTURE_2D, last_texture); + */ } const char* Gui::getClipboardText() { diff --git a/testbed/src/Gui.h b/testbed/src/Gui.h index 6ed1b152..d61a6cdf 100644 --- a/testbed/src/Gui.h +++ b/testbed/src/Gui.h @@ -30,6 +30,9 @@ #include #include #include +#include "openglframework.h" + +using namespace openglframework; // Class Gui class Gui { @@ -44,11 +47,13 @@ class Gui { // TODO : Delete this static GLFWwindow* mWindow; + static Shader mShader; + static double g_Time; static bool g_MousePressed[3]; static float g_MouseWheel; static GLuint g_FontTexture; - static int g_ShaderHandle, g_VertHandle, g_FragHandle; + //static int g_ShaderHandle, g_VertHandle, g_FragHandle; static int g_AttribLocationTex, g_AttribLocationProjMtx; static int g_AttribLocationPosition, g_AttribLocationUV, g_AttribLocationColor; static size_t g_VboSize; diff --git a/testbed/src/TestbedApplication.cpp b/testbed/src/TestbedApplication.cpp index fc0721c4..c89da4f8 100644 --- a/testbed/src/TestbedApplication.cpp +++ b/testbed/src/TestbedApplication.cpp @@ -55,8 +55,8 @@ TestbedApplication::TestbedApplication() : mFPS(0), mNbFrames(0), mPreviousTime( mCurrentScene = NULL; mEngineSettings.timeStep = DEFAULT_TIMESTEP; mIsMultisamplingActive = true; - mWidth = 1000; - mHeight = 800; + mWidth = 1280; + mHeight = 720; } // Destructor @@ -85,6 +85,12 @@ void TestbedApplication::init() { std::exit(EXIT_FAILURE); } + // OpenGL version required + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); + // Active the multi-sampling by default if (mIsMultisamplingActive) { glfwWindowHint(GLFW_SAMPLES, 4); @@ -131,7 +137,7 @@ void TestbedApplication::init() { Gui::getInstance().setWindow(mWindow); // Init the GUI - Gui::getInstance().init(); + //Gui::getInstance().init(); mTimer.start(); } @@ -143,20 +149,24 @@ void TestbedApplication::createScenes() { CubesScene* cubeScene = new CubesScene("Cubes"); mScenes.push_back(cubeScene); + /* // Joints scene JointsScene* jointsScene = new JointsScene("Joints"); mScenes.push_back(jointsScene); + */ // Collision shapes scene CollisionShapesScene* collisionShapesScene = new CollisionShapesScene("Collision Shapes"); mScenes.push_back(collisionShapesScene); + /* // Raycast scene RaycastScene* raycastScene = new RaycastScene("Raycast"); mScenes.push_back(raycastScene); + */ assert(mScenes.size() > 0); - mCurrentScene = mScenes[0]; + mCurrentScene = mScenes[1]; } // Remove all the scenes @@ -201,7 +211,6 @@ void TestbedApplication::update() { // Compute the interpolation factor float factor = mTimer.computeInterpolationFactor(mEngineSettings.timeStep); assert(factor >= 0.0f && factor <= 1.0f); - std::cout << "Factor : " << factor << std::endl; // Notify the scene about the interpolation factor mCurrentScene->setInterpolationFactor(factor); @@ -220,7 +229,7 @@ void TestbedApplication::render() { mCurrentScene->render(); // Display the GUI - Gui::getInstance().render(); + //Gui::getInstance().render(); // Check the OpenGL errors checkOpenGLErrors(); @@ -251,6 +260,8 @@ void TestbedApplication::startMainLoop() { // Loop until the user closes the window while (!glfwWindowShouldClose(mWindow)) { + checkOpenGLErrors(); + // Reshape the viewport reshape(); @@ -265,6 +276,8 @@ void TestbedApplication::startMainLoop() { // Process events glfwPollEvents(); + + checkOpenGLErrors(); } } @@ -283,7 +296,7 @@ void TestbedApplication::checkOpenGLErrors() { // Display the error if (stringError) - std::cerr << "OpenGL Error #" << glError << "(" << gluErrorString(glError) << std::endl; + std::cerr << "OpenGL Error #" << glError << "(" << gluErrorString(glError) << ")" << std::endl; else std::cerr << "OpenGL Error #" << glError << " (no message available)" << std::endl;