moved aabb calcs
This commit is contained in:
@@ -35,7 +35,6 @@ public:
|
|||||||
DffModel *dff;
|
DffModel *dff;
|
||||||
float scale;
|
float scale;
|
||||||
color_t color;
|
color_t color;
|
||||||
Vector3 boxVertices[8];
|
|
||||||
|
|
||||||
Mesh();
|
Mesh();
|
||||||
~Mesh();
|
~Mesh();
|
||||||
@@ -55,7 +54,7 @@ public:
|
|||||||
u8 isMd2Loaded, isObjLoaded, isDffLoaded, isSpecInitialized;
|
u8 isMd2Loaded, isObjLoaded, isDffLoaded, isSpecInitialized;
|
||||||
clutbuffer_t clut;
|
clutbuffer_t clut;
|
||||||
lod_t lod;
|
lod_t lod;
|
||||||
MeshTexture *textures;
|
MeshTexture **textures;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void setupLodAndClut();
|
void setupLodAndClut();
|
||||||
@@ -63,7 +62,6 @@ private:
|
|||||||
u32 verticesCount;
|
u32 verticesCount;
|
||||||
Vector3 *vertices;
|
Vector3 *vertices;
|
||||||
void loadTextures(char *t_subfolder, char *t_extension);
|
void loadTextures(char *t_subfolder, char *t_extension);
|
||||||
void computeBoundingBox();
|
|
||||||
void setVerticesReference(u32 t_verticesCount, Vector3 *t_verticesRef);
|
void setVerticesReference(u32 t_verticesCount, Vector3 *t_verticesRef);
|
||||||
void createSpecIfNotCreated();
|
void createSpecIfNotCreated();
|
||||||
void setDefaultColor();
|
void setDefaultColor();
|
||||||
|
|||||||
@@ -62,6 +62,12 @@ public:
|
|||||||
/** Array of materials. Size of getMaterialsCount() */
|
/** Array of materials. Size of getMaterialsCount() */
|
||||||
MeshMaterial *getMaterials() const { return materials; };
|
MeshMaterial *getMaterials() const { return materials; };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns bounding box (AABB) vertex.
|
||||||
|
* Total length: 8
|
||||||
|
*/
|
||||||
|
Vector3 &getBoundingBox(const u8 &i) { return boundingBox[i]; };
|
||||||
|
|
||||||
// ----
|
// ----
|
||||||
// Setters
|
// Setters
|
||||||
// ----
|
// ----
|
||||||
@@ -92,6 +98,7 @@ public:
|
|||||||
const u8 &areVerticesAllocated() const { return _areVerticesAllocated; };
|
const u8 &areVerticesAllocated() const { return _areVerticesAllocated; };
|
||||||
const u8 &areNormalsAllocated() const { return _areNormalsAllocated; };
|
const u8 &areNormalsAllocated() const { return _areNormalsAllocated; };
|
||||||
const u8 &areMaterialsAllocated() const { return _areMaterialsAllocated; };
|
const u8 &areMaterialsAllocated() const { return _areMaterialsAllocated; };
|
||||||
|
const u8 &isBoundingBoxCalculated() const { return _isBoundingBoxCalculated; };
|
||||||
|
|
||||||
/** Set STs count and allocate memory. */
|
/** Set STs count and allocate memory. */
|
||||||
void allocateSTs(const u32 &t_val);
|
void allocateSTs(const u32 &t_val);
|
||||||
@@ -105,8 +112,20 @@ public:
|
|||||||
/** Set materials count and allocate memory. */
|
/** Set materials count and allocate memory. */
|
||||||
void allocateMaterials(const u32 &t_val);
|
void allocateMaterials(const u32 &t_val);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates bounding box (AABB).
|
||||||
|
* Should be called by data loader,
|
||||||
|
* so there is no need to run it again.
|
||||||
|
*/
|
||||||
|
void calculateBoundingBox();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
u8 _areSTsAllocated, _areVerticesAllocated, _areNormalsAllocated, _areMaterialsAllocated;
|
Vector3 boundingBox[8];
|
||||||
|
u8 _areSTsAllocated,
|
||||||
|
_areVerticesAllocated,
|
||||||
|
_areNormalsAllocated,
|
||||||
|
_areMaterialsAllocated,
|
||||||
|
_isBoundingBoxCalculated;
|
||||||
u32 vertexCount, stsCount, normalsCount, materialsCount;
|
u32 vertexCount, stsCount, normalsCount, materialsCount;
|
||||||
MeshMaterial *materials;
|
MeshMaterial *materials;
|
||||||
Point *sts __attribute__((aligned(16)));
|
Point *sts __attribute__((aligned(16)));
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
/*
|
||||||
|
# ______ ____ ___
|
||||||
|
# | \/ ____| |___|
|
||||||
|
# | | | \ | |
|
||||||
|
#-----------------------------------------------------------------------
|
||||||
|
# Copyright 2020, tyra - https://github.com/h4570/tyra
|
||||||
|
# Licenced under Apache License 2.0
|
||||||
|
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _TYRA_TEXTURE_REPOSITORY_
|
||||||
|
#define _TYRA_TEXTURE_REPOSITORY_
|
||||||
|
|
||||||
|
#include <tamtypes.h>
|
||||||
|
#include <draw_buffers.h>
|
||||||
|
#include "../models/mesh.hpp"
|
||||||
|
|
||||||
|
/** Class responsible for intializing draw env, textures and buffers */
|
||||||
|
class TextureRepository
|
||||||
|
{
|
||||||
|
|
||||||
|
public:
|
||||||
|
TextureRepository();
|
||||||
|
~TextureRepository();
|
||||||
|
|
||||||
|
private:
|
||||||
|
void allocateTextureBuffer(u16 t_width, u16 t_height);
|
||||||
|
void deallocateTextureBuffer();
|
||||||
|
void changeTexture(Mesh *t_mesh, u8 t_textureIndex);
|
||||||
|
texbuffer_t textureBuffer;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -100,6 +100,7 @@ void ObjLoader::load(MeshFrame *o_result, char *t_filename, float t_scale, u8 in
|
|||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
o_result->calculateBoundingBox();
|
||||||
fclose(file);
|
fclose(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+13
-45
@@ -139,7 +139,6 @@ void Mesh::setVerticesReference(u32 t_verticesCount, Vector3 *t_verticesRef)
|
|||||||
{
|
{
|
||||||
verticesCount = t_verticesCount;
|
verticesCount = t_verticesCount;
|
||||||
vertices = t_verticesRef;
|
vertices = t_verticesRef;
|
||||||
computeBoundingBox();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
u32 Mesh::getVertexCount()
|
u32 Mesh::getVertexCount()
|
||||||
@@ -172,26 +171,29 @@ void Mesh::loadTextures(char *t_subfolder, char *t_extension)
|
|||||||
BmpLoader bmpLoader = BmpLoader();
|
BmpLoader bmpLoader = BmpLoader();
|
||||||
if (isObjLoaded)
|
if (isObjLoaded)
|
||||||
{
|
{
|
||||||
textures = new MeshTexture[obj->frames[0].getMaterialsCount()];
|
textures = new MeshTexture *[obj->frames[0].getMaterialsCount()];
|
||||||
for (u8 i = 0; i < obj->frames[0].getMaterialsCount(); i++)
|
for (u8 i = 0; i < obj->frames[0].getMaterialsCount(); i++)
|
||||||
{
|
{
|
||||||
bmpLoader.load(textures[i], t_subfolder, obj->frames[0].getMaterial(i).getName(), t_extension);
|
textures[i] = new MeshTexture();
|
||||||
|
bmpLoader.load(*textures[i], t_subfolder, obj->frames[0].getMaterial(i).getName(), t_extension);
|
||||||
// setDefaultWrapSettings(spec->textures[i].wrapSettings);
|
// setDefaultWrapSettings(spec->textures[i].wrapSettings);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (isMd2Loaded)
|
else if (isMd2Loaded)
|
||||||
{
|
{
|
||||||
textures = new MeshTexture[1];
|
textures = new MeshTexture *[1];
|
||||||
bmpLoader.load(textures[0], t_subfolder, md2->filename, t_extension);
|
textures[0] = new MeshTexture();
|
||||||
|
bmpLoader.load(*textures[0], t_subfolder, md2->filename, t_extension);
|
||||||
// setDefaultWrapSettings(spec->textures[0].wrapSettings);
|
// setDefaultWrapSettings(spec->textures[0].wrapSettings);
|
||||||
}
|
}
|
||||||
else if (isDffLoaded)
|
else if (isDffLoaded)
|
||||||
{
|
{
|
||||||
textures = new MeshTexture[dff->clump.geometryList.geometries[0].materialList.data.materialCount];
|
textures = new MeshTexture *[dff->clump.geometryList.geometries[0].materialList.data.materialCount];
|
||||||
for (u8 i = 0; i < dff->clump.geometryList.geometries[0].materialList.data.materialCount; i++)
|
for (u8 i = 0; i < dff->clump.geometryList.geometries[0].materialList.data.materialCount; i++)
|
||||||
for (u8 j = 0; j < dff->clump.geometryList.geometries[0].materialList.materials[i].data.textureCount; j++)
|
for (u8 j = 0; j < dff->clump.geometryList.geometries[0].materialList.materials[i].data.textureCount; j++)
|
||||||
{
|
{
|
||||||
bmpLoader.load(textures[i], t_subfolder, dff->clump.geometryList.geometries[0].materialList.materials[i].textures[j].textureName.text, t_extension);
|
textures[i] = new MeshTexture();
|
||||||
|
bmpLoader.load(*textures[i], t_subfolder, dff->clump.geometryList.geometries[0].materialList.materials[i].textures[j].textureName.text, t_extension);
|
||||||
// setDefaultWrapSettings(spec->textures[i].wrapSettings);
|
// setDefaultWrapSettings(spec->textures[i].wrapSettings);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -291,9 +293,10 @@ void Mesh::playAnimation(u32 t_startFrame, u32 t_endFrame)
|
|||||||
*/
|
*/
|
||||||
void Mesh::getFarthestVertex(Vector3 *o_result, int t_offset)
|
void Mesh::getFarthestVertex(Vector3 *o_result, int t_offset)
|
||||||
{
|
{
|
||||||
o_result->x = boxVertices[t_offset].x + position.x;
|
// TODO current frame
|
||||||
o_result->y = boxVertices[t_offset].y + position.y;
|
o_result->x = obj->frames[0].getBoundingBox(t_offset).x + position.x;
|
||||||
o_result->z = boxVertices[t_offset].z + position.z;
|
o_result->y = obj->frames[0].getBoundingBox(t_offset).y + position.y;
|
||||||
|
o_result->z = obj->frames[0].getBoundingBox(t_offset).z + position.z;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Sets texture level of details settings and CLUT settings */
|
/** Sets texture level of details settings and CLUT settings */
|
||||||
@@ -344,38 +347,3 @@ u8 Mesh::isInFrustum(Plane *t_frustumPlanes)
|
|||||||
}
|
}
|
||||||
return boxResult;
|
return boxResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Compute 8 farthest corners for intersection check */
|
|
||||||
void Mesh::computeBoundingBox()
|
|
||||||
{
|
|
||||||
float lowX, lowY, lowZ, hiX, hiY, hiZ;
|
|
||||||
lowX = hiX = vertices[0].x;
|
|
||||||
lowY = hiY = vertices[0].y;
|
|
||||||
lowZ = hiZ = vertices[0].z;
|
|
||||||
for (u32 i = 0; i < verticesCount; i++)
|
|
||||||
{
|
|
||||||
if (lowX > vertices[i].x)
|
|
||||||
lowX = vertices[i].x;
|
|
||||||
if (hiX < vertices[i].x)
|
|
||||||
hiX = vertices[i].x;
|
|
||||||
|
|
||||||
if (lowY > vertices[i].y)
|
|
||||||
lowY = vertices[i].y;
|
|
||||||
if (hiY < vertices[i].y)
|
|
||||||
hiY = vertices[i].y;
|
|
||||||
|
|
||||||
if (lowZ > vertices[i].z)
|
|
||||||
lowZ = vertices[i].z;
|
|
||||||
if (hiZ < vertices[i].z)
|
|
||||||
hiZ = vertices[i].z;
|
|
||||||
}
|
|
||||||
boxVertices[0].set(lowX, lowY, lowZ);
|
|
||||||
boxVertices[1].set(lowX, lowY, hiZ);
|
|
||||||
boxVertices[2].set(lowX, hiY, lowZ);
|
|
||||||
boxVertices[3].set(lowX, hiY, hiZ);
|
|
||||||
|
|
||||||
boxVertices[4].set(hiX, lowY, lowZ);
|
|
||||||
boxVertices[5].set(hiX, lowY, hiZ);
|
|
||||||
boxVertices[6].set(hiX, hiY, lowZ);
|
|
||||||
boxVertices[7].set(hiX, hiY, hiZ);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -90,3 +90,43 @@ void MeshFrame::allocateMaterials(const u32 &t_val)
|
|||||||
materials = new MeshMaterial[t_val];
|
materials = new MeshMaterial[t_val];
|
||||||
_areMaterialsAllocated = true;
|
_areMaterialsAllocated = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MeshFrame::calculateBoundingBox()
|
||||||
|
{
|
||||||
|
if (!_areVerticesAllocated)
|
||||||
|
{
|
||||||
|
PRINT_ERR("Can't calculate bounding box, because vertices was not allocated!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
float lowX, lowY, lowZ, hiX, hiY, hiZ;
|
||||||
|
lowX = hiX = vertices[0].x;
|
||||||
|
lowY = hiY = vertices[0].y;
|
||||||
|
lowZ = hiZ = vertices[0].z;
|
||||||
|
for (u32 i = 0; i < vertexCount; i++)
|
||||||
|
{
|
||||||
|
if (lowX > vertices[i].x)
|
||||||
|
lowX = vertices[i].x;
|
||||||
|
if (hiX < vertices[i].x)
|
||||||
|
hiX = vertices[i].x;
|
||||||
|
|
||||||
|
if (lowY > vertices[i].y)
|
||||||
|
lowY = vertices[i].y;
|
||||||
|
if (hiY < vertices[i].y)
|
||||||
|
hiY = vertices[i].y;
|
||||||
|
|
||||||
|
if (lowZ > vertices[i].z)
|
||||||
|
lowZ = vertices[i].z;
|
||||||
|
if (hiZ < vertices[i].z)
|
||||||
|
hiZ = vertices[i].z;
|
||||||
|
}
|
||||||
|
boundingBox[0].set(lowX, lowY, lowZ);
|
||||||
|
boundingBox[1].set(lowX, lowY, hiZ);
|
||||||
|
boundingBox[2].set(lowX, hiY, lowZ);
|
||||||
|
boundingBox[3].set(lowX, hiY, hiZ);
|
||||||
|
|
||||||
|
boundingBox[4].set(hiX, lowY, lowZ);
|
||||||
|
boundingBox[5].set(hiX, lowY, hiZ);
|
||||||
|
boundingBox[6].set(hiX, hiY, lowZ);
|
||||||
|
boundingBox[7].set(hiX, hiY, hiZ);
|
||||||
|
_isBoundingBoxCalculated = true;
|
||||||
|
}
|
||||||
|
|||||||
@@ -115,12 +115,12 @@ void Renderer::deallocateTextureBuffer()
|
|||||||
|
|
||||||
void Renderer::changeTexture(Mesh *t_mesh, u8 t_textureIndex)
|
void Renderer::changeTexture(Mesh *t_mesh, u8 t_textureIndex)
|
||||||
{
|
{
|
||||||
if (t_mesh->textures[t_textureIndex].getId() != lastTextureId)
|
if (t_mesh->textures[t_textureIndex]->getId() != lastTextureId)
|
||||||
{
|
{
|
||||||
lastTextureId = t_mesh->textures[t_textureIndex].getId();
|
lastTextureId = t_mesh->textures[t_textureIndex]->getId();
|
||||||
deallocateTextureBuffer();
|
deallocateTextureBuffer();
|
||||||
allocateTextureBuffer(t_mesh->textures[t_textureIndex].getWidth(), t_mesh->textures[t_textureIndex].getHeight());
|
allocateTextureBuffer(t_mesh->textures[t_textureIndex]->getWidth(), t_mesh->textures[t_textureIndex]->getHeight());
|
||||||
GifSender::sendTexture(t_mesh->textures[t_textureIndex], &textureBuffer);
|
GifSender::sendTexture(*t_mesh->textures[t_textureIndex], &textureBuffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
# ______ ____ ___
|
||||||
|
# | \/ ____| |___|
|
||||||
|
# | | | \ | |
|
||||||
|
#-----------------------------------------------------------------------
|
||||||
|
# Copyright 2020, tyra - https://github.com/h4570/tyra
|
||||||
|
# Licenced under Apache License 2.0
|
||||||
|
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "../include/modules/texture_repository.hpp"
|
||||||
|
#include "../include/utils/debug.hpp"
|
||||||
|
|
||||||
|
// ----
|
||||||
|
// Constructors/Destructors
|
||||||
|
// ----
|
||||||
|
|
||||||
|
TextureRepository::TextureRepository()
|
||||||
|
{
|
||||||
|
PRINT_LOG("Initializing texture repository");
|
||||||
|
|
||||||
|
PRINT_LOG("Texture repository initialized!");
|
||||||
|
}
|
||||||
|
|
||||||
|
TextureRepository::~TextureRepository() {}
|
||||||
|
|
||||||
|
// ----
|
||||||
|
// Methods
|
||||||
|
// ----
|
||||||
@@ -30,4 +30,36 @@ private:
|
|||||||
Vector3 playerNextPosition;
|
Vector3 playerNextPosition;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// TODO Rysowanie idzie wg tekstur
|
||||||
|
|
||||||
|
// 2 Graczy, 1 mesh 2 tekstury
|
||||||
|
// sharedFrames = objLoader.load("meshes/", "block", 1.0F);
|
||||||
|
// var addedTextures = texturesRepo.addByMaterialNames("textures/dirt/", sharedFrames);
|
||||||
|
// var addedTexture = texturesRepo.add("textures/blocks/sand.bmp");
|
||||||
|
// dirt = new Mesh(sharedFrames);
|
||||||
|
// sand = new Mesh(sharedFrames);
|
||||||
|
// addedTextures[0].addUsage(dirt.Id, sharedFrames.getMaterialId(0));
|
||||||
|
// addedTexture.addUsage(sand.Id, sand.getMaterialId(0));
|
||||||
|
|
||||||
|
// 1 gracz, 2 dynamic tekstury
|
||||||
|
// sharedFrames = objLoader.load("meshes/", "block", 1.0F);
|
||||||
|
// var addedTexture1 = texturesRepo.add("textures/blocks/sand.bmp");
|
||||||
|
// var addedTexture2 = texturesRepo.add("textures/blocks/sand_damaged.bmp");
|
||||||
|
// sand = new Mesh(sharedFrames);
|
||||||
|
// addedTexture1.removeUsage(sand.Id, sand.getMaterialId(0));
|
||||||
|
// draw()
|
||||||
|
// addedTexture2.addUsage(sand.Id, sand.getMaterialId(0));
|
||||||
|
|
||||||
|
// 2 różne obj, te same tekstury
|
||||||
|
// frames1 = objLoader.load("meshes/", "island", 1.0F);
|
||||||
|
// frames2 = objLoader.load("meshes/", "islandAddons", 1.0F);
|
||||||
|
// island = new Mesh(frames1);
|
||||||
|
// islandAddons = new Mesh(frames2);
|
||||||
|
// var addedTextures1 = texturesRepo.addByMaterialNames("textures/island/",frames1);
|
||||||
|
// var addedTextures2 = texturesRepo.addByMaterialNames("textures/island/",frames2);
|
||||||
|
// texturesRepo.remove(addedTextures2[0]); // duplicate
|
||||||
|
// addedTextures1[0].addUsage(islandAddons.Id, islandAddons.getMaterialId(0));
|
||||||
|
|
||||||
|
// Mesh -> position, rotation, color, scale, shouldBeLighted, clut, lod
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user