void OpenGLShaderPointLight::copyLight(PointLight &light) { setParamValue("position", light.getPosition()); setParamValue("ambient", light.getAmbient()); setParamValue("diffuse", light.getDiffuse()); setParamValue("specular", light.getSpecular()); setParamValue("attenuation", light.getAttenuation()); setParamValue("cutoff", light.getCutoff()); bind(); }
/*! Sets the \a lightChunk fields based on light. \dev DeferredShadingStage can not use the light's own chunk, because it computes the light position based on DrawEnv::getCameraViewing(), which is just the ortho camera for rendering the full screen quads, not the perspective camera used during the gbuffer pass. \enddev */ void DeferredShadingStage::updateLightChunk( DSLightChunk *lightChunk, Light *light) { lightChunk->setBeacon (light->getBeacon ()); lightChunk->setAmbient (light->getAmbient ()); lightChunk->setDiffuse (light->getDiffuse ()); lightChunk->setSpecular(light->getSpecular()); if(light->getType() == DirectionalLight::getClassType()) { DirectionalLight *dirL = static_cast<DirectionalLight *>(light); Vec4f dir(dirL->getDirection()); dir[3] = 0.f; lightChunk->setPosition(dir); } else if(light->getType() == PointLight::getClassType()) { PointLight *pointL = static_cast<PointLight *>(light); Vec4f pos(pointL->getPosition()); pos[3] = 1.f; lightChunk->setPosition (pos ); lightChunk->setConstantAttenuation (pointL->getConstantAttenuation ()); lightChunk->setLinearAttenuation (pointL->getLinearAttenuation ()); lightChunk->setQuadraticAttenuation(pointL->getQuadraticAttenuation()); lightChunk->setCutoff (180.f ); } else if(light->getType() == SpotLight::getClassType()) { SpotLight *spotL = static_cast<SpotLight *>(light); Vec4f pos(spotL->getPosition()); pos[3] = 1.f; lightChunk->setPosition (pos ); lightChunk->setConstantAttenuation (spotL->getConstantAttenuation ()); lightChunk->setLinearAttenuation (spotL->getLinearAttenuation ()); lightChunk->setQuadraticAttenuation(spotL->getQuadraticAttenuation()); lightChunk->setDirection (spotL->getDirection ()); lightChunk->setExponent (spotL->getSpotExponent ()); lightChunk->setCutoff ( osgRad2Degree(spotL->getSpotCutOff())); } else { SWARNING << "DeferredShadingStage::updateLightChunk: " << "Unknown light type." << endLog; } }
void VarianceShadowMapHandler::createShadowMapsFBO(RenderAction *a, DrawEnv *pEnv) { UInt32 mSize = _pStage->getMapSize(); if(mSize > 2048) mSize = 2048; //------Setting up Window to fit size of ShadowMap---------------- // disable all lights more speed std::vector<bool> vLocalLightStates; const ShadowStageData::LightStore &vLights = _pStageData->getLights(); const ShadowStageData::LStateStore &vLightStates = _pStageData->getLightStates(); const ShadowStageData::CamStore &vLCams = _pStageData->getLightCameras(); const ShadowStageData::StatusStore &vExclActive = _pStageData->getExcludeNodeActive(); for(UInt32 i = 0;i < vLights.size();++i) { // store old states. vLocalLightStates.push_back(vLights[i].second->getOn()); vLights[i].second->setOn(false); } // activate exclude nodes: for(UInt32 i = 0;i < _pStage->getMFExcludeNodes()->size();++i) { Node *exnode = _pStage->getExcludeNodes(i); if(exnode != NULL) { if(vExclActive[i]) { exnode->setTravMask(0); } } } UInt32 uiActiveLightCount = 0; ShadowStageData::ShadowMapStore &vShadowMaps = _pStageData->getShadowMaps(); for(UInt32 i = 0;i < vLights.size();++i) { if(vLightStates[i] != 0) { if(_pStage->getGlobalShadowIntensity() != 0.0 || vLights[i].second->getShadowIntensity() != 0.0) { GLenum *buffers = NULL; buffers = new GLenum[1]; buffers[0] = GL_COLOR_ATTACHMENT0_EXT; Pnt3f lPos; bool isDirLight; Real32 sceneDiagLength; if(vLights[i].second->getType() == PointLight::getClassType()) { PointLight *tmpPoint; tmpPoint = dynamic_cast<PointLight *>(vLights[i].second.get()); lPos = tmpPoint->getPosition(); if(tmpPoint->getBeacon() != NULL) { Matrix m = tmpPoint->getBeacon()->getToWorld(); m.mult(lPos, lPos); } isDirLight = false; Pnt3f center; _pStageData->getLightRoot(i)->getVolume().getCenter(center); Vec3f dir = lPos - center; Real32 dirLength = dir.length(); Vec3f diff = (_pStageData->getLightRoot(i)->getVolume().getMax() - center); Real32 diffLength = diff.length(); sceneDiagLength = dirLength + diffLength; } else if(vLights[i].second->getType() == SpotLight::getClassType()) { SpotLight *tmpSpot; tmpSpot = dynamic_cast<SpotLight *>(vLights[i].second.get()); lPos = tmpSpot->getPosition(); if(tmpSpot->getBeacon() != NULL) { Matrix m = tmpSpot->getBeacon()->getToWorld(); m.mult(lPos, lPos); } isDirLight = false; Pnt3f center; _pStageData->getLightRoot(i)->getVolume().getCenter(center); Vec3f dir = lPos - center; Real32 dirLength = dir.length(); Vec3f diff = (_pStageData->getLightRoot(i)->getVolume().getMax() - center); Real32 diffLength = diff.length(); sceneDiagLength = dirLength + diffLength; } else { isDirLight = true; sceneDiagLength = 1.0; } if(_vDepthCmat.size() == uiActiveLightCount) { _vDepthCmat.push_back(ChunkMaterial::createLocal()); } OSG_ASSERT(uiActiveLightCount < _vDepthCmat.size()); if(_vDepthSHLVar.size() == uiActiveLightCount) { _vDepthSHLVar.push_back( SimpleSHLVariableChunk::createLocal()); #ifndef OSG_NEW_SHADER _vDepthSHLVar[uiActiveLightCount]->setSHLChunk(_depthSHL); #endif } OSG_ASSERT(uiActiveLightCount < _vDepthSHLVar.size()); _vDepthSHLVar[uiActiveLightCount]->addUniformVariable( "sceneDiagLength", Real32(sceneDiagLength)); _vDepthSHLVar[uiActiveLightCount]->addUniformVariable( "isDirLight", bool(isDirLight)); _vDepthCmat[uiActiveLightCount]->clearChunks(); _vDepthCmat[uiActiveLightCount]->addChunk(_depthSHL); _vDepthCmat[uiActiveLightCount]->addChunk( _vDepthSHLVar[uiActiveLightCount]); commitChanges(); _pStage->pushPartition(a); { RenderPartition *pPart = a->getActivePartition(); pPart->addPreRenderCallback( &ShadowTreeHandler::setupAmbientModel); pPart->addPostRenderCallback( &ShadowTreeHandler::endAmbientModel); pPart->setRenderTarget(vShadowMaps[i].pFBO); pPart->setDrawBuffer (*buffers ); pPart->setWindow (a->getWindow()); pPart->calcViewportDimension(0.f, 0.f, mSize - 1, mSize - 1, mSize, mSize); RenderFunctor f = boost::bind(&VarianceShadowMapHandler::genMipMapCB, this, _1, i); pPart->addPreRenderCallback(f); Matrix m, t; // set the projection vLCams[i]->getProjection( m, pPart->getViewportWidth (), pPart->getViewportHeight()); vLCams[i]->getProjectionTranslation( t, pPart->getViewportWidth (), pPart->getViewportHeight()); pPart->setupProjection(m, t); vLCams[i]->getViewing( m, pPart->getViewportWidth (), pPart->getViewportHeight()); pPart->setupViewing(m); pPart->setNear (vLCams[i]->getNear()); pPart->setFar (vLCams[i]->getFar ()); pPart->calcFrustum(); pPart->setBackground(_pClearSMapBack); Node *light = vLights[i].first; Node *parent = light->getParent(); if(parent != NULL) { a->pushMatrix(parent->getToWorld()); } a->overrideMaterial(_vDepthCmat[uiActiveLightCount], a->getActNode()); _pStage->recurse(a, light); a->overrideMaterial( NULL, a->getActNode()); if(parent != NULL) { a->popMatrix(); } } _pStage->popPartition(a); ++uiActiveLightCount; } } } //-------Restoring old states of Window and Viewport---------- // enable all lights. for(UInt32 i = 0;i < vLights.size();++i) { // restore old states. vLights[i].second->setOn(vLocalLightStates[i]); } // activate exclude nodes: for(UInt32 i = 0;i < _pStage->getMFExcludeNodes()->size();++i) { Node *exnode = _pStage->getExcludeNodes(i); if(exnode != NULL) { if(vExclActive[i]) { exnode->setTravMask(TypeTraits<UInt32>::BitsSet); } } } }
void VarianceShadowMapHandler::createShadowFactorMapFBO( RenderAction *a, DrawEnv *pEnv, UInt32 num, UInt32 uiActiveLightCount) { glClearColor(0.0, 0.0, 0.0, 1.0); //Finde alle aktiven Lichtquellen Real32 activeLights = 0; const ShadowStageData::LightStore &vLights = _pStageData->getLights(); const ShadowStageData::LStateStore &vLightStates = _pStageData->getLightStates(); const ShadowStageData::CamStore &vLCams = _pStageData->getLightCameras(); if(_pStage->getGlobalShadowIntensity() != 0.0) { for(UInt32 i = 0;i < vLights.size();i++) { if(vLightStates[i] != 0) activeLights++; } } else { for(UInt32 i = 0;i < vLights.size();i++) { if(vLightStates[i] != 0 && vLights [i].second->getShadowIntensity() != 0.0) { activeLights++; } } } Real32 shadowIntensity; if(_pStage->getGlobalShadowIntensity() != 0.0) { shadowIntensity = (_pStage->getGlobalShadowIntensity() / activeLights); } else { shadowIntensity = (vLights[num].second->getShadowIntensity() / activeLights); } if(vLights[num].second->getShadowIntensity() != 0.0 || _pStage->getGlobalShadowIntensity() != 0.0) { Matrix LVM, LPM, CVM; vLCams[num]->getViewing(LVM, pEnv->getPixelWidth(), pEnv->getPixelHeight()); vLCams[num]->getProjection(LPM, pEnv->getPixelWidth(), pEnv->getPixelHeight()); CVM = pEnv->getCameraViewing(); Matrix iCVM = CVM; iCVM.invert(); Real32 texFactor; if(vLights[num].second->getType() == SpotLight ::getClassType() || vLights[num].second->getType() == PointLight::getClassType()) { texFactor = Real32(_width) / Real32(_height); } else { texFactor = 1.0; } Matrix shadowMatrix = LPM; shadowMatrix.mult(LVM); shadowMatrix.mult(iCVM); Matrix shadowMatrix2 = LVM; shadowMatrix2.mult(iCVM); Real32 xFactor = 1.0; Real32 yFactor = 1.0; Pnt3f lPos; bool isDirLight; Real32 sceneDiagLength; if(vLights[num].second->getType() == PointLight::getClassType()) { PointLight *tmpPoint; tmpPoint = dynamic_cast<PointLight *>( vLights[num].second.get()); lPos = tmpPoint->getPosition(); if(tmpPoint->getBeacon() != NULL) { Matrix m = tmpPoint->getBeacon()->getToWorld(); m.mult(lPos, lPos); } isDirLight = false; Pnt3f center; _pStageData->getLightRoot(num)->getVolume().getCenter(center); Vec3f dir = lPos - center; Real32 dirLength = dir.length(); Vec3f diff = (_pStageData->getLightRoot(num)->getVolume ().getMax() - center); Real32 diffLength = diff.length(); sceneDiagLength = dirLength + diffLength; } else if(vLights[num].second->getType() == SpotLight::getClassType()) { SpotLight *tmpSpot; tmpSpot = dynamic_cast<SpotLight *>( vLights[num].second.get()); lPos = tmpSpot->getPosition(); if(tmpSpot->getBeacon() != NULL) { Matrix m = tmpSpot->getBeacon()->getToWorld(); m.mult(lPos, lPos); } isDirLight = false; Pnt3f center; _pStageData->getLightRoot(num)->getVolume().getCenter(center); Vec3f dir = lPos - center; Real32 dirLength = dir.length(); Vec3f diff = (_pStageData->getLightRoot(num)->getVolume ().getMax() - center); Real32 diffLength = diff.length(); sceneDiagLength = dirLength + diffLength; } else { isDirLight = true; sceneDiagLength = 1.0; } Real32 lod; if(_pStage->getShadowSmoothness() <= 0.1999) lod = 0.5; else if(_pStage->getShadowSmoothness() <= 0.3999) lod = 1.5; else if(_pStage->getShadowSmoothness() <= 0.5999) lod = 2.5; else if(_pStage->getShadowSmoothness() <= 0.7999) lod = 3.5; else lod = 4.5; if(_vShadowCmat.size() == uiActiveLightCount) { _vShadowCmat.push_back(ChunkMaterial::createLocal()); } OSG_ASSERT( uiActiveLightCount < _vShadowCmat.size()); if(_vShadowSHLVar.size() == uiActiveLightCount) { _vShadowSHLVar.push_back(SimpleSHLVariableChunk::createLocal()); #ifndef OSG_NEW_SHADER _vShadowSHLVar[uiActiveLightCount]->setSHLChunk(_shadowSHL); #endif } OSG_ASSERT(uiActiveLightCount < _vShadowSHLVar.size()); _shadowSHL->addUniformVariable("shadowMap", 0); _shadowSHL->addUniformVariable("oldFactorMap", 1); _vShadowSHLVar[uiActiveLightCount]->addUniformVariable( "firstRun", (uiActiveLightCount == 0) ? Int32(1) : Int32(0)); _vShadowSHLVar[uiActiveLightCount]->addUniformVariable( "intensity", shadowIntensity); _vShadowSHLVar[uiActiveLightCount]->addUniformVariable( "texFactor", texFactor); //_shadowSHL->addUniformVariable("shadowBias", 0.0075f); _vShadowSHLVar[uiActiveLightCount]->addUniformVariable( "lightPM", shadowMatrix); _vShadowSHLVar[uiActiveLightCount]->addUniformVariable( "lightPM2", shadowMatrix2); //_shadowSHL->addUniformVariable("shadowRange", Real32(shadowRange)); _vShadowSHLVar[uiActiveLightCount]->addUniformVariable( "xFactor", Real32(xFactor)); _vShadowSHLVar[uiActiveLightCount]->addUniformVariable( "yFactor", Real32(yFactor)); _vShadowSHLVar[uiActiveLightCount]->addUniformVariable( "sceneDiagLength", Real32(sceneDiagLength)); _vShadowSHLVar[uiActiveLightCount]->addUniformVariable( "Lod", Real32(lod)); _vShadowSHLVar[uiActiveLightCount]->addUniformVariable( "isDirLight", bool(isDirLight)); ShadowStageData::ShadowMapStore &vShadowMaps = _pStageData->getShadowMaps(); _vShadowCmat[uiActiveLightCount]->clearChunks(); _vShadowCmat[uiActiveLightCount]->addChunk( _shadowSHL); _vShadowCmat[uiActiveLightCount]->addChunk( _vShadowSHLVar[uiActiveLightCount]); _vShadowCmat[uiActiveLightCount]->addChunk( vShadowMaps[num].pTexO); _vShadowCmat[uiActiveLightCount]->addChunk( vShadowMaps[num].pTexE); _vShadowCmat[uiActiveLightCount]->addChunk( _shadowFactorMapO); _pStage->pushPartition(a, (RenderPartition::CopyWindow | RenderPartition::CopyViewing | RenderPartition::CopyProjection | RenderPartition::CopyFrustum | RenderPartition::CopyNearFar | RenderPartition::CopyViewportSize), RenderPartition::StateSorting); { RenderPartition *pPart = a->getActivePartition(); pPart->addPreRenderCallback (&ShadowTreeHandler::setupAmbientModel); pPart->addPostRenderCallback(&ShadowTreeHandler::endAmbientModel ); pPart->setRenderTarget(_pSceneFBO); pPart->setDrawBuffer (GL_COLOR_ATTACHMENT1_EXT); Node *light = vLights[num].first; Node *parent = light->getParent(); if(parent != NULL) { a->pushMatrix(parent->getToWorld()); } if(uiActiveLightCount == 0) { pPart->setBackground(_pClearBackground); } commitChanges(); a->overrideMaterial(_vShadowCmat[uiActiveLightCount], a->getActNode()); _pStage->recurse(a, light); a->overrideMaterial( NULL, a->getActNode()); if(parent != NULL) { a->popMatrix(); } } _pStage->popPartition(a); _firstRun = 0; } }
void VRShadowEngine::setupLightChunk(Light *pLight, LightTypeE eType, RenderAction *pAction, EngineDataPtr pEngineData) { if(eType == Directional) { DirectionalLight *pDLight = dynamic_cast<DirectionalLight *>(pLight); LightChunkUnrecPtr pChunk = pEngineData->getLightChunk(); if(pChunk == NULL) { pChunk = LightChunk::createLocal(); pEngineData->setLightChunk(pChunk); } Color4f tmpVal(0.0, 0.0, 0.0, 1.0); pChunk->setSpecular(tmpVal); tmpVal.setValuesRGBA(0.2f, 0.2f, 0.2f, 1.0f); pChunk->setDiffuse (tmpVal); tmpVal.setValuesRGBA(0.0, 0.0, 0.0, 1.0); pChunk->setAmbient (tmpVal); Vec4f dir(pDLight->getDirection()); dir[3] = 0; pChunk->setPosition(dir); pChunk->setBeacon(pLight->getBeacon()); } else if(eType == Point) { PointLight *pPLight = dynamic_cast<PointLight *>(pLight); LightChunkUnrecPtr pChunk = pEngineData->getLightChunk(); if(pChunk == NULL) { pChunk = LightChunk::createLocal(); pEngineData->setLightChunk(pChunk); } Color4f tmpVal(0.0, 0.0, 0.0, 1.0); pChunk->setSpecular(tmpVal); tmpVal.setValuesRGBA(this->getShadowColor()[0], this->getShadowColor()[1], this->getShadowColor()[2], this->getShadowColor()[3]); pChunk->setDiffuse (tmpVal); tmpVal.setValuesRGBA(0.0, 0.0, 0.0, 1.0); pChunk->setAmbient (tmpVal); Vec4f pos(pPLight->getPosition()); pos[3] = 1; pChunk->setPosition(pos); pChunk->setBeacon(pLight->getBeacon()); } }
void VRShadowEngine::setupCamera(Light *pLight, LightTypeE eType, RenderAction *pAction, EngineDataPtr pEngineData) { if(eType == Directional) { DirectionalLight *pDLight = dynamic_cast<DirectionalLight *>(pLight); MatrixCameraUnrecPtr pCam = dynamic_cast<MatrixCamera *>(pEngineData->getCamera()); if(pCam == NULL) { pCam = MatrixCamera::createLocal(); pEngineData->setCamera(pCam); } Vec3f diff; Pnt3f center; Matrix transMatrix; Node *pNode = pAction->getActNode(); // tmpDir = DirectionalLightPtr::dcast(_lights[i]); diff = (pNode->getVolume().getMax() - pNode->getVolume().getMin()); Real32 sceneWidth = diff.length() * 0.5f; // Not final values. May get tweaked in the future Real32 sceneHeight = diff.length() * 0.5f; pNode->getVolume().getCenter(center); Vec3f lightdir = pDLight->getDirection(); if(pLight->getBeacon() != NULL) { Matrix m = pLight->getBeacon()->getToWorld(); m.mult(lightdir, lightdir); } MatrixLookAt(transMatrix, center + lightdir, center, Vec3f(0,1,0)); transMatrix.invert(); Matrix proMatrix; proMatrix.setIdentity(); MatrixOrthogonal( proMatrix, -sceneWidth, sceneWidth, -sceneHeight, sceneHeight, -sceneWidth, sceneWidth); pCam->setProjectionMatrix(proMatrix ); pCam->setModelviewMatrix (transMatrix); } else if(eType == Point) { PointLight *pPLight = dynamic_cast<PointLight *>(pLight); MatrixCameraUnrecPtr pCam = dynamic_cast<MatrixCamera *>(pEngineData->getCamera()); if(pCam == NULL) { pCam = MatrixCamera::createLocal(); pEngineData->setCamera(pCam); } Real32 angle; Vec3f dist; Pnt3f center; Vec3f diff; Matrix transMatrix; Node *pNode = pAction->getActNode(); pNode->getVolume().getCenter(center); Pnt3f lightpos = pPLight->getPosition(); if(pLight->getBeacon() != NULL) { Matrix m = pLight->getBeacon()->getToWorld(); m.mult(lightpos, lightpos); } MatrixLookAt(transMatrix, lightpos, center, Vec3f(0,1,0)); transMatrix.invert(); diff = (pNode->getVolume().getMax() - pNode->getVolume().getMin()); dist = lightpos - center; angle = atan((diff.length() * 0.5) / dist.length()); Matrix proMatrix; proMatrix.setIdentity(); MatrixPerspective( proMatrix, 2.f * angle, 1, pAction->getActivePartition()->getNear(), pAction->getActivePartition()->getFar ()); pCam->setProjectionMatrix(proMatrix ); pCam->setModelviewMatrix (transMatrix); } }
PVScene::PVScene(Scene *scene) { glGenVertexArrays(1, &vertexArray); glBindVertexArray(vertexArray); GLCHECK(); glGenBuffers(1, &vertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); GLCHECK(); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glEnableVertexAttribArray(2); glEnableVertexAttribArray(3); glVertexAttribPointer(0, 3, GL_FLOAT, false, 64, (void *)0); glVertexAttribPointer(1, 3, GL_FLOAT, false, 64, (void *)16); glVertexAttribPointer(2, 3, GL_FLOAT, false, 64, (void *)32); glVertexAttribPointer(3, 2, GL_FLOAT, false, 64, (void *)48); GLCHECK(); std::vector<Vertex> vertices; int vertexCount = 0; int vertexOffset = 0; int curr_material = scene->getTriangles()[0].material_id; for (Triangle & tri : scene->getTriangles()) { if (curr_material != tri.material_id) { addMesh(scene, curr_material, vertexOffset, vertexCount); vertexOffset += vertexCount; vertexCount = 0; curr_material = tri.material_id; } vertices.push_back(tri.v[0]); vertices.push_back(tri.v[1]); vertices.push_back(tri.v[2]); vertexCount += 3; } addMesh(scene, curr_material, vertexOffset, vertexCount); Camera *camera = scene->getCamera(); viewMatrix = lookAtLH(camera->getPosition(), camera->getTarget(), camera->getUp()); projectionMatrix = perspectiveLH(camera->getFOV(), camera->getAspectRatio(), 0.1f, 100.0f); GLCHECK(); glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0], GL_DYNAMIC_DRAW); GLCHECK(); glBindVertexArray(0); GLCHECK(); for (int i = 0; i < scene->getNumLights(); i++) { PointLight *light = (PointLight *)scene->getLight(i); lights.push_back(new PVLight(light->getPosition(), light->getRadiance())); } shader = new PVShader(vs3d_source, fs3d_source); GLCHECK(); }