Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
C-Fu
OpenGL
Commits
06d403d5
Commit
06d403d5
authored
Feb 06, 2017
by
Anakin
Browse files
support normal map now,
support "glow" now, update preview.jpg
parent
541a9756
Changes
6
Hide whitespace changes
Inline
Side-by-side
QtMeshViewer/Resources/fshader.glsl
View file @
06d403d5
#version 450
#ifdef GL_ES
// Set default precision to medium
precision
mediump
int
;
precision
mediump
float
;
#endif
uniform
mat3
n
_m
atrix
;
uniform
mat3
n
ormalM
atrix
;
uniform
vec3
cameraPosition
;
uniform
sampler2D
texture
;
uniform
sampler2D
secondTexture
;
uniform
float
materialShininess
;
uniform
vec3
materialSpecularColor
;
uniform
sampler2D
tx0
;
uniform
sampler2D
tx1
;
uniform
bool
b_transparent
;
uniform
bool
b_specular
;
uniform
bool
b_normalmap
;
uniform
bool
b_light
;
uniform
struct
Material
{
float
shininess
;
vec3
specularColor
;
bool
isTransparent
;
bool
hasSpecularmap
;
bool
hasNormalmap
;
bool
isGlow
;
}
material
;
uniform
bool
useLight
;
uniform
struct
Light
{
vec4
position
;
...
...
@@ -24,82 +29,99 @@ uniform struct Light {
float
ambientCoefficient
;
}
light
;
attribute
vec3
a_polyNorm
;
attribute
vec3
a_polyTan
;
attribute
vec3
a_polyBiTan
;
varying
vec2
v_surfaceUV
;
varying
vec3
v_surfacePosition
;
varying
vec3
v_surfaceNormal
;
varying
vec3
v_polyNorm
;
varying
vec3
v_polyTan
;
varying
vec3
v_polyBiTan
;
void
main
()
{
if
(
b_light
)
if
(
useLight
&&
!
material
.
isGlow
)
{
// some values
mat3
tbn
=
transpose
(
mat3
(
a_polyTan
,
a_polyBiTan
,
a_polyNorm
));
// get the color and undo gamma correction
vec4
surfaceColor
=
vec4
(
texture2D
(
tx0
,
v_surfaceUV
));
surfaceColor
.
rgb
=
pow
(
surfaceColor
.
rgb
,
vec3
(
2
.
2
));
vec3
finalNormal
=
normalize
(
n_matrix
*
v_surfaceNormal
);
// attenutation depending on the distance to the light
float
distanceToLight
=
length
(
light
.
position
.
xyz
-
v_surfacePosition
);
float
attenuation
=
1
.
0
/
(
1
.
0
+
light
.
attenuationFactor
*
pow
(
distanceToLight
,
2
));
// if(b_normalmap)
// {
// finalNormal = texture2D(secondTexture, v_surfaceUV).rgb;
// finalNormal = normalize(finalNormal * 2.0 -1.0)
// }
// normal vector
vec3
normal
=
normalize
(
normalMatrix
*
v_surfaceNormal
);
vec4
surfaceColor
=
vec4
(
texture2D
(
texture
,
v_surfaceUV
));
surfaceColor
.
rgb
=
pow
(
surfaceColor
.
rgb
,
vec3
(
2
.
2
));
// direction from surface to light depending on the light type
vec3
surfaceToLight
;
float
attenuation
;
// directional light
if
(
light
.
position
.
w
==
0
.
0
f
)
{
if
(
light
.
position
.
w
==
0
.
0
)
// directional light
surfaceToLight
=
normalize
(
light
.
position
.
xyz
);
}
// point light
else
{
else
// point light
surfaceToLight
=
normalize
(
light
.
position
.
xyz
-
v_surfacePosition
);
}
float
distanceToLight
=
length
(
light
.
position
.
xyz
-
v_surfacePosition
);
attenuation
=
1
.
0
/
(
1
.
0
+
light
.
attenuationFactor
*
pow
(
distanceToLight
,
2
));
// direction from surface to camera
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
;
// diffuse
float
diffuseCoefficient
=
max
(
0
.
0
,
dot
(
finalNormal
,
surfaceToLight
));
/////////////////////////////////////////////////////////////////////////////////////
// diffuse component
float
diffuseCoefficient
=
max
(
0
.
0
,
dot
(
normal
,
surfaceToLight
));
vec3
diffuse
=
diffuseCoefficient
*
surfaceColor
.
rgb
*
light
.
intensities
;
// specular
/////////////////////////////////////////////////////////////////////////////////////
// specular component
float
specularCoefficient
=
0
.
0
;
if
(
diffuseCoefficient
>
0
.
0
)
specularCoefficient
=
pow
(
max
(
0
.
0
,
dot
(
surfaceToCamera
,
reflect
(
-
surfaceToLight
,
finalNormal
))),
materialShininess
);
vec3
specColor
;
if
(
b_specular
)
specColor
=
vec3
(
surfaceColor
.
a
);
else
specColor
=
materialSpecularColor
;
specularCoefficient
=
pow
(
max
(
0
.
0
,
dot
(
surfaceToCamera
,
reflect
(
-
surfaceToLight
,
normal
))),
material
.
shininess
);
float
specularWeight
=
1
;
if
(
material
.
hasSpecularmap
)
specularWeight
=
surfaceColor
.
a
;
vec3
specColor
=
specularWeight
*
material
.
specularColor
;
vec3
specular
=
specularCoefficient
*
specColor
*
light
.
intensities
;
// linear color before gamma correction)
/////////////////////////////////////////////////////////////////////////////////////
// linear color before gamma correction
vec3
linearColor
=
ambient
+
attenuation
*
(
diffuse
+
specular
);
// final color after gama correction
/////////////////////////////////////////////////////////////////////////////////////
// gama correction
vec3
gamma
=
vec3
(
1
.
0
/
2
.
2
);
if
(
!
b_transparent
)
surfaceColor
.
a
=
1
.
0
f
;
if
(
!
material
.
isTransparent
)
surfaceColor
.
a
=
1
.
0
;
gl_FragColor
=
vec4
(
pow
(
linearColor
,
gamma
),
surfaceColor
.
a
);
}
// don't use light
else
{
vec4
surfaceColor
=
vec4
(
texture2D
(
texture
,
v_surfaceUV
));
if
(
!
b_transparent
)
surfaceColor
.
a
=
1
.
0
f
;
vec4
surfaceColor
=
vec4
(
texture2D
(
tx0
,
v_surfaceUV
));
if
(
!
material
.
isTransparent
)
surfaceColor
.
a
=
1
.
0
;
gl_FragColor
=
surfaceColor
;
}
...
...
QtMeshViewer/Resources/vshader.glsl
View file @
06d403d5
#version 450
#ifdef GL_ES
// Set default precision to medium
precision
mediump
int
;
precision
mediump
float
;
#endif
uniform
mat4
v
p_matrix
;
uniform
mat4
norm
_matrix
;
uniform
mat4
m
_m
atrix
;
uniform
mat4
v
iewProjection
;
uniform
mat4
norm
alizeModel
;
uniform
mat4
m
odelM
atrix
;
attribute
vec4
a_position
;
attribute
vec2
a_texcoord
;
attribute
vec3
a_normal
;
attribute
vec3
a_polyNorm
;
attribute
vec3
a_polyTan
;
attribute
vec3
a_polyBiTan
;
varying
vec2
v_surfaceUV
;
varying
vec3
v_surfacePosition
;
varying
vec3
v_surfaceNormal
;
varying
vec3
v_polyNorm
;
varying
vec3
v_polyTan
;
varying
vec3
v_polyBiTan
;
void
main
()
{
// Calculate vertex position in screen space
gl_Position
=
v
p_matrix
*
norm_matrix
*
m_m
atrix
*
a_position
;
gl_Position
=
v
iewProjection
*
normalizeModel
*
modelM
atrix
*
a_position
;
// Pass data to fragment shader
// Value will be automatically interpolated to fragments inside polygon faces
v_surfaceUV
=
a_texcoord
;
v_surfacePosition
=
vec3
(
norm
_matrix
*
m_m
atrix
*
a_position
);
v_surfacePosition
=
vec3
(
norm
alizeModel
*
modelM
atrix
*
a_position
);
v_surfaceNormal
=
a_normal
;
v_polyNorm
=
a_polyNorm
;
v_polyTan
=
a_polyTan
;
v_polyBiTan
=
a_polyBiTan
;
}
QtMeshViewer/Source/GeometryEngine.cpp
View file @
06d403d5
...
...
@@ -5,7 +5,6 @@
#include
"..\Header\OutputDevice.h"
#include
<QRegExp>
#include
"..\Header\Profiler.h"
/////////////////////////////////////////////////////////////////////////
// constructor/destructor
...
...
@@ -119,11 +118,11 @@ void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
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
);
program
->
setUniformValue
(
"norm
alizeModel
"
,
normMatrix
);
// Allways use texture unit 0 and 1
program
->
setUniformValue
(
"t
exture
"
,
0
);
program
->
setUniformValue
(
"
secondTexture
"
,
1
);
program
->
setUniformValue
(
"t
x0
"
,
0
);
program
->
setUniformValue
(
"
tx1
"
,
1
);
//setup the pipeline
setupPipeline
(
program
);
...
...
@@ -135,6 +134,7 @@ void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
bool
tmp_transparent
(
false
);
bool
tmp_specular
(
false
);
bool
tmp_normalmap
(
false
);
bool
tmp_glow
(
false
);
float
shininess
(
0.0
);
QVector3D
specularColor
;
...
...
@@ -155,6 +155,9 @@ void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
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
{
...
...
@@ -163,19 +166,18 @@ void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
}
// Set model matrix
program
->
setUniformValue
(
"m
_m
atrix"
,
it
.
modelMatrix
);
program
->
setUniformValue
(
"m
odelM
atrix"
,
it
.
modelMatrix
);
// Set normal matrix
program
->
setUniformValue
(
"n_matrix"
,
(
normMatrix
*
it
.
modelMatrix
).
normalMatrix
());
// set some more values
program
->
setUniformValue
(
"b_transparent"
,
tmp_transparent
);
program
->
setUniformValue
(
"b_specular"
,
tmp_specular
);
program
->
setUniformValue
(
"b_normalmap"
,
tmp_normalmap
);
program
->
setUniformValue
(
"normalMatrix"
,
(
normMatrix
*
it
.
modelMatrix
).
normalMatrix
());
// set some material attributes
program
->
setUniformValue
(
"materialShininess"
,
shininess
);
program
->
setUniformValue
(
"materialSpecularColor"
,
specularColor
);
program
->
setUniformValue
(
"material.shininess"
,
shininess
);
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
glDrawElements
(
GL_TRIANGLES
,
it
.
size
,
GL_UNSIGNED_INT
,
(
void
*
)(
it
.
offset
*
sizeof
(
GLuint
)));
...
...
@@ -185,7 +187,6 @@ void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
void
GeometryEngine
::
loadFile
(
QString
filePath
)
{
TIC
(
"Start"
);
// cleanup old stuff and recreate buffers
clearData
();
m_arrayBuf
.
create
();
...
...
@@ -260,7 +261,5 @@ void GeometryEngine::loadFile(QString filePath)
clearData
();
OutputDevice
::
getInstance
()
->
print
(
QString
(
e
.
what
()),
2
);
}
TOC
(
"End"
);
}
QtMeshViewer/Source/MshFile.cpp
View file @
06d403d5
...
...
@@ -921,7 +921,7 @@ void MshFile::loadTexture(QOpenGLTexture *& destination, QString filepath, QStri
if
(
!
loadSuccess
)
{
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
.
fill
(
QColor
(
m_materials
->
back
().
diffuseColor
[
0
]
*
255
,
m_materials
->
back
().
diffuseColor
[
1
]
*
255
,
m_materials
->
back
().
diffuseColor
[
2
]
*
255
));
filename
+=
" *"
;
...
...
QtMeshViewer/Source/OglViewerWidget.cpp
View file @
06d403d5
...
...
@@ -143,10 +143,10 @@ void OglViewerWidget::paintGL()
glClear
(
GL_COLOR_BUFFER_BIT
|
GL_DEPTH_BUFFER_BIT
);
// Set view-projection matrix
m_program
.
setUniformValue
(
"v
p_matrix
"
,
m_projection
*
m_camera
->
getMatrix
());
m_program
.
setUniformValue
(
"v
iewProjection
"
,
m_projection
*
m_camera
->
getMatrix
());
// Set Light values
m_program
.
setUniformValue
(
"
b_l
ight"
,
m_lightOn
);
m_program
.
setUniformValue
(
"
useL
ight"
,
m_lightOn
);
m_program
.
setUniformValue
(
"light.position"
,
m_light
.
position
);
m_program
.
setUniformValue
(
"light.intensities"
,
m_light
.
intensities
);
m_program
.
setUniformValue
(
"light.attenuationFactor"
,
m_light
.
attenuationFactor
);
...
...
preview.jpg
View replaced file @
541a9756
View file @
06d403d5
43.4 KB
|
W:
|
H:
49.6 KB
|
W:
|
H:
2-up
Swipe
Onion skin
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment