summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomi Korpipää <tomi.korpipaa@digia.com>2013-05-07 11:03:21 +0300
committerTomi Korpipää <tomi.korpipaa@digia.com>2013-05-07 11:12:26 +0300
commit9149d433a19613056be914d1d96a0f3517be8589 (patch)
treed53056794f0a17c0603c64a85776beb1a41f67b5
parentbec634f4141be5300ab2354d784186f9c95a8db5 (diff)
Added depth texture support to drawObject
Change-Id: I718d751bf623d5db526ae34ee6673e53c237f649 Change-Id: I718d751bf623d5db526ae34ee6673e53c237f649 Reviewed-by: Miikka Heikkinen <miikka.heikkinen@digia.com>
-rw-r--r--src/datavis3d/engine/drawer.cpp19
-rw-r--r--src/datavis3d/engine/drawer_p.h4
-rw-r--r--src/datavis3d/engine/q3dmaps.cpp63
3 files changed, 25 insertions, 61 deletions
diff --git a/src/datavis3d/engine/drawer.cpp b/src/datavis3d/engine/drawer.cpp
index 8e041741..722a54b8 100644
--- a/src/datavis3d/engine/drawer.cpp
+++ b/src/datavis3d/engine/drawer.cpp
@@ -85,16 +85,23 @@ void Drawer::setTransparency(LabelTransparency transparency)
emit drawerChanged();
}
-void Drawer::drawObject(ShaderHelper *shader, ObjectHelper *object, bool textured,
- GLuint textureId)
+void Drawer::drawObject(ShaderHelper *shader, ObjectHelper *object, GLuint textureId,
+ GLuint depthTextureId)
{
- if (textured) {
+ if (textureId) {
// Activate texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureId);
shader->setUniformValue(shader->texture(), 0);
}
+ if (depthTextureId) {
+ // Activate depth texture
+ glActiveTexture(GL_TEXTURE1);
+ glBindTexture(GL_TEXTURE_2D, depthTextureId);
+ shader->setUniformValue(shader->shadow(), 1);
+ }
+
// 1st attribute buffer : vertices
glEnableVertexAttribArray(shader->posAtt());
glBindBuffer(GL_ARRAY_BUFFER, object->vertexBuf());
@@ -105,7 +112,7 @@ void Drawer::drawObject(ShaderHelper *shader, ObjectHelper *object, bool texture
glBindBuffer(GL_ARRAY_BUFFER, object->normalBuf());
glVertexAttribPointer(shader->normalAtt(), 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
- if (textured) {
+ if (textureId || depthTextureId) {
// 3rd attribute buffer : UVs
glEnableVertexAttribArray(shader->uvAtt());
glBindBuffer(GL_ARRAY_BUFFER, object->uvBuf());
@@ -122,7 +129,7 @@ void Drawer::drawObject(ShaderHelper *shader, ObjectHelper *object, bool texture
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
- if (textured) {
+ if (textureId || depthTextureId) {
glBindTexture(GL_TEXTURE_2D, 0);
glDisableVertexAttribArray(shader->uvAtt());
}
@@ -259,7 +266,7 @@ void Drawer::drawLabel(const QDataItem &item, const LabelItem &label,
shader->setUniformValue(shader->MVP(), MVPMatrix);
// Draw the object
- drawObject(shader, object, true, labelItem.textureId());
+ drawObject(shader, object, labelItem.textureId());
}
void Drawer::generateLabelTexture(QDataItem *item)
diff --git a/src/datavis3d/engine/drawer_p.h b/src/datavis3d/engine/drawer_p.h
index ae2ebc78..3b6a2c49 100644
--- a/src/datavis3d/engine/drawer_p.h
+++ b/src/datavis3d/engine/drawer_p.h
@@ -78,8 +78,8 @@ public:
void setFont(const QFont &font);
void setTransparency(LabelTransparency transparency);
- void drawObject(ShaderHelper *shader, ObjectHelper *object, bool textured = false,
- GLuint textureId = 0);
+ void drawObject(ShaderHelper *shader, ObjectHelper *object, GLuint textureId = 0,
+ GLuint depthTextureId = 0);
void drawLabel(const QDataItem &item, const LabelItem &label,
const QMatrix4x4 &viewmatrix, const QMatrix4x4 &projectionmatrix,
const QVector3D &positionComp, const QVector3D &rotation, GLfloat maxHeight,
diff --git a/src/datavis3d/engine/q3dmaps.cpp b/src/datavis3d/engine/q3dmaps.cpp
index fa9d2c38..3ea4c435 100644
--- a/src/datavis3d/engine/q3dmaps.cpp
+++ b/src/datavis3d/engine/q3dmaps.cpp
@@ -96,8 +96,6 @@ void Q3DMaps::initialize()
}
d_ptr->initBackgroundShaders(QStringLiteral(":/shaders/vertexShadow"),
QStringLiteral(":/shaders/fragmentShadow"));
-// d_ptr->initBackgroundShaders(QStringLiteral(":/shaders/vertexTexture"),
-// QStringLiteral(":/shaders/fragmentTexture"));
d_ptr->initLabelShaders(QStringLiteral(":/shaders/vertexLabel"),
QStringLiteral(":/shaders/fragmentLabel"));
d_ptr->initSelectionShader();
@@ -132,7 +130,7 @@ void Q3DMaps::initialize()
// Set initial camera position
// X must be 0 for rotation to work - we can use "setCameraRotation" for setting it later
- CameraHelper::setDefaultCameraOrientation(QVector3D(0.0f, 0.0f, 3.0f * zComp),
+ CameraHelper::setDefaultCameraOrientation(QVector3D(0.0f, 0.0f, 1.0f + 2.9f * zComp),
QVector3D(0.0f, 0.0f, zComp),
QVector3D(0.0f, 1.0f, 0.0f));
@@ -273,7 +271,7 @@ void Q3DMaps::drawScene()
depthViewMatrix.lookAt(lightPos, QVector3D(0.0f, -d_ptr->m_yAdjustment, zComp),
QVector3D(0.0f, 1.0f, 0.0f));
QMatrix4x4 depthProjectionMatrix;
- depthProjectionMatrix.ortho(-10,10,-10,10,-10,20);
+ depthProjectionMatrix.ortho(-10, 10, -10, 10, -10, 20);
#if 0
// Draw background to depth buffer
if (d_ptr->m_backgroundObj) {
@@ -359,6 +357,7 @@ void Q3DMaps::drawScene()
d_ptr->m_depthShader->release();
#if 0 // Use this if you want to see what is being drawn to the framebuffer
+ // You'll also have to comment out GL_COMPARE_R_TO_TEXTURE -line in texturehelper
d_ptr->m_labelShader->bind();
glEnable(GL_TEXTURE_2D);
QMatrix4x4 modelMatrix;
@@ -369,7 +368,7 @@ void Q3DMaps::drawScene()
modelMatrix.translate(0.0, 0.0, zComp);
QMatrix4x4 MVPMatrix = projectionMatrix * viewmatrix * modelMatrix;
d_ptr->m_labelShader->setUniformValue(d_ptr->m_labelShader->MVP(), MVPMatrix);
- d_ptr->m_drawer->drawObject(d_ptr->m_labelShader, d_ptr->m_labelObj, true,
+ d_ptr->m_drawer->drawObject(d_ptr->m_labelShader, d_ptr->m_labelObj,
d_ptr->m_depthTexture);
glDisable(GL_TEXTURE_2D);
d_ptr->m_labelShader->release();
@@ -460,7 +459,7 @@ void Q3DMaps::drawScene()
modelMatrix.translate(0.0, 0.0, zComp);
QMatrix4x4 MVPMatrix = projectionMatrix * viewmatrix * modelMatrix;
d_ptr->m_labelShader->setUniformValue(d_ptr->m_labelShader->MVP(), MVPMatrix);
- d_ptr->m_drawer->drawObject(d_ptr->m_labelShader, d_ptr->m_labelObj, true,
+ d_ptr->m_drawer->drawObject(d_ptr->m_labelShader, d_ptr->m_labelObj,
d_ptr->m_selectionTexture);
glDisable(GL_TEXTURE_2D);
d_ptr->m_labelShader->release();
@@ -579,7 +578,8 @@ void Q3DMaps::drawScene()
// Set shader bindings
d_ptr->m_backgroundShader->setUniformValue(d_ptr->m_backgroundShader->lightP(),
- QVector3D(0.0f, -d_ptr->m_yAdjustment, zComp) - lightPos);
+ QVector3D(0.0f, -d_ptr->m_yAdjustment, zComp)
+ - lightPos);
d_ptr->m_backgroundShader->setUniformValue(d_ptr->m_backgroundShader->view(),
viewMatrix);
d_ptr->m_backgroundShader->setUniformValue(d_ptr->m_backgroundShader->model(),
@@ -595,52 +595,9 @@ void Q3DMaps::drawScene()
d_ptr->m_backgroundShader->setUniformValue(d_ptr->m_backgroundShader->ambientS(),
d_ptr->m_theme->m_ambientStrength * 3.0f);
- // TODO: Add parameter to drawObject to allow using shadow mapping
-// // Draw the object
-// d_ptr->m_drawer->drawObject(d_ptr->m_backgroundShader, d_ptr->m_backgroundObj, true,
-// d_ptr->m_bgrTexture, d_ptr->m_depthTexture);
-
- // TODO: Done here for now
- // Activate texture
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, d_ptr->m_bgrTexture);
- d_ptr->m_backgroundShader->setUniformValue(d_ptr->m_backgroundShader->texture(), 0);
-
- // Activate depth texture
- glActiveTexture(GL_TEXTURE1);
- glBindTexture(GL_TEXTURE_2D, d_ptr->m_depthTexture);
- d_ptr->m_backgroundShader->setUniformValue(d_ptr->m_backgroundShader->shadow(), 1);
-
- // 1st attribute buffer : vertices
- glEnableVertexAttribArray(d_ptr->m_backgroundShader->posAtt());
- glBindBuffer(GL_ARRAY_BUFFER, d_ptr->m_backgroundObj->vertexBuf());
- glVertexAttribPointer(d_ptr->m_backgroundShader->posAtt(), 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
-
- // 2nd attribute buffer : normals
- glEnableVertexAttribArray(d_ptr->m_backgroundShader->normalAtt());
- glBindBuffer(GL_ARRAY_BUFFER, d_ptr->m_backgroundObj->normalBuf());
- glVertexAttribPointer(d_ptr->m_backgroundShader->normalAtt(), 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
-
- // 3rd attribute buffer : UVs
- glEnableVertexAttribArray(d_ptr->m_backgroundShader->uvAtt());
- glBindBuffer(GL_ARRAY_BUFFER, d_ptr->m_backgroundObj->uvBuf());
- glVertexAttribPointer(d_ptr->m_backgroundShader->uvAtt(), 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
-
- // Index buffer
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, d_ptr->m_backgroundObj->elementBuf());
-
- // Draw the triangles
- glDrawElements(GL_TRIANGLES, d_ptr->m_backgroundObj->indexCount(), GL_UNSIGNED_SHORT, (void*)0);
-
- // Free buffers
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
-
- glBindTexture(GL_TEXTURE_2D, 0);
- glDisableVertexAttribArray(d_ptr->m_backgroundShader->uvAtt());
-
- glDisableVertexAttribArray(d_ptr->m_backgroundShader->normalAtt());
- glDisableVertexAttribArray(d_ptr->m_backgroundShader->posAtt());
+ // Draw the object
+ d_ptr->m_drawer->drawObject(d_ptr->m_backgroundShader, d_ptr->m_backgroundObj,
+ d_ptr->m_bgrTexture, d_ptr->m_depthTexture);
}
// Disable textures