2 * Copyright 2008, Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Alexandre Deckner <alex@zappotek.com>
9 #include "MeshInstance.h"
10 #include "MathUtils.h"
13 //#include <stdio.h> // debug
16 MeshInstance::MeshInstance(Mesh
* mesh
, Texture
* texture
,
17 const Vector3
& position
, const Quaternion
& orientation
, float animOffset
)
20 fTextureReference(texture
),
22 fOrientation(orientation
),
25 fAnimOffset(animOffset
),
32 MeshInstance::~MeshInstance()
38 MeshInstance::Update(float dt
)
40 fTextureReference
->Update(dt
);
42 float animDuration
= 4.0f
;
45 if (fTime
>= fAnimOffset
) {
46 float animTime
= (fTime
- fAnimOffset
);
48 float rotAngle
= MathUtils::EaseInOutQuart(animTime
, 0,
49 3 * 2 * 3.14159, animDuration
);
51 fOrientation
= Quaternion(Vector3(0.0f
, 1.0f
, 0.0f
), rotAngle
);
54 if (fTime
>= fAnimOffset
+ animDuration
) {
55 fOrientation
= Quaternion(Vector3(0.0f
, 1.0f
, 0.0f
), 0.0);
58 if (fTime
>= animDuration
* 6) {
65 MeshInstance::Render()
67 if (fMeshReference
->FaceCount() == 0)
71 glTranslatef(fPosition
.x(), fPosition
.y(), fPosition
.z());
73 fOrientation
.toOpenGLMatrix(mat
);
74 glMultMatrixf((GLfloat
*) mat
);
75 glScalef(fScale
, fScale
, fScale
);
76 glBindTexture(GL_TEXTURE_2D
, fTextureReference
->Id());
78 int lastVertexCount
= 0;
80 for (uint32 i
= 0; i
< fMeshReference
->FaceCount(); i
++) {
82 const Face
& face
= fMeshReference
->GetFace(i
);
85 if (face
.vertexCount
!= lastVertexCount
) {
86 if (lastVertexCount
!= 0)
89 if (face
.vertexCount
== 3)
90 glBegin(GL_TRIANGLES
);
96 Vector3
lu(face
.v
[0].p
- face
.v
[1].p
);
97 Vector3
lv(face
.v
[1].p
- face
.v
[2].p
);
98 Vector3
normal(lu
.cross(lv
));
99 if (normal
.length() <= 0.000001)
100 normal
.setValue(0, 0, -1.0);
104 glNormal3f(normal
.x(), normal
.y(), normal
.z());
105 glTexCoord2f(face
.v
[0].u
, face
.v
[0].v
);
106 glVertex3f(face
.v
[0].p
.x(), face
.v
[0].p
.y(), face
.v
[0].p
.z());
108 glNormal3f(normal
.x(), normal
.y(), normal
.z());
109 glTexCoord2f(face
.v
[1].u
, face
.v
[1].v
);
110 glVertex3f(face
.v
[1].p
.x(), face
.v
[1].p
.y(), face
.v
[1].p
.z());
112 glNormal3f(normal
.x(), normal
.y(), normal
.z());
113 glTexCoord2f(face
.v
[2].u
, face
.v
[2].v
);
114 glVertex3f(face
.v
[2].p
.x(), face
.v
[2].p
.y(), face
.v
[2].p
.z());
116 if (face
.vertexCount
== 4) {
117 glNormal3f(normal
.x(), normal
.y(), normal
.z());
118 glTexCoord2f(face
.v
[3].u
, face
.v
[3].v
);
119 glVertex3f(face
.v
[3].p
.x(), face
.v
[3].p
.y(), face
.v
[3].p
.z());
123 if (face
.vertexCount
== 4) {
124 glNormal3f(-normal
.x(), -normal
.y(), -normal
.z());
125 glTexCoord2f(face
.v
[3].u
, face
.v
[3].v
);
126 glVertex3f(face
.v
[3].p
.x(), face
.v
[3].p
.y(), face
.v
[3].p
.z());
129 glNormal3f(-normal
.x(), -normal
.y(), -normal
.z());
130 glTexCoord2f(face
.v
[2].u
, face
.v
[2].v
);
131 glVertex3f(face
.v
[2].p
.x(), face
.v
[2].p
.y(), face
.v
[2].p
.z());
133 glNormal3f(-normal
.x(), -normal
.y(), -normal
.z());
134 glTexCoord2f(face
.v
[1].u
, face
.v
[1].v
);
135 glVertex3f(face
.v
[1].p
.x(), face
.v
[1].p
.y(), face
.v
[1].p
.z());
137 glNormal3f(-normal
.x(), -normal
.y(), -normal
.z());
138 glTexCoord2f(face
.v
[0].u
, face
.v
[0].v
);
139 glVertex3f(face
.v
[0].p
.x(), face
.v
[0].p
.y(), face
.v
[0].p
.z());
141 lastVertexCount
= face
.vertexCount
;
147 for (uint32 i
= 0; i
< fMeshReference
->FaceCount(); i
++) {
149 const Face
& face
= fMeshReference
->GetFace(i
);
151 if (face
.vertexCount
== 4) {
154 Vector3
lu(face
.v
[0].p
- face
.v
[1].p
);
155 Vector3
lv(face
.v
[1].p
- face
.v
[2].p
);
156 Vector3
normal(lu
.cross(lv
));
157 if (normal
.length() <= 0.000001)
158 normal
.setValue(0, 0, -1.0);
161 // center of the face
163 if (face
.vertexCount
== 4)
164 g
= (face
.v
[0].p
+ face
.v
[1].p
+ face
.v
[2].p
+ face
.v
[3].p
)
167 g
= (face
.v
[0].p
+ face
.v
[1].p
+ face
.v
[2].p
) / 3.0;
169 Vector3
h(g
+ normal
);
171 glVertex3f(g
.x(), g
.y(), g
.z());
172 glVertex3f(h
.x(), h
.y(), h
.z());