draw refactoring #1

This commit is contained in:
Sandro Sobczyński
2020-10-29 17:47:28 +01:00
parent 9e00ef3634
commit 5320d7f84d
15 changed files with 267 additions and 78 deletions
+6 -3
View File
@@ -33,7 +33,7 @@ Vector3::Vector3(const Vector3 &another)
z = another.z;
}
void Vector3::setByLerp(const Vector3 &v1, const Vector3 &v2, const float t_interp)
void Vector3::setByLerp(const Vector3 &v1, const Vector3 &v2, const float &t_interp, const float &t_scale)
{
asm volatile(
"lqc2 vf4, 0x0(%1) \n\t" // vf4 = v1
@@ -47,6 +47,9 @@ void Vector3::setByLerp(const Vector3 &v1, const Vector3 &v2, const float t_inte
:
: "r"(&this->xyz), "r"(&v1.xyz), "r"(&v2.xyz), "f"(t_interp)
: "$8");
x *= t_scale;
y *= t_scale;
z *= t_scale;
}
/** Create empty vector */
@@ -245,14 +248,14 @@ float Vector3::innerProduct(Vector3 &v)
// return (x * v.x + y * v.y + z * v.z);
}
void Vector3::set(float x, float y, float z)
void Vector3::set(const float &x, const float &y, const float &z)
{
this->x = x;
this->y = y;
this->z = z;
}
void Vector3::set(Vector3 &v)
void Vector3::set(const Vector3 &v)
{
this->x = v.x;
this->y = v.y;
+1 -4
View File
@@ -83,10 +83,7 @@ u32 MD2Model::getCurrentFrameData(VECTOR *o_vertices, VECTOR *o_normals, VECTOR
const u32 NEXT_VERTICE =
triangles[iTri].verticeIndexes[iVert] +
(verticesPerFrameCount * animState.nextFrame);
calcVector.setByLerp(vertices[CURR_VERTICE], vertices[NEXT_VERTICE], animState.interpolation);
calc3Vectors[iVert].x = calcVector.x * t_scale;
calc3Vectors[iVert].y = calcVector.y * t_scale;
calc3Vectors[iVert].z = calcVector.z * t_scale;
calc3Vectors[iVert].setByLerp(vertices[CURR_VERTICE], vertices[NEXT_VERTICE], animState.interpolation, t_scale);
}
}
if (!t_shouldBeBackfaceCulled || Vector3::shouldBeBackfaceCulled(&t_cameraPos, &calc3Vectors[2], &calc3Vectors[1], &calc3Vectors[0]) == 0)
+2 -1
View File
@@ -93,6 +93,7 @@ void Mesh::loadObj(char *t_subfolder, char *t_objFile, Vector3 &t_initPos, float
for (u16 i = 0; i < framesAmount; i++)
{
// obj->frames[i] = Frame();
char *part3 = String::createU32ToString(i + 1); // 0 -> "1"
char *part4 = String::createWithLeadingZeros(part3); // "000001"
char *part5 = String::createConcatenated(part2, part4); // "folder/object_000001"
@@ -182,7 +183,7 @@ void Mesh::loadTextures(char *t_subfolder, char *t_extension)
spec->textures = new Texture[obj->frames[0].materialsCount];
for (u8 i = 0; i < obj->frames[0].materialsCount; i++)
{
bmpLoader.load(spec->textures[i], t_subfolder, obj->frames[0].materials[i].materialName, t_extension);
bmpLoader.load(spec->textures[i], t_subfolder, obj->frames[0].materials[i].getName(), t_extension);
setDefaultWrapSettings(spec->textures[i].wrapSettings);
}
}
+24
View File
@@ -0,0 +1,24 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2020, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#include "../include/models/mesh_frame.hpp"
#include "../include/utils/debug.hpp"
// ----
// Constructors/Destructors
// ----
MeshFrame::MeshFrame() {}
MeshFrame::~MeshFrame() {}
// ----
// Methods
// ----
+68
View File
@@ -0,0 +1,68 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2020, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#include "../include/models/mesh_material.hpp"
#include "../include/utils/debug.hpp"
#include "../include/utils/string.hpp"
// ----
// Constructors/Destructors
// ----
MeshMaterial::MeshMaterial()
{
facesCount = 0;
isNameAllocated = false;
areFacesAllocated = false;
}
MeshMaterial::~MeshMaterial()
{
if (areFacesAllocated)
{
delete[] vertexFaces;
delete[] stFaces;
delete[] normalFaces;
}
if (isNameAllocated)
{
delete[] name;
}
}
// ----
// Methods
// ----
void MeshMaterial::setFacesCount(const u32 &t_val)
{
if (areFacesAllocated)
{
PRINT_ERR("Can't set faces, because faces were already set!");
return;
}
facesCount = t_val;
stFaces = new u32[t_val];
normalFaces = new u32[t_val];
vertexFaces = new u32[t_val];
areFacesAllocated = true;
}
void MeshMaterial::setName(char *t_val)
{
if (isNameAllocated)
{
PRINT_ERR("Can't set name, because was already set!");
return;
}
name = String::createCopy(t_val);
isNameAllocated = true;
}
+30 -39
View File
@@ -56,15 +56,13 @@ u32 ObjModel::getFacesCount()
{
u32 result = 0; // TODO
for (u16 i = 0; i < frames[0].materialsCount; i++)
result += frames[0].materials[i].facesCount;
result += frames[0].materials[i].getFacesCount();
return result;
}
u32 ObjModel::getDrawData(u32 t_materialIndex, VECTOR *o_vertices, VECTOR *o_normals, VECTOR *o_coordinates, VECTOR *o_colors, Vector3 &t_cameraPos, float t_scale, u8 t_shouldBeBackfaceCulled)
void ObjModel::animate()
{
u32 addedFaces = 0;
animState.interpolation += animState.speed; // TODO move it out
animState.interpolation += animState.speed;
if (animState.interpolation >= 1.0F)
{
animState.interpolation = 0.0F;
@@ -72,44 +70,42 @@ u32 ObjModel::getDrawData(u32 t_materialIndex, VECTOR *o_vertices, VECTOR *o_nor
if (++animState.nextFrame > animState.endFrame)
animState.nextFrame = animState.startFrame;
}
}
for (u32 j = 0; j < frames[animState.currentFrame].materials[t_materialIndex].facesCount; j += 3)
u32 ObjModel::getDrawData(u32 t_materialIndex, VECTOR *o_vertices, VECTOR *o_normals, VECTOR *o_coordinates, VECTOR *o_colors, Vector3 &t_cameraPos, float t_scale, u8 t_shouldBeBackfaceCulled)
{
#define CURR_FRAME frames[animState.currentFrame]
#define NEXT_FRAME frames[animState.nextFrame]
#define MATERIAL CURR_FRAME.materials[t_materialIndex]
#define CURR_VERT CURR_FRAME.vertices[MATERIAL.getVertexFace(matI + vertI)]
#define NEXT_VERT NEXT_FRAME.vertices[MATERIAL.getVertexFace(matI + vertI)]
u32 addedFaces = 0;
for (u32 matI = 0; matI < MATERIAL.getFacesCount(); matI += 3)
{
for (u32 iVert = 0; iVert < 3; iVert++)
{
const ObjMaterial CURR_MAT = frames[animState.currentFrame].materials[t_materialIndex];
const Vector3 CURR_VERT = frames[animState.currentFrame].vertices[CURR_MAT.verticeFaces[j + iVert]];
for (u32 vertI = 0; vertI < 3; vertI++)
if (animState.startFrame == animState.endFrame)
{
calc3Vectors[iVert].x = CURR_VERT.x * t_scale;
calc3Vectors[iVert].y = CURR_VERT.y * t_scale;
calc3Vectors[iVert].z = CURR_VERT.z * t_scale;
}
calc3Vectors[vertI].set(CURR_VERT * t_scale);
else
calc3Vectors[vertI].setByLerp(CURR_VERT, NEXT_VERT, animState.interpolation, t_scale);
if (!t_shouldBeBackfaceCulled ||
Vector3::shouldBeBackfaceCulled(&t_cameraPos, &calc3Vectors[2], &calc3Vectors[1], &calc3Vectors[0]))
for (u32 vertI = 0; vertI < 3; vertI++)
{
const Vector3 NEXT_VERT = frames[animState.nextFrame].vertices[CURR_MAT.verticeFaces[j + iVert]];
calcVector.setByLerp(CURR_VERT, NEXT_VERT, animState.interpolation);
calc3Vectors[iVert].x = calcVector.x * t_scale;
calc3Vectors[iVert].y = calcVector.y * t_scale;
calc3Vectors[iVert].z = calcVector.z * t_scale;
}
}
if (!t_shouldBeBackfaceCulled || Vector3::shouldBeBackfaceCulled(&t_cameraPos, &calc3Vectors[2], &calc3Vectors[1], &calc3Vectors[0]) == 0)
for (u32 iVert = 0; iVert < 3; iVert++)
{
const u32 CURR_FRAME = animState.currentFrame;
o_vertices[addedFaces][0] = calc3Vectors[iVert].x;
o_vertices[addedFaces][1] = calc3Vectors[iVert].y;
o_vertices[addedFaces][2] = calc3Vectors[iVert].z;
o_vertices[addedFaces][0] = calc3Vectors[vertI].x;
o_vertices[addedFaces][1] = calc3Vectors[vertI].y;
o_vertices[addedFaces][2] = calc3Vectors[vertI].z;
o_vertices[addedFaces][3] = 1.0F;
o_normals[addedFaces][0] = frames[CURR_FRAME].normals[frames[CURR_FRAME].materials[t_materialIndex].normalFaces[j]].x;
o_normals[addedFaces][1] = frames[CURR_FRAME].normals[frames[CURR_FRAME].materials[t_materialIndex].normalFaces[j]].y;
o_normals[addedFaces][2] = frames[CURR_FRAME].normals[frames[CURR_FRAME].materials[t_materialIndex].normalFaces[j]].z;
o_normals[addedFaces][0] = CURR_FRAME.normals[MATERIAL.getNormalFace(matI + vertI)].x;
o_normals[addedFaces][1] = CURR_FRAME.normals[MATERIAL.getNormalFace(matI + vertI)].y;
o_normals[addedFaces][2] = CURR_FRAME.normals[MATERIAL.getNormalFace(matI + vertI)].z;
o_normals[addedFaces][3] = 1.0F;
o_coordinates[addedFaces][0] = frames[CURR_FRAME].coordinates[frames[CURR_FRAME].materials[t_materialIndex].coordinateFaces[j]].x;
o_coordinates[addedFaces][1] = frames[CURR_FRAME].coordinates[frames[CURR_FRAME].materials[t_materialIndex].coordinateFaces[j]].y;
o_coordinates[addedFaces][0] = CURR_FRAME.coordinates[MATERIAL.getStFace(matI + vertI)].x;
o_coordinates[addedFaces][1] = CURR_FRAME.coordinates[MATERIAL.getStFace(matI + vertI)].y;
o_coordinates[addedFaces][2] = 1.0F;
o_coordinates[addedFaces][3] = 1.0F;
@@ -118,11 +114,6 @@ u32 ObjModel::getDrawData(u32 t_materialIndex, VECTOR *o_vertices, VECTOR *o_nor
o_colors[addedFaces][2] = 1.0F;
o_colors[addedFaces++][3] = 1.0F;
}
// fillNextFace(o_vertices, o_normals, o_coordinates, o_colors, t_materialIndex, j, addedFaces++);
}
return addedFaces;
}
void ObjModel::fillNextFace(VECTOR *o_vertices, VECTOR *o_normals, VECTOR *o_coordinates, VECTOR *o_colors, u32 t_materialI, u32 t_getI, u32 t_setI)
{
}