Jump to content
  • Advertisement
Sign in to follow this  
dblalock04

Quick texturing question

This topic is 2142 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been trying to make time to look into this for several weeks now, but graduate studies, work, family, etc, don't leave enough time for personal projects, so I'm turning to gamedev to hopefully point me in the right direction, or even show me the obvious problem I introduced originally.

This project is mostly a collection of sub-projects and implementations of ideas and techniques, so please excuse the messy/naive code, but I always welcome constructive criticism.

My initial thought was the texture coordinates are wrong, but they appear correct on a quick check in 3ds Max. The screenshot has some notations, I'm also using an object loading that's slightly modified from one I found in one of the DirectX11 game books (.obj is a stop gap until I can write my own format). Please let me know if I've forgotten something.


#pragma pack(push, 1)
private:
struct VertexType
{
XMFLOAT3 position;
XMFLOAT2 texture;
XMFLOAT3 normal;
};
#pragma pack(pop)
bool Mesh::InitializeBuffers(ID3D11Device* device)
VertexType* vertices;
unsigned long* indices;
D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
D3D11_SUBRESOURCE_DATA vertexData, indexData;
HRESULT result;
int i;
vertices = new VertexType[m_vertexCount];
if (!vertices)
return false;
indices = new unsigned long[m_indexCount];
if (!indices)
return false;
for (i = 0; i < m_vertexCount; ++i)
{
vertices.position = XMFLOAT3(m_mesh.x, m_mesh.y, m_mesh.z);
vertices.texture = XMFLOAT2(m_mesh.tu, m_mesh.tv);
vertices.normal = XMFLOAT3(m_mesh.nx, m_mesh.ny, m_mesh.nz);
indices = i;
}

bool Mesh::LoadMesh(WCHAR* meshFilename)
{
bool result;
float *verts, *norms, *texCoords;
int i;
m_objLoader = new ObjLoader;
result = m_objLoader->LoadFromFile(meshFilename);
if (!result)
return false;
m_vertexCount = m_objLoader->GetVertexCount();
m_indexCount = m_vertexCount;
verts = m_objLoader->GetVerticesPtr();
norms = m_objLoader->GetNormalsPtr();
texCoords = m_objLoader->GetTexCoordsPtr();

int vIndex = 0;
int nIndex = 0;
int tIndex = 0;

m_mesh = new MeshType[m_vertexCount];
for (i = 0; i < m_vertexCount; ++i)
{
m_mesh.z = verts[vIndex] * -1.0f; // Correct vertex winding
vIndex++;
m_mesh.y = verts[vIndex];
vIndex++;
m_mesh.x = verts[vIndex];
vIndex++;
}
for (i = 0; i < m_objLoader->GetTexCoordsCount(); ++i)
{
m_mesh.tu = texCoords[tIndex];
tIndex++;
m_mesh.tv = texCoords[tIndex];
tIndex++;
}
for (i = 0; i < m_vertexCount / 3; ++i)
{
m_mesh.nx = norms[nIndex];
nIndex++;
m_mesh.ny = norms[nIndex];
nIndex++;
m_mesh.nz = norms[nIndex];
nIndex++;
}
if (m_objLoader)
{
m_objLoader->Shutdown();
delete m_objLoader;
m_objLoader = 0;
}
return true;
}

Share this post


Link to post
Share on other sites
Advertisement
It looks to me that there's at least three problems with the code (I only looked at the code inlined into your post, not the .zip):

1. You're filling m_mesh with m_vertexCount vertex positions, but only m_vertexCount/3 normals, and later you're using the uninitialised normals.
2. You're filling m_mesh with m_vertexCount vertex positions, but m_objLoader->GetTexCoordsCount() UVs. I'm not sure if m_objLoader->GetTexCoordsCount() is appropriate or not, but purely going off the function name, I suspect it's not.
3. You're setting up the indices as 1, 2, 3, 4, 5... etc. You probably need to be extracting the correct indices from the file somehow instead.

Share this post


