QD3D: Normals & UVs are now optional in TQ3TriMeshData constructor

This commit is contained in:
Iliyas Jorio 2021-05-02 15:37:15 +02:00
parent fd0b31c915
commit 375d14e266
3 changed files with 54 additions and 23 deletions

View File

@ -188,7 +188,7 @@ uint32_t Q3MetaFileParser::Parse1Chunk()
{
Assert(chunkSize == 4, "illegal rfrn size");
uint32_t target = f.Read<uint32_t>();
printf("TOC#%d -----> %08lx", target, referenceTOC.at(target));
printf("TOC#%d -----> %08lx", target, referenceTOC.at(target).offset);
auto jumpBackTo = f.Tell();
f.Goto(referenceTOC.at(target).offset);
Parse1Chunk();
@ -293,11 +293,9 @@ void Q3MetaFileParser::Parse_tmsh(uint32_t chunkSize)
Assert(0 == numEdges, "edges are not supported");
Assert(0 == numEdgeAttributes, "edge attributes are not supported");
currentMesh = Q3TriMeshData_New(
numTriangles,
numVertices,
false // Don't allocate per-vertex colors yet. We'll allocate them later if the mesh needs them.
);
// Allocate the mesh.
// Don't allocate vertex UVs, colors or normals yet. We'll allocate them later if the mesh needs them.
currentMesh = Q3TriMeshData_New(numTriangles, numVertices, 0);
__Q3EnlargeArray(metaFile.meshes, metaFile.numMeshes, 'MLST');
metaFile.meshes[metaFile.numMeshes-1] = currentMesh;
@ -383,7 +381,10 @@ void Q3MetaFileParser::Parse_atar(uint32_t chunkSize)
if (isVertexAttribute && attributeType == kQ3AttributeTypeShadingUV)
{
printf("vertex UVs");
Assert(currentMesh->vertexUVs, "current mesh has no vertex UV array");
Assert(!currentMesh->vertexUVs, "current mesh already had a vertex UV array");
currentMesh->vertexUVs = __Q3Alloc<TQ3Param2D>(currentMesh->numPoints, 'TMuv');
for (int i = 0; i < currentMesh->numPoints; i++)
{
float u = f.Read<float>();
@ -395,7 +396,11 @@ void Q3MetaFileParser::Parse_atar(uint32_t chunkSize)
{
printf("vertex normals");
Assert(positionInArray == 0, "PIA must be 0 for normals");
Assert(currentMesh->vertexNormals, "current mesh has no vertex normal array");
Assert(!currentMesh->vertexNormals, "current mesh already had a vertex normal array");
currentMesh->vertexNormals = __Q3Alloc<TQ3Vector3D>(currentMesh->numPoints, 'TMvn');
currentMesh->hasVertexNormals = true;
for (int i = 0; i < currentMesh->numPoints; i++)
{
currentMesh->vertexNormals[i].x = f.Read<float>();
@ -409,6 +414,7 @@ void Q3MetaFileParser::Parse_atar(uint32_t chunkSize)
// Assert(positionInArray == 0, "PIA must be 0 for colors");
Assert(!currentMesh->vertexColors, "current mesh already had a vertex color array");
currentMesh->vertexColors = __Q3Alloc<TQ3ColorRGBA>(currentMesh->numPoints, 'TMvc');
currentMesh->hasVertexColors = true;
for (int i = 0; i < currentMesh->numPoints; i++)
{
currentMesh->vertexColors[i].r = f.Read<float>();
@ -416,7 +422,6 @@ void Q3MetaFileParser::Parse_atar(uint32_t chunkSize)
currentMesh->vertexColors[i].b = f.Read<float>();
currentMesh->vertexColors[i].a = 1.0f;
}
currentMesh->hasVertexColors = true;
}
else if (isTriangleAttribute && attributeType == kQ3AttributeTypeNormal) // face normals
{

View File

@ -148,7 +148,7 @@ void Q3Pixmap_Dispose(TQ3Pixmap* pixmap)
#pragma mark -
TQ3TriMeshData* Q3TriMeshData_New(int numTriangles, int numPoints, bool perVertexColors)
TQ3TriMeshData* Q3TriMeshData_New(int numTriangles, int numPoints, int featureFlags)
{
TQ3TriMeshData* mesh = __Q3Alloc<TQ3TriMeshData>(1, 'MESH');
@ -156,28 +156,45 @@ TQ3TriMeshData* Q3TriMeshData_New(int numTriangles, int numPoints, bool perVerte
mesh->numPoints = numPoints;
mesh->points = __Q3Alloc<TQ3Point3D>(numPoints, 'TMpt');
mesh->triangles = __Q3Alloc<TQ3TriMeshTriangleData>(numTriangles, 'TMtr');
mesh->vertexNormals = __Q3Alloc<TQ3Vector3D>(numPoints, 'TMvn');
mesh->vertexUVs = __Q3Alloc<TQ3Param2D>(numPoints, 'TMuv');
mesh->vertexColors = perVertexColors? __Q3Alloc<TQ3ColorRGBA>(numPoints, 'TMvc'): nullptr;
mesh->diffuseColor = {1, 1, 1, 1};
mesh->texturingMode = kQ3TexturingModeOff;
mesh->internalTextureID = -1;
mesh->bBox = {{0,0,0}, {0,0,0}, kQ3True}; // empty
for (int i = 0; i < numPoints; i++)
{
mesh->vertexNormals[i] = {0, 1, 0};
mesh->vertexUVs[i] = {.5f, .5f};
}
if (perVertexColors)
if (featureFlags & kQ3TriMeshDataFeatureVertexUVs)
{
mesh->vertexUVs = __Q3Alloc<TQ3Param2D>(numPoints, 'TMuv');
for (int i = 0; i < numPoints; i++)
mesh->vertexColors[i] = {1, 1, 1, 1};
mesh->hasVertexColors = true;
mesh->vertexUVs[i] = {.5f, .5f};
}
else
{
mesh->vertexUVs = nullptr;
}
if (featureFlags & kQ3TriMeshDataFeatureVertexNormals)
{
mesh->hasVertexNormals = true;
mesh->vertexNormals = __Q3Alloc<TQ3Vector3D>(numPoints, 'TMvn');
for (int i = 0; i < numPoints; i++)
mesh->vertexNormals[i] = {0, 1, 0};
}
else
{
mesh->vertexNormals = nullptr;
mesh->hasVertexNormals = false;
}
if (featureFlags & kQ3TriMeshDataFeatureVertexColors)
{
mesh->hasVertexColors = true;
mesh->vertexColors = __Q3Alloc<TQ3ColorRGBA>(numPoints, 'TMvc');
for (int i = 0; i < numPoints; i++)
mesh->vertexColors[i] = {1, 1, 1, 1};
}
else
{
mesh->vertexColors = nullptr;
mesh->hasVertexColors = false;
}

View File

@ -143,6 +143,14 @@ typedef enum
kQ3EndianSize32 = 0xFFFFFFFF
} TQ3Endian;
typedef enum
{
kQ3TriMeshDataFeatureNone = 0,
kQ3TriMeshDataFeatureVertexUVs = 1 << 0,
kQ3TriMeshDataFeatureVertexNormals = 1 << 1,
kQ3TriMeshDataFeatureVertexColors = 1 << 2,
} TQ3TriMeshDataFeatureFlags;
typedef struct TQ3Param2D
{
float u;
@ -280,6 +288,7 @@ typedef struct TQ3TriMeshData
int internalTextureID;
uint32_t glTextureName;
bool hasVertexNormals;
bool hasVertexColors;
TQ3ColorRGBA diffuseColor;
@ -350,7 +359,7 @@ void Q3Pixmap_Dispose(TQ3Pixmap*);
#pragma mark -
TQ3TriMeshData* Q3TriMeshData_New(int numTriangles, int numPoints, bool perVertexColors);
TQ3TriMeshData* Q3TriMeshData_New(int numTriangles, int numPoints, int featureFlags);
TQ3TriMeshData* Q3TriMeshData_Duplicate(const TQ3TriMeshData* source);