testbed/src/SceneDemo.cpp
void SceneDemo::render() {

    // Update the VBO for the debug infos
    updateDebugVBO();

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);

    glClearColor(mBackgroundColor.r, mBackgroundColor.g, mBackgroundColor.b, mBackgroundColor.a);

	Matrix4 shadowMapProjMatrix[NB_SHADOW_MAPS];
	openglframework::Matrix4 worldToLightCameraMatrix[NB_SHADOW_MAPS];
	for (int i = 0; i < NB_SHADOW_MAPS; i++) {

		shadowMapProjMatrix[i] = mShadowMapLightCameras[i].getProjectionMatrix();
		worldToLightCameraMatrix[i] = mShadowMapLightCameras[i].getTransformMatrix().getInverse();
	}

    // ---------- Render the scene to generate the shadow map (first pass) ----------- //

    // If Shadow Mapping is enabled
    if (mIsShadowMappingEnabled) {

        // Culling switching, rendering only backface, this is done to avoid self-shadowing
        glCullFace(GL_BACK);

		// For each shadow map
		for (int i = 0; i < NB_SHADOW_MAPS; i++) {

			mFBOShadowMap[i].bind();

			// Bind the shader
			mDepthShader.bind();

			// Set the variables of the shader
			mDepthShader.setMatrix4x4Uniform("projectionMatrix", shadowMapProjMatrix[i]);

			// Set the viewport to render into the shadow map texture
			glViewport(0, 0, SHADOWMAP_WIDTH, SHADOWMAP_HEIGHT);

			// Clear previous frame values
			glClear(GL_DEPTH_BUFFER_BIT);

			// Disable color rendering, we only want to write to the Z-Buffer
			glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);

			// Render the objects of the scene
			renderSinglePass(mDepthShader, worldToLightCameraMatrix[i]);

			// Unbind the shader
			mDepthShader.unbind();

			mFBOShadowMap[i].unbind();
		}

        glDisable(GL_POLYGON_OFFSET_FILL);
    }

    // ---------- Render the scene for final rendering (second pass) ----------- //

    glCullFace(GL_BACK);

    // Get the world-space to camera-space matrix
    const openglframework::Matrix4 worldToCameraMatrix = mCamera.getTransformMatrix().getInverse();

    mPhongShader.bind();

	// Is shadow mapping is enabled
	GLint textureUnits[NB_SHADOW_MAPS];
	if (mIsShadowMappingEnabled) {

		for (int i = 0; i < NB_SHADOW_MAPS; i++) {
			mShadowMapTexture[i].bind();
		}
		for (int i = 0; i < NB_SHADOW_MAPS; i++) {
			textureUnits[i] = mShadowMapTexture[i].getUnit();
		}
	}

    // Set the variables of the phong shader
    mPhongShader.setMatrix4x4Uniform("projectionMatrix", mCamera.getProjectionMatrix());
    mPhongShader.setMatrix4x4Uniform("shadowMapLight0ProjectionMatrix", mShadowMapBiasMatrix * shadowMapProjMatrix[0]);
    mPhongShader.setMatrix4x4Uniform("shadowMapLight1ProjectionMatrix", mShadowMapBiasMatrix * shadowMapProjMatrix[1]);
    mPhongShader.setMatrix4x4Uniform("worldToLight0CameraMatrix", worldToLightCameraMatrix[0]);
    mPhongShader.setMatrix4x4Uniform("worldToLight1CameraMatrix", worldToLightCameraMatrix[1]);
    mPhongShader.setVector3Uniform("light0PosCameraSpace", worldToCameraMatrix * mLight0.getOrigin());
    mPhongShader.setVector3Uniform("light1PosCameraSpace", worldToCameraMatrix * mLight1.getOrigin());
    mPhongShader.setVector3Uniform("light2PosCameraSpace", worldToCameraMatrix * mLight2.getOrigin());
    mPhongShader.setVector3Uniform("lightAmbientColor", Vector3(0.1f, 0.1f, 0.1f));
    mPhongShader.setVector3Uniform("light0DiffuseColor", Vector3(mLight0.getDiffuseColor().r, mLight0.getDiffuseColor().g, mLight0.getDiffuseColor().b));
    mPhongShader.setVector3Uniform("light1DiffuseColor", Vector3(mLight1.getDiffuseColor().r, mLight1.getDiffuseColor().g, mLight1.getDiffuseColor().b));
    mPhongShader.setVector3Uniform("light2DiffuseColor", Vector3(mLight2.getDiffuseColor().r, mLight2.getDiffuseColor().g, mLight2.getDiffuseColor().b));
    mPhongShader.setIntUniform("shadowMapSampler0", textureUnits[0]);
    mPhongShader.setIntUniform("shadowMapSampler1", textureUnits[1]);
    mPhongShader.setIntUniform("isShadowEnabled", mIsShadowMappingEnabled);
    mPhongShader.setVector2Uniform("shadowMapDimension", Vector2(SHADOWMAP_WIDTH, SHADOWMAP_HEIGHT));
	mPhongShader.unbind();

	// Set the variables of the color shader
	mColorShader.bind();
	mColorShader.setMatrix4x4Uniform("projectionMatrix", mCamera.getProjectionMatrix());
	mColorShader.unbind();

    // 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);

    // Render the objects of the scene
    renderSinglePass(mPhongShader, worldToCameraMatrix);

    // Render the snapshots contact points
    renderSnapshotsContactPoints(mPhongShader, worldToCameraMatrix);

    // Render the debug infos
    if (mPhysicsWorld->getIsDebugRenderingEnabled()) {
        renderDebugInfos(mColorShader, worldToCameraMatrix);
    }

	// Is shadow mapping is enabled
	if (mIsShadowMappingEnabled) {

		for (int i = 0; i < NB_SHADOW_MAPS; i++) {
			mShadowMapTexture[i].unbind();
		}
	}

    mPhongShader.unbind();

   //drawTextureQuad();
}
// Render the scene (in multiple passes for shadow mapping)
Render the scene (in multiple passes for shadow mapping)
Render the scene ( in multiple passes for shadow mapping )
void SceneDemo::render() {
    updateDebugVBO();
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glClearColor(mBackgroundColor.r, mBackgroundColor.g, mBackgroundColor.b, mBackgroundColor.a);
	Matrix4 shadowMapProjMatrix[NB_SHADOW_MAPS];
	openglframework::Matrix4 worldToLightCameraMatrix[NB_SHADOW_MAPS];
	for (int i = 0; i < NB_SHADOW_MAPS; i++) {
		shadowMapProjMatrix[i] = mShadowMapLightCameras[i].getProjectionMatrix();
		worldToLightCameraMatrix[i] = mShadowMapLightCameras[i].getTransformMatrix().getInverse();
	}
    if (mIsShadowMappingEnabled) {
        glCullFace(GL_BACK);
		for (int i = 0; i < NB_SHADOW_MAPS; i++) {
			mFBOShadowMap[i].bind();
			mDepthShader.bind();
			mDepthShader.setMatrix4x4Uniform("projectionMatrix", shadowMapProjMatrix[i]);
			glViewport(0, 0, SHADOWMAP_WIDTH, SHADOWMAP_HEIGHT);
			glClear(GL_DEPTH_BUFFER_BIT);
			glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
			renderSinglePass(mDepthShader, worldToLightCameraMatrix[i]);
			mDepthShader.unbind();
			mFBOShadowMap[i].unbind();
		}
        glDisable(GL_POLYGON_OFFSET_FILL);
    }
    glCullFace(GL_BACK);
    const openglframework::Matrix4 worldToCameraMatrix = mCamera.getTransformMatrix().getInverse();
    mPhongShader.bind();
	GLint textureUnits[NB_SHADOW_MAPS];
	if (mIsShadowMappingEnabled) {
		for (int i = 0; i < NB_SHADOW_MAPS; i++) {
			mShadowMapTexture[i].bind();
		}
		for (int i = 0; i < NB_SHADOW_MAPS; i++) {
			textureUnits[i] = mShadowMapTexture[i].getUnit();
		}
	}
    mPhongShader.setMatrix4x4Uniform("projectionMatrix", mCamera.getProjectionMatrix());
    mPhongShader.setMatrix4x4Uniform("shadowMapLight0ProjectionMatrix", mShadowMapBiasMatrix * shadowMapProjMatrix[0]);
    mPhongShader.setMatrix4x4Uniform("shadowMapLight1ProjectionMatrix", mShadowMapBiasMatrix * shadowMapProjMatrix[1]);
    mPhongShader.setMatrix4x4Uniform("worldToLight0CameraMatrix", worldToLightCameraMatrix[0]);
    mPhongShader.setMatrix4x4Uniform("worldToLight1CameraMatrix", worldToLightCameraMatrix[1]);
    mPhongShader.setVector3Uniform("light0PosCameraSpace", worldToCameraMatrix * mLight0.getOrigin());
    mPhongShader.setVector3Uniform("light1PosCameraSpace", worldToCameraMatrix * mLight1.getOrigin());
    mPhongShader.setVector3Uniform("light2PosCameraSpace", worldToCameraMatrix * mLight2.getOrigin());
    mPhongShader.setVector3Uniform("lightAmbientColor", Vector3(0.1f, 0.1f, 0.1f));
    mPhongShader.setVector3Uniform("light0DiffuseColor", Vector3(mLight0.getDiffuseColor().r, mLight0.getDiffuseColor().g, mLight0.getDiffuseColor().b));
    mPhongShader.setVector3Uniform("light1DiffuseColor", Vector3(mLight1.getDiffuseColor().r, mLight1.getDiffuseColor().g, mLight1.getDiffuseColor().b));
    mPhongShader.setVector3Uniform("light2DiffuseColor", Vector3(mLight2.getDiffuseColor().r, mLight2.getDiffuseColor().g, mLight2.getDiffuseColor().b));
    mPhongShader.setIntUniform("shadowMapSampler0", textureUnits[0]);
    mPhongShader.setIntUniform("shadowMapSampler1", textureUnits[1]);
    mPhongShader.setIntUniform("isShadowEnabled", mIsShadowMappingEnabled);
    mPhongShader.setVector2Uniform("shadowMapDimension", Vector2(SHADOWMAP_WIDTH, SHADOWMAP_HEIGHT));
	mPhongShader.unbind();
	mColorShader.bind();
	mColorShader.setMatrix4x4Uniform("projectionMatrix", mCamera.getProjectionMatrix());
	mColorShader.unbind();
    glViewport(mViewportX, mViewportY, mViewportWidth, mViewportHeight);
    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    renderSinglePass(mPhongShader, worldToCameraMatrix);
    renderSnapshotsContactPoints(mPhongShader, worldToCameraMatrix);
    if (mPhysicsWorld->getIsDebugRenderingEnabled()) {
        renderDebugInfos(mColorShader, worldToCameraMatrix);
    }
	if (mIsShadowMappingEnabled) {
		for (int i = 0; i < NB_SHADOW_MAPS; i++) {
			mShadowMapTexture[i].unbind();
		}
	}
    mPhongShader.unbind();
}
void SceneDemo :: render ( ) { updateDebugVBO ( ) ; glEnable ( GL_DEPTH_TEST ) ; glEnable ( GL_CULL_FACE ) ; glClearColor ( mBackgroundColor . r , mBackgroundColor . g , mBackgroundColor . b , mBackgroundColor . a ) ; Matrix4 shadowMapProjMatrix [ NB_SHADOW_MAPS ] ; openglframework :: Matrix4 worldToLightCameraMatrix [ NB_SHADOW_MAPS ] ; for ( int i = 0 ; i < NB_SHADOW_MAPS ; i ++ ) { shadowMapProjMatrix [ i ] = mShadowMapLightCameras [ i ] . getProjectionMatrix ( ) ; worldToLightCameraMatrix [ i ] = mShadowMapLightCameras [ i ] . getTransformMatrix ( ) . getInverse ( ) ; } if ( mIsShadowMappingEnabled ) { glCullFace ( GL_BACK ) ; for ( int i = 0 ; i < NB_SHADOW_MAPS ; i ++ ) { mFBOShadowMap [ i ] . bind ( ) ; mDepthShader . bind ( ) ; mDepthShader . setMatrix4x4Uniform ( " " , shadowMapProjMatrix [ i ] ) ; glViewport ( 0 , 0 , SHADOWMAP_WIDTH , SHADOWMAP_HEIGHT ) ; glClear ( GL_DEPTH_BUFFER_BIT ) ; glColorMask ( GL_FALSE , GL_FALSE , GL_FALSE , GL_FALSE ) ; renderSinglePass ( mDepthShader , worldToLightCameraMatrix [ i ] ) ; mDepthShader . unbind ( ) ; mFBOShadowMap [ i ] . unbind ( ) ; } glDisable ( GL_POLYGON_OFFSET_FILL ) ; } glCullFace ( GL_BACK ) ; const openglframework :: Matrix4 worldToCameraMatrix = mCamera . getTransformMatrix ( ) . getInverse ( ) ; mPhongShader . bind ( ) ; GLint textureUnits [ NB_SHADOW_MAPS ] ; if ( mIsShadowMappingEnabled ) { for ( int i = 0 ; i < NB_SHADOW_MAPS ; i ++ ) { mShadowMapTexture [ i ] . bind ( ) ; } for ( int i = 0 ; i < NB_SHADOW_MAPS ; i ++ ) { textureUnits [ i ] = mShadowMapTexture [ i ] . getUnit ( ) ; } } mPhongShader . setMatrix4x4Uniform ( " " , mCamera . getProjectionMatrix ( ) ) ; mPhongShader . setMatrix4x4Uniform ( " " , mShadowMapBiasMatrix * shadowMapProjMatrix [ 0 ] ) ; mPhongShader . setMatrix4x4Uniform ( " " , mShadowMapBiasMatrix * shadowMapProjMatrix [ 1 ] ) ; mPhongShader . setMatrix4x4Uniform ( " " , worldToLightCameraMatrix [ 0 ] ) ; mPhongShader . setMatrix4x4Uniform ( " " , worldToLightCameraMatrix [ 1 ] ) ; mPhongShader . setVector3Uniform ( " " , worldToCameraMatrix * mLight0 . getOrigin ( ) ) ; mPhongShader . setVector3Uniform ( " " , worldToCameraMatrix * mLight1 . getOrigin ( ) ) ; mPhongShader . setVector3Uniform ( " " , worldToCameraMatrix * mLight2 . getOrigin ( ) ) ; mPhongShader . setVector3Uniform ( " " , Vector3 ( 0.1f , 0.1f , 0.1f ) ) ; mPhongShader . setVector3Uniform ( " " , Vector3 ( mLight0 . getDiffuseColor ( ) . r , mLight0 . getDiffuseColor ( ) . g , mLight0 . getDiffuseColor ( ) . b ) ) ; mPhongShader . setVector3Uniform ( " " , Vector3 ( mLight1 . getDiffuseColor ( ) . r , mLight1 . getDiffuseColor ( ) . g , mLight1 . getDiffuseColor ( ) . b ) ) ; mPhongShader . setVector3Uniform ( " " , Vector3 ( mLight2 . getDiffuseColor ( ) . r , mLight2 . getDiffuseColor ( ) . g , mLight2 . getDiffuseColor ( ) . b ) ) ; mPhongShader . setIntUniform ( " " , textureUnits [ 0 ] ) ; mPhongShader . setIntUniform ( " " , textureUnits [ 1 ] ) ; mPhongShader . setIntUniform ( " " , mIsShadowMappingEnabled ) ; mPhongShader . setVector2Uniform ( " " , Vector2 ( SHADOWMAP_WIDTH , SHADOWMAP_HEIGHT ) ) ; mPhongShader . unbind ( ) ; mColorShader . bind ( ) ; mColorShader . setMatrix4x4Uniform ( " " , mCamera . getProjectionMatrix ( ) ) ; mColorShader . unbind ( ) ; glViewport ( mViewportX , mViewportY , mViewportWidth , mViewportHeight ) ; glColorMask ( GL_TRUE , GL_TRUE , GL_TRUE , GL_TRUE ) ; glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ; renderSinglePass ( mPhongShader , worldToCameraMatrix ) ; renderSnapshotsContactPoints ( mPhongShader , worldToCameraMatrix ) ; if ( mPhysicsWorld -> getIsDebugRenderingEnabled ( ) ) { renderDebugInfos ( mColorShader , worldToCameraMatrix ) ; } if ( mIsShadowMappingEnabled ) { for ( int i = 0 ; i < NB_SHADOW_MAPS ; i ++ ) { mShadowMapTexture [ i ] . unbind ( ) ; } } mPhongShader . unbind ( ) ; }
Render the scene (in multiple passes for shadow mapping)
Render the scene ( in multiple passes for shadow mapping )
// Update the VBO for the debug infos // ---------- Render the scene to generate the shadow map (first pass) ----------- // // If Shadow Mapping is enabled // Culling switching, rendering only backface, this is done to avoid self-shadowing // For each shadow map // Bind the shader // Set the variables of the shader // Set the viewport to render into the shadow map texture // Clear previous frame values // Disable color rendering, we only want to write to the Z-Buffer // Render the objects of the scene // Unbind the shader // ---------- Render the scene for final rendering (second pass) ----------- // // Get the world-space to camera-space matrix // Is shadow mapping is enabled // Set the variables of the phong shader // Set the variables of the color shader // Set the viewport to render the scene //Enabling color write (previously disabled for light POV z-buffer rendering) // Clear previous frame values // Render the objects of the scene // Render the snapshots contact points // Render the debug infos // Is shadow mapping is enabled //drawTextureQuad();