Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
GraPa
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kai Westerkamp
GraPa
Commits
63e1d174
Commit
63e1d174
authored
Jan 31, 2016
by
Kai Westerkamp
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Zwischenstand
parent
10e12c16
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
208 additions
and
121 deletions
+208
-121
animate.vert
A5/Animation/animate.vert
+11
-5
mainwidget.cpp
A5/Animation/mainwidget.cpp
+6
-5
mesh.cpp
A5/Animation/mesh.cpp
+186
-110
mesh.h
A5/Animation/mesh.h
+4
-1
texture.cpp
A5/Animation/texture.cpp
+1
-0
No files found.
A5/Animation/animate.vert
View file @
63e1d174
...
@@ -24,18 +24,24 @@ void main(void)
...
@@ -24,18 +24,24 @@ void main(void)
debugout
=
Weights
;
debugout
=
Weights
;
//debugout = vec4(1);
//debugout = vec4(1);
mat4x4
BoneTransform
=
Bones
[
BoneIDs
[
0
]]
*
Weights
[
0
];
mat4x4
BoneTransform
=
mat4x4
(
0
.
0
);
BoneTransform
+=
Bones
[
BoneIDs
[
1
]]
*
Weights
[
1
];
BoneTransform
+=
Bones
[
BoneIDs
[
2
]]
*
Weights
[
2
];
for
(
int
i
=
0
;
i
<
4
;
i
++
){
BoneTransform
+=
Bones
[
BoneIDs
[
3
]]
*
Weights
[
3
];
BoneTransform
+=
Bones
[
BoneIDs
[
i
]]
*
Weights
[
i
];
}
if
(
Weights
[
0
]
==
0
.
0
){
BoneTransform
=
mat4x4
(
1
.
0
);
}
vec4
Pos
=
BoneTransform
*
vec4
(
Position
,
1
.
0
);
vec4
Pos
=
BoneTransform
*
vec4
(
Position
,
1
.
0
);
vec4
Nor
=
BoneTransform
*
vec4
(
Normal
,
0
.
0
);
//Pos = vec4(Position,1.0);
//Pos = vec4(Position,1.0);
vCamPosition
=
vec4
(
MV
*
Pos
).
xyz
;
vCamPosition
=
vec4
(
MV
*
Pos
).
xyz
;
vCamNormal
=
N
*
Normal
;
vCamNormal
=
N
*
(
Nor
.
xyz
)
;
vUV
=
UV
;
vUV
=
UV
;
...
...
A5/Animation/mainwidget.cpp
View file @
63e1d174
...
@@ -44,7 +44,7 @@ void MainWidget::initializeGL(){
...
@@ -44,7 +44,7 @@ void MainWidget::initializeGL(){
// Shader
// Shader
animationShader
=
initShader
(
QLatin1String
(
":/animate.frag"
),
QLatin1String
(
":/animate.vert"
));
animationShader
=
initShader
(
QLatin1String
(
":/animate.frag"
),
QLatin1String
(
":/animate.vert"
));
camDistance
=
3
0.0
;
camDistance
=
10
0.0
;
m_view
=
QMatrix4x4
();
m_view
=
QMatrix4x4
();
m_view
.
lookAt
(
QVector3D
(
0.0
,
camDistance
,
camDistance
),
QVector3D
(
0.0
,
0.0
,
0.0
),
QVector3D
(
0.0
,
1.0
,
0.0
));
m_view
.
lookAt
(
QVector3D
(
0.0
,
camDistance
,
camDistance
),
QVector3D
(
0.0
,
0.0
,
0.0
),
QVector3D
(
0.0
,
1.0
,
0.0
));
...
@@ -54,7 +54,7 @@ void MainWidget::initializeGL(){
...
@@ -54,7 +54,7 @@ void MainWidget::initializeGL(){
void
MainWidget
::
loadNewMesh
(){
void
MainWidget
::
loadNewMesh
(){
QString
fn
=
QFileDialog
::
getOpenFileName
(
NULL
,
tr
(
"Open Mesh..."
),
QString
fn
=
QFileDialog
::
getOpenFileName
(
NULL
,
tr
(
"Open Mesh..."
),
QString
(
"D:
\\
Projekte
\\
GraPa
\\
A5
\\
Models"
),
QString
(
"D:
\\
Projekte
\\
GraPa
\\
A5
\\
Models"
),
tr
(
"*.md5mesh *.3ds *.md2 *.obj"
));
tr
(
"*.md5mesh *.3ds *.md2 *.obj
*.dae *.dxf
"
));
if
(
fn
.
isEmpty
())
if
(
fn
.
isEmpty
())
return
;
return
;
...
@@ -98,15 +98,16 @@ void MainWidget::paintGL(){
...
@@ -98,15 +98,16 @@ void MainWidget::paintGL(){
QMatrix4x4
rot
;
QMatrix4x4
rot
;
int
time
=
QTime
::
currentTime
().
second
()
*
1000
+
QTime
::
currentTime
().
msec
();
int
time
=
QTime
::
currentTime
().
second
()
*
1000
+
QTime
::
currentTime
().
msec
();
rot
.
rotate
(
time
/
100.0
*
36
/
5
,
QVector3D
(
0
,
1
,
0
));
//
rot.rotate(time/100.0*36/5,QVector3D(0,1,0));
animationShader
->
bind
();
animationShader
->
bind
();
animationShader
->
setUniformValue
(
"colorTexture"
,
0
);
animationShader
->
setUniformValue
(
"colorTexture"
,
0
);
animationShader
->
setUniformValue
(
"LightPos"
,
QVector3D
(
0
,
100
,
100
));
animationShader
->
setUniformValue
(
"LightPos"
,
QVector3D
(
0
,
100
,
100
));
time
=
startTime
.
msecsTo
(
QTime
::
currentTime
())
/
1000.0
;
float
f
time
=
startTime
.
msecsTo
(
QTime
::
currentTime
())
/
1000.0
;
mesh
->
render
(
animationShader
,
m_view
*
rot
,
m_projection
,(
float
)
time
);
mesh
->
render
(
animationShader
,
m_view
*
rot
,
m_projection
,(
float
)
ftime
);
animationShader
->
release
();
animationShader
->
release
();
update
();
update
();
...
...
A5/Animation/mesh.cpp
View file @
63e1d174
...
@@ -12,6 +12,7 @@ void Vertex::AddBoneData(uint BoneID, float Weight){
...
@@ -12,6 +12,7 @@ void Vertex::AddBoneData(uint BoneID, float Weight){
}
}
qCritical
()
<<
"More tahn 4 Bones at Vertex"
<<
pos
<<
BoneID
<<
Weight
<<
"added:"
qCritical
()
<<
"More tahn 4 Bones at Vertex"
<<
pos
<<
BoneID
<<
Weight
<<
"added:"
<<
IDs
[
0
]
<<
weights
[
0
]
<<
IDs
[
1
]
<<
weights
[
1
]
<<
IDs
[
2
]
<<
weights
[
2
]
<<
IDs
[
3
]
<<
weights
[
3
];
<<
IDs
[
0
]
<<
weights
[
0
]
<<
IDs
[
1
]
<<
weights
[
1
]
<<
IDs
[
2
]
<<
weights
[
2
]
<<
IDs
[
3
]
<<
weights
[
3
];
}
}
Mesh
::
MeshEntry
::
MeshEntry
()
Mesh
::
MeshEntry
::
MeshEntry
()
...
@@ -61,8 +62,6 @@ Mesh::Node::Node()
...
@@ -61,8 +62,6 @@ Mesh::Node::Node()
children
.
clear
();
children
.
clear
();
}
}
Mesh
::
Mesh
(
QOpenGLFunctions_4_3_Core
*
f
,
QString
fileName
)
Mesh
::
Mesh
(
QOpenGLFunctions_4_3_Core
*
f
,
QString
fileName
)
{
{
loaded
=
false
;
loaded
=
false
;
...
@@ -138,6 +137,20 @@ Mesh::Mesh(QOpenGLFunctions_4_3_Core *f,QString fileName)
...
@@ -138,6 +137,20 @@ Mesh::Mesh(QOpenGLFunctions_4_3_Core *f,QString fileName)
globalInverseTransform
=
rootNode
.
transformation
.
inverted
();
globalInverseTransform
=
rootNode
.
transformation
.
inverted
();
//Test Interpolation:
if
(
false
){
const
aiAnimation
*
animation
=
scene
->
mAnimations
[
0
];
const
aiNodeAnim
*
nodeAnimation
=
findAnimOfNode
(
animation
,
"upperarm.R"
);
qDebug
()
<<
"KeyFrames"
;
for
(
uint
i
=
0
;
i
<
nodeAnimation
->
mNumPositionKeys
;
i
++
){
qDebug
()
<<
nodeAnimation
->
mPositionKeys
[
i
].
mTime
<<
convert
(
nodeAnimation
->
mPositionKeys
[
i
].
mValue
);
}
qDebug
()
<<
"Interpolated"
;
for
(
float
f
=
0.0
;
f
<
(
float
)
animation
->
mDuration
;
f
+=
0.5
f
){
qDebug
()
<<
f
<<
calcInterpolatedTranslation
(
f
,
nodeAnimation
);
}
}
//Todo read skeleton
//Todo read skeleton
...
@@ -145,6 +158,12 @@ Mesh::Mesh(QOpenGLFunctions_4_3_Core *f,QString fileName)
...
@@ -145,6 +158,12 @@ Mesh::Mesh(QOpenGLFunctions_4_3_Core *f,QString fileName)
}
}
}
}
Mesh
::~
Mesh
()
{
entries
.
clear
();
materials
.
clear
();
}
void
Mesh
::
initNode
(
const
aiScene
*
scene
,
aiNode
*
node
,
Node
&
newNode
,
QString
debugoffset
){
void
Mesh
::
initNode
(
const
aiScene
*
scene
,
aiNode
*
node
,
Node
&
newNode
,
QString
debugoffset
){
newNode
.
name
=
node
->
mName
.
length
!=
0
?
node
->
mName
.
C_Str
()
:
""
;
newNode
.
name
=
node
->
mName
.
length
!=
0
?
node
->
mName
.
C_Str
()
:
""
;
newNode
.
transformation
=
QMatrix4x4
(
node
->
mTransformation
[
0
]);
newNode
.
transformation
=
QMatrix4x4
(
node
->
mTransformation
[
0
]);
...
@@ -173,7 +192,6 @@ void Mesh::initNode(const aiScene *scene, aiNode *node, Node &newNode,QString de
...
@@ -173,7 +192,6 @@ void Mesh::initNode(const aiScene *scene, aiNode *node, Node &newNode,QString de
}
}
void
Mesh
::
initMaterial
(
QString
dir
,
unsigned
int
i
,
const
aiMaterial
*
material
)
void
Mesh
::
initMaterial
(
QString
dir
,
unsigned
int
i
,
const
aiMaterial
*
material
)
{
{
aiString
mname
;
aiString
mname
;
...
@@ -203,7 +221,9 @@ void Mesh::initMaterial(QString dir, unsigned int i, const aiMaterial* material)
...
@@ -203,7 +221,9 @@ void Mesh::initMaterial(QString dir, unsigned int i, const aiMaterial* material)
aiString
Path
;
aiString
Path
;
if
(
material
->
GetTexture
(
aiTextureType_DIFFUSE
,
0
,
&
Path
,
NULL
,
NULL
,
NULL
,
NULL
,
NULL
)
==
AI_SUCCESS
)
{
if
(
material
->
GetTexture
(
aiTextureType_DIFFUSE
,
0
,
&
Path
,
NULL
,
NULL
,
NULL
,
NULL
,
NULL
)
==
AI_SUCCESS
)
{
std
::
string
FullPath
=
dir
.
toStdString
()
+
"/"
+
Path
.
data
;
QString
temp
=
QString
(
Path
.
data
);
temp
=
temp
.
split
(
"
\\
"
).
last
();
std
::
string
FullPath
=
dir
.
toStdString
()
+
"/"
+
temp
.
toStdString
();
materials
[
i
].
texture
.
Load
(
GL_TEXTURE_2D
,
QString
(
FullPath
.
c_str
()));
materials
[
i
].
texture
.
Load
(
GL_TEXTURE_2D
,
QString
(
FullPath
.
c_str
()));
materials
[
i
].
hasTexture
=
true
;
materials
[
i
].
hasTexture
=
true
;
}
else
{
}
else
{
...
@@ -280,8 +300,95 @@ void Mesh::initMeshEntry(int index, aiMesh * entry){
...
@@ -280,8 +300,95 @@ void Mesh::initMeshEntry(int index, aiMesh * entry){
}
}
void
Mesh
::
render
(
QOpenGLShaderProgram
*
shader
,
QMatrix4x4
V
,
QMatrix4x4
P
,
float
TimeInSeconds
){
if
(
!
loaded
)
return
;
if
(
f
==
NULL
){
qDebug
()
<<
"f = null"
;
return
;
}
f
->
glEnableVertexAttribArray
(
positionIndex
);
f
->
glEnableVertexAttribArray
(
normalIndex
);
f
->
glEnableVertexAttribArray
(
uvIndex
);
f
->
glEnableVertexAttribArray
(
boneIndex
);
f
->
glEnableVertexAttribArray
(
boneweightIndex
);
updateBoneTransform
(
TimeInSeconds
);
QVector
<
QMatrix4x4
>
boneTransforms
=
QVector
<
QMatrix4x4
>
(
bones
.
size
());
for
(
int
i
=
0
;
i
<
bones
.
size
();
i
++
){
boneTransforms
[
i
]
=
bones
[
i
].
FinalTransformation
;
}
if
(
boneTransforms
.
size
()
==
0
){
boneTransforms
.
push_back
(
QMatrix4x4
());
}
shader
->
setUniformValueArray
(
"Bones"
,
boneTransforms
.
constData
(),
boneTransforms
.
size
());
renderNode
(
shader
,
rootNode
,
V
,
P
,
QMatrix4x4
());
f
->
glDisableVertexAttribArray
(
positionIndex
);
f
->
glDisableVertexAttribArray
(
normalIndex
);
f
->
glDisableVertexAttribArray
(
uvIndex
);
f
->
glDisableVertexAttribArray
(
boneIndex
);
f
->
glDisableVertexAttribArray
(
boneweightIndex
);
}
void
Mesh
::
renderNode
(
QOpenGLShaderProgram
*
shader
,
Node
&
node
,
QMatrix4x4
V
,
QMatrix4x4
P
,
QMatrix4x4
M
){
M
*=
node
.
transformation
;
QMatrix4x4
MV
=
V
*
M
;
QMatrix4x4
MVP
=
P
*
MV
;
QMatrix3x3
normalMat
=
MV
.
normalMatrix
();
shader
->
setUniformValue
(
"MVP"
,
MVP
);
shader
->
setUniformValue
(
"MV"
,
MV
);
shader
->
setUniformValue
(
"N"
,
normalMat
);
for
(
int
i
=
0
;
i
<
node
.
meshes
.
size
()
;
i
++
)
{
int
index
=
node
.
meshes
[
i
];
//load Material
renderMesh
(
shader
,
index
);
}
for
(
int
i
=
0
;
i
<
node
.
children
.
size
()
;
i
++
)
{
renderNode
(
shader
,
node
.
children
[
i
],
V
,
P
,
M
);
}
}
void
Mesh
::
renderMesh
(
QOpenGLShaderProgram
*
shader
,
int
index
)
{
int
MaterialIndex
=
entries
[
index
].
materialIndex
;
if
(
MaterialIndex
<
materials
.
size
())
{
shader
->
setUniformValue
(
"materialInfo.Diffuse"
,
materials
[
MaterialIndex
].
Diffuse
);
shader
->
setUniformValue
(
"materialInfo.Specular"
,
materials
[
MaterialIndex
].
Specular
);
shader
->
setUniformValue
(
"materialInfo.Shininess"
,
materials
[
MaterialIndex
].
Shininess
);
shader
->
setUniformValue
(
"materialInfo.hasTexture"
,
materials
[
MaterialIndex
].
hasTexture
);
if
(
materials
[
MaterialIndex
].
hasTexture
)
materials
[
MaterialIndex
].
texture
.
bind
(
f
,
GL_TEXTURE0
);
}
// Draw Vertex Array
f
->
glBindBuffer
(
GL_ARRAY_BUFFER
,
entries
[
index
].
VB
);
f
->
glVertexAttribPointer
(
positionIndex
,
3
,
GL_FLOAT
,
GL_FALSE
,
sizeof
(
Vertex
),
0
);
f
->
glVertexAttribPointer
(
normalIndex
,
3
,
GL_FLOAT
,
GL_FALSE
,
sizeof
(
Vertex
),
(
const
GLvoid
*
)
12
);
//12
f
->
glVertexAttribPointer
(
uvIndex
,
2
,
GL_FLOAT
,
GL_FALSE
,
sizeof
(
Vertex
),
(
const
GLvoid
*
)
24
);
// 12+12
f
->
glVertexAttribIPointer
(
boneIndex
,
4
,
GL_INT
,
sizeof
(
Vertex
),
(
const
GLvoid
*
)
32
);
// 12+12+8
f
->
glVertexAttribPointer
(
boneweightIndex
,
4
,
GL_FLOAT
,
GL_FALSE
,
sizeof
(
Vertex
),
(
const
GLvoid
*
)
48
);
//12+12+8+16
f
->
glBindBuffer
(
GL_ELEMENT_ARRAY_BUFFER
,
entries
[
index
].
IB
);
f
->
glDrawElements
(
GL_TRIANGLES
,
entries
[
index
].
numIndex
,
GL_UNSIGNED_INT
,
0
);
}
void
Mesh
::
updateBoneTransform
(
float
TimeInSeconds
){
void
Mesh
::
updateBoneTransform
(
float
TimeInSeconds
){
if
(
!
scene
)
if
(
!
scene
||
bones
.
size
()
==
0
)
return
;
return
;
const
aiAnimation
*
animation
=
scene
->
mAnimations
[
0
];
const
aiAnimation
*
animation
=
scene
->
mAnimations
[
0
];
...
@@ -297,7 +404,6 @@ void Mesh::updateBoneTransform(float TimeInSeconds){
...
@@ -297,7 +404,6 @@ void Mesh::updateBoneTransform(float TimeInSeconds){
}
}
void
Mesh
::
updateBoneHeirachy
(
float
AnimTime
,
const
aiAnimation
*
animation
,
Node
node
,
QMatrix4x4
parentTransform
){
void
Mesh
::
updateBoneHeirachy
(
float
AnimTime
,
const
aiAnimation
*
animation
,
Node
node
,
QMatrix4x4
parentTransform
){
QMatrix4x4
nodeTransformation
=
node
.
transformation
;
QMatrix4x4
nodeTransformation
=
node
.
transformation
;
...
@@ -308,6 +414,14 @@ void Mesh::updateBoneHeirachy(float AnimTime,const aiAnimation* animation, Node
...
@@ -308,6 +414,14 @@ void Mesh::updateBoneHeirachy(float AnimTime,const aiAnimation* animation, Node
//calc Tcurrent Trasnformation;
//calc Tcurrent Trasnformation;
if
(
nodeAnimation
)
{
if
(
nodeAnimation
)
{
// if(node.name.endsWith("upperarm.L")){
// qDebug()<<node.name;
// for (uint i = 0 ; i < nodeAnimation->mNumPositionKeys ; i++) {
// qDebug()<<convert(nodeAnimation->mPositionKeys[i].mValue)<<nodeAnimation->mPositionKeys[i].mTime;
// }
// }
QVector3D
scale
=
calcInterpolatedScaling
(
AnimTime
,
nodeAnimation
);
QVector3D
scale
=
calcInterpolatedScaling
(
AnimTime
,
nodeAnimation
);
QQuaternion
rot
=
calcInterpolatedRotation
(
AnimTime
,
nodeAnimation
);
QQuaternion
rot
=
calcInterpolatedRotation
(
AnimTime
,
nodeAnimation
);
...
@@ -336,7 +450,6 @@ void Mesh::updateBoneHeirachy(float AnimTime,const aiAnimation* animation, Node
...
@@ -336,7 +450,6 @@ void Mesh::updateBoneHeirachy(float AnimTime,const aiAnimation* animation, Node
}
}
const
aiNodeAnim
*
Mesh
::
findAnimOfNode
(
const
aiAnimation
*
animation
,
QString
NodeName
){
const
aiNodeAnim
*
Mesh
::
findAnimOfNode
(
const
aiAnimation
*
animation
,
QString
NodeName
){
for
(
uint
i
=
0
;
i
<
animation
->
mNumChannels
;
i
++
)
{
for
(
uint
i
=
0
;
i
<
animation
->
mNumChannels
;
i
++
)
{
const
aiNodeAnim
*
nodeanim
=
animation
->
mChannels
[
i
];
const
aiNodeAnim
*
nodeanim
=
animation
->
mChannels
[
i
];
...
@@ -350,29 +463,8 @@ const aiNodeAnim* Mesh::findAnimOfNode(const aiAnimation* animation, QString Nod
...
@@ -350,29 +463,8 @@ const aiNodeAnim* Mesh::findAnimOfNode(const aiAnimation* animation, QString Nod
}
}
QVector3D
Mesh
::
calcInterpolatedScaling
(
float
AnimTime
,
const
aiNodeAnim
*
nodeAnimation
){
if
(
nodeAnimation
->
mNumScalingKeys
==
1
){
return
convert
(
nodeAnimation
->
mScalingKeys
[
0
].
mValue
);
}
//Index find
int
index
=
0
;
for
(
uint
i
=
0
;
i
<
nodeAnimation
->
mNumScalingKeys
-
1
;
i
++
)
{
if
(
AnimTime
<
(
float
)
nodeAnimation
->
mScalingKeys
[
i
+
1
].
mTime
)
{
index
=
i
;
break
;
}
}
float
DeltaTime
=
(
float
)(
nodeAnimation
->
mScalingKeys
[
index
+
1
].
mTime
-
nodeAnimation
->
mScalingKeys
[
index
].
mTime
);
float
Factor
=
(
AnimTime
-
(
float
)
nodeAnimation
->
mScalingKeys
[
index
].
mTime
)
/
DeltaTime
;
QVector3D
start
=
convert
(
nodeAnimation
->
mScalingKeys
[
index
].
mValue
);
QVector3D
end
=
convert
(
nodeAnimation
->
mScalingKeys
[
index
+
1
].
mValue
);
return
linInterpolate
(
start
,
end
,
Factor
);
}
//Interpolarisation
QQuaternion
Mesh
::
calcInterpolatedRotation
(
float
AnimTime
,
const
aiNodeAnim
*
nodeAnimation
){
QQuaternion
Mesh
::
calcInterpolatedRotation
(
float
AnimTime
,
const
aiNodeAnim
*
nodeAnimation
){
if
(
nodeAnimation
->
mNumRotationKeys
==
1
){
if
(
nodeAnimation
->
mNumRotationKeys
==
1
){
return
convert
(
nodeAnimation
->
mRotationKeys
[
0
].
mValue
);
return
convert
(
nodeAnimation
->
mRotationKeys
[
0
].
mValue
);
...
@@ -400,118 +492,102 @@ QQuaternion Mesh::calcInterpolatedRotation(float AnimTime,const aiNodeAnim *node
...
@@ -400,118 +492,102 @@ QQuaternion Mesh::calcInterpolatedRotation(float AnimTime,const aiNodeAnim *node
//return convert(start);
//return convert(start);
}
}
QVector3D
Mesh
::
calcInterpolatedScaling
(
float
AnimTime
,
const
aiNodeAnim
*
nodeAnimation
){
return
calcInterpolatedVectorKey
(
AnimTime
,
nodeAnimation
,
nodeAnimation
->
mScalingKeys
,
nodeAnimation
->
mNumScalingKeys
);
}
QVector3D
Mesh
::
calcInterpolatedTranslation
(
float
AnimTime
,
const
aiNodeAnim
*
nodeAnimation
){
QVector3D
Mesh
::
calcInterpolatedTranslation
(
float
AnimTime
,
const
aiNodeAnim
*
nodeAnimation
){
if
(
nodeAnimation
->
mNumPositionKeys
==
1
){
return
calcInterpolatedVectorKey
(
AnimTime
,
nodeAnimation
,
nodeAnimation
->
mPositionKeys
,
nodeAnimation
->
mNumPositionKeys
);
convert
(
nodeAnimation
->
mPositionKeys
[
0
].
mValue
);
}
QVector3D
Mesh
::
calcInterpolatedVectorKey
(
float
AnimTime
,
const
aiNodeAnim
*
nodeAnimation
,
const
aiVectorKey
*
keys
,
const
int
numberOfKeys
){
if
(
numberOfKeys
==
1
){
return
convert
(
keys
[
0
].
mValue
);
}
}
//Index find
//Index find
int
index
=
0
;
int
index
=
0
;
for
(
uint
i
=
0
;
i
<
nodeAnimation
->
mNumPositionKeys
-
1
;
i
++
)
{
for
(
int
i
=
0
;
i
<
numberOfKeys
-
1
;
i
++
)
{
if
(
AnimTime
<
(
float
)
nodeAnimation
->
mPositionK
eys
[
i
+
1
].
mTime
)
{
if
(
AnimTime
<
(
float
)
k
eys
[
i
+
1
].
mTime
)
{
index
=
i
;
index
=
i
;
break
;
break
;
}
}
}
}
float
DeltaTime
=
(
float
)(
keys
[
index
+
1
].
mTime
-
keys
[
index
].
mTime
);
float
Factor
=
(
AnimTime
-
(
float
)
keys
[
index
].
mTime
)
/
DeltaTime
;
float
DeltaTime
=
(
float
)(
nodeAnimation
->
mPositionKeys
[
index
+
1
].
mTime
-
nodeAnimation
->
mPositionKeys
[
index
].
mTime
);
QVector3D
start
=
convert
(
keys
[
index
].
mValue
);
float
Factor
=
(
AnimTime
-
(
float
)
nodeAnimation
->
mPositionKeys
[
index
].
mTime
)
/
DeltaTime
;
QVector3D
end
=
convert
(
keys
[
index
+
1
].
mValue
);
QVector3D
start
=
convert
(
nodeAnimation
->
mPositionKeys
[
index
].
mValue
);
QVector3D
end
=
convert
(
nodeAnimation
->
mPositionKeys
[
index
+
1
].
mValue
);
return
linInterpolate
(
start
,
end
,
Factor
);
return
linInterpolate
(
start
,
end
,
Factor
);
}
void
Mesh
::
render
(
QOpenGLShaderProgram
*
shader
,
QMatrix4x4
V
,
QMatrix4x4
P
,
float
TimeInSeconds
){
if
(
start
.
distanceToPoint
(
end
)
==
0
){
// same keyframse not interpolation
if
(
!
loaded
)
return
start
;
return
;
if
(
f
==
NULL
){
qDebug
()
<<
"f = null"
;
return
;
}
}
f
->
glEnableVertexAttribArray
(
positionIndex
);
if
(
index
==
0
){
f
->
glEnableVertexAttribArray
(
normalIndex
);
switch
(
nodeAnimation
->
mPreState
)
{
f
->
glEnableVertexAttribArray
(
uvIndex
);
case
aiAnimBehaviour_CONSTANT
:
// nearest Key
f
->
glEnableVertexAttribArray
(
boneIndex
);
return
Factor
<
0.5
f
?
start
:
end
;
f
->
glEnableVertexAttribArray
(
boneweightIndex
);
break
;
case
aiAnimBehaviour_REPEAT
:
// use start or end
//TODO
updateBoneTransform
(
TimeInSeconds
);
case
aiAnimBehaviour_LINEAR
:
// linear interpolate
QVector
<
QMatrix4x4
>
boneTransforms
=
QVector
<
QMatrix4x4
>
(
bones
.
size
());
return
linInterpolate
(
start
,
end
,
Factor
);
for
(
int
i
=
0
;
i
<
bones
.
size
();
i
++
){
case
aiAnimBehaviour_DEFAULT
:
// take default node transformation
boneTransforms
[
i
]
=
bones
[
i
].
FinalTransformation
;
default
:
return
linInterpolate
(
start
,
end
,
Factor
);
//TODO
}
}
shader
->
setUniformValueArray
(
"Bones"
,
boneTransforms
.
constData
(),
bones
.
size
());
}
else
if
((
index
+
2
)
==
numberOfKeys
){
//end
renderNode
(
shader
,
rootNode
,
V
,
P
,
QMatrix4x4
()
);
return
linInterpolate
(
start
,
end
,
Factor
);
}
else
{
QVector3D
before
=
convert
(
keys
[
index
-
1
].
mValue
);
QVector3D
after
=
convert
(
keys
[
index
+
2
].
mValue
);
f
->
glDisableVertexAttribArray
(
positionIndex
);
// if(before.distanceToPoint(start) == 0|| end.distanceToPoint(after) == 0){
f
->
glDisableVertexAttribArray
(
normalIndex
);
// qDebug()<<"Zeror before or after"<<AnimTime;
f
->
glDisableVertexAttribArray
(
uvIndex
);
// return linInterpolate(start,end,Factor);
f
->
glDisableVertexAttribArray
(
boneIndex
);
// }
f
->
glDisableVertexAttribArray
(
boneweightIndex
);
QVector3D
interpolated
=
Catmul
(
before
,
start
,
end
,
after
,
Factor
);
//qDebug()<<nodeAnimation->mNodeName.C_Str()<<AnimTime<<start<<end<<interpolated;
return
interpolated
;
}
}
}
void
Mesh
::
renderNode
(
QOpenGLShaderProgram
*
shader
,
Node
&
node
,
QMatrix4x4
V
,
QMatrix4x4
P
,
QMatrix4x4
M
){
QVector3D
Mesh
::
Catmul
(
QVector3D
before
,
QVector3D
start
,
QVector3D
end
,
QVector3D
after
,
float
Factor
){
M
*=
node
.
transformation
;
float
t0
=
0
;
float
t1
=
timeCatmul
(
before
,
start
,
t0
);
float
t2
=
timeCatmul
(
start
,
end
,
t1
);
float
t3
=
timeCatmul
(
end
,
after
,
t2
);
float
t
=
Factor
;
//(1-Factor)*t1+Factor*t2;
//qDebug()<<before<<start<<end<<after;
//qDebug()<<t0<<t1<<t2<<t3<<t;
Q
Matrix4x4
MV
=
V
*
M
;
Q
Vector3D
A1
=
(
t1
-
t
)
/
(
t1
-
t0
)
*
before
+
(
t
-
t0
)
/
(
t1
-
t0
)
*
start
;
Q
Matrix4x4
MVP
=
P
*
MV
;
Q
Vector3D
A2
=
(
t2
-
t
)
/
(
t2
-
t1
)
*
start
+
(
t
-
t1
)
/
(
t2
-
t1
)
*
end
;
Q
Matrix3x3
normalMat
=
MV
.
normalMatrix
()
;
Q
Vector3D
A3
=
(
t3
-
t
)
/
(
t3
-
t2
)
*
end
+
(
t
-
t2
)
/
(
t3
-
t2
)
*
after
;
shader
->
setUniformValue
(
"MVP"
,
MVP
);
QVector3D
B1
=
(
t2
-
t
)
/
(
t2
-
t0
)
*
A1
+
(
t
-
t0
)
/
(
t2
-
t0
)
*
A2
;
shader
->
setUniformValue
(
"MV"
,
MV
);
QVector3D
B2
=
(
t3
-
t
)
/
(
t3
-
t1
)
*
A2
+
(
t
-
t1
)
/
(
t3
-
t1
)
*
A3
;
shader
->
setUniformValue
(
"N"
,
normalMat
);
for
(
int
i
=
0
;
i
<
node
.
meshes
.
size
()
;
i
++
)
{
QVector3D
C
=
(
t2
-
t
)
/
(
t2
-
t1
)
*
B1
+
(
t
-
t1
)
/
(
t2
-
t1
)
*
B2
;
int
index
=
node
.
meshes
[
i
];
return
C
;
//load Material
renderMesh
(
shader
,
index
);
}
for
(
int
i
=
0
;
i
<
node
.
children
.
size
()
;
i
++
)
{
renderNode
(
shader
,
node
.
children
[
i
],
V
,
P
,
M
);
}
}
}
void
Mesh
::
renderMesh
(
QOpenGLShaderProgram
*
shader
,
int
index
)
float
Mesh
::
timeCatmul
(
QVector3D
p1
,
QVector3D
p2
,
float
t_1
){
{
float
dist
=
p1
.
distanceToPoint
(
p2
);
int
MaterialIndex
=
entries
[
index
].
materialIndex
;
float
t
=
powf
(
dist
,
0.5
)
+
t_1
;
if
(
MaterialIndex
<
materials
.
size
())
{
// qDebug()<<dist<<t;
shader
->
setUniformValue
(
"materialInfo.Diffuse"
,
materials
[
MaterialIndex
].
Diffuse
);
return
t
;
shader
->
setUniformValue
(
"materialInfo.Specular"
,
materials
[
MaterialIndex
].
Specular
);
shader
->
setUniformValue
(
"materialInfo.Shininess"
,
materials
[
MaterialIndex
].
Shininess
);
shader
->
setUniformValue
(
"materialInfo.hasTexture"
,
materials
[
MaterialIndex
].
hasTexture
);
if
(
materials
[
MaterialIndex
].
hasTexture
)
materials
[
MaterialIndex
].
texture
.
bind
(
f
,
GL_TEXTURE0
);
}
// Draw Vertex Array
f
->
glBindBuffer
(
GL_ARRAY_BUFFER
,
entries
[
index
].
VB
);
f
->
glVertexAttribPointer
(
positionIndex
,
3
,
GL_FLOAT
,
GL_FALSE
,
sizeof
(
Vertex
),
0
);
f
->
glVertexAttribPointer
(
normalIndex
,
3
,
GL_FLOAT
,
GL_FALSE
,
sizeof
(
Vertex
),
(
const
GLvoid
*
)
12
);
//12
f
->
glVertexAttribPointer
(
uvIndex
,
2
,
GL_FLOAT
,
GL_FALSE
,
sizeof
(
Vertex
),
(
const
GLvoid
*
)
24
);
// 12+12
f
->
glVertexAttribIPointer
(
boneIndex
,
4
,
GL_INT
,
sizeof
(
Vertex
),
(
const
GLvoid
*
)
32
);
// 12+12+8
f
->
glVertexAttribPointer
(
boneweightIndex
,
4
,
GL_FLOAT
,
GL_FALSE
,
sizeof
(
Vertex
),
(
const
GLvoid
*
)
48
);
//12+12+8+16
f
->
glBindBuffer
(
GL_ELEMENT_ARRAY_BUFFER
,
entries
[
index
].
IB
);
f
->
glDrawElements
(
GL_TRIANGLES
,
entries
[
index
].
numIndex
,
GL_UNSIGNED_INT
,
0
);
}
}
Mesh
::~
Mesh
()
{
entries
.
clear
();
materials
.
clear
();
}
A5/Animation/mesh.h
View file @
63e1d174
...
@@ -133,6 +133,7 @@ private:
...
@@ -133,6 +133,7 @@ private:
QVector3D
calcInterpolatedScaling
(
float
AnimTime
,
const
aiNodeAnim
*
nodeAnimation
);
QVector3D
calcInterpolatedScaling
(
float
AnimTime
,
const
aiNodeAnim
*
nodeAnimation
);
QQuaternion
calcInterpolatedRotation
(
float
AnimTime
,
const
aiNodeAnim
*
nodeAnimation
);
QQuaternion
calcInterpolatedRotation
(
float
AnimTime
,
const
aiNodeAnim
*
nodeAnimation
);
QVector3D
calcInterpolatedTranslation
(
float
AnimTime
,
const
aiNodeAnim
*
nodeAnimation
);
QVector3D
calcInterpolatedTranslation
(
float
AnimTime
,
const
aiNodeAnim
*
nodeAnimation
);
QVector3D
calcInterpolatedVectorKey
(
float
AnimTime
,
const
aiNodeAnim
*
nodeAnimation
,
const
aiVectorKey
*
keys
,
const
int
numberOfKeys
);
void
renderNode
(
QOpenGLShaderProgram
*
shader
,
Node
&
node
,
QMatrix4x4
V
,
QMatrix4x4
P
,
QMatrix4x4
M
);
void
renderNode
(
QOpenGLShaderProgram
*
shader
,
Node
&
node
,
QMatrix4x4
V
,
QMatrix4x4
P
,
QMatrix4x4
M
);
...
@@ -140,8 +141,10 @@ private:
...
@@ -140,8 +141,10 @@ private:
QVector3D
convert
(
aiVector3D
in
){
return
QVector3D
(
in
.
x
,
in
.
y
,
in
.
z
);}
QVector3D
convert
(
aiVector3D
in
){
return
QVector3D
(
in
.
x
,
in
.
y
,
in
.
z
);}
QQuaternion
convert
(
aiQuaternion
quat
){
return
QQuaternion
(
quat
.
w
,
quat
.
x
,
quat
.
y
,
quat
.
z
);}
QQuaternion
convert
(
aiQuaternion
quat
){
return
QQuaternion
(
quat
.
w
,
quat
.
x
,
quat
.
y
,
quat
.
z
);}
QVector3D
linInterpolate
(
QVector3D
start
,
QVector3D
end
,
float
Factor
){
return
(
1
.
0
-
Factor
)
*
start
+
Factor
*
end
;}
QVector3D
linInterpolate
(
QVector3D
start
,
QVector3D
end
,
float
Factor
){
return
(
1
.
0
-
Factor
)
*
start
+
Factor
*
end
;}
QVector3D
Catmul
(
QVector3D
before
,
QVector3D
start
,
QVector3D
end
,
QVector3D
after
,
float
Factor
);
float
timeCatmul
(
QVector3D
p1
,
QVector3D
p2
,
float
t_1
);
};
};
...
...
A5/Animation/texture.cpp
View file @
63e1d174
...
@@ -17,6 +17,7 @@ void Texture::Load(GLenum textureTarget, QString fileName)
...
@@ -17,6 +17,7 @@ void Texture::Load(GLenum textureTarget, QString fileName)
qDebug
()
<<
"No Image to load"
;
qDebug
()
<<
"No Image to load"
;
return
;
return
;
}
}
qDebug
()
<<
"Loading Texture:"
<<
fileName
;
qDebug
()
<<
"Loading Texture:"
<<
fileName
;
QString
suffix
=
QFileInfo
(
fileName
).
completeSuffix
();
QString
suffix
=
QFileInfo
(
fileName
).
completeSuffix
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a 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