• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
dblalock04

Quick texturing question

3 posts in this topic

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.

[CODE]
#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 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[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++;
}
if (m_objLoader)
{
m_objLoader->Shutdown();
delete m_objLoader;
m_objLoader = 0;
}
return true;
}
[/CODE]
0

Share this post


Link to post
Share on other sites
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.
1

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.

[CODE]
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;
}
[/CODE]
1

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 :)
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0