diff options
author | Tomi Korpipää <tomi.korpipaa@digia.com> | 2013-05-08 11:05:24 +0300 |
---|---|---|
committer | Tomi Korpipää <tomi.korpipaa@digia.com> | 2013-05-08 11:32:31 +0300 |
commit | 161232582e7f7e7e6e991def2fe87d78e668d08f (patch) | |
tree | be871f288561150fae7a4a7e3bb13bb04c98ac21 /src/datavis3d/utils | |
parent | 9149d433a19613056be914d1d96a0f3517be8589 (diff) |
Shadow mapping implementation
Partially works, but mostly doesn't.
Change-Id: I415833d07148d2aeae64c0c311e14766ab29ad5e
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@digia.com>
Diffstat (limited to 'src/datavis3d/utils')
-rw-r--r-- | src/datavis3d/utils/camerahelper.cpp | 32 | ||||
-rw-r--r-- | src/datavis3d/utils/camerahelper_p.h | 3 | ||||
-rw-r--r-- | src/datavis3d/utils/shaderhelper.cpp | 4 | ||||
-rw-r--r-- | src/datavis3d/utils/shaderhelper_p.h | 2 | ||||
-rw-r--r-- | src/datavis3d/utils/texturehelper.cpp | 18 |
5 files changed, 32 insertions, 27 deletions
diff --git a/src/datavis3d/utils/camerahelper.cpp b/src/datavis3d/utils/camerahelper.cpp index 7e9b7def..2e868cac 100644 --- a/src/datavis3d/utils/camerahelper.cpp +++ b/src/datavis3d/utils/camerahelper.cpp @@ -54,12 +54,12 @@ QVector3D m_up = QVector3D(0, 1, 0); QPoint m_previousMousePos(0, 0); -float m_xRotation = 0; -float m_yRotation = 0; -float m_defaultXRotation = 0; -float m_defaultYRotation = 0; +GLfloat m_xRotation = 0; +GLfloat m_yRotation = 0; +GLfloat m_defaultXRotation = 0; +GLfloat m_defaultYRotation = 0; -float m_rotationSpeed = 100; +GLfloat m_rotationSpeed = 100; // FUNCTIONS void CameraHelper::setRotationSpeed(int speed) @@ -91,9 +91,9 @@ QMatrix4x4 CameraHelper::calculateViewMatrix(const QPoint &mousePos, int zoom, QMatrix4x4 viewMatrix; // Calculate mouse movement since last frame - float mouseMoveX = float(m_previousMousePos.x() - mousePos.x()) + GLfloat mouseMoveX = GLfloat(m_previousMousePos.x() - mousePos.x()) / (screenWidth / m_rotationSpeed); - float mouseMoveY = float(m_previousMousePos.y() - mousePos.y()) + GLfloat mouseMoveY = GLfloat(m_previousMousePos.y() - mousePos.y()) / (screenHeight / m_rotationSpeed); // Apply to rotations m_xRotation -= mouseMoveX; @@ -117,7 +117,7 @@ QMatrix4x4 CameraHelper::calculateViewMatrix(const QPoint &mousePos, int zoom, // y rotation is always "clean" viewMatrix.rotate(m_yRotation, 1.0f, 0.0f, 0.0f); // handle zoom by scaling - viewMatrix.scale((float)zoom / 100.0f); + viewMatrix.scale((GLfloat)zoom / 100.0f); // Compensate for translation (if m_target is off origin) viewMatrix.translate(-m_target.x(), -m_target.y(), -m_target.z()); //qDebug() << m_xRotation << m_yRotation; @@ -131,12 +131,14 @@ QMatrix4x4 CameraHelper::calculateViewMatrix(const QPoint &mousePos, int zoom, return viewMatrix; } -QVector3D CameraHelper::calculateLightPosition(const QVector3D &lightPosition, float fixedRotation) +QVector3D CameraHelper::calculateLightPosition(const QVector3D &lightPosition, + GLfloat fixedRotation, GLfloat distanceModifier) { // Move light with camera QVector3D newLightPosition; - float xAngle; - float yAngle; + GLfloat radiusFactor = lightPosition.z() * (1.5f + distanceModifier); // for making sure light is outside the scene at its lowest point + GLfloat xAngle; + GLfloat yAngle; if (!fixedRotation) { xAngle = m_xRotation * m_pi / 180.0f; yAngle = m_yRotation * m_pi / 180.0f; @@ -144,10 +146,10 @@ QVector3D CameraHelper::calculateLightPosition(const QVector3D &lightPosition, f xAngle = fixedRotation * m_pi / 180.0f; yAngle = 0; } - float radius = lightPosition.y() * 2.0f; // set radius to match the highest height of the light - float zPos = radius * cos(xAngle) * cos(yAngle); - float xPos = radius * sin(xAngle) * cos(yAngle); - float yPos = lightPosition.y() * sin(yAngle); + GLfloat radius = (radiusFactor + lightPosition.y()); // set radius to match the highest height of the light + GLfloat zPos = radius * cos(xAngle) * cos(yAngle); + GLfloat xPos = radius * sin(xAngle) * cos(yAngle); + GLfloat yPos = (radiusFactor + lightPosition.y()) * sin(yAngle); // Keep light in the set position in relation to camera // TODO: Does not work perfectly yet; Light seems wrong when viewing scene from sides (or isometrically) newLightPosition = QVector3D(-xPos + lightPosition.x(), diff --git a/src/datavis3d/utils/camerahelper_p.h b/src/datavis3d/utils/camerahelper_p.h index e63061a2..68e7e5a2 100644 --- a/src/datavis3d/utils/camerahelper_p.h +++ b/src/datavis3d/utils/camerahelper_p.h @@ -82,7 +82,8 @@ class CameraHelper // Calcluate light position based on rotation. Call after calling calculateViewMatrix to get // up-to-date position static QVector3D calculateLightPosition(const QVector3D &lightPosition, - float fixedRotation = 0.0f); + GLfloat fixedRotation = 0.0f, + GLfloat distanceModifier = 0.0f); static void updateMousePos(const QPoint &mousePos); static void setCameraPreset(CameraPreset preset); }; diff --git a/src/datavis3d/utils/shaderhelper.cpp b/src/datavis3d/utils/shaderhelper.cpp index 86b3d964..f08daff0 100644 --- a/src/datavis3d/utils/shaderhelper.cpp +++ b/src/datavis3d/utils/shaderhelper.cpp @@ -92,7 +92,7 @@ void ShaderHelper::initialize() m_viewMatrixUniform = m_program->uniformLocation("V"); m_modelMatrixUniform = m_program->uniformLocation("M"); m_invTransModelMatrixUniform = m_program->uniformLocation("itM"); - m_depthBiasMatrixUniform = m_program->uniformLocation("depthBiasMVP"); + m_depthMatrixUniform = m_program->uniformLocation("depthMVP"); m_lightPositionUniform = m_program->uniformLocation("lightPosition_wrld"); m_lightStrengthUniform = m_program->uniformLocation("lightStrength"); m_ambientStrengthUniform = m_program->uniformLocation("ambientStrength"); @@ -169,7 +169,7 @@ GLuint ShaderHelper::depth() { if (!m_initialized) qFatal("Shader not initialized"); - return m_depthBiasMatrixUniform; + return m_depthMatrixUniform; } GLuint ShaderHelper::lightP() diff --git a/src/datavis3d/utils/shaderhelper_p.h b/src/datavis3d/utils/shaderhelper_p.h index 42c5e9af..d1c72ba5 100644 --- a/src/datavis3d/utils/shaderhelper_p.h +++ b/src/datavis3d/utils/shaderhelper_p.h @@ -114,7 +114,7 @@ class ShaderHelper GLuint m_viewMatrixUniform; GLuint m_modelMatrixUniform; GLuint m_invTransModelMatrixUniform; - GLuint m_depthBiasMatrixUniform; + GLuint m_depthMatrixUniform; GLuint m_mvpMatrixUniform; GLuint m_lightPositionUniform; GLuint m_lightStrengthUniform; diff --git a/src/datavis3d/utils/texturehelper.cpp b/src/datavis3d/utils/texturehelper.cpp index 4007953f..808ac309 100644 --- a/src/datavis3d/utils/texturehelper.cpp +++ b/src/datavis3d/utils/texturehelper.cpp @@ -195,23 +195,25 @@ GLuint TextureHelper::createDepthTexture(const QSize &size, GLuint &frameBuffer) { GLuint depthtextureid; - // Create frame buffer - glGenFramebuffers(1, &frameBuffer); - glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer); - // Create depth texture for the shadow mapping glGenTextures(1, &depthtextureid); glBindTexture(GL_TEXTURE_2D, depthtextureid); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE); - glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, size.width(), size.height(), 0, - GL_DEPTH_COMPONENT, GL_FLOAT, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, size.width(), size.height(), 0, + GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); glBindTexture(GL_TEXTURE_2D, 0); + // Create frame buffer + glGenFramebuffers(1, &frameBuffer); + glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer); + // Attach texture to depth attachment glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthtextureid, 0); |