Link to post
Share on other sites
Yea, going with what C0lumbo said, it looks like you were parsing the faces improperly (looking at the OBJ file it looks like each face is defined by 3 indices - which makes sense, but then you were grabbing 9 at a time to define 1 face?) and then the loops you had at the end seemed to be a bit of a mess. I tried to clean it up real quick (haven't compiled this but it should be mostly good). Hope this helps.


struct ObjModelVert
{
float m_position[3];
float m_normal[3];
float m_uv[2];
};

struct ObjModel
{
vector<ObjModelVert> m_vertices;
vector<int> m_indices;
};

bool ObjLoader::LoadFromFile(wchar_t* filename)
{
ifstream fin;
char* buffer;
int filesize;
Tokenizer tokenStream, lineStream, faceStream;
vector<float> verts;
vector<float> norms;
vector<float> texC;

// Assume we have the following member in this class:
// ObjModel m_loadedModel;

string tempLine, token;
fin.open(filename, ios_base::in);

if (fin.fail())
return false;

fin.seekg(0, ios_base::end);
filesize = static_cast<int>(fin.tellg());
fin.seekg(0, ios_base::beg);
buffer = new char[filesize];
memset(buffer, '\0', filesize);
fin.read(buffer, filesize);
fin.close();

// Send buffer to tokenstream
tokenStream.SetStream(buffer);
if (buffer)
{
delete[] buffer;
buffer = 0;
}

char lineDelimiters[2] = { '\n', ' ' };
while (tokenStream.MoveToNextLine(&tempLine))
{

lineStream.SetStream((char*)tempLine.c_str());
tokenStream.GetNextToken(0, 0, 0);

if (!lineStream.GetNextToken(&token, lineDelimiters, 2))
continue;

if (strcmp(token.c_str(), "v") == 0)
{
lineStream.GetNextToken(&token, lineDelimiters, 2);
verts.push_back((float)atof(token.c_str()));
lineStream.GetNextToken(&token, lineDelimiters, 2);
verts.push_back((float)atof(token.c_str()));
lineStream.GetNextToken(&token, lineDelimiters, 2);
verts.push_back((float)atof(token.c_str()));
}
else if (strcmp(token.c_str(), "vn") == 0)
{
lineStream.GetNextToken(&token, lineDelimiters, 2);
norms.push_back((float)atof(token.c_str()));
lineStream.GetNextToken(&token, lineDelimiters, 2);
norms.push_back((float)atof(token.c_str()));

lineStream.GetNextToken(&token, lineDelimiters, 2);
norms.push_back((float)atof(token.c_str()));
}
else if (strcmp(token.c_str(), "vt") == 0)
{
lineStream.GetNextToken(&token, lineDelimiters, 2);
texC.push_back((float)atof(token.c_str()));
lineStream.GetNextToken(&token, lineDelimiters, 2);
texC.push_back((float)atof(token.c_str()));
}
else if (strcmp(token.c_str(), "f") == 0)
{
char faceTokens[3] = { '\n', ' ', '/' };
string faceIndex;
faceStream.SetStream((char*)tempLine.c_str());
faceStream.GetNextToken(0, 0, 0);

// OBJ has face indices as 1 based rather than 0 based. Also this data doesn't need any special re-ordering
// so we can copy it to our loaded model object directly.
faceStream.GetNextToken(&faceIndex, faceTokens, 3);
m_loadedModel.m_indices.push_back((int)atoi(faceIndex.c_str()) - 1);
faceStream.GetNextToken(&faceIndex, faceTokens, 3);
m_loadedModel.m_indices.push_back((int)atoi(faceIndex.c_str()) - 1);
faceStream.GetNextToken(&faceIndex, faceTokens, 3);
m_loadedModel.m_indices.push_back((int)atoi(faceIndex.c_str()) - 1);
}
token[0] = '\0';
}

int vIndex = 0, nIndex = 0, tIndex = 0;
for(; vIndex < verts.size(); vIndex += 3, nIndex += 3, tIndex +=2)
{
ObjModelVert newVert;

// Position Data
newVert.m_position[0] = verts[vIndex]; // You could probably just do a memcpy here and grab all components at once, but I'll be explicit in this case.
newVert.m_position[1] = verts[vIndex + 1];
newVert.m_position[2] = verts[vIndex + 2];

// Normal data
newVert.m_normal[0] = norms[nIndex];
newVert.m_normal[1] = norms[nIndex + 1];
newVert.m_normal[2] = norms[nIndex + 2];

// Tex Coordinates
newVert.m_uv[0] = texC[tIndex];
newVert.m_uv[1] = texC[tIndex + 1];

m_loadedModel.m_vertices.push_back(newVert);
}

// Don't need to clean up the temporary vectors since they were allocated on the stack, they'll automatically get cleaned up when they go out of scope.
return true;
}

Share this post


Link to post
Share on other sites
The day after I started this thread, I came down with the flu. Right before finals too; lucky me.

Thank you both for your replies, I'll dive into the code this weekend and get it fixed. I never liked using the tokenizer or object loader. They came from a book which has revealed itself to have more than a couple problems with its code examples. Ultimately I want to write a utility to convert from Collada to a binary format that I can copy directly into buffers, but that's pretty far down the list of things I want to implement. Thanks again for the help :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!