HaikuDepot: notify work status from main window
[haiku.git] / src / apps / haiku3d / MeshInstance.cpp
blobc270237a8d0c86288d6cf2185808522a0c018277
1 /*
2 * Copyright 2008, Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Alexandre Deckner <alex@zappotek.com>
7 */
9 #include "MeshInstance.h"
10 #include "MathUtils.h"
11 #include <GL/gl.h>
13 //#include <stdio.h> // debug
16 MeshInstance::MeshInstance(Mesh* mesh, Texture* texture,
17 const Vector3& position, const Quaternion& orientation, float animOffset)
19 fMeshReference(mesh),
20 fTextureReference(texture),
21 fPosition(position),
22 fOrientation(orientation),
23 fScale(1.0f),
24 fTime(0.0f),
25 fAnimOffset(animOffset),
26 fDoubleSided(true),
27 fDrawNormals(false)
32 MeshInstance::~MeshInstance()
37 void
38 MeshInstance::Update(float dt)
40 fTextureReference->Update(dt);
42 float animDuration = 4.0f;
44 fTime += dt;
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) {
59 fTime = 0.0;
64 void
65 MeshInstance::Render()
67 if (fMeshReference->FaceCount() == 0)
68 return;
70 glPushMatrix();
71 glTranslatef(fPosition.x(), fPosition.y(), fPosition.z());
72 float mat[4][4];
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);
84 // switch face mode
85 if (face.vertexCount != lastVertexCount) {
86 if (lastVertexCount != 0)
87 glEnd();
89 if (face.vertexCount == 3)
90 glBegin(GL_TRIANGLES);
91 else
92 glBegin(GL_QUADS);
95 // calculate normal
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);
101 normal.normalize();
103 // draw face
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());
122 if (fDoubleSided) {
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;
143 glEnd();
145 if (fDrawNormals) {
146 glBegin(GL_LINES);
147 for (uint32 i = 0; i < fMeshReference->FaceCount(); i++) {
149 const Face& face = fMeshReference->GetFace(i);
151 if (face.vertexCount == 4) {
153 // calculate normal
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);
159 normal.normalize();
161 // center of the face
162 Vector3 g;
163 if (face.vertexCount == 4)
164 g = (face.v[0].p + face.v[1].p + face.v[2].p + face.v[3].p)
165 / 4.0;
166 else
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());
175 glEnd();
178 glPopMatrix();