\$2

### Image of the Day Submit

IOTD | Top Screenshots

## Quick texturing question

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1fallenphoenix  Members

Posted 04 December 2012 - 09:54 AM

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[i].position = XMFLOAT3(m_mesh[i].x, m_mesh[i].y, m_mesh[i].z);
vertices[i].texture = XMFLOAT2(m_mesh[i].tu, m_mesh[i].tv);
vertices[i].normal = XMFLOAT3(m_mesh[i].nx, m_mesh[i].ny, m_mesh[i].nz);
indices[i] = i;
}

{
bool result;
float *verts, *norms, *texCoords;
int i;
if (!result)
return false;
m_indexCount = m_vertexCount;

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

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


### #2C0lumbo  Members

Posted 04 December 2012 - 10:32 AM

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.

Posted 05 December 2012 - 10:14 AM

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;
};

{
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:

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.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);
faceStream.GetNextToken(&faceIndex, faceTokens, 3);
faceStream.GetNextToken(&faceIndex, faceTokens, 3);
}
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];

}

// 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;
}


### #4fallenphoenix  Members

Posted 07 December 2012 - 01:30 PM

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.