void Stage::renderDirectionalLightShadowMap(Light* light, Camera* shadowMapCamera) { if (light->shadow != Shadows::None) { DirectionalLight* dirLight = (DirectionalLight*) light; //directional light only needs one shadow map texture //light camera is supposed to be setup so that ortho frustum contains all that the main camera sees float quantizeFactor = 0; std::vector<glm::vec3> frustumCorners; if (dirLight->getShadowMethod() == DirectionalShadowMethods::Basic) { frustumCorners = m_camera->getFrustumCorners(); quantizeFactor = m_camera->getFar() * 0.3f; } else if (dirLight->getShadowMethod() == DirectionalShadowMethods::Near) { Camera newCamera; newCamera.initPerspective(m_camera->getFov(), m_camera->getAspectRatio(), m_camera->getNear(), m_camera->getFar() * dirLight->getShadowMethodValue()); newCamera.setTransform(m_camera->getTransform()); frustumCorners = newCamera.getFrustumCorners(); quantizeFactor = m_camera->getFar() * dirLight->getShadowMethodValue() * 0.3f; } glm::mat4 lightView = light->getTransform(); glm::mat4 invLightView = glm::inverse(lightView); for (int j = 0; j < 8; ++j) { frustumCorners[j] = glm::vec3(lightView * glm::vec4(frustumCorners[j], 1)); frustumCorners[j].x = glm::ceil(frustumCorners[j].x / quantizeFactor) * quantizeFactor; frustumCorners[j].y = glm::ceil(frustumCorners[j].y / quantizeFactor) * quantizeFactor; frustumCorners[j].z = glm::ceil(frustumCorners[j].z / quantizeFactor) * quantizeFactor; } BoundingBox lightBox(frustumCorners); lightBox.max.z += 100; lightBox.min.z -= 100; glm::vec3 boxSize = lightBox.max - lightBox.min; glm::vec3 halfBoxSize = boxSize * 0.5f; glm::vec3 lightPosition = lightBox.min + halfBoxSize; lightPosition.z = lightBox.min.z; lightPosition = glm::vec3(invLightView * glm::vec4(lightPosition, 1)); shadowMapCamera->lookAt(lightPosition, lightPosition + light->getForward(), light->getUp()); shadowMapCamera->setProjection(glm::ortho(-boxSize.x, boxSize.x, -boxSize.y, boxSize.y, -boxSize.z, boxSize.z)); ShaderProgram* depthShader = &m_allShaders.esmDepth; if (light->shadow == Shadows::VSM) { depthShader = &m_allShaders.vsmDepth; } m_shadowMap.bind(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); renderScene(depthShader, shadowMapCamera, light); m_shadowMap.unbind(); } }
void MFixedRenderer::drawScene(MScene * scene, MOCamera * camera) { M_PROFILE_SCOPE(MFixedRenderer::drawScene); struct MEntityLight { MBox3d lightBox; MOLight * light; }; struct MSubMeshPass { unsigned int subMeshId; unsigned int lightsNumber; MObject3d * object; MOLight * lights[4]; }; // sub objects #define MAX_TRANSP_SUBOBJ 4096 static int transpList[MAX_TRANSP_SUBOBJ]; static float transpZList[MAX_TRANSP_SUBOBJ]; static MSubMeshPass transpSubObjs[MAX_TRANSP_SUBOBJ]; // lights list #define MAX_ENTITY_LIGHTS 256 static int entityLightsList[MAX_ENTITY_LIGHTS]; static float entityLightsZList[MAX_ENTITY_LIGHTS]; static MEntityLight entityLights[MAX_ENTITY_LIGHTS]; // get render MRenderingContext * render = MEngine::getInstance()->getRenderingContext(); render->enableLighting(); render->enableBlending(); // make frustum MFrustum * frustum = camera->getFrustum(); frustum->makeVolume(camera); // update visibility updateVisibility(scene, camera); // fog enableFog(camera); // camera MVector3 cameraPos = camera->getTransformedPosition(); // transp sub obj number unsigned int transpSubObsNumber = 0; // lights unsigned int l; unsigned int lSize = scene->getLightsNumber(); // entities unsigned int i; unsigned int eSize = scene->getEntitiesNumber(); for(i=0; i<eSize; i++) { // get entity MOEntity * entity = scene->getEntityByIndex(i); MMesh * mesh = entity->getMesh(); if(! entity->isActive()) continue; if(! entity->isVisible()) { if(mesh) { MArmature * armature = mesh->getArmature(); MArmatureAnim * armatureAnim = mesh->getArmatureAnim(); if(armature) { // animate armature if(armatureAnim) animateArmature( mesh->getArmature(), mesh->getArmatureAnim(), entity->getCurrentFrame() ); // TODO : optimize and add a tag to desactivate it updateSkinning(mesh, armature); (*entity->getBoundingBox()) = (*mesh->getBoundingBox()); } } continue; } // draw mesh if(mesh) { MVector3 scale = entity->getTransformedScale(); MBox3d * entityBox = entity->getBoundingBox(); float minScale = scale.x; minScale = MIN(minScale, scale.y); minScale = MIN(minScale, scale.z); minScale = 1.0f / minScale; unsigned int entityLightsNumber = 0; for(l=0; l<lSize; l++) { // get entity MOLight * light = scene->getLightByIndex(l); if(! light->isActive()) continue; if(! light->isVisible()) continue; // light box MVector3 lightPos = light->getTransformedPosition(); MVector3 localPos = entity->getInversePosition(lightPos); float localRadius = light->getRadius() * minScale; MBox3d lightBox( MVector3(localPos - localRadius), MVector3(localPos + localRadius) ); if(! entityBox->isInCollisionWith(&lightBox)) continue; MEntityLight * entityLight = &entityLights[entityLightsNumber]; entityLight->lightBox = lightBox; entityLight->light = light; entityLightsNumber++; if(entityLightsNumber == MAX_ENTITY_LIGHTS) break; } // animate armature if(mesh->getArmature() && mesh->getArmatureAnim()) animateArmature( mesh->getArmature(), mesh->getArmatureAnim(), entity->getCurrentFrame() ); // animate textures if(mesh->getTexturesAnim()) animateTextures(mesh, mesh->getTexturesAnim(), entity->getCurrentFrame()); // animate materials if(mesh->getMaterialsAnim()) animateMaterials(mesh, mesh->getMaterialsAnim(), entity->getCurrentFrame()); unsigned int s; unsigned int sSize = mesh->getSubMeshsNumber(); for(s=0; s<sSize; s++) { MSubMesh * subMesh = &mesh->getSubMeshs()[s]; MBox3d * box = subMesh->getBoundingBox(); // check if submesh visible if(sSize > 1) { MVector3 * min = box->getMin(); MVector3 * max = box->getMax(); MVector3 points[8] = { entity->getTransformedVector(MVector3(min->x, min->y, min->z)), entity->getTransformedVector(MVector3(min->x, max->y, min->z)), entity->getTransformedVector(MVector3(max->x, max->y, min->z)), entity->getTransformedVector(MVector3(max->x, min->y, min->z)), entity->getTransformedVector(MVector3(min->x, min->y, max->z)), entity->getTransformedVector(MVector3(min->x, max->y, max->z)), entity->getTransformedVector(MVector3(max->x, max->y, max->z)), entity->getTransformedVector(MVector3(max->x, min->y, max->z)) }; if(! frustum->isVolumePointsVisible(points, 8)) continue; } // subMesh center MVector3 center = (*box->getMin()) + ((*box->getMax()) - (*box->getMin()))*0.5f; center = entity->getTransformedVector(center); // sort entity lights unsigned int lightsNumber = 0; for(l=0; l<entityLightsNumber; l++) { MEntityLight * entityLight = &entityLights[l]; if(! box->isInCollisionWith(&entityLight->lightBox)) continue; MOLight * light = entityLight->light; float z = (center - light->getTransformedPosition()).getLength(); entityLightsList[lightsNumber] = l; entityLightsZList[l] = (1.0f/z)*light->getRadius(); lightsNumber++; } if(lightsNumber > 1) sortFloatList(entityLightsList, entityLightsZList, 0, (int)lightsNumber-1); // local lights if(lightsNumber > 4) lightsNumber = 4; for(l=0; l<lightsNumber; l++) { MEntityLight * entityLight = &entityLights[entityLightsList[l]]; MOLight * light = entityLight->light; // attenuation float quadraticAttenuation = (8.0f / light->getRadius()); quadraticAttenuation = (quadraticAttenuation*quadraticAttenuation)*light->getIntensity(); // color MVector3 color = light->getFinalColor(); // set light render->enableLight(l); render->setLightPosition(l, light->getTransformedPosition()); render->setLightDiffuse(l, MVector4(color)); render->setLightSpecular(l, MVector4(color)); render->setLightAmbient(l, MVector3(0, 0, 0)); render->setLightAttenuation(l, 1, 0, quadraticAttenuation); // spot render->setLightSpotAngle(l, light->getSpotAngle()); if(light->getSpotAngle() < 90){ render->setLightSpotDirection(l, light->getRotatedVector(MVector3(0, 0, -1)).getNormalized()); render->setLightSpotExponent(l, light->getSpotExponent()); } else { render->setLightSpotExponent(l, 0.0f); } } for(l=lightsNumber; l<4; l++){ render->disableLight(l); } render->pushMatrix(); render->multMatrix(entity->getMatrix()); // draw opaques drawOpaques(subMesh, mesh->getArmature()); if(subMesh->hasTransparency()) { if(transpSubObsNumber < MAX_TRANSP_SUBOBJ) { // transparent subMesh pass MSubMeshPass * subMeshPass = &transpSubObjs[transpSubObsNumber]; // lights subMeshPass->lightsNumber = lightsNumber; for(l=0; l<lightsNumber; l++) subMeshPass->lights[l] = entityLights[entityLightsList[l]].light; // z distance to camera float z = getDistanceToCam(camera, center); // set values transpList[transpSubObsNumber] = transpSubObsNumber; transpZList[transpSubObsNumber] = z; subMeshPass->subMeshId = s; subMeshPass->object = entity; transpSubObsNumber++; } } render->popMatrix(); } mesh->updateBoundingBox(); (*entity->getBoundingBox()) = (*mesh->getBoundingBox()); } } // texts unsigned int tSize = scene->getTextsNumber(); for(i=0; i<tSize; i++) { MOText * text = scene->getTextByIndex(i); if(text->isActive() && text->isVisible()) { // transparent pass MSubMeshPass * subMeshPass = &transpSubObjs[transpSubObsNumber]; // center MBox3d * box = text->getBoundingBox(); MVector3 center = (*box->getMin()) + ((*box->getMax()) - (*box->getMin()))*0.5f; center = text->getTransformedVector(center); // z distance to camera float z = getDistanceToCam(camera, center); // set values transpList[transpSubObsNumber] = transpSubObsNumber; transpZList[transpSubObsNumber] = z; subMeshPass->object = text; transpSubObsNumber++; } } // sort transparent list if(transpSubObsNumber > 1) sortFloatList(transpList, transpZList, 0, (int)transpSubObsNumber-1); // draw transparents for(i=0; i<transpSubObsNumber; i++) { MSubMeshPass * subMeshPass = &transpSubObjs[transpList[i]]; MObject3d * object = subMeshPass->object; // objects switch(object->getType()) { case M_OBJECT3D_ENTITY: { MOEntity * entity = (MOEntity *)object; unsigned int subMeshId = subMeshPass->subMeshId; MMesh * mesh = entity->getMesh(); MSubMesh * subMesh = &mesh->getSubMeshs()[subMeshId]; // animate armature if(mesh->getArmature() && mesh->getArmatureAnim()) animateArmature( mesh->getArmature(), mesh->getArmatureAnim(), entity->getCurrentFrame() ); // animate textures if(mesh->getTexturesAnim()) animateTextures(mesh, mesh->getTexturesAnim(), entity->getCurrentFrame()); // animate materials if(mesh->getMaterialsAnim()) animateMaterials(mesh, mesh->getMaterialsAnim(), entity->getCurrentFrame()); // lights for(l=0; l<subMeshPass->lightsNumber; l++) { MOLight * light = subMeshPass->lights[l]; // attenuation float quadraticAttenuation = (8.0f / light->getRadius()); quadraticAttenuation = (quadraticAttenuation*quadraticAttenuation)*light->getIntensity(); // color MVector3 color = light->getFinalColor(); // set light render->enableLight(l); render->setLightPosition(l, light->getTransformedPosition()); render->setLightDiffuse(l, MVector4(color)); render->setLightSpecular(l, MVector4(color)); render->setLightAmbient(l, MVector3(0, 0, 0)); render->setLightAttenuation(l, 1, 0, quadraticAttenuation); // spot render->setLightSpotAngle(l, light->getSpotAngle()); if(light->getSpotAngle() < 90){ render->setLightSpotDirection(l, light->getRotatedVector(MVector3(0, 0, -1)).getNormalized()); render->setLightSpotExponent(l, light->getSpotExponent()); } else { render->setLightSpotExponent(l, 0.0f); } } for(l=subMeshPass->lightsNumber; l<4; l++){ render->disableLight(l); } render->pushMatrix(); render->multMatrix(entity->getMatrix()); drawTransparents(subMesh, mesh->getArmature()); render->popMatrix(); mesh->updateBoundingBox(); (*entity->getBoundingBox()) = (*mesh->getBoundingBox()); } break; case M_OBJECT3D_TEXT: { MOText * text = (MOText *)object; render->pushMatrix(); render->multMatrix(text->getMatrix()); drawText(text); render->popMatrix(); } break; } } render->disableLighting(); render->disableFog(); }
void DebugRenderSystem::Process() { uint32 size = entities.size(); for(uint32 i = 0; i < size; ++i) { Entity * entity = entities[i]; DebugRenderComponent * debugRenderComponent = cast_if_equal<DebugRenderComponent*>(entity->GetComponent(Component::DEBUG_RENDER_COMPONENT)); TransformComponent * transformComponent = cast_if_equal<TransformComponent*>(entity->GetComponent(Component::TRANSFORM_COMPONENT)); //RenderComponent * renderComponent = cast_if_equal<RenderComponent*>(entity->GetComponent(Component::RENDER_COMPONENT)); Matrix4 worldTransform = /*(*transformComponent->GetWorldTransform()) * */camera->GetMatrix(); RenderManager::Instance()->SetMatrix(RenderManager::MATRIX_MODELVIEW, camera->GetMatrix()); AABBox3 debugBoundigBox = entity->GetWTMaximumBoundingBoxSlow(); uint32 debugFlags = debugRenderComponent->GetDebugFlags(); // Camera debug draw if(debugFlags & DebugRenderComponent::DEBUG_DRAW_CAMERA) { CameraComponent * entityCameraComp = (CameraComponent *) entity->GetComponent(Component::CAMERA_COMPONENT); if(NULL != entityCameraComp) { Camera* entityCamera = entityCameraComp->GetCamera(); if(NULL != entityCamera && camera != entityCamera) { Color camColor(0.0f, 1.0f, 0.0f, 1.0f); Vector3 camPos = entityCamera->GetPosition(); //Vector3 camDirect = entityCamera->GetDirection(); AABBox3 camBox(camPos, 2.5f); // If this is clip camera - show it as red camera if (entityCamera == entity->GetScene()->GetClipCamera()) camColor = Color(1.0f, 0.0f, 0.0f, 1.0f); RenderManager::Instance()->SetRenderEffect(RenderManager::FLAT_COLOR); RenderManager::Instance()->SetState(RenderState::STATE_COLORMASK_ALL | RenderState::STATE_DEPTH_WRITE); RenderManager::Instance()->SetColor(camColor); RenderHelper::Instance()->DrawBox(camBox, 2.5f); RenderManager::Instance()->SetState(RenderState::DEFAULT_3D_STATE); RenderManager::Instance()->ResetColor(); debugBoundigBox = camBox; } } } // UserNode debug draw if(debugFlags & DebugRenderComponent::DEBUG_DRAW_USERNODE) { if(NULL != entity->GetComponent(Component::USER_COMPONENT)) { Color dcColor(0.0f, 0.0f, 1.0f, 1.0f); AABBox3 dcBox(Vector3(), 1.0f); Matrix4 prevMatrix = RenderManager::Instance()->GetMatrix(RenderManager::MATRIX_MODELVIEW); Matrix4 finalMatrix = transformComponent->GetWorldTransform() * prevMatrix; RenderManager::Instance()->SetMatrix(RenderManager::MATRIX_MODELVIEW, finalMatrix); RenderManager::Instance()->SetRenderEffect(RenderManager::FLAT_COLOR); RenderManager::Instance()->SetState(RenderState::STATE_COLORMASK_ALL | RenderState::STATE_DEPTH_WRITE | RenderState::STATE_DEPTH_TEST); RenderManager::Instance()->SetColor(1.f, 1.f, 0, 1.0f); RenderHelper::Instance()->DrawLine(Vector3(0, 0, 0), Vector3(1.f, 0, 0)); RenderManager::Instance()->SetColor(1.f, 0, 1.f, 1.0f); RenderHelper::Instance()->DrawLine(Vector3(0, 0, 0), Vector3(0, 1.f, 0)); RenderManager::Instance()->SetColor(0, 1.f, 1.f, 1.0f); RenderHelper::Instance()->DrawLine(Vector3(0, 0, 0), Vector3(0, 0, 1.f)); RenderManager::Instance()->SetColor(dcColor); RenderHelper::Instance()->DrawBox(dcBox); RenderManager::Instance()->SetState(RenderState::DEFAULT_3D_STATE); RenderManager::Instance()->ResetColor(); RenderManager::Instance()->SetMatrix(RenderManager::MATRIX_MODELVIEW, prevMatrix); dcBox.GetTransformedBox(transformComponent->GetWorldTransform(), debugBoundigBox); } } // LightNode debug draw if (debugFlags & DebugRenderComponent::DEBUG_DRAW_LIGHT_NODE) { LightComponent *lightComp = (LightComponent *) entity->GetComponent(Component::LIGHT_COMPONENT); if(NULL != lightComp) { Light* light = lightComp->GetLightObject(); if(NULL != light) { Vector3 lPosition = light->GetPosition(); RenderManager::Instance()->SetRenderEffect(RenderManager::FLAT_COLOR); RenderManager::Instance()->SetState(RenderState::STATE_COLORMASK_ALL | RenderState::STATE_DEPTH_WRITE); RenderManager::Instance()->SetColor(1.0f, 1.0f, 0.0f, 1.0f); switch (light->GetType()) { case Light::TYPE_DIRECTIONAL: { Vector3 lDirection = light->GetDirection(); RenderHelper::Instance()->DrawArrow(lPosition, lPosition + lDirection * 10, 2.5f); RenderHelper::Instance()->DrawBox(AABBox3(lPosition, 0.5f), 1.5f); debugBoundigBox = AABBox3(lPosition, 2.5f); } break; default: { AABBox3 lightBox(lPosition, 2.5f); RenderHelper::Instance()->DrawBox(lightBox, 2.5f); debugBoundigBox = lightBox; } break; } RenderManager::Instance()->SetState(RenderState::DEFAULT_3D_STATE); RenderManager::Instance()->ResetColor(); } } } if ((debugFlags & DebugRenderComponent::DEBUG_DRAW_AABOX_CORNERS)) { RenderManager::Instance()->SetRenderEffect(RenderManager::FLAT_COLOR); RenderManager::Instance()->SetState(RenderState::STATE_COLORMASK_ALL | RenderState::STATE_DEPTH_WRITE | RenderState::STATE_DEPTH_TEST); RenderManager::Instance()->SetColor(1.0f, 1.0f, 1.0f, 1.0f); RenderHelper::Instance()->DrawCornerBox(debugBoundigBox, 1.5f); RenderManager::Instance()->SetState(RenderState::DEFAULT_3D_STATE); RenderManager::Instance()->SetColor(1.0f, 1.0f, 1.0f, 1.0f); // RenderManager::Instance()->SetMatrix(RenderManager::MATRIX_MODELVIEW, prevMatrix); } if (debugFlags & DebugRenderComponent::DEBUG_DRAW_RED_AABBOX) { RenderManager::Instance()->SetRenderEffect(RenderManager::FLAT_COLOR); RenderManager::Instance()->SetState(RenderState::STATE_COLORMASK_ALL | RenderState::STATE_DEPTH_WRITE); RenderManager::Instance()->SetColor(1.0f, 0.0f, 0.0f, 1.0f); RenderHelper::Instance()->DrawBox(debugBoundigBox, 1.5f); RenderManager::Instance()->SetState(RenderState::DEFAULT_3D_STATE); RenderManager::Instance()->SetColor(1.0f, 1.0f, 1.0f, 1.0f); } // UserNode Draw #if 0 if (debugFlags & DEBUG_DRAW_USERNODE) { Matrix4 prevMatrix = RenderManager::Instance()->GetMatrix(RenderManager::MATRIX_MODELVIEW); Matrix4 finalMatrix = worldTransform * prevMatrix; RenderManager::Instance()->SetMatrix(RenderManager::MATRIX_MODELVIEW, finalMatrix); RenderManager::Instance()->SetRenderEffect(RenderManager::FLAT_COLOR); RenderManager::Instance()->SetState(RenderStateBlock::STATE_COLORMASK_ALL | RenderStateBlock::STATE_DEPTH_WRITE | RenderStateBlock::STATE_DEPTH_TEST); RenderManager::Instance()->SetColor(0, 0, 1.0f, 1.0f); RenderHelper::Instance()->DrawBox(drawBox); RenderManager::Instance()->SetColor(1.f, 1.f, 0, 1.0f); RenderHelper::Instance()->DrawLine(Vector3(0, 0, 0), Vector3(1.f, 0, 0)); RenderManager::Instance()->SetColor(1.f, 0, 1.f, 1.0f); RenderHelper::Instance()->DrawLine(Vector3(0, 0, 0), Vector3(0, 1.f, 0)); RenderManager::Instance()->SetColor(0, 1.f, 1.f, 1.0f); RenderHelper::Instance()->DrawLine(Vector3(0, 0, 0), Vector3(0, 0, 1.f)); RenderManager::Instance()->SetState(RenderStateBlock::DEFAULT_3D_STATE); RenderManager::Instance()->SetColor(1.0f, 1.0f, 1.0f, 1.0f); RenderManager::Instance()->SetMatrix(RenderManager::MATRIX_MODELVIEW, prevMatrix); } #endif #if 0 // ParticleEffectNode if (debugFlags != DEBUG_DRAW_NONE) { if (!(flags & SceneNode::NODE_VISIBLE))return; RenderManager::Instance()->SetRenderEffect(RenderManager::FLAT_COLOR); RenderManager::Instance()->SetState(RenderStateBlock::STATE_COLORMASK_ALL | RenderStateBlock::STATE_DEPTH_WRITE); Vector3 position = Vector3(0.0f, 0.0f, 0.0f) * GetWorldTransform(); Matrix3 rotationPart(GetWorldTransform()); Vector3 direction = Vector3(0.0f, 0.0f, 1.0f) * rotationPart; direction.Normalize(); RenderManager::Instance()->SetColor(0.0f, 0.0f, 1.0f, 1.0f); RenderHelper::Instance()->DrawLine(position, position + direction * 10, 2.f); RenderManager::Instance()->SetState(RenderStateBlock::DEFAULT_3D_STATE); RenderManager::Instance()->SetColor(1.0f, 1.0f, 1.0f, 1.0f); } #endif } }