Compare commits
6 Commits
Version_1.
...
Version_1.
Author | SHA1 | Date | |
---|---|---|---|
![]() |
67657061b6 | ||
![]() |
06d403d546 | ||
![]() |
541a975624 | ||
![]() |
30f1a1e627 | ||
![]() |
cdf19911f6 | ||
![]() |
4c40d140a9 |
@@ -18,6 +18,9 @@ struct VertexData
|
|||||||
QVector3D position;
|
QVector3D position;
|
||||||
QVector2D texCoord;
|
QVector2D texCoord;
|
||||||
QVector3D vertexNormal;
|
QVector3D vertexNormal;
|
||||||
|
QVector3D polygonNormal;
|
||||||
|
QVector3D tangent;
|
||||||
|
QVector3D bitangent;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Segment {
|
struct Segment {
|
||||||
@@ -42,10 +45,10 @@ struct Material {
|
|||||||
QString tx3d;
|
QString tx3d;
|
||||||
QOpenGLTexture* texture0 = Q_NULLPTR;
|
QOpenGLTexture* texture0 = Q_NULLPTR;
|
||||||
QOpenGLTexture* texture1 = Q_NULLPTR;
|
QOpenGLTexture* texture1 = Q_NULLPTR;
|
||||||
QVector4D specularColor = { 1.0, 1.0, 1.0, 1.0 };
|
QVector4D specularColor = { 0.1f, 0.1f, 0.1f, 1.0 };
|
||||||
QVector4D diffuseColor = { 1.0, 0.0, 0.0, 1.0 };
|
QVector4D diffuseColor = { 1.0, 0.0, 0.0, 1.0 };
|
||||||
QVector4D ambientColor = { 1.0, 1.0, 1.0, 1.0 };
|
QVector4D ambientColor = { 1.0, 1.0, 1.0, 1.0 };
|
||||||
float shininess = 80;
|
float shininess = 1;
|
||||||
bool flags[8] = { false };
|
bool flags[8] = { false };
|
||||||
bool transparent = false;
|
bool transparent = false;
|
||||||
quint8 rendertype = 0;
|
quint8 rendertype = 0;
|
||||||
|
@@ -34,6 +34,7 @@ private:
|
|||||||
// functions
|
// functions
|
||||||
private:
|
private:
|
||||||
void clearData();
|
void clearData();
|
||||||
|
void setupPipeline(QOpenGLShaderProgram * program);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void drawGeometry(QOpenGLShaderProgram *program);
|
void drawGeometry(QOpenGLShaderProgram *program);
|
||||||
|
@@ -1,19 +1,26 @@
|
|||||||
|
#version 450
|
||||||
#ifdef GL_ES
|
#ifdef GL_ES
|
||||||
// Set default precision to medium
|
// Set default precision to medium
|
||||||
precision mediump int;
|
precision mediump int;
|
||||||
precision mediump float;
|
precision mediump float;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
uniform mat3 n_matrix;
|
uniform mat3 normalMatrix;
|
||||||
uniform vec3 cameraPosition;
|
uniform vec3 cameraPosition;
|
||||||
|
|
||||||
uniform sampler2D texture;
|
uniform sampler2D tx0;
|
||||||
uniform float materialShininess;
|
uniform sampler2D tx1;
|
||||||
uniform vec3 materialSpecularColor;
|
|
||||||
|
|
||||||
uniform bool b_transparent;
|
uniform struct Material {
|
||||||
uniform bool b_specular;
|
float shininess;
|
||||||
uniform bool b_light;
|
vec3 specularColor;
|
||||||
|
bool isTransparent;
|
||||||
|
bool hasSpecularmap;
|
||||||
|
bool hasNormalmap;
|
||||||
|
bool isGlow;
|
||||||
|
} material;
|
||||||
|
|
||||||
|
uniform bool useLight;
|
||||||
|
|
||||||
uniform struct Light {
|
uniform struct Light {
|
||||||
vec4 position;
|
vec4 position;
|
||||||
@@ -25,67 +32,96 @@ uniform struct Light {
|
|||||||
varying vec2 v_surfaceUV;
|
varying vec2 v_surfaceUV;
|
||||||
varying vec3 v_surfacePosition;
|
varying vec3 v_surfacePosition;
|
||||||
varying vec3 v_surfaceNormal;
|
varying vec3 v_surfaceNormal;
|
||||||
|
varying vec3 v_polyNorm;
|
||||||
|
varying vec3 v_polyTan;
|
||||||
|
varying vec3 v_polyBiTan;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
if(b_light)
|
if(useLight && !material.isGlow)
|
||||||
{
|
{
|
||||||
// some values
|
// get the color and undo gamma correction
|
||||||
vec3 normalWorld = normalize(n_matrix * v_surfaceNormal);
|
vec4 surfaceColor = vec4(texture2D(tx0, v_surfaceUV));
|
||||||
|
|
||||||
vec4 surfaceColor = vec4(texture2D(texture, v_surfaceUV));
|
|
||||||
surfaceColor.rgb = pow(surfaceColor.rgb, vec3(2.2));
|
surfaceColor.rgb = pow(surfaceColor.rgb, vec3(2.2));
|
||||||
|
|
||||||
vec3 surfaceToLight;
|
// attenutation depending on the distance to the light
|
||||||
float attenuation;
|
|
||||||
// directional light
|
|
||||||
if(light.position.w == 0.0f)
|
|
||||||
{
|
|
||||||
surfaceToLight = normalize(light.position.xyz);
|
|
||||||
}
|
|
||||||
// point light
|
|
||||||
else
|
|
||||||
{
|
|
||||||
surfaceToLight = normalize(light.position.xyz - v_surfacePosition);
|
|
||||||
}
|
|
||||||
|
|
||||||
float distanceToLight = length(light.position.xyz - v_surfacePosition);
|
float distanceToLight = length(light.position.xyz - v_surfacePosition);
|
||||||
attenuation = 1.0 / (1.0 + light.attenuationFactor * pow(distanceToLight, 2));
|
float attenuation = 1.0 / (1.0 + light.attenuationFactor * pow(distanceToLight, 2));
|
||||||
|
|
||||||
|
// normal vector
|
||||||
|
vec3 normal = normalize(normalMatrix * v_surfaceNormal);
|
||||||
|
|
||||||
|
// direction from surface to light depending on the light type
|
||||||
|
vec3 surfaceToLight;
|
||||||
|
if(light.position.w == 0.0) // directional light
|
||||||
|
surfaceToLight = normalize(light.position.xyz);
|
||||||
|
else // point light
|
||||||
|
surfaceToLight = normalize(light.position.xyz - v_surfacePosition);
|
||||||
|
|
||||||
|
// direction from surface to camera
|
||||||
vec3 surfaceToCamera = normalize(cameraPosition - v_surfacePosition);
|
vec3 surfaceToCamera = normalize(cameraPosition - v_surfacePosition);
|
||||||
|
|
||||||
// ambient
|
// adjust the values if material has normal map
|
||||||
|
if(material.hasNormalmap)
|
||||||
|
{
|
||||||
|
vec3 surfaceTangent = normalize(normalMatrix * v_polyTan);
|
||||||
|
vec3 surfaceBitangent = normalize(normalMatrix * -v_polyBiTan);
|
||||||
|
vec3 surfaceNormal = normalize(normalMatrix * v_surfaceNormal);
|
||||||
|
mat3 tbn = transpose(mat3(surfaceTangent, surfaceBitangent, surfaceNormal));
|
||||||
|
normal = texture2D(tx1, v_surfaceUV).rgb;
|
||||||
|
normal = normalize(normal * 2.0 -1.0);
|
||||||
|
surfaceToLight = tbn * surfaceToLight;
|
||||||
|
surfaceToCamera = tbn * surfaceToCamera;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// ambient component
|
||||||
|
|
||||||
vec3 ambient = light.ambientCoefficient * surfaceColor.rgb * light.intensities;
|
vec3 ambient = light.ambientCoefficient * surfaceColor.rgb * light.intensities;
|
||||||
|
|
||||||
// diffuse
|
|
||||||
float diffuseCoefficient = max(0.0, dot(normalWorld, surfaceToLight));
|
/////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// diffuse component
|
||||||
|
|
||||||
|
float diffuseCoefficient = max(0.0, dot(normal, surfaceToLight));
|
||||||
vec3 diffuse = diffuseCoefficient * surfaceColor.rgb * light.intensities;
|
vec3 diffuse = diffuseCoefficient * surfaceColor.rgb * light.intensities;
|
||||||
|
|
||||||
// specular
|
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// specular component
|
||||||
|
|
||||||
float specularCoefficient = 0.0;
|
float specularCoefficient = 0.0;
|
||||||
if(diffuseCoefficient > 0.0)
|
if(diffuseCoefficient > 0.0)
|
||||||
specularCoefficient = pow(max(0.0, dot(surfaceToCamera, reflect(-surfaceToLight, normalWorld))), materialShininess);
|
specularCoefficient = pow(max(0.0, dot(surfaceToCamera, reflect(-surfaceToLight, normal))), material.shininess);
|
||||||
vec3 specColor;
|
|
||||||
if(b_specular)
|
float specularWeight = 1;
|
||||||
specColor = vec3(surfaceColor.a);
|
if(material.hasSpecularmap)
|
||||||
else
|
specularWeight = surfaceColor.a;
|
||||||
specColor = materialSpecularColor;
|
vec3 specColor = specularWeight * material.specularColor;
|
||||||
|
|
||||||
vec3 specular = specularCoefficient * specColor * light.intensities;
|
vec3 specular = specularCoefficient * specColor * light.intensities;
|
||||||
|
|
||||||
// linear color before gamma correction)
|
/////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// linear color before gamma correction
|
||||||
vec3 linearColor = ambient + attenuation * (diffuse + specular);
|
vec3 linearColor = ambient + attenuation * (diffuse + specular);
|
||||||
|
|
||||||
// final color after gama correction
|
/////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// gama correction
|
||||||
vec3 gamma = vec3(1.0/2.2);
|
vec3 gamma = vec3(1.0/2.2);
|
||||||
if(!b_transparent)
|
|
||||||
surfaceColor.a = 1.0f;
|
if(!material.isTransparent)
|
||||||
|
surfaceColor.a = 1.0;
|
||||||
|
|
||||||
gl_FragColor = vec4(pow(linearColor, gamma), surfaceColor.a);
|
gl_FragColor = vec4(pow(linearColor, gamma), surfaceColor.a);
|
||||||
}
|
}
|
||||||
|
// don't use light
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
vec4 surfaceColor = vec4(texture2D(texture, v_surfaceUV));
|
vec4 surfaceColor = vec4(texture2D(tx0, v_surfaceUV));
|
||||||
if(!b_transparent)
|
|
||||||
surfaceColor.a = 1.0f;
|
if(!material.isTransparent)
|
||||||
|
surfaceColor.a = 1.0;
|
||||||
|
|
||||||
gl_FragColor = surfaceColor;
|
gl_FragColor = surfaceColor;
|
||||||
}
|
}
|
||||||
|
@@ -1,29 +1,39 @@
|
|||||||
|
#version 450
|
||||||
#ifdef GL_ES
|
#ifdef GL_ES
|
||||||
// Set default precision to medium
|
// Set default precision to medium
|
||||||
precision mediump int;
|
precision mediump int;
|
||||||
precision mediump float;
|
precision mediump float;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
uniform mat4 vp_matrix;
|
uniform mat4 viewProjection;
|
||||||
uniform mat4 norm_matrix;
|
uniform mat4 normalizeModel;
|
||||||
uniform mat4 m_matrix;
|
uniform mat4 modelMatrix;
|
||||||
|
|
||||||
attribute vec4 a_position;
|
attribute vec4 a_position;
|
||||||
attribute vec2 a_texcoord;
|
attribute vec2 a_texcoord;
|
||||||
attribute vec3 a_normal;
|
attribute vec3 a_normal;
|
||||||
|
attribute vec3 a_polyNorm;
|
||||||
|
attribute vec3 a_polyTan;
|
||||||
|
attribute vec3 a_polyBiTan;
|
||||||
|
|
||||||
varying vec2 v_surfaceUV;
|
varying vec2 v_surfaceUV;
|
||||||
varying vec3 v_surfacePosition;
|
varying vec3 v_surfacePosition;
|
||||||
varying vec3 v_surfaceNormal;
|
varying vec3 v_surfaceNormal;
|
||||||
|
varying vec3 v_polyNorm;
|
||||||
|
varying vec3 v_polyTan;
|
||||||
|
varying vec3 v_polyBiTan;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
// Calculate vertex position in screen space
|
// Calculate vertex position in screen space
|
||||||
gl_Position = vp_matrix * norm_matrix * m_matrix * a_position;
|
gl_Position = viewProjection * normalizeModel * modelMatrix * a_position;
|
||||||
|
|
||||||
// Pass data to fragment shader
|
// Pass data to fragment shader
|
||||||
// Value will be automatically interpolated to fragments inside polygon faces
|
// Value will be automatically interpolated to fragments inside polygon faces
|
||||||
v_surfaceUV = a_texcoord;
|
v_surfaceUV = a_texcoord;
|
||||||
v_surfacePosition = vec3(norm_matrix * m_matrix * a_position);
|
v_surfacePosition = vec3(normalizeModel * modelMatrix * a_position);
|
||||||
v_surfaceNormal = a_normal;
|
v_surfaceNormal = a_normal;
|
||||||
|
v_polyNorm = a_polyNorm;
|
||||||
|
v_polyTan = a_polyTan;
|
||||||
|
v_polyBiTan = a_polyBiTan;
|
||||||
}
|
}
|
||||||
|
@@ -5,7 +5,6 @@
|
|||||||
#include "..\Header\OutputDevice.h"
|
#include "..\Header\OutputDevice.h"
|
||||||
#include <QRegExp>
|
#include <QRegExp>
|
||||||
|
|
||||||
#include "..\Header\Profiler.h"
|
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////
|
||||||
// constructor/destructor
|
// constructor/destructor
|
||||||
@@ -52,26 +51,8 @@ void GeometryEngine::clearData()
|
|||||||
m_drawList.clear();
|
m_drawList.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
|
void GeometryEngine::setupPipeline(QOpenGLShaderProgram *program)
|
||||||
{
|
{
|
||||||
if (!m_arrayBuf.isCreated() || !m_indexBuf.isCreated())
|
|
||||||
return;
|
|
||||||
|
|
||||||
// Setup
|
|
||||||
// Tell OpenGL which VBOs to use
|
|
||||||
m_arrayBuf.bind();
|
|
||||||
m_indexBuf.bind();
|
|
||||||
|
|
||||||
// Allways normalize by this
|
|
||||||
QMatrix4x4 normMatrix;
|
|
||||||
float maxExtent = std::max(std::max(m_boundings.extents[0], m_boundings.extents[1]), m_boundings.extents[2]);
|
|
||||||
normMatrix.scale(1 / maxExtent);
|
|
||||||
normMatrix.translate(-m_boundings.center[0], -m_boundings.center[1], -m_boundings.center[2]);
|
|
||||||
program->setUniformValue("norm_matrix", normMatrix);
|
|
||||||
|
|
||||||
// Allways use texture unit 0
|
|
||||||
program->setUniformValue("texture", 0);
|
|
||||||
|
|
||||||
// Offset for position
|
// Offset for position
|
||||||
quintptr offset = 0;
|
quintptr offset = 0;
|
||||||
|
|
||||||
@@ -88,13 +69,63 @@ void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
|
|||||||
program->enableAttributeArray(texcoordLocation);
|
program->enableAttributeArray(texcoordLocation);
|
||||||
program->setAttributeBuffer(texcoordLocation, GL_FLOAT, offset, 2, sizeof(VertexData));
|
program->setAttributeBuffer(texcoordLocation, GL_FLOAT, offset, 2, sizeof(VertexData));
|
||||||
|
|
||||||
//Offset for normal
|
//Offset for vertexNormal
|
||||||
offset += sizeof(QVector2D);
|
offset += sizeof(QVector2D);
|
||||||
|
|
||||||
// Tell OpenGL programmable pipeline how to locate vertex normal data
|
// Tell OpenGL programmable pipeline how to locate vertex normal data
|
||||||
int normLocation = program->attributeLocation("a_normal");
|
int vertNormLocation = program->attributeLocation("a_normal");
|
||||||
program->enableAttributeArray(normLocation);
|
program->enableAttributeArray(vertNormLocation);
|
||||||
program->setAttributeBuffer(normLocation, GL_FLOAT, offset, 3, sizeof(VertexData));
|
program->setAttributeBuffer(vertNormLocation, GL_FLOAT, offset, 3, sizeof(VertexData));
|
||||||
|
|
||||||
|
//Offset for polygonNormal
|
||||||
|
offset += sizeof(QVector3D);
|
||||||
|
|
||||||
|
// Tell OpenGL programmable pipeline how to locate polygon normal data
|
||||||
|
int polyNormLocation = program->attributeLocation("a_polyNorm");
|
||||||
|
program->enableAttributeArray(polyNormLocation);
|
||||||
|
program->setAttributeBuffer(polyNormLocation, GL_FLOAT, offset, 3, sizeof(VertexData));
|
||||||
|
|
||||||
|
//Offset for polygonTangent
|
||||||
|
offset += sizeof(QVector3D);
|
||||||
|
|
||||||
|
// Tell OpenGL programmable pipeline how to locate polygon tangent data
|
||||||
|
int polyTanLocation = program->attributeLocation("a_polyTan");
|
||||||
|
program->enableAttributeArray(polyTanLocation);
|
||||||
|
program->setAttributeBuffer(polyTanLocation, GL_FLOAT, offset, 3, sizeof(VertexData));
|
||||||
|
|
||||||
|
//Offset for polygonBitangent
|
||||||
|
offset += sizeof(QVector3D);
|
||||||
|
|
||||||
|
// Tell OpenGL programmable pipeline how to locate polygon bitangent data
|
||||||
|
int polyBiTanLocation = program->attributeLocation("a_polyBiTan");
|
||||||
|
program->enableAttributeArray(polyBiTanLocation);
|
||||||
|
program->setAttributeBuffer(polyBiTanLocation, GL_FLOAT, offset, 3, sizeof(VertexData));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
|
||||||
|
{
|
||||||
|
if (!m_arrayBuf.isCreated() || !m_indexBuf.isCreated())
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Setup
|
||||||
|
// Tell OpenGL which VBOs to use
|
||||||
|
m_arrayBuf.bind();
|
||||||
|
m_indexBuf.bind();
|
||||||
|
|
||||||
|
// Allways normalize by this
|
||||||
|
QMatrix4x4 normMatrix;
|
||||||
|
float maxExtent = std::max(std::max(m_boundings.extents[0], m_boundings.extents[1]), m_boundings.extents[2]);
|
||||||
|
normMatrix.scale(1 / maxExtent);
|
||||||
|
normMatrix.translate(-m_boundings.center[0], -m_boundings.center[1], -m_boundings.center[2]);
|
||||||
|
program->setUniformValue("normalizeModel", normMatrix);
|
||||||
|
|
||||||
|
// Allways use texture unit 0 and 1
|
||||||
|
program->setUniformValue("tx0", 0);
|
||||||
|
program->setUniformValue("tx1", 1);
|
||||||
|
|
||||||
|
//setup the pipeline
|
||||||
|
setupPipeline(program);
|
||||||
|
|
||||||
// Paint
|
// Paint
|
||||||
|
|
||||||
@@ -102,36 +133,51 @@ void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
|
|||||||
{
|
{
|
||||||
bool tmp_transparent(false);
|
bool tmp_transparent(false);
|
||||||
bool tmp_specular(false);
|
bool tmp_specular(false);
|
||||||
|
bool tmp_normalmap(false);
|
||||||
|
bool tmp_glow(false);
|
||||||
float shininess(0.0);
|
float shininess(0.0);
|
||||||
QVector3D specularColor;
|
QVector3D specularColor;
|
||||||
|
|
||||||
// bind the correct texture
|
// bind the correct texture
|
||||||
if (it.textureIndex < (unsigned)m_materials->size() && m_materials->at(it.textureIndex).texture0 != Q_NULLPTR)
|
if (it.textureIndex < (unsigned)m_materials->size() && m_materials->at(it.textureIndex).texture0 != Q_NULLPTR)
|
||||||
{
|
{
|
||||||
m_materials->at(it.textureIndex).texture0->bind();
|
m_materials->at(it.textureIndex).texture0->bind(0);
|
||||||
tmp_transparent = m_materials->at(it.textureIndex).transparent;
|
tmp_transparent = m_materials->at(it.textureIndex).transparent;
|
||||||
tmp_specular = m_materials->at(it.textureIndex).flags[7];
|
tmp_specular = m_materials->at(it.textureIndex).flags[7];
|
||||||
shininess = m_materials->at(it.textureIndex).shininess;
|
shininess = m_materials->at(it.textureIndex).shininess;
|
||||||
specularColor = m_materials->at(it.textureIndex).specularColor.toVector3D();
|
specularColor = m_materials->at(it.textureIndex).specularColor.toVector3D();
|
||||||
|
|
||||||
|
if (m_materials->at(it.textureIndex).rendertype == 27 || m_materials->at(it.textureIndex).rendertype == 28)
|
||||||
|
{
|
||||||
|
if (m_materials->at(it.textureIndex).texture1 != Q_NULLPTR)
|
||||||
|
{
|
||||||
|
tmp_normalmap = true;
|
||||||
|
m_materials->at(it.textureIndex).texture1->bind(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_materials->at(it.textureIndex).flags[0] || m_materials->at(it.textureIndex).flags[1] || m_materials->at(it.textureIndex).rendertype == 1)
|
||||||
|
tmp_glow = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
m_defaultMaterial->texture0->bind();
|
m_defaultMaterial->texture0->bind(0);
|
||||||
tmp_transparent = m_defaultMaterial->transparent;
|
tmp_transparent = m_defaultMaterial->transparent;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set model matrix
|
// Set model matrix
|
||||||
program->setUniformValue("m_matrix", it.modelMatrix);
|
program->setUniformValue("modelMatrix", it.modelMatrix);
|
||||||
|
|
||||||
// Set normal matrix
|
// Set normal matrix
|
||||||
program->setUniformValue("n_matrix", (normMatrix * it.modelMatrix).normalMatrix());
|
program->setUniformValue("normalMatrix", (normMatrix * it.modelMatrix).normalMatrix());
|
||||||
|
|
||||||
// set some more values
|
|
||||||
program->setUniformValue("b_transparent", tmp_transparent);
|
|
||||||
program->setUniformValue("b_specular", tmp_specular);
|
|
||||||
|
|
||||||
// set some material attributes
|
// set some material attributes
|
||||||
program->setUniformValue("materialShininess", shininess);
|
program->setUniformValue("material.shininess", shininess);
|
||||||
program->setUniformValue("materialSpecularColor", specularColor);
|
program->setUniformValue("material.specularColor", specularColor);
|
||||||
|
program->setUniformValue("material.isTransparent", tmp_transparent);
|
||||||
|
program->setUniformValue("material.hasSpecularmap", tmp_specular);
|
||||||
|
program->setUniformValue("material.hasNormalmap", tmp_normalmap);
|
||||||
|
program->setUniformValue("material.isGlow", tmp_glow);
|
||||||
|
|
||||||
// Draw cube geometry using indices from VBO 1
|
// Draw cube geometry using indices from VBO 1
|
||||||
glDrawElements(GL_TRIANGLES, it.size, GL_UNSIGNED_INT, (void*)(it.offset * sizeof(GLuint)));
|
glDrawElements(GL_TRIANGLES, it.size, GL_UNSIGNED_INT, (void*)(it.offset * sizeof(GLuint)));
|
||||||
@@ -141,7 +187,6 @@ void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
|
|||||||
|
|
||||||
void GeometryEngine::loadFile(QString filePath)
|
void GeometryEngine::loadFile(QString filePath)
|
||||||
{
|
{
|
||||||
TIC("Start");
|
|
||||||
// cleanup old stuff and recreate buffers
|
// cleanup old stuff and recreate buffers
|
||||||
clearData();
|
clearData();
|
||||||
m_arrayBuf.create();
|
m_arrayBuf.create();
|
||||||
@@ -216,7 +261,5 @@ void GeometryEngine::loadFile(QString filePath)
|
|||||||
clearData();
|
clearData();
|
||||||
OutputDevice::getInstance()->print(QString(e.what()), 2);
|
OutputDevice::getInstance()->print(QString(e.what()), 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
TOC("End");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
#include "..\Header\MshFile.h"
|
#include "..\Header\MshFile.h"
|
||||||
#include "..\Header\tga.h"
|
#include "..\Header\tga.h"
|
||||||
#include "..\Header\OutputDevice.h"
|
#include "..\Header\OutputDevice.h"
|
||||||
|
#include <QVector3D>
|
||||||
|
|
||||||
|
|
||||||
// helper function to save data from file to any variable type
|
// helper function to save data from file to any variable type
|
||||||
@@ -571,18 +572,106 @@ void MshFile::analyseSegmChunks(Model * dataDestination, QList<ChunkHeader*>& ch
|
|||||||
|
|
||||||
if (tmp_buffer.size() == 5)
|
if (tmp_buffer.size() == 5)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < 3; i++)
|
// calculate poylgon normal, tangent and bitangent
|
||||||
new_segment->indices.push_back(tmp_buffer.takeFirst());
|
QVector3D vec1, vec2, norm, tan, bi;
|
||||||
|
QVector2D uv1, uv2;
|
||||||
|
float f;
|
||||||
|
|
||||||
|
vec1 = new_segment->vertices[tmp_buffer[0]].position - new_segment->vertices[tmp_buffer[1]].position;
|
||||||
|
vec2 = new_segment->vertices[tmp_buffer[0]].position - new_segment->vertices[tmp_buffer[2]].position;
|
||||||
|
uv1 = new_segment->vertices[tmp_buffer[0]].texCoord - new_segment->vertices[tmp_buffer[1]].texCoord;
|
||||||
|
uv2 = new_segment->vertices[tmp_buffer[0]].texCoord - new_segment->vertices[tmp_buffer[2]].texCoord;
|
||||||
|
f = 1.0f / (uv1.x() * uv2.y() - uv2.x() * uv1.y());
|
||||||
|
|
||||||
|
norm = QVector3D::crossProduct(vec1, vec2).normalized();
|
||||||
|
|
||||||
|
tan.setX(f * (uv2.y() * vec1.x() - uv1.y() * vec2.x()));
|
||||||
|
tan.setY(f * (uv2.y() * vec1.y() - uv1.y() * vec2.y()));
|
||||||
|
tan.setZ(f * (uv2.y() * vec1.z() - uv1.y() * vec2.z()));
|
||||||
|
tan.normalize();
|
||||||
|
|
||||||
|
bi.setX(f * (-uv2.x() * vec1.x() + uv1.x() * vec2.x()));
|
||||||
|
bi.setY(f * (-uv2.x() * vec1.y() + uv1.x() * vec2.y()));
|
||||||
|
bi.setZ(f * (-uv2.x() * vec1.z() + uv1.x() * vec2.z()));
|
||||||
|
bi.normalize();
|
||||||
|
|
||||||
|
for (int k = 0; k < 3; k++)
|
||||||
|
{
|
||||||
|
// polygon normal wasn't calculated before
|
||||||
|
if (new_segment->vertices[tmp_buffer[k]].polygonNormal == QVector3D(0, 0, 0))
|
||||||
|
{
|
||||||
|
new_segment->vertices[tmp_buffer[k]].polygonNormal = norm;
|
||||||
|
new_segment->vertices[tmp_buffer[k]].tangent = tan;
|
||||||
|
new_segment->vertices[tmp_buffer[k]].bitangent = bi;
|
||||||
|
|
||||||
|
new_segment->indices.push_back(tmp_buffer[k]);
|
||||||
|
}
|
||||||
|
// polygon normal already calculated so duplicate the vertex
|
||||||
|
else
|
||||||
|
{
|
||||||
|
new_segment->vertices.push_back(new_segment->vertices[tmp_buffer[k]]);
|
||||||
|
new_segment->vertices.back().polygonNormal = norm;
|
||||||
|
new_segment->vertices.back().tangent = tan;
|
||||||
|
new_segment->vertices.back().bitangent = bi;
|
||||||
|
new_segment->indices.push_back(new_segment->vertices.size() - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tmp_buffer.remove(0, 3);
|
||||||
}
|
}
|
||||||
else if (tmp_buffer.size() > 5)
|
else if (tmp_buffer.size() > 5)
|
||||||
{
|
{
|
||||||
unsigned int tmp_multiPolySize = tmp_buffer.size() - 2;
|
unsigned int tmp_multiPolySize = tmp_buffer.size() - 2;
|
||||||
|
|
||||||
|
// calculate poylgon normal, tangent and bitangent
|
||||||
|
QVector3D vec1, vec2, norm, tan, bi;
|
||||||
|
QVector2D uv1, uv2;
|
||||||
|
float f;
|
||||||
|
|
||||||
|
vec1 = new_segment->vertices[tmp_buffer[0]].position - new_segment->vertices[tmp_buffer[1]].position;
|
||||||
|
vec2 = new_segment->vertices[tmp_buffer[0]].position - new_segment->vertices[tmp_buffer[2]].position;
|
||||||
|
uv1 = new_segment->vertices[tmp_buffer[0]].texCoord - new_segment->vertices[tmp_buffer[1]].texCoord;
|
||||||
|
uv2 = new_segment->vertices[tmp_buffer[0]].texCoord - new_segment->vertices[tmp_buffer[2]].texCoord;
|
||||||
|
f = 1.0f / (uv1.x() * uv2.y() - uv2.x() * uv1.y());
|
||||||
|
|
||||||
|
norm = QVector3D::crossProduct(vec1, vec2).normalized();
|
||||||
|
|
||||||
|
tan.setX(f * (uv2.y() * vec1.x() - uv1.y() * vec2.x()));
|
||||||
|
tan.setY(f * (uv2.y() * vec1.y() - uv1.y() * vec2.y()));
|
||||||
|
tan.setZ(f * (uv2.y() * vec1.z() - uv1.y() * vec2.z()));
|
||||||
|
tan.normalize();
|
||||||
|
|
||||||
|
bi.setX(f * (-uv2.x() * vec1.x() + uv1.x() * vec2.x()));
|
||||||
|
bi.setY(f * (-uv2.x() * vec1.y() + uv1.x() * vec2.y()));
|
||||||
|
bi.setZ(f * (-uv2.x() * vec1.z() + uv1.x() * vec2.z()));
|
||||||
|
bi.normalize();
|
||||||
|
|
||||||
// for every triangle of the multi polygon..
|
// for every triangle of the multi polygon..
|
||||||
for (unsigned int tri = 0; tri < tmp_multiPolySize - 2; tri++)
|
for (unsigned int tri = 0; tri < tmp_multiPolySize - 2; tri++)
|
||||||
|
{
|
||||||
// ..calculate the edge indices
|
// ..calculate the edge indices
|
||||||
for (int triEdge = 0; triEdge < 3; triEdge++)
|
for (int triEdge = 0; triEdge < 3; triEdge++)
|
||||||
new_segment->indices.push_back(tmp_buffer[(tri + triEdge - ((tri % 2) * (triEdge - 1) * 2))]);
|
{
|
||||||
|
int curIndi = tmp_buffer[(tri + triEdge - ((tri % 2) * (triEdge - 1) * 2))];
|
||||||
|
|
||||||
|
// polygon normal wasn't calculated before
|
||||||
|
if (new_segment->vertices[curIndi].polygonNormal == QVector3D(0, 0, 0))
|
||||||
|
{
|
||||||
|
new_segment->vertices[curIndi].polygonNormal = norm;
|
||||||
|
new_segment->vertices[curIndi].tangent = tan;
|
||||||
|
new_segment->vertices[curIndi].bitangent = bi;
|
||||||
|
new_segment->indices.push_back(curIndi);
|
||||||
|
}
|
||||||
|
// polygon normal already calculated so duplicate the vertex
|
||||||
|
else
|
||||||
|
{
|
||||||
|
new_segment->vertices.push_back(new_segment->vertices[curIndi]);
|
||||||
|
new_segment->vertices.back().polygonNormal = norm;
|
||||||
|
new_segment->vertices.back().tangent = tan;
|
||||||
|
new_segment->vertices.back().bitangent = bi;
|
||||||
|
new_segment->indices.push_back(new_segment->vertices.size() - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
tmp_buffer.remove(0, tmp_multiPolySize);
|
tmp_buffer.remove(0, tmp_multiPolySize);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -593,17 +682,107 @@ void MshFile::analyseSegmChunks(Model * dataDestination, QList<ChunkHeader*>& ch
|
|||||||
// save the last polygon (no 2 high bit followed)
|
// save the last polygon (no 2 high bit followed)
|
||||||
if (tmp_buffer.size() == 3)
|
if (tmp_buffer.size() == 3)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < 3; i++)
|
// calculate poylgon normal, tangent and bitangent
|
||||||
new_segment->indices.push_back(tmp_buffer.takeFirst());
|
QVector3D vec1, vec2, norm, tan, bi;
|
||||||
|
QVector2D uv1, uv2;
|
||||||
|
float f;
|
||||||
|
|
||||||
|
vec1 = new_segment->vertices[tmp_buffer[0]].position - new_segment->vertices[tmp_buffer[1]].position;
|
||||||
|
vec2 = new_segment->vertices[tmp_buffer[0]].position - new_segment->vertices[tmp_buffer[2]].position;
|
||||||
|
uv1 = new_segment->vertices[tmp_buffer[0]].texCoord - new_segment->vertices[tmp_buffer[1]].texCoord;
|
||||||
|
uv2 = new_segment->vertices[tmp_buffer[0]].texCoord - new_segment->vertices[tmp_buffer[2]].texCoord;
|
||||||
|
f = 1.0f / (uv1.x() * uv2.y() - uv2.x() * uv1.y());
|
||||||
|
|
||||||
|
norm = QVector3D::crossProduct(vec1, vec2).normalized();
|
||||||
|
|
||||||
|
tan.setX(f * (uv2.y() * vec1.x() - uv1.y() * vec2.x()));
|
||||||
|
tan.setY(f * (uv2.y() * vec1.y() - uv1.y() * vec2.y()));
|
||||||
|
tan.setZ(f * (uv2.y() * vec1.z() - uv1.y() * vec2.z()));
|
||||||
|
tan.normalize();
|
||||||
|
|
||||||
|
bi.setX(f * (-uv2.x() * vec1.x() + uv1.x() * vec2.x()));
|
||||||
|
bi.setY(f * (-uv2.x() * vec1.y() + uv1.x() * vec2.y()));
|
||||||
|
bi.setZ(f * (-uv2.x() * vec1.z() + uv1.x() * vec2.z()));
|
||||||
|
bi.normalize();
|
||||||
|
|
||||||
|
for (int k = 0; k < 3; k++)
|
||||||
|
{
|
||||||
|
//TODO: buffer size == 1; k = 2;
|
||||||
|
// polygon normal wasn't calculated before
|
||||||
|
if (new_segment->vertices[tmp_buffer[k]].polygonNormal == QVector3D(0, 0, 0))
|
||||||
|
{
|
||||||
|
new_segment->vertices[tmp_buffer[k]].polygonNormal = norm;
|
||||||
|
new_segment->vertices[tmp_buffer[k]].tangent = tan;
|
||||||
|
new_segment->vertices[tmp_buffer[k]].bitangent = bi;
|
||||||
|
|
||||||
|
new_segment->indices.push_back(tmp_buffer[k]);
|
||||||
|
}
|
||||||
|
// polygon normal already calculated so duplicate the vertex
|
||||||
|
else
|
||||||
|
{
|
||||||
|
new_segment->vertices.push_back(new_segment->vertices[tmp_buffer[k]]);
|
||||||
|
new_segment->vertices.back().polygonNormal = norm;
|
||||||
|
new_segment->vertices.back().tangent = tan;
|
||||||
|
new_segment->vertices.back().bitangent = bi;
|
||||||
|
new_segment->indices.push_back(new_segment->vertices.size() - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tmp_buffer.remove(0, 3);
|
||||||
}
|
}
|
||||||
else if (tmp_buffer.size() > 3)
|
else if (tmp_buffer.size() > 3)
|
||||||
{
|
{
|
||||||
unsigned int tmp_multiPolySize = tmp_buffer.size();
|
unsigned int tmp_multiPolySize = tmp_buffer.size();
|
||||||
|
|
||||||
|
// calculate poylgon normal, tangent and bitangent
|
||||||
|
QVector3D vec1, vec2, norm, tan, bi;
|
||||||
|
QVector2D uv1, uv2;
|
||||||
|
float f;
|
||||||
|
|
||||||
|
vec1 = new_segment->vertices[tmp_buffer[0]].position - new_segment->vertices[tmp_buffer[1]].position;
|
||||||
|
vec2 = new_segment->vertices[tmp_buffer[0]].position - new_segment->vertices[tmp_buffer[2]].position;
|
||||||
|
uv1 = new_segment->vertices[tmp_buffer[0]].texCoord - new_segment->vertices[tmp_buffer[1]].texCoord;
|
||||||
|
uv2 = new_segment->vertices[tmp_buffer[0]].texCoord - new_segment->vertices[tmp_buffer[2]].texCoord;
|
||||||
|
f = 1.0f / (uv1.x() * uv2.y() - uv2.x() * uv1.y());
|
||||||
|
|
||||||
|
norm = QVector3D::crossProduct(vec1, vec2).normalized();
|
||||||
|
|
||||||
|
tan.setX(f * (uv2.y() * vec1.x() - uv1.y() * vec2.x()));
|
||||||
|
tan.setY(f * (uv2.y() * vec1.y() - uv1.y() * vec2.y()));
|
||||||
|
tan.setZ(f * (uv2.y() * vec1.z() - uv1.y() * vec2.z()));
|
||||||
|
tan.normalize();
|
||||||
|
|
||||||
|
bi.setX(f * (-uv2.x() * vec1.x() + uv1.x() * vec2.x()));
|
||||||
|
bi.setY(f * (-uv2.x() * vec1.y() + uv1.x() * vec2.y()));
|
||||||
|
bi.setZ(f * (-uv2.x() * vec1.z() + uv1.x() * vec2.z()));
|
||||||
|
bi.normalize();
|
||||||
|
|
||||||
// for every triangle of the multi polygon..
|
// for every triangle of the multi polygon..
|
||||||
for (unsigned int tri = 0; tri < tmp_multiPolySize - 2; tri++)
|
for (unsigned int tri = 0; tri < tmp_multiPolySize - 2; tri++)
|
||||||
|
{
|
||||||
// ..calculate the edge indices
|
// ..calculate the edge indices
|
||||||
for (int triEdge = 0; triEdge < 3; triEdge++)
|
for (int triEdge = 0; triEdge < 3; triEdge++)
|
||||||
new_segment->indices.push_back(tmp_buffer[(tri + triEdge - ((tri % 2) * (triEdge - 1) * 2))]);
|
{
|
||||||
|
int curIndi = tmp_buffer[(tri + triEdge - ((tri % 2) * (triEdge - 1) * 2))];
|
||||||
|
|
||||||
|
// polygon normal wasn't calculated before
|
||||||
|
if (new_segment->vertices[curIndi].polygonNormal == QVector3D(0, 0, 0))
|
||||||
|
{
|
||||||
|
new_segment->vertices[curIndi].polygonNormal = norm;
|
||||||
|
new_segment->vertices[curIndi].tangent = tan;
|
||||||
|
new_segment->vertices[curIndi].bitangent = bi;
|
||||||
|
new_segment->indices.push_back(curIndi);
|
||||||
|
}
|
||||||
|
// polygon normal already calculated so duplicate the vertex
|
||||||
|
else
|
||||||
|
{
|
||||||
|
new_segment->vertices.push_back(new_segment->vertices[curIndi]);
|
||||||
|
new_segment->vertices.back().polygonNormal = norm;
|
||||||
|
new_segment->vertices.back().tangent = tan;
|
||||||
|
new_segment->vertices.back().bitangent = bi;
|
||||||
|
new_segment->indices.push_back(new_segment->vertices.size() - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -662,12 +841,26 @@ void MshFile::analyseClthChunks(Model * dataDestination, QList<ChunkHeader*>& ch
|
|||||||
m_file.read(F2V(tmp_size), sizeof(tmp_size));
|
m_file.read(F2V(tmp_size), sizeof(tmp_size));
|
||||||
|
|
||||||
// for every triangle..
|
// for every triangle..
|
||||||
for (unsigned int i = 0; i < tmp_size * 3; i++)
|
for (unsigned int i = 0; i < tmp_size; i++)
|
||||||
{
|
{
|
||||||
quint32 tmp_value;
|
quint32 tmp_value[3];
|
||||||
m_file.read(F2V(tmp_value), sizeof(quint32));
|
for (unsigned int j = 0; j < 3; j++)
|
||||||
|
{
|
||||||
|
m_file.read(F2V(tmp_value[j]), sizeof(quint32));
|
||||||
|
new_segment->indices.push_back((GLuint)tmp_value[j]);
|
||||||
|
}
|
||||||
|
|
||||||
new_segment->indices.push_back((GLuint)tmp_value);
|
QVector3D vec1, vec2, norm;
|
||||||
|
|
||||||
|
vec1 = new_segment->vertices[new_segment->indices[i * 3]].position - new_segment->vertices[new_segment->indices[i * 3 + 1]].position;
|
||||||
|
vec2 = new_segment->vertices[new_segment->indices[i * 3]].position - new_segment->vertices[new_segment->indices[i * 3 + 2]].position;
|
||||||
|
norm = QVector3D::crossProduct(vec1, vec2);
|
||||||
|
|
||||||
|
for (int k = 0; k < 3; k++)
|
||||||
|
{
|
||||||
|
new_segment->vertices[new_segment->indices[i * 3 + k]].vertexNormal += norm;
|
||||||
|
new_segment->vertices[new_segment->indices[i * 3 + k]].vertexNormal.normalize();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -728,7 +921,7 @@ void MshFile::loadTexture(QOpenGLTexture *& destination, QString filepath, QStri
|
|||||||
if (!loadSuccess)
|
if (!loadSuccess)
|
||||||
{
|
{
|
||||||
OutputDevice::getInstance()->print("WARNING: texture not found or corrupted: " + filename, 1);
|
OutputDevice::getInstance()->print("WARNING: texture not found or corrupted: " + filename, 1);
|
||||||
|
//TODO: use the correct diffuse color or return with null
|
||||||
img = QImage(1, 1, QImage::Format_RGB32);
|
img = QImage(1, 1, QImage::Format_RGB32);
|
||||||
img.fill(QColor(m_materials->back().diffuseColor[0] * 255, m_materials->back().diffuseColor[1] * 255, m_materials->back().diffuseColor[2] * 255));
|
img.fill(QColor(m_materials->back().diffuseColor[0] * 255, m_materials->back().diffuseColor[1] * 255, m_materials->back().diffuseColor[2] * 255));
|
||||||
filename += " *";
|
filename += " *";
|
||||||
|
@@ -143,10 +143,10 @@ void OglViewerWidget::paintGL()
|
|||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
// Set view-projection matrix
|
// Set view-projection matrix
|
||||||
m_program.setUniformValue("vp_matrix", m_projection * m_camera->getMatrix());
|
m_program.setUniformValue("viewProjection", m_projection * m_camera->getMatrix());
|
||||||
|
|
||||||
// Set Light values
|
// Set Light values
|
||||||
m_program.setUniformValue("b_light", m_lightOn);
|
m_program.setUniformValue("useLight", m_lightOn);
|
||||||
m_program.setUniformValue("light.position", m_light.position);
|
m_program.setUniformValue("light.position", m_light.position);
|
||||||
m_program.setUniformValue("light.intensities", m_light.intensities);
|
m_program.setUniformValue("light.intensities", m_light.intensities);
|
||||||
m_program.setUniformValue("light.attenuationFactor", m_light.attenuationFactor);
|
m_program.setUniformValue("light.attenuationFactor", m_light.attenuationFactor);
|
||||||
|
Binary file not shown.
BIN
preview.jpg
Normal file
BIN
preview.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 50 KiB |
Reference in New Issue
Block a user