summaryrefslogtreecommitdiffstats
path: root/src/datavis3d
diff options
context:
space:
mode:
authorTomi Korpipää <tomi.korpipaa@digia.com>2013-04-10 13:30:02 +0300
committerMika Salmela <mika.salmela@digia.com>2013-04-10 18:21:01 +0300
commit268f96670c05719d14775aeb3c80b58b9421c990 (patch)
treecdfb42975f928e88653c47c8bce4d35e65991ba0 /src/datavis3d
parentd54dad5da5aca15230889dd801a591b35a0afcb0 (diff)
Preliminary texture support added
Change-Id: Ic52800160b10bbc9d4e7df7159d8891373623fbf Reviewed-by: Miikka Heikkinen <miikka.heikkinen@digia.com> Reviewed-by: Mika Salmela <mika.salmela@digia.com>
Diffstat (limited to 'src/datavis3d')
-rw-r--r--src/datavis3d/engine/engine.qrc3
-rw-r--r--src/datavis3d/engine/meshes/plane.obj15
-rw-r--r--src/datavis3d/engine/q3dbars.cpp19
-rw-r--r--src/datavis3d/engine/shaders/fragmentShaderTexture33
-rw-r--r--src/datavis3d/engine/shaders/vertexShaderTexture25
-rw-r--r--src/datavis3d/utils/objecthelper.cpp12
-rw-r--r--src/datavis3d/utils/shaderhelper.cpp8
-rw-r--r--src/datavis3d/utils/shaderhelper_p.h3
-rw-r--r--src/datavis3d/utils/utils.cpp2
9 files changed, 109 insertions, 11 deletions
diff --git a/src/datavis3d/engine/engine.qrc b/src/datavis3d/engine/engine.qrc
index a521ae32..8e781f05 100644
--- a/src/datavis3d/engine/engine.qrc
+++ b/src/datavis3d/engine/engine.qrc
@@ -10,6 +10,7 @@
<file alias="cylinderSmooth">meshes/cylinderSmooth.obj</file>
<file alias="background">meshes/backgroudFlat.obj</file>
<file alias="backgroundSmooth">meshes/backgroudSmooth.obj</file>
+ <file alias="label">meshes/plane.obj</file>
</qresource>
<qresource prefix="/shaders">
<file alias="fragment">shaders/fragmentShader</file>
@@ -18,5 +19,7 @@
<file alias="fragmentColorOnY">shaders/fragmentShaderColorOnY</file>
<file alias="fragmentSelection">shaders/fragmentShaderSelection</file>
<file alias="vertexSelection">shaders/vertexShaderSelection</file>
+ <file alias="fragmentTexture">shaders/fragmentShaderTexture</file>
+ <file alias="vertexTexture">shaders/vertexShaderTexture</file>
</qresource>
</RCC>
diff --git a/src/datavis3d/engine/meshes/plane.obj b/src/datavis3d/engine/meshes/plane.obj
new file mode 100644
index 00000000..a03e3532
--- /dev/null
+++ b/src/datavis3d/engine/meshes/plane.obj
@@ -0,0 +1,15 @@
+# Blender v2.66 (sub 0) OBJ File: ''
+# www.blender.org
+o Plane
+v 1.000000 0.000000 1.000000
+v -1.000000 0.000000 1.000000
+v 1.000000 0.000000 -1.000000
+v -1.000000 0.000000 -1.000000
+vt 0.003058 1.000000
+vt 0.000000 0.003058
+vt 0.996942 0.000000
+vt 1.000000 0.996942
+vn 0.000000 1.000000 -0.000000
+s off
+f 2/1/1 1/2/1 3/3/1
+f 4/4/1 2/1/1 3/3/1
diff --git a/src/datavis3d/engine/q3dbars.cpp b/src/datavis3d/engine/q3dbars.cpp
index 8c030f7f..5965cc37 100644
--- a/src/datavis3d/engine/q3dbars.cpp
+++ b/src/datavis3d/engine/q3dbars.cpp
@@ -623,6 +623,12 @@ void Q3DBars::drawScene()
// Bind background shader
d_ptr->m_backgroundShader->bind();
+ // Texture test
+ //glEnable(GL_TEXTURE_2D);
+ //GLuint bgrTexture = QGLContext::bindTexture(QImage(":/cube.png"));
+ //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+
// Draw background
if (d_ptr->m_backgroundObj) {
QMatrix4x4 modelMatrix;
@@ -655,6 +661,11 @@ void Q3DBars::drawScene()
d_ptr->m_backgroundShader->setUniformValue(d_ptr->m_backgroundShader->ambientS()
, d_ptr->m_ambientStrength);
+ // Bind the texture in texture unit 0
+// glActiveTexture(GL_TEXTURE0);
+// glBindTexture(GL_TEXTURE_2D, bgrTexture);
+// glUniform1i(d_ptr->m_backgroundShader->texture(), 0);
+
// 1st attribute buffer : vertices
glEnableVertexAttribArray(d_ptr->m_backgroundShader->posAtt());
glBindBuffer(GL_ARRAY_BUFFER, d_ptr->m_backgroundObj->vertexBuf());
@@ -668,10 +679,10 @@ void Q3DBars::drawScene()
, 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);
+ 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());
diff --git a/src/datavis3d/engine/shaders/fragmentShaderTexture b/src/datavis3d/engine/shaders/fragmentShaderTexture
new file mode 100644
index 00000000..f1b11908
--- /dev/null
+++ b/src/datavis3d/engine/shaders/fragmentShaderTexture
@@ -0,0 +1,33 @@
+varying highp vec2 UV;
+varying highp vec2 coords_mdl;
+varying highp vec3 position_wrld;
+varying highp vec3 normal_cmr;
+varying highp vec3 eyeDirection_cmr;
+varying highp vec3 lightDirection_cmr;
+uniform highp vec3 lightPosition_wrld;
+uniform highp vec3 color_mdl;
+uniform highp float lightStrength;
+uniform highp float ambientStrength;
+uniform sampler2D textureSampler;
+void main() {
+ highp vec3 materialDiffuseColor = texture2D(textureSampler, UV);
+ highp vec3 materialAmbientColor = vec3(ambientStrength, ambientStrength, ambientStrength) * materialDiffuseColor;
+ highp vec3 materialSpecularColor = vec3(1.0, 1.0, 1.0);
+ highp float distance = length(lightPosition_wrld - position_wrld);
+ highp vec3 n = normalize(normal_cmr);
+ highp vec3 l = normalize(lightDirection_cmr);
+ highp float cosTheta = dot(n, l);
+ if (cosTheta < 0.0) { cosTheta = 0.0; }
+ if (cosTheta > 1.0) { cosTheta = 1.0; }
+ highp vec3 E = normalize(eyeDirection_cmr);
+ highp vec3 R = reflect(-l, n);
+ highp float cosAlpha = dot(E, R);
+ if (cosAlpha < 0.0) { cosAlpha = 0.0; }
+ if (cosAlpha > 1.0) { cosAlpha = 1.0; }
+ gl_FragColor.rgb = //color_mdl + color_mdl * vec3(cosTheta * cosTheta) / (distance * distance) + vec3(cosAlpha * cosAlpha * cosAlpha * cosAlpha * cosAlpha * cosAlpha * cosAlpha * cosAlpha) / (distance * distance);
+ materialAmbientColor +
+ materialDiffuseColor * lightStrength * (cosTheta * cosTheta) / distance +
+ materialSpecularColor * lightStrength * (cosAlpha * cosAlpha * cosAlpha * cosAlpha * cosAlpha * cosAlpha * cosAlpha * cosAlpha * cosAlpha * cosAlpha) / distance;//(distance * distance);
+ gl_FragColor.a = 1.0;
+}
+
diff --git a/src/datavis3d/engine/shaders/vertexShaderTexture b/src/datavis3d/engine/shaders/vertexShaderTexture
new file mode 100644
index 00000000..19f9d617
--- /dev/null
+++ b/src/datavis3d/engine/shaders/vertexShaderTexture
@@ -0,0 +1,25 @@
+attribute highp vec3 vertexPosition_mdl;
+attribute highp vec2 vertexUV;
+attribute highp vec3 vertexNormal_mdl;
+uniform highp mat4 MVP;
+uniform highp mat4 V;
+uniform highp mat4 M;
+uniform highp mat4 itM;
+uniform highp vec3 lightPosition_wrld;
+varying highp vec2 UV;
+varying highp vec3 position_wrld;
+varying highp vec3 normal_cmr;
+varying highp vec3 eyeDirection_cmr;
+varying highp vec3 lightDirection_cmr;
+varying highp vec2 coords_mdl;
+void main() {
+ gl_Position = MVP * vec4(vertexPosition_mdl, 1.0);
+ coords_mdl = vertexPosition_mdl.xy;
+ position_wrld = (M * vec4(vertexPosition_mdl, 1.0)).xyz;
+ vec3 vertexPosition_cmr = (V * M * vec4(vertexPosition_mdl, 1.0)).xyz;
+ eyeDirection_cmr = vec3(0.0, 0.0, 0.0) - vertexPosition_cmr;
+ vec3 lightPosition_cmr = (V * vec4(lightPosition_wrld, 1.0)).xyz;
+ lightDirection_cmr = lightPosition_cmr + eyeDirection_cmr;
+ normal_cmr = (V * itM * vec4(vertexNormal_mdl, 0.0)).xyz; // Use modelMatrix's transposed inverse, as it's scaled
+ UV = vertexUV;
+}
diff --git a/src/datavis3d/utils/objecthelper.cpp b/src/datavis3d/utils/objecthelper.cpp
index e854836a..b6193bfe 100644
--- a/src/datavis3d/utils/objecthelper.cpp
+++ b/src/datavis3d/utils/objecthelper.cpp
@@ -113,15 +113,15 @@ void ObjectHelper::load()
, &indexed_normals.at(0)
, GL_STATIC_DRAW);
- //m_caller->glGenBuffers(1, &m_uvbuffer);
- //m_caller->glBindBuffer(GL_ARRAY_BUFFER, m_uvbuffer);
- //m_caller->glBufferData(GL_ARRAY_BUFFER, indexed_uvs.size() * sizeof(QVector2D), &indexed_uvs.at(0)
- // , GL_STATIC_DRAW);
+ m_caller->glGenBuffers(1, &m_uvbuffer);
+ m_caller->glBindBuffer(GL_ARRAY_BUFFER, m_uvbuffer);
+ m_caller->glBufferData(GL_ARRAY_BUFFER, indexed_uvs.size() * sizeof(QVector2D)
+ , &indexed_uvs.at(0), GL_STATIC_DRAW);
m_caller->glGenBuffers(1, &m_elementbuffer);
m_caller->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_elementbuffer);
- m_caller->glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned short), &indices.at(0)
- , GL_STATIC_DRAW);
+ m_caller->glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned short)
+ , &indices.at(0), GL_STATIC_DRAW);
m_caller->glBindBuffer(GL_ARRAY_BUFFER, 0);
m_caller->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
diff --git a/src/datavis3d/utils/shaderhelper.cpp b/src/datavis3d/utils/shaderhelper.cpp
index e73d77c6..73f6b7ed 100644
--- a/src/datavis3d/utils/shaderhelper.cpp
+++ b/src/datavis3d/utils/shaderhelper.cpp
@@ -96,6 +96,7 @@ void ShaderHelper::initialize()
m_lightStrengthUniform = m_program->uniformLocation("lightStrength");
m_ambientStrengthUniform = m_program->uniformLocation("ambientStrength");
m_colorUniform = m_program->uniformLocation("color_mdl");
+ m_textureUniform = m_program->uniformLocation("textureSampler");
m_positionAttr = m_program->attributeLocation("vertexPosition_mdl");
m_uvAttr = m_program->attributeLocation("vertexUV");
@@ -190,6 +191,13 @@ GLuint ShaderHelper::color()
return m_colorUniform;
}
+GLuint ShaderHelper::texture()
+{
+ if (!m_initialized)
+ qFatal("Shader not initialized");
+ return m_textureUniform;
+}
+
GLuint ShaderHelper::posAtt()
{
if (!m_initialized)
diff --git a/src/datavis3d/utils/shaderhelper_p.h b/src/datavis3d/utils/shaderhelper_p.h
index 14781e76..b6bb1eb6 100644
--- a/src/datavis3d/utils/shaderhelper_p.h
+++ b/src/datavis3d/utils/shaderhelper_p.h
@@ -78,6 +78,7 @@ class ShaderHelper
GLuint lightS();
GLuint ambientS();
GLuint color();
+ GLuint texture();
GLuint posAtt();
GLuint uvAtt();
@@ -105,8 +106,8 @@ class ShaderHelper
GLuint m_lightPositionUniform;
GLuint m_lightStrengthUniform;
GLuint m_ambientStrengthUniform;
+ GLuint m_textureUniform;
- //GLuint m_texture; // currently unused
//GLuint m_depthTexture; // currently unused
GLboolean m_initialized;
diff --git a/src/datavis3d/utils/utils.cpp b/src/datavis3d/utils/utils.cpp
index 7cad0bea..74e7cca7 100644
--- a/src/datavis3d/utils/utils.cpp
+++ b/src/datavis3d/utils/utils.cpp
@@ -46,6 +46,8 @@
#include <QPainter>
#include <QPoint>
+#include <qmath.h>
+
#include <QDebug>
QTCOMMERCIALDATAVIS3D_BEGIN_NAMESPACE