2015-07-21 23:18:32 +00:00
|
|
|
/********************************************************************************
|
|
|
|
* ReactPhysics3D physics library, http://www.reactphysics3d.com *
|
|
|
|
* Copyright (c) 2010-2015 Daniel Chappuis *
|
|
|
|
*********************************************************************************
|
|
|
|
* *
|
|
|
|
* This software is provided 'as-is', without any express or implied warranty. *
|
|
|
|
* In no event will the authors be held liable for any damages arising from the *
|
|
|
|
* use of this software. *
|
|
|
|
* *
|
|
|
|
* Permission is granted to anyone to use this software for any purpose, *
|
|
|
|
* including commercial applications, and to alter it and redistribute it *
|
|
|
|
* freely, subject to the following restrictions: *
|
|
|
|
* *
|
|
|
|
* 1. The origin of this software must not be misrepresented; you must not claim *
|
|
|
|
* that you wrote the original software. If you use this software in a *
|
|
|
|
* product, an acknowledgment in the product documentation would be *
|
|
|
|
* appreciated but is not required. *
|
|
|
|
* *
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and must not be *
|
|
|
|
* misrepresented as being the original software. *
|
|
|
|
* *
|
|
|
|
* 3. This notice may not be removed or altered from any source distribution. *
|
|
|
|
* *
|
|
|
|
********************************************************************************/
|
|
|
|
|
|
|
|
// Libraries
|
|
|
|
#include "SceneDemo.h"
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
|
|
|
|
using namespace openglframework;
|
|
|
|
|
2015-08-03 21:05:55 +00:00
|
|
|
int SceneDemo::shadowMapTextureLevel = 0;
|
2015-08-08 09:40:37 +00:00
|
|
|
openglframework::Color SceneDemo::mGreyColorDemo = Color(0.70f, 0.70f, 0.7f, 1.0);
|
|
|
|
openglframework::Color SceneDemo::mYellowColorDemo = Color(0.9, 0.88, 0.145, 1.0);
|
|
|
|
openglframework::Color SceneDemo::mBlueColorDemo = Color(0, 0.66, 0.95, 1.0);
|
|
|
|
openglframework::Color SceneDemo::mOrangeColorDemo = Color(0.9, 0.35, 0, 1.0);
|
|
|
|
openglframework::Color SceneDemo::mPinkColorDemo = Color(0.83, 0.48, 0.64, 1.0);
|
|
|
|
openglframework::Color SceneDemo::mRedColorDemo = Color(0.95, 0, 0, 1.0);
|
|
|
|
int SceneDemo::mNbDemoColors = 4;
|
|
|
|
openglframework::Color SceneDemo::mDemoColors[] = {SceneDemo::mYellowColorDemo, SceneDemo::mBlueColorDemo,
|
|
|
|
SceneDemo::mOrangeColorDemo, SceneDemo::mPinkColorDemo};
|
2015-08-03 21:05:55 +00:00
|
|
|
|
2015-07-21 23:18:32 +00:00
|
|
|
// Constructor
|
2015-08-06 19:06:35 +00:00
|
|
|
SceneDemo::SceneDemo(const std::string& name, float sceneRadius, bool isShadowMappingEnabled)
|
|
|
|
: Scene(name, isShadowMappingEnabled),
|
|
|
|
mLight0(0), mIsShadowMappingInitialized(false),
|
2015-07-21 23:18:32 +00:00
|
|
|
mDepthShader("shaders/depth.vert", "shaders/depth.frag"),
|
2015-07-25 00:26:26 +00:00
|
|
|
mPhongShader("shaders/phong.vert", "shaders/phong.frag"),
|
|
|
|
mQuadShader("shaders/quad.vert", "shaders/quad.frag"),
|
2015-08-11 16:32:45 +00:00
|
|
|
mVBOQuad(GL_ARRAY_BUFFER), mMeshFolderPath("meshes/") {
|
2015-07-21 23:18:32 +00:00
|
|
|
|
2015-08-03 21:05:55 +00:00
|
|
|
shadowMapTextureLevel++;
|
|
|
|
|
2015-07-21 23:18:32 +00:00
|
|
|
// Move the light0
|
2015-08-08 09:40:37 +00:00
|
|
|
mLight0.translateWorld(Vector3(-2, 35, 40));
|
2015-07-21 23:18:32 +00:00
|
|
|
|
|
|
|
// Camera at light0 postion for the shadow map
|
|
|
|
mShadowMapLightCamera.translateWorld(mLight0.getOrigin());
|
2015-07-29 16:15:20 +00:00
|
|
|
mShadowMapLightCamera.rotateLocal(Vector3(1, 0, 0), -PI / 4.0f);
|
2015-08-08 09:40:37 +00:00
|
|
|
mShadowMapLightCamera.rotateWorld(Vector3(0, 1, 0), PI / 8.0f);
|
|
|
|
|
2015-07-21 23:18:32 +00:00
|
|
|
mShadowMapLightCamera.setDimensions(SHADOWMAP_WIDTH, SHADOWMAP_HEIGHT);
|
2015-08-08 09:40:37 +00:00
|
|
|
mShadowMapLightCamera.setFieldOfView(80.0f);
|
2015-07-31 19:54:02 +00:00
|
|
|
mShadowMapLightCamera.setSceneRadius(100);
|
2015-07-21 23:18:32 +00:00
|
|
|
|
2015-07-26 22:30:45 +00:00
|
|
|
mShadowMapBiasMatrix.setAllValues(0.5, 0.0, 0.0, 0.5,
|
|
|
|
0.0, 0.5, 0.0, 0.5,
|
|
|
|
0.0, 0.0, 0.5, 0.5,
|
|
|
|
0.0, 0.0, 0.0, 1.0);
|
|
|
|
|
2015-07-21 23:18:32 +00:00
|
|
|
// Create the Shadow map FBO and texture
|
2015-08-03 21:05:55 +00:00
|
|
|
if (mIsShadowMappingEnabled) {
|
|
|
|
createShadowMapFBOAndTexture();
|
|
|
|
}
|
2015-07-25 00:26:26 +00:00
|
|
|
|
|
|
|
createQuadVBO();
|
2015-08-11 16:32:45 +00:00
|
|
|
|
|
|
|
VisualContactPoint::createStaticData(mMeshFolderPath);
|
2015-07-21 23:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Destructor
|
|
|
|
SceneDemo::~SceneDemo() {
|
|
|
|
|
|
|
|
mShadowMapTexture.destroy();
|
|
|
|
mFBOShadowMap.destroy();
|
2015-07-29 16:15:20 +00:00
|
|
|
mVBOQuad.destroy();
|
|
|
|
|
2015-08-11 16:32:45 +00:00
|
|
|
// Destroy the contact points
|
|
|
|
removeAllContactPoints();
|
|
|
|
|
|
|
|
VisualContactPoint::destroyStaticData();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the scene
|
|
|
|
void SceneDemo::update() {
|
|
|
|
|
|
|
|
// Update the contact points
|
|
|
|
updateContactPoints();
|
2015-07-21 23:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Render the scene (in multiple passes for shadow mapping)
|
|
|
|
void SceneDemo::render() {
|
|
|
|
|
2015-08-03 21:05:55 +00:00
|
|
|
const Color& diffCol = mLight0.getDiffuseColor();
|
|
|
|
|
2015-07-21 23:18:32 +00:00
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glEnable(GL_CULL_FACE);
|
|
|
|
|
2015-07-25 00:26:26 +00:00
|
|
|
// ---------- Render the scene to generate the shadow map (first pass) ----------- //
|
|
|
|
|
2015-08-03 21:05:55 +00:00
|
|
|
const Matrix4 shadowMapProjMatrix = mShadowMapLightCamera.getProjectionMatrix();
|
2015-07-25 00:26:26 +00:00
|
|
|
const openglframework::Matrix4 worldToLightCameraMatrix = mShadowMapLightCamera.getTransformMatrix().getInverse();
|
2015-07-21 23:18:32 +00:00
|
|
|
|
2015-08-03 21:05:55 +00:00
|
|
|
// If Shadow Mapping is enabled
|
|
|
|
if (mIsShadowMappingEnabled) {
|
2015-07-21 23:18:32 +00:00
|
|
|
|
2015-08-03 21:05:55 +00:00
|
|
|
// Culling switching, rendering only backface, this is done to avoid self-shadowing
|
|
|
|
glCullFace(GL_BACK);
|
2015-07-21 23:18:32 +00:00
|
|
|
|
2015-08-03 21:05:55 +00:00
|
|
|
mFBOShadowMap.bind();
|
2015-07-25 00:26:26 +00:00
|
|
|
|
2015-08-03 21:05:55 +00:00
|
|
|
// Bind the shader
|
|
|
|
mDepthShader.bind();
|
2015-07-25 00:26:26 +00:00
|
|
|
|
2015-08-03 21:05:55 +00:00
|
|
|
// Set the variables of the shader
|
|
|
|
mDepthShader.setMatrix4x4Uniform("projectionMatrix", shadowMapProjMatrix);
|
2015-07-25 00:26:26 +00:00
|
|
|
|
2015-08-03 21:05:55 +00:00
|
|
|
// Set the viewport to render into the shadow map texture
|
|
|
|
glViewport(0, 0, SHADOWMAP_WIDTH, SHADOWMAP_HEIGHT);
|
2015-07-21 23:18:32 +00:00
|
|
|
|
2015-08-03 21:05:55 +00:00
|
|
|
// Clear previous frame values
|
|
|
|
glClear(GL_DEPTH_BUFFER_BIT);
|
2015-07-21 23:18:32 +00:00
|
|
|
|
2015-08-03 21:05:55 +00:00
|
|
|
// Disable color rendering, we only want to write to the Z-Buffer
|
|
|
|
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
|
2015-07-21 23:18:32 +00:00
|
|
|
|
2015-08-03 21:05:55 +00:00
|
|
|
// Render the objects of the scene
|
|
|
|
renderSinglePass(mDepthShader, worldToLightCameraMatrix);
|
2015-07-25 00:26:26 +00:00
|
|
|
|
2015-08-03 21:05:55 +00:00
|
|
|
// Unbind the shader
|
|
|
|
mDepthShader.unbind();
|
2015-07-31 19:54:02 +00:00
|
|
|
|
2015-08-03 21:05:55 +00:00
|
|
|
mFBOShadowMap.unbind();
|
|
|
|
|
|
|
|
glDisable(GL_POLYGON_OFFSET_FILL);
|
|
|
|
}
|
2015-07-25 00:26:26 +00:00
|
|
|
|
2015-07-21 23:18:32 +00:00
|
|
|
// ---------- Render the scene for final rendering (second pass) ----------- //
|
|
|
|
|
2015-07-25 23:28:27 +00:00
|
|
|
glCullFace(GL_BACK);
|
|
|
|
|
2015-07-25 00:26:26 +00:00
|
|
|
// Get the world-space to camera-space matrix
|
|
|
|
const openglframework::Matrix4 worldToCameraMatrix = mCamera.getTransformMatrix().getInverse();
|
|
|
|
|
2015-07-21 23:18:32 +00:00
|
|
|
mPhongShader.bind();
|
|
|
|
|
2015-08-03 21:05:55 +00:00
|
|
|
if (mIsShadowMappingEnabled) mShadowMapTexture.bind();
|
|
|
|
const GLuint textureUnit = 0;
|
2015-07-25 23:28:27 +00:00
|
|
|
|
2015-07-21 23:18:32 +00:00
|
|
|
// Set the variables of the shader
|
|
|
|
mPhongShader.setMatrix4x4Uniform("projectionMatrix", mCamera.getProjectionMatrix());
|
2015-07-26 22:30:45 +00:00
|
|
|
mPhongShader.setMatrix4x4Uniform("shadowMapProjectionMatrix", mShadowMapBiasMatrix * shadowMapProjMatrix);
|
2015-07-25 23:28:27 +00:00
|
|
|
mPhongShader.setMatrix4x4Uniform("worldToLight0CameraMatrix", worldToLightCameraMatrix);
|
|
|
|
mPhongShader.setVector3Uniform("light0PosCameraSpace", worldToCameraMatrix * mLight0.getOrigin());
|
2015-08-03 21:05:55 +00:00
|
|
|
mPhongShader.setVector3Uniform("lightAmbientColor", Vector3(0.4f, 0.4f, 0.4f));
|
2015-07-21 23:18:32 +00:00
|
|
|
mPhongShader.setVector3Uniform("light0DiffuseColor", Vector3(diffCol.r, diffCol.g, diffCol.b));
|
2015-08-03 21:05:55 +00:00
|
|
|
mPhongShader.setIntUniform("shadowMapSampler", textureUnit);
|
|
|
|
mPhongShader.setIntUniform("isShadowEnabled", mIsShadowMappingEnabled);
|
2015-08-11 20:28:16 +00:00
|
|
|
mPhongShader.setVector2Uniform("shadowMapDimension", Vector2(SHADOWMAP_WIDTH, SHADOWMAP_HEIGHT));
|
2015-07-21 23:18:32 +00:00
|
|
|
|
2015-07-25 00:26:26 +00:00
|
|
|
// Set the viewport to render the scene
|
|
|
|
glViewport(mViewportX, mViewportY, mViewportWidth, mViewportHeight);
|
|
|
|
|
|
|
|
//Enabling color write (previously disabled for light POV z-buffer rendering)
|
|
|
|
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
|
|
|
|
|
|
|
// Clear previous frame values
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
2015-07-21 23:18:32 +00:00
|
|
|
// Render the objects of the scene
|
2015-07-25 00:26:26 +00:00
|
|
|
renderSinglePass(mPhongShader, worldToCameraMatrix);
|
2015-07-21 23:18:32 +00:00
|
|
|
|
2015-08-11 16:32:45 +00:00
|
|
|
// Render the contact points
|
|
|
|
if (mIsContactPointsDisplayed) {
|
|
|
|
renderContactPoints(mPhongShader, worldToCameraMatrix);
|
|
|
|
}
|
|
|
|
|
2015-08-03 21:05:55 +00:00
|
|
|
if (mIsShadowMappingEnabled) mShadowMapTexture.unbind();
|
2015-07-21 23:18:32 +00:00
|
|
|
mPhongShader.unbind();
|
2015-07-25 00:26:26 +00:00
|
|
|
|
2015-08-11 19:40:07 +00:00
|
|
|
//drawTextureQuad();
|
2015-07-21 23:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the Shadow map FBO and texture
|
|
|
|
void SceneDemo::createShadowMapFBOAndTexture() {
|
|
|
|
|
|
|
|
// Create the texture for the depth values
|
2015-07-26 22:30:45 +00:00
|
|
|
mShadowMapTexture.create(SHADOWMAP_WIDTH, SHADOWMAP_HEIGHT, GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT,
|
2015-07-21 23:18:32 +00:00
|
|
|
GL_UNSIGNED_BYTE, GL_NEAREST, GL_NEAREST, GL_CLAMP, GL_CLAMP, NULL);
|
|
|
|
|
|
|
|
// Create the FBO for the shadow map
|
|
|
|
mFBOShadowMap.create(0, 0, false);
|
2015-07-25 00:26:26 +00:00
|
|
|
mFBOShadowMap.bind();
|
|
|
|
|
|
|
|
// Tell OpenGL that we won't bind a color texture with the currently binded FBO
|
|
|
|
glDrawBuffer(GL_NONE);
|
|
|
|
glReadBuffer(GL_NONE);
|
|
|
|
|
2015-07-21 23:18:32 +00:00
|
|
|
mFBOShadowMap.attachTexture(GL_DEPTH_ATTACHMENT_EXT, mShadowMapTexture.getID());
|
2015-07-25 00:26:26 +00:00
|
|
|
mFBOShadowMap.unbind();
|
2015-08-03 21:05:55 +00:00
|
|
|
|
|
|
|
mIsShadowMappingInitialized = true;
|
2015-07-25 00:26:26 +00:00
|
|
|
}
|
|
|
|
|
2015-08-11 16:32:45 +00:00
|
|
|
// Used for debugging shadow maps
|
2015-07-25 00:26:26 +00:00
|
|
|
void SceneDemo::createQuadVBO() {
|
|
|
|
|
|
|
|
mVAOQuad.create();
|
|
|
|
mVAOQuad.bind();
|
|
|
|
|
|
|
|
static const GLfloat quadVertexData[] = {
|
|
|
|
-1.0f, -1.0f, 0.0f,
|
|
|
|
1.0f, -1.0f, 0.0f,
|
|
|
|
-1.0f, 1.0f, 0.0f,
|
|
|
|
-1.0f, 1.0f, 0.0f,
|
|
|
|
1.0f, -1.0f, 0.0f,
|
|
|
|
1.0f, 1.0f, 0.0f,
|
|
|
|
};
|
|
|
|
|
|
|
|
mVBOQuad.create();
|
|
|
|
mVBOQuad.bind();
|
|
|
|
mVBOQuad.copyDataIntoVBO(sizeof(quadVertexData), quadVertexData, GL_STATIC_DRAW);
|
|
|
|
mVBOQuad.unbind();
|
|
|
|
|
|
|
|
mVAOQuad.unbind();
|
2015-07-21 23:18:32 +00:00
|
|
|
}
|
|
|
|
|
2015-07-25 00:26:26 +00:00
|
|
|
void SceneDemo::drawTextureQuad() {
|
|
|
|
|
|
|
|
glViewport(mViewportX, mViewportY, mViewportWidth, mViewportHeight);
|
|
|
|
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
|
|
|
|
|
|
|
// Clear previous frame values
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
2015-07-21 23:18:32 +00:00
|
|
|
|
2015-08-08 09:40:37 +00:00
|
|
|
const GLuint textureUnit = 0;
|
|
|
|
|
2015-07-25 00:26:26 +00:00
|
|
|
mVAOQuad.bind();
|
|
|
|
mQuadShader.bind();
|
|
|
|
mShadowMapTexture.bind();
|
2015-08-08 09:40:37 +00:00
|
|
|
mQuadShader.setIntUniform("textureSampler", textureUnit);
|
2015-07-25 00:26:26 +00:00
|
|
|
mVBOQuad.bind();
|
|
|
|
|
|
|
|
GLint vertexPositionLoc = mQuadShader.getAttribLocation("vertexPosition");
|
|
|
|
glEnableVertexAttribArray(vertexPositionLoc);
|
|
|
|
|
|
|
|
glVertexAttribPointer(
|
|
|
|
vertexPositionLoc, // attribute 0. No particular reason for 0, but must match the layout in the shader.
|
|
|
|
3, // size
|
|
|
|
GL_FLOAT, // type
|
|
|
|
GL_FALSE, // normalized?
|
|
|
|
0, // stride
|
|
|
|
(void*)0 // array buffer offset
|
|
|
|
);
|
|
|
|
|
|
|
|
// Draw the triangles !
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 6); // 2*3 indices starting at 0 -> 2 triangles
|
|
|
|
|
|
|
|
glDisableVertexAttribArray(vertexPositionLoc);
|
|
|
|
|
|
|
|
mVBOQuad.unbind();
|
|
|
|
mShadowMapTexture.unbind();
|
|
|
|
mQuadShader.unbind();
|
|
|
|
mVAOQuad.unbind();
|
2015-07-21 23:18:32 +00:00
|
|
|
}
|
2015-08-11 16:32:45 +00:00
|
|
|
|
|
|
|
// Gather and create contact points
|
|
|
|
void SceneDemo::updateContactPoints() {
|
|
|
|
|
|
|
|
// Remove the previous contact points
|
|
|
|
removeAllContactPoints();
|
|
|
|
|
|
|
|
if (mIsContactPointsDisplayed) {
|
|
|
|
|
|
|
|
// Get the current contact points of the scene
|
|
|
|
std::vector<ContactPoint> contactPoints = getContactPoints();
|
|
|
|
|
|
|
|
// For each contact point
|
|
|
|
std::vector<ContactPoint>::const_iterator it;
|
|
|
|
for (it = contactPoints.begin(); it != contactPoints.end(); ++it) {
|
|
|
|
|
|
|
|
// Create a visual contact point for rendering
|
|
|
|
VisualContactPoint* point = new VisualContactPoint(it->point, mMeshFolderPath);
|
|
|
|
mContactPoints.push_back(point);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render the contact points
|
|
|
|
void SceneDemo::renderContactPoints(openglframework::Shader& shader,
|
|
|
|
const openglframework::Matrix4& worldToCameraMatrix) {
|
|
|
|
|
|
|
|
// Render all the raycast hit points
|
|
|
|
for (std::vector<VisualContactPoint*>::iterator it = mContactPoints.begin();
|
|
|
|
it != mContactPoints.end(); ++it) {
|
|
|
|
(*it)->render(shader, worldToCameraMatrix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SceneDemo::removeAllContactPoints() {
|
|
|
|
|
|
|
|
// Destroy all the visual contact points
|
|
|
|
for (std::vector<VisualContactPoint*>::iterator it = mContactPoints.begin();
|
|
|
|
it != mContactPoints.end(); ++it) {
|
|
|
|
delete (*it);
|
|
|
|
}
|
|
|
|
mContactPoints.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return all the contact points of the scene
|
|
|
|
std::vector<ContactPoint> SceneDemo::computeContactPointsOfWorld(const rp3d::DynamicsWorld* world) const {
|
|
|
|
|
|
|
|
std::vector<ContactPoint> contactPoints;
|
|
|
|
|
|
|
|
// Get the list of contact manifolds from the world
|
|
|
|
std::vector<const rp3d::ContactManifold*> manifolds = world->getContactsList();
|
|
|
|
|
|
|
|
// For each contact manifold
|
|
|
|
std::vector<const rp3d::ContactManifold*>::const_iterator it;
|
|
|
|
for (it = manifolds.begin(); it != manifolds.end(); ++it) {
|
|
|
|
|
|
|
|
const rp3d::ContactManifold* manifold = *it;
|
|
|
|
|
|
|
|
// For each contact point of the manifold
|
|
|
|
for (uint i=0; i<manifold->getNbContactPoints(); i++) {
|
|
|
|
|
|
|
|
rp3d::ContactPoint* contactPoint = manifold->getContactPoint(i);
|
|
|
|
rp3d::Vector3 point = contactPoint->getWorldPointOnBody1();
|
|
|
|
ContactPoint contact(openglframework::Vector3(point.x, point.y, point.z));
|
|
|
|
contactPoints.push_back(contact);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return contactPoints;
|
|
|
|
}
|