| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- /*
- * PolyCamera.cpp
- * Poly
- *
- * Created by Ivan Safrin on 3/26/08.
- * Copyright 2008 Ivan Safrin. All rights reserved.
- *
- */
- #include "PolyCamera.h"
- using namespace Polycode;
-
- Camera::Camera(Scene *parentScene) : SceneEntity() {
- setParentScene(parentScene);
- orthoMode = false;
- fov = 45.0f;
- exposureLevel = 1.0f;
- _hasFilterShader = false;
- fovSet = false;
- }
- Camera::~Camera() {
- }
- void Camera::setExposureLevel(Number level) {
- exposureLevel = level;
- }
- Number Camera::getExposureLevel() {
- return exposureLevel;
- }
- void Camera::setFOV(Number fov) {
- this->fov = fov;
- fovSet = true;
- }
- Number Camera::getFOV() {
- return fov;
- }
- bool Camera::isSphereInFrustrum(Vector3 pos, Number fRadius) {
- for( int i = 0; i < 6; ++i )
- {
- if( frustumPlanes[i][0] * pos.x +
- frustumPlanes[i][1] * pos.y +
- frustumPlanes[i][2] * pos.z +
- frustumPlanes[i][3] <= -fRadius )
- return false;
- }
- return true;
- }
- void Camera::setOrthoMode(bool mode) {
- orthoMode = mode;
- }
- bool Camera::getOrthoMode() {
- return orthoMode;
- }
- void Camera::buildFrustrumPlanes() {
- Matrix4 p;
- Matrix4 mv;
- Matrix4 mvp;
- Number t;
- p = CoreServices::getInstance()->getRenderer()->getProjectionMatrix();
- mv = CoreServices::getInstance()->getRenderer()->getModelviewMatrix();
- //
- // Concatenate the projection matrix and the model-view matrix to produce
- // a combined model-view-projection matrix.
- //
- mvp.ml[ 0] = mv.ml[ 0] * p.ml[ 0] + mv.ml[ 1] * p.ml[ 4] + mv.ml[ 2] * p.ml[ 8] + mv.ml[ 3] * p.ml[12];
- mvp.ml[ 1] = mv.ml[ 0] * p.ml[ 1] + mv.ml[ 1] * p.ml[ 5] + mv.ml[ 2] * p.ml[ 9] + mv.ml[ 3] * p.ml[13];
- mvp.ml[ 2] = mv.ml[ 0] * p.ml[ 2] + mv.ml[ 1] * p.ml[ 6] + mv.ml[ 2] * p.ml[10] + mv.ml[ 3] * p.ml[14];
- mvp.ml[ 3] = mv.ml[ 0] * p.ml[ 3] + mv.ml[ 1] * p.ml[ 7] + mv.ml[ 2] * p.ml[11] + mv.ml[ 3] * p.ml[15];
- mvp.ml[ 4] = mv.ml[ 4] * p.ml[ 0] + mv.ml[ 5] * p.ml[ 4] + mv.ml[ 6] * p.ml[ 8] + mv.ml[ 7] * p.ml[12];
- mvp.ml[ 5] = mv.ml[ 4] * p.ml[ 1] + mv.ml[ 5] * p.ml[ 5] + mv.ml[ 6] * p.ml[ 9] + mv.ml[ 7] * p.ml[13];
- mvp.ml[ 6] = mv.ml[ 4] * p.ml[ 2] + mv.ml[ 5] * p.ml[ 6] + mv.ml[ 6] * p.ml[10] + mv.ml[ 7] * p.ml[14];
- mvp.ml[ 7] = mv.ml[ 4] * p.ml[ 3] + mv.ml[ 5] * p.ml[ 7] + mv.ml[ 6] * p.ml[11] + mv.ml[ 7] * p.ml[15];
- mvp.ml[ 8] = mv.ml[ 8] * p.ml[ 0] + mv.ml[ 9] * p.ml[ 4] + mv.ml[10] * p.ml[ 8] + mv.ml[11] * p.ml[12];
- mvp.ml[ 9] = mv.ml[ 8] * p.ml[ 1] + mv.ml[ 9] * p.ml[ 5] + mv.ml[10] * p.ml[ 9] + mv.ml[11] * p.ml[13];
- mvp.ml[10] = mv.ml[ 8] * p.ml[ 2] + mv.ml[ 9] * p.ml[ 6] + mv.ml[10] * p.ml[10] + mv.ml[11] * p.ml[14];
- mvp.ml[11] = mv.ml[ 8] * p.ml[ 3] + mv.ml[ 9] * p.ml[ 7] + mv.ml[10] * p.ml[11] + mv.ml[11] * p.ml[15];
- mvp.ml[12] = mv.ml[12] * p.ml[ 0] + mv.ml[13] * p.ml[ 4] + mv.ml[14] * p.ml[ 8] + mv.ml[15] * p.ml[12];
- mvp.ml[13] = mv.ml[12] * p.ml[ 1] + mv.ml[13] * p.ml[ 5] + mv.ml[14] * p.ml[ 9] + mv.ml[15] * p.ml[13];
- mvp.ml[14] = mv.ml[12] * p.ml[ 2] + mv.ml[13] * p.ml[ 6] + mv.ml[14] * p.ml[10] + mv.ml[15] * p.ml[14];
- mvp.ml[15] = mv.ml[12] * p.ml[ 3] + mv.ml[13] * p.ml[ 7] + mv.ml[14] * p.ml[11] + mv.ml[15] * p.ml[15];
- //
- // Extract the frustum's right clipping plane and normalize it.
- //
- frustumPlanes[0][0] = mvp.ml[ 3] - mvp.ml[ 0];
- frustumPlanes[0][1] = mvp.ml[ 7] - mvp.ml[ 4];
- frustumPlanes[0][2] = mvp.ml[11] - mvp.ml[ 8];
- frustumPlanes[0][3] = mvp.ml[15] - mvp.ml[12];
- t = (Number) sqrt( frustumPlanes[0][0] * frustumPlanes[0][0] +
- frustumPlanes[0][1] * frustumPlanes[0][1] +
- frustumPlanes[0][2] * frustumPlanes[0][2] );
- frustumPlanes[0][0] /= t;
- frustumPlanes[0][1] /= t;
- frustumPlanes[0][2] /= t;
- frustumPlanes[0][3] /= t;
- //
- // Extract the frustum's left clipping plane and normalize it.
- //
- frustumPlanes[1][0] = mvp.ml[ 3] + mvp.ml[ 0];
- frustumPlanes[1][1] = mvp.ml[ 7] + mvp.ml[ 4];
- frustumPlanes[1][2] = mvp.ml[11] + mvp.ml[ 8];
- frustumPlanes[1][3] = mvp.ml[15] + mvp.ml[12];
- t = (Number) sqrt( frustumPlanes[1][0] * frustumPlanes[1][0] +
- frustumPlanes[1][1] * frustumPlanes[1][1] +
- frustumPlanes[1][2] * frustumPlanes[1][2] );
- frustumPlanes[1][0] /= t;
- frustumPlanes[1][1] /= t;
- frustumPlanes[1][2] /= t;
- frustumPlanes[1][3] /= t;
- //
- // Extract the frustum's bottom clipping plane and normalize it.
- //
- frustumPlanes[2][0] = mvp.ml[ 3] + mvp.ml[ 1];
- frustumPlanes[2][1] = mvp.ml[ 7] + mvp.ml[ 5];
- frustumPlanes[2][2] = mvp.ml[11] + mvp.ml[ 9];
- frustumPlanes[2][3] = mvp.ml[15] + mvp.ml[13];
- t = (Number) sqrt( frustumPlanes[2][0] * frustumPlanes[2][0] +
- frustumPlanes[2][1] * frustumPlanes[2][1] +
- frustumPlanes[2][2] * frustumPlanes[2][2] );
- frustumPlanes[2][0] /= t;
- frustumPlanes[2][1] /= t;
- frustumPlanes[2][2] /= t;
- frustumPlanes[2][3] /= t;
- //
- // Extract the frustum's top clipping plane and normalize it.
- //
- frustumPlanes[3][0] = mvp.ml[ 3] - mvp.ml[ 1];
- frustumPlanes[3][1] = mvp.ml[ 7] - mvp.ml[ 5];
- frustumPlanes[3][2] = mvp.ml[11] - mvp.ml[ 9];
- frustumPlanes[3][3] = mvp.ml[15] - mvp.ml[13];
- t = (Number) sqrt( frustumPlanes[3][0] * frustumPlanes[3][0] +
- frustumPlanes[3][1] * frustumPlanes[3][1] +
- frustumPlanes[3][2] * frustumPlanes[3][2] );
- frustumPlanes[3][0] /= t;
- frustumPlanes[3][1] /= t;
- frustumPlanes[3][2] /= t;
- frustumPlanes[3][3] /= t;
- //
- // Extract the frustum's far clipping plane and normalize it.
- //
- frustumPlanes[4][0] = mvp.ml[ 3] - mvp.ml[ 2];
- frustumPlanes[4][1] = mvp.ml[ 7] - mvp.ml[ 6];
- frustumPlanes[4][2] = mvp.ml[11] - mvp.ml[10];
- frustumPlanes[4][3] = mvp.ml[15] - mvp.ml[14];
- t = (Number) sqrt( frustumPlanes[4][0] * frustumPlanes[4][0] +
- frustumPlanes[4][1] * frustumPlanes[4][1] +
- frustumPlanes[4][2] * frustumPlanes[4][2] );
- frustumPlanes[4][0] /= t;
- frustumPlanes[4][1] /= t;
- frustumPlanes[4][2] /= t;
- frustumPlanes[4][3] /= t;
- //
- // Extract the frustum's near clipping plane and normalize it.
- //
- frustumPlanes[5][0] = mvp.ml[ 3] + mvp.ml[ 2];
- frustumPlanes[5][1] = mvp.ml[ 7] + mvp.ml[ 6];
- frustumPlanes[5][2] = mvp.ml[11] + mvp.ml[10];
- frustumPlanes[5][3] = mvp.ml[15] + mvp.ml[14];
- t = (Number) sqrt( frustumPlanes[5][0] * frustumPlanes[5][0] +
- frustumPlanes[5][1] * frustumPlanes[5][1] +
- frustumPlanes[5][2] * frustumPlanes[5][2] );
- frustumPlanes[5][0] /= t;
- frustumPlanes[5][1] /= t;
- frustumPlanes[5][2] /= t;
- frustumPlanes[5][3] /= t;
- }
- bool Camera::canSee(SceneEntity *entity) {
- return isSphereInFrustrum(*entity->getPosition(), entity->getBBoxRadius());
- }
- void Camera::setParentScene(Scene *parentScene) {
- this->parentScene = parentScene;
- }
- void Camera::setPostFilter(String shaderName) {
- Material *shaderMaterial = (Material*) CoreServices::getInstance()->getResourceManager()->getResource(Resource::RESOURCE_MATERIAL, shaderName);
- if(shaderMaterial)
- createPostFilter(shaderMaterial);
- }
- void Camera::createPostFilter(Material *shaderMaterial) {
- if(!shaderMaterial)
- return;
- if(shaderMaterial->getNumShaders() == 0)
- return;
-
- this->filterShaderMaterial = shaderMaterial;
-
- // TODO: make it save the textures to resource manager and check if they there
- // originalSceneTexture = CoreServices::getInstance()->getMaterialManager()->createNewTexture(CoreServices::getInstance()->getCore()->getXRes(), CoreServices::getInstance()->getCore()->getYRes());
- // zBufferSceneTexture = CoreServices::getInstance()->getMaterialManager()->createFramebufferTexture(CoreServices::getInstance()->getCore()->getXRes(), CoreServices::getInstance()->getCore()->getYRes(), 0);
- CoreServices::getInstance()->getRenderer()->createRenderTextures(&originalSceneTexture, &zBufferSceneTexture, CoreServices::getInstance()->getCore()->getXRes(), CoreServices::getInstance()->getCore()->getYRes());
-
- for(int i=0; i < shaderMaterial->getNumShaders(); i++) {
- ShaderBinding* binding = shaderMaterial->getShader(i)->createBinding();
- binding->addTexture("PolySceneRender", originalSceneTexture);
- binding->addTexture("PolySceneZBuffer", zBufferSceneTexture);
- localShaderOptions.push_back(binding);
- }
-
- _hasFilterShader = true;
- }
- bool Camera::hasFilterShader() {
- return _hasFilterShader;
- }
- void Camera::setLightDepthTexture(Texture *texture) {
- for(int i=0; i < localShaderOptions.size(); i++) {
- localShaderOptions[i]->clearTexture("PolyLight0ZBuffer");
- localShaderOptions[i]->addTexture("PolyLight0ZBuffer", texture);
- }
- }
- void Camera::drawFilter() {
- if(!filterShaderMaterial)
- return;
- CoreServices::getInstance()->getRenderer()->bindFrameBufferTexture(zBufferSceneTexture);
- parentScene->Render();
- CoreServices::getInstance()->getRenderer()->unbindFramebuffers();
- ShaderBinding* materialBinding;
- for(int i=0; i < filterShaderMaterial->getNumShaders(); i++) {
- materialBinding = filterShaderMaterial->getShaderBinding(i);
- CoreServices::getInstance()->getRenderer()->applyMaterial(filterShaderMaterial, localShaderOptions[i], i);
- if(i==filterShaderMaterial->getNumShaders()-1) {
- CoreServices::getInstance()->getRenderer()->clearScreen();
- CoreServices::getInstance()->getRenderer()->loadIdentity();
- CoreServices::getInstance()->getRenderer()->drawScreenQuad(CoreServices::getInstance()->getRenderer()->getXRes(), CoreServices::getInstance()->getRenderer()->getYRes());
- } else {
- for(int j=0; j < materialBinding->getNumOutTargetBindings(); j++) {
- CoreServices::getInstance()->getRenderer()->bindFrameBufferTexture(materialBinding->getOutTargetBinding(j)->texture);
- CoreServices::getInstance()->getRenderer()->drawScreenQuad(materialBinding->getOutTargetBinding(j)->width, materialBinding->getOutTargetBinding(j)->height);
- CoreServices::getInstance()->getRenderer()->unbindFramebuffers();
- }
- }
- CoreServices::getInstance()->getRenderer()->clearShader();
- CoreServices::getInstance()->getRenderer()->loadIdentity();
- }
- }
- void Camera::doCameraTransform() {
- if(fovSet)
- CoreServices::getInstance()->getRenderer()->setFOV(fov);
- CoreServices::getInstance()->getRenderer()->setExposureLevel(exposureLevel);
- if(matrixDirty) {
- rebuildTransformMatrix();
- }
- Matrix4 camMatrix = getConcatenatedMatrix();
- CoreServices::getInstance()->getRenderer()->setCameraMatrix(camMatrix);
- camMatrix = camMatrix.inverse();
- CoreServices::getInstance()->getRenderer()->multModelviewMatrix(camMatrix);
- }
|