Skip to content

Commit

Permalink
Merge pull request #95 from Tynukua/group-obj-file
Browse files Browse the repository at this point in the history
WIP Group Mesh
  • Loading branch information
gecko0307 authored Nov 22, 2024
2 parents 2dbbee6 + 4e403e8 commit 1e98e0d
Showing 1 changed file with 89 additions and 42 deletions.
131 changes: 89 additions & 42 deletions src/dagon/resource/obj.d
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ struct ObjFace
class OBJAsset: Asset
{
Mesh mesh;
Mesh[string] groupMesh;

this(Owner o)
{
Expand Down Expand Up @@ -146,6 +147,8 @@ class OBJAsset: Asset
tmpNormals[] = Vector3f(0, 0, 0);
tmpTexcoords[] = Vector2f(0, 0);

string currentGroup;

float x, y, z;
uint v1, v2, v3, v4;
uint t1, t2, t3, t4;
Expand Down Expand Up @@ -182,6 +185,33 @@ class OBJAsset: Asset
}
else if (line.startsWith("vp"))
{
}
else if (line.startsWith("g"))
{
auto m = fillMesh(
tmpFaces,
tmpTexcoords, numTexcoords,
tmpNormals, numNormals,
tmpVertices, numVerts,
needGenNormals
);

if (currentGroup != "")
{
groupMesh[currentGroup] = m;
}
else
{
mesh = m;
}
// TODO:
// nulify tmp normals, faces, vertises
// another solution: use slice for fill group mesh
if (formattedRead(line, "g %s", &currentGroup)){
}
else
assert(0);

}
else if (line.startsWith("f"))
{
Expand Down Expand Up @@ -239,84 +269,101 @@ class OBJAsset: Asset
assert(0);
}
}

mesh = fillMesh(
tmpFaces,
tmpTexcoords, numTexcoords,
tmpNormals, numNormals,
tmpVertices, numVerts,
needGenNormals
);
Delete(fileStr);

mesh.indices = New!(uint[3][])(tmpFaces.length);
uint numUniqueVerts = cast(uint)mesh.indices.length * 3;
mesh.vertices = New!(Vector3f[])(numUniqueVerts);
mesh.normals = New!(Vector3f[])(numUniqueVerts);
if (tmpVertices.length)
Delete(tmpVertices);
if (tmpNormals.length)
Delete(tmpNormals);
if (tmpTexcoords.length)
Delete(tmpTexcoords);
tmpFaces.free();

mesh.calcBoundingBox();

mesh.dataReady = true;

return true;
}

Mesh fillMesh(
Array!ObjFace faces,
Vector2f[] tmpTexcoords, uint numTexcoords,
Vector3f[] tmpNormals, uint numNormals,
Vector3f[] tmpVertices, uint numVerts,
bool needGenNormals) {
auto m = New!Mesh(this);
m.indices = New!(uint[3][])(faces.length);
uint numUniqueVerts = cast(uint)m.indices.length * 3;
m.vertices = New!(Vector3f[])(numUniqueVerts);
m.normals = New!(Vector3f[])(numUniqueVerts);
mesh.texcoords = New!(Vector2f[])(numUniqueVerts);

uint index = 0;

foreach(i, ref ObjFace f; tmpFaces)
foreach(i, ref ObjFace f; faces)
{
if (numVerts)
{
mesh.vertices[index] = tmpVertices[f.v[0]];
mesh.vertices[index+1] = tmpVertices[f.v[1]];
mesh.vertices[index+2] = tmpVertices[f.v[2]];
m.vertices[index] = tmpVertices[f.v[0]];
m.vertices[index+1] = tmpVertices[f.v[1]];
m.vertices[index+2] = tmpVertices[f.v[2]];
}
else
{
mesh.vertices[index] = Vector3f(0, 0, 0);
mesh.vertices[index+1] = Vector3f(0, 0, 0);
mesh.vertices[index+2] = Vector3f(0, 0, 0);
m.vertices[index] = Vector3f(0, 0, 0);
m.vertices[index+1] = Vector3f(0, 0, 0);
m.vertices[index+2] = Vector3f(0, 0, 0);
}

if (numNormals)
{
mesh.normals[index] = tmpNormals[f.n[0]];
mesh.normals[index+1] = tmpNormals[f.n[1]];
mesh.normals[index+2] = tmpNormals[f.n[2]];
m.normals[index] = tmpNormals[f.n[0]];
m.normals[index+1] = tmpNormals[f.n[1]];
m.normals[index+2] = tmpNormals[f.n[2]];
}
else
{
mesh.normals[index] = Vector3f(0, 0, 0);
mesh.normals[index+1] = Vector3f(0, 0, 0);
mesh.normals[index+2] = Vector3f(0, 0, 0);
m.normals[index] = Vector3f(0, 0, 0);
m.normals[index+1] = Vector3f(0, 0, 0);
m.normals[index+2] = Vector3f(0, 0, 0);
}

if (numTexcoords)
{
mesh.texcoords[index] = tmpTexcoords[f.t[0]];
mesh.texcoords[index+1] = tmpTexcoords[f.t[1]];
mesh.texcoords[index+2] = tmpTexcoords[f.t[2]];
m.texcoords[index] = tmpTexcoords[f.t[0]];
m.texcoords[index+1] = tmpTexcoords[f.t[1]];
m.texcoords[index+2] = tmpTexcoords[f.t[2]];
}
else
{
mesh.texcoords[index] = Vector2f(0, 0);
mesh.texcoords[index+1] = Vector2f(0, 0);
mesh.texcoords[index+2] = Vector2f(0, 0);
m.texcoords[index] = Vector2f(0, 0);
m.texcoords[index+1] = Vector2f(0, 0);
m.texcoords[index+2] = Vector2f(0, 0);
}

mesh.indices[i][0] = index;
mesh.indices[i][1] = index + 1;
mesh.indices[i][2] = index + 2;
m.indices[i][0] = index;
m.indices[i][1] = index + 1;
m.indices[i][2] = index + 2;

index += 3;
}


if (needGenNormals)
mesh.generateNormals();

if (tmpVertices.length)
Delete(tmpVertices);
if (tmpNormals.length)
Delete(tmpNormals);
if (tmpTexcoords.length)
Delete(tmpTexcoords);
tmpFaces.free();

mesh.calcBoundingBox();

mesh.dataReady = true;

return true;
m.generateNormals();

return m;
}


override bool loadThreadUnsafePart()
{
mesh.prepareVAO();
Expand Down

0 comments on commit 1e98e0d

Please sign in to comment.