History

Progress towards shadow maps.

Andrzej Kapolka committed 12 years ago

Commit 5cc5d9b1 parent 893eadc9 files · +188 −48Browse files

Showing the changes to interface/src/Application.cpp (1 of 9 files). Show all

Unified · Split

interface/src/Application.cpp+139−42
@@ -424,12 +424,15 @@ void Application::paintGL() { · 423 unmodified lines
424424 whichCamera = _viewFrustumOffsetCamera;
425425 }
426426
427+ if (Menu::getInstance()->isOptionChecked(MenuOption::Shadows)) {
428+ updateShadowMap();
429+ }
430+
427431 if (OculusManager::isConnected()) {
428432 displayOculus(whichCamera);
429433
430434 } else {
431435 _glowEffect.prepare();
432−
433436
434437 glMatrixMode(GL_MODELVIEW);
435438 glPushMatrix();
@@ -2754,6 +2757,76 @@ void Application::loadViewFrustum(Camera& camera, ViewFrustum& viewFrustum) { · 2318 unmodified lines
27542757 viewFrustum.calculate();
27552758 }
27562759
2760+glm::vec3 Application::getSunDirection() {
2761+ return glm::normalize(_environment.getClosestData(_myCamera.getPosition()).getSunLocation() - _myCamera.getPosition());
2762+}
2763+
2764+void Application::updateShadowMap() {
2765+ QOpenGLFramebufferObject* fbo = _textureCache.getShadowFramebufferObject();
2766+ fbo->bind();
2767+ glEnable(GL_DEPTH_TEST);
2768+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2769+
2770+ glViewport(0, 0, fbo->width(), fbo->height());
2771+
2772+ glm::vec3 lightDirection = -getSunDirection();
2773+ glm::quat rotation = glm::inverse(rotationBetween(IDENTITY_FRONT, lightDirection));
2774+ glm::vec3 translation = glm::vec3();
2775+ float nearScale = 0.0f;
2776+ float farScale = 0.000001f;
2777+ /* glm::vec3 points[] = {
2778+ rotation * (glm::mix(_viewFrustum.getNearTopLeft(), _viewFrustum.getFarTopLeft(), nearScale) + translation),
2779+ rotation * (glm::mix(_viewFrustum.getNearTopRight(), _viewFrustum.getFarTopRight(), nearScale) + translation),
2780+ rotation * (glm::mix(_viewFrustum.getNearBottomLeft(), _viewFrustum.getFarBottomLeft(), nearScale) + translation),
2781+ rotation * (glm::mix(_viewFrustum.getNearBottomRight(), _viewFrustum.getFarBottomRight(), nearScale) + translation),
2782+ rotation * (glm::mix(_viewFrustum.getNearTopLeft(), _viewFrustum.getFarTopLeft(), farScale) + translation),
2783+ rotation * (glm::mix(_viewFrustum.getNearTopRight(), _viewFrustum.getFarTopRight(), farScale) + translation),
2784+ rotation * (glm::mix(_viewFrustum.getNearBottomLeft(), _viewFrustum.getFarBottomLeft(), farScale) + translation),
2785+ rotation * (glm::mix(_viewFrustum.getNearBottomRight(), _viewFrustum.getFarBottomRight(), farScale) + translation) }; */
2786+ glm::vec3 points[] = {
2787+ rotation * (_myAvatar.getPosition() + glm::vec3(-1, 0, 0) + translation),
2788+ rotation * (_myAvatar.getPosition() + glm::vec3(1, 0, 0) + translation),
2789+ rotation * (_myAvatar.getPosition() + glm::vec3(0, -1, 0) + translation),
2790+ rotation * (_myAvatar.getPosition() + glm::vec3(0, 1, 0) + translation),
2791+ rotation * (_myAvatar.getPosition() + glm::vec3(0, 0, -1) + translation),
2792+ rotation * (_myAvatar.getPosition() + glm::vec3(0, 0, 1) + translation) };
2793+ glm::vec3 minima(FLT_MAX, FLT_MAX, FLT_MAX), maxima(-FLT_MAX, -FLT_MAX, -FLT_MAX);
2794+ for (int i = 0; i < sizeof(points) / sizeof(points[0]); i++) {
2795+ minima = glm::min(minima, points[i]);
2796+ maxima = glm::max(maxima, points[i]);
2797+ }
2798+
2799+ glMatrixMode(GL_PROJECTION);
2800+ glPushMatrix();
2801+ glLoadIdentity();
2802+ glOrtho(minima.x, maxima.x, minima.y, maxima.y, -maxima.z, -minima.z);
2803+
2804+ glMatrixMode(GL_MODELVIEW);
2805+ glPushMatrix();
2806+ glLoadIdentity();
2807+ glm::vec3 axis = glm::axis(rotation);
2808+ glRotatef(glm::angle(rotation), axis.x, axis.y, axis.z);
2809+
2810+ // store view matrix without translation, which we'll use for precision-sensitive objects
2811+ glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat*)&_untranslatedViewMatrix);
2812+ _viewMatrixTranslation = translation;
2813+
2814+ glTranslatef(translation.x, translation.y, translation.z);
2815+
2816+ renderAvatars(_myCamera);
2817+
2818+ glPopMatrix();
2819+
2820+ glMatrixMode(GL_PROJECTION);
2821+ glPopMatrix();
2822+
2823+ glMatrixMode(GL_MODELVIEW);
2824+
2825+ fbo->release();
2826+
2827+ glViewport(0, 0, _glWidget->width(), _glWidget->height());
2828+}
2829+
27572830 // this shader is an adaptation (HLSL -> GLSL, removed conditional) of the one in the Oculus sample
27582831 // code (Samples/OculusRoomTiny/RenderTiny_D3D1X_Device.cpp), which is under the Apache license
27592832 // (http://www.apache.org/licenses/LICENSE-2.0)
@@ -2896,15 +2969,14 @@ void Application::displayOculus(Camera& whichCamera) { · 136 unmodified lines
28962969 const GLfloat WHITE_SPECULAR_COLOR[] = { 1.0f, 1.0f, 1.0f, 1.0f };
28972970 const GLfloat NO_SPECULAR_COLOR[] = { 0.0f, 0.0f, 0.0f, 1.0f };
28982971
2899−void Application::setupWorldLight(Camera& whichCamera) {
2972+void Application::setupWorldLight() {
29002973
29012974 // Setup 3D lights (after the camera transform, so that they are positioned in world space)
29022975 glEnable(GL_COLOR_MATERIAL);
29032976 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
29042977
2905− glm::vec3 relativeSunLoc = glm::normalize(_environment.getClosestData(whichCamera.getPosition()).getSunLocation() -
2906− whichCamera.getPosition());
2907− GLfloat light_position0[] = { relativeSunLoc.x, relativeSunLoc.y, relativeSunLoc.z, 0.0 };
2978+ glm::vec3 sunDirection = getSunDirection();
2979+ GLfloat light_position0[] = { sunDirection.x, sunDirection.y, sunDirection.z, 0.0 };
29082980 glLightfv(GL_LIGHT0, GL_POSITION, light_position0);
29092981 GLfloat ambient_color[] = { 0.7, 0.7, 0.8 };
29102982 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_color);
@@ -2962,7 +3034,7 @@ void Application::displaySide(Camera& whichCamera, bool selfAvatarOnly) { · 51 unmodified lines
29623034 glTranslatef(_viewMatrixTranslation.x, _viewMatrixTranslation.y, _viewMatrixTranslation.z);
29633035
29643036 // Setup 3D lights (after the camera transform, so that they are positioned in world space)
2965− setupWorldLight(whichCamera);
3037+ setupWorldLight();
29663038
29673039 if (!selfAvatarOnly && Menu::getInstance()->isOptionChecked(MenuOption::Stars)) {
29683040 PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings),
@@ -3099,44 +3171,11 @@ void Application::displaySide(Camera& whichCamera, bool selfAvatarOnly) { · 130 unmodified lines
30993171 }
31003172 }
31013173
3102− _myAvatar.renderScreenTint(SCREEN_TINT_BEFORE_AVATARS, whichCamera);
3174+ _myAvatar.renderScreenTint(SCREEN_TINT_BEFORE_AVATARS);
31033175
3104− if (Menu::getInstance()->isOptionChecked(MenuOption::Avatars)) {
3105− PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings),
3106− "Application::displaySide() ... Avatars...");
3107−
3108−
3109− if (!selfAvatarOnly) {
3110− // Render avatars of other nodes
3111− NodeList* nodeList = NodeList::getInstance();
3112−
3113− for (NodeList::iterator node = nodeList->begin(); node != nodeList->end(); node++) {
3114− node->lock();
3115−
3116− if (node->getLinkedData() != NULL && node->getType() == NODE_TYPE_AGENT) {
3117− Avatar *avatar = (Avatar *)node->getLinkedData();
3118− if (!avatar->isInitialized()) {
3119− avatar->init();
3120− }
3121− avatar->render(false, Menu::getInstance()->isOptionChecked(MenuOption::AvatarAsBalls));
3122− avatar->setDisplayingLookatVectors(Menu::getInstance()->isOptionChecked(MenuOption::LookAtVectors));
3123− }
3124−
3125− node->unlock();
3126− }
3127− }
3128−
3129− // Render my own Avatar
3130− _myAvatar.render(whichCamera.getMode() == CAMERA_MODE_MIRROR,
3131− Menu::getInstance()->isOptionChecked(MenuOption::AvatarAsBalls));
3132− _myAvatar.setDisplayingLookatVectors(Menu::getInstance()->isOptionChecked(MenuOption::LookAtVectors));
3176+ renderAvatars(whichCamera, selfAvatarOnly);
31333177
3134− if (Menu::getInstance()->isOptionChecked(MenuOption::LookAtIndicator) && _lookatTargetAvatar) {
3135− renderLookatIndicator(_lookatOtherPosition, whichCamera);
3136− }
3137− }
3138−
3139− _myAvatar.renderScreenTint(SCREEN_TINT_AFTER_AVATARS, whichCamera);
3178+ _myAvatar.renderScreenTint(SCREEN_TINT_AFTER_AVATARS);
31403179
31413180 if (!selfAvatarOnly) {
31423181 // Render the world box
@@ -3336,6 +3375,29 @@ void Application::displayOverlay() { · 193 unmodified lines
33363375 // render the webcam input frame
33373376 _webcam.renderPreview(_glWidget->width(), _glWidget->height());
33383377
3378+ if (Menu::getInstance()->isOptionChecked(MenuOption::Shadows)) {
3379+ glBindTexture(GL_TEXTURE_2D, _textureCache.getShadowDepthTextureID());
3380+ glEnable(GL_TEXTURE_2D);
3381+ glColor3f(1.0f, 1.0f, 1.0f);
3382+
3383+ glBegin(GL_QUADS);
3384+ glTexCoord2f(0.0f, 0.0f);
3385+ glVertex2f(500, 500);
3386+
3387+ glTexCoord2f(1.0f, 0.0f);
3388+ glVertex2f(1000, 500);
3389+
3390+ glTexCoord2f(1.0f, 1.0f);
3391+ glVertex2f(1000, 0);
3392+
3393+ glTexCoord2f(0.0f, 1.0f);
3394+ glVertex2f(500, 0);
3395+ glEnd();
3396+
3397+ glDisable(GL_TEXTURE_2D);
3398+ glBindTexture(GL_TEXTURE_2D, 0);
3399+ }
3400+
33393401 _palette.render(_glWidget->width(), _glWidget->height());
33403402
33413403 QAction* paintColorAction = NULL;
@@ -3758,7 +3820,42 @@ void Application::renderCoverageMapsRecursively(CoverageMap* map) { · 416 unmodified lines
37583820 }
37593821 }
37603822
3823+void Application::renderAvatars(Camera& whichCamera, bool selfAvatarOnly) {
3824+ if (!Menu::getInstance()->isOptionChecked(MenuOption::Avatars)) {
3825+ return;
3826+ }
3827+ PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings),
3828+ "Application::displaySide() ... Avatars...");
37613829
3830+ if (!selfAvatarOnly) {
3831+ // Render avatars of other nodes
3832+ NodeList* nodeList = NodeList::getInstance();
3833+
3834+ for (NodeList::iterator node = nodeList->begin(); node != nodeList->end(); node++) {
3835+ node->lock();
3836+
3837+ if (node->getLinkedData() != NULL && node->getType() == NODE_TYPE_AGENT) {
3838+ Avatar *avatar = (Avatar *)node->getLinkedData();
3839+ if (!avatar->isInitialized()) {
3840+ avatar->init();
3841+ }
3842+ avatar->render(false, Menu::getInstance()->isOptionChecked(MenuOption::AvatarAsBalls));
3843+ avatar->setDisplayingLookatVectors(Menu::getInstance()->isOptionChecked(MenuOption::LookAtVectors));
3844+ }
3845+
3846+ node->unlock();
3847+ }
3848+ }
3849+
3850+ // Render my own Avatar
3851+ _myAvatar.render(whichCamera.getMode() == CAMERA_MODE_MIRROR,
3852+ Menu::getInstance()->isOptionChecked(MenuOption::AvatarAsBalls));
3853+ _myAvatar.setDisplayingLookatVectors(Menu::getInstance()->isOptionChecked(MenuOption::LookAtVectors));
3854+
3855+ if (Menu::getInstance()->isOptionChecked(MenuOption::LookAtIndicator) && _lookatTargetAvatar) {
3856+ renderLookatIndicator(_lookatOtherPosition, whichCamera);
3857+ }
3858+}
37623859
37633860 // renderViewFrustum()
37643861 //

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t