doctorzeus

07-10-2013, 10:49 AM

Hello,

I am a newbe to OpenGL but have been coding C/C++ for years now and have decided to try my hand at Opengl :) ..

I am currently trying to make my own obj model loader and viewer, have decided to ensure that I also learn as much as possible in regards to OpenGL I want to code the obj loader myself. Unfortunatly I am a bit confused about if I am doing it right and also how I would utilize the data once it is loaded into memory.. I have been reading LOADS of documentation on loading wavefront files however all of them seem to use 3rd party loaders and tools to do so and I want to do it all myself..

Here is the class I created that loads and stores the data:

#include <fstream>

#include <stdlib.h>

const unsigned int MAXVERTEXES = 10000;

const unsigned int MAXUVS = 10000;

const unsigned int MAXNORMALS = 10000;

const unsigned int MAXFACES = 100;

const unsigned int SCALER = 10;

class GraphicsStruc_Model

{

private:

bool IsModelLoaded;

unsigned int numVertexes, numUVS, numNormals, numFaces;

float VERTEXES[MAXVERTEXES][2];

float UVS[MAXUVS][2];

float NORMALS[MAXNORMALS][2];

unsigned int VERTEXES_INDEX[MAXFACES][2];

unsigned int UVS_INDEX[MAXFACES][2];

unsigned int NORMALS_INDEX[MAXFACES][2];

public:

GraphicsStruc_Model()

{

IsModelLoaded = false;

}

float GetVertex(int vertexNum, int xYZ)

{

return VERTEXES[vertexNum][xYZ]/SCALER;

}

float GetUV(int uvNum, int xYZ)

{

return UVS[uvNum][xYZ]/SCALER;

}

float GetNormal(int normalNum, int xYZ)

{

return NORMALS[normalNum][xYZ]/SCALER;

}

unsigned int GetFaces_Verticies(int faceNum, int xYZ)

{

return VERTEXES_INDEX[faceNum][xYZ];

}

unsigned int GetFaces_Uvs(int faceNum, int xYZ)

{

return UVS_INDEX[faceNum][xYZ];

}

unsigned int GetFaces_Normals(int faceNum, int xYZ)

{

return NORMALS_INDEX[faceNum][xYZ];

}

bool GetIsModelLoaded()

{

return IsModelLoaded;

}

unsigned int GetNumberVertextes()

{

return numVertexes;

}

unsigned int GetNumberNormals()

{

return numNormals;

}

unsigned int GetNumberFaces()

{

return numFaces;

}

bool LoadModel(char* modelLocation)

{

std::ifstream fileHandle;

std::string readStr;

unsigned int index, CountN, CountM, vertexCount, uvCount, normalCount, faceCount;

fileHandle.open(modelLocation);

if (fileHandle)

{

CountN = vertexCount = uvCount = normalCount = 0;

while (std::getline(fileHandle, readStr))

{

//Load Vertexes first

if (readStr.substr(0, 2) == "v ") //Vertexes

{

readStr.erase(0,2);

CountN = 0;

while (CountN < 3)

{

if (CountN == 2)

index = readStr.length();

else

index = readStr.find(" ", 0);

VERTEXES[vertexCount][CountN] = atof(readStr.substr(0, index).c_str());

readStr.erase(0, index+1);

++CountN;

}

++vertexCount;

}

else if (readStr.substr(0,2) == "vt") //UVS

{

readStr.erase(0,2);

CountN = 0;

while (CountN < 3)

{

if (CountN == 2)

index = readStr.length();

else

index = readStr.find(" ", 0);

UVS[uvCount][CountN] = atof(readStr.substr(0, index).c_str());

readStr.erase(0, index+1);

++CountN;

}

++uvCount;

}

else if (readStr.substr(0,2) == "vn") //NORMALS

{

readStr.erase(0,2);

CountN = 0;

while (CountN < 3)

{

if (CountN == 2)

index = readStr.length();

else

index = readStr.find(" ", 0);

NORMALS[normalCount][CountN] = atof(readStr.substr(0, index).c_str());

readStr.erase(0, index+1);

++CountN;

}

++normalCount;

}

else if (readStr.substr(0,2) == "f ") //FACES

{

readStr.erase(0,2);

CountN = CountM = 0;

while (CountN < 3)

{

while (CountM < 3)

{

if (CountM == 0) //VERTEXES

{

index = readStr.find("/", 0);

VERTEXES_INDEX[vertexCount][CountN] = atoi(readStr.substr(0, index).c_str());

}

else if (CountM == 1) //UVS

{

index = readStr.find("/", 0);

UVS_INDEX[vertexCount][CountN] = atoi(readStr.substr(0, index).c_str());

}

else if (CountM == 2) //NORMALS

{

index = readStr.find(" ", 0);

NORMALS_INDEX[vertexCount][CountN] = atoi(readStr.substr(0, index).c_str());

}

readStr.erase(0, index+1);

++CountM;

++faceCount;

}

++CountN;

}

}

}

fileHandle.close();

IsModelLoaded = true;

numVertexes = vertexCount;

numUVS = uvCount;

numNormals = normalCount;

numFaces = faceCount;

}

else

IsModelLoaded = false;

return IsModelLoaded;

}

};

And also the wavefront file I am trying to load (it should be a simple cube I explorted from Blender):

# Blender v2.67 (sub 1) OBJ File: ''

# www.blender.org

o Cube

v 1.000000 -1.000000 -1.000000

v 1.000000 -1.000000 1.000000

v -1.000000 -1.000000 1.000000

v -1.000000 -1.000000 -1.000000

v 1.000000 1.000000 -0.999999

v 0.999999 1.000000 1.000001

v -1.000000 1.000000 1.000000

v -1.000000 1.000000 -1.000000

vn 0.000000 -1.000000 0.000000

vn -0.000000 1.000000 0.000000

vn 1.000000 -0.000000 0.000001

vn -0.000000 -0.000000 1.000000

vn -1.000000 -0.000000 -0.000000

vn 0.000000 0.000000 -1.000000

vn 1.000000 0.000000 -0.000000

s off

f 1//1 2//1 3//1

f 5//2 8//2 7//2

f 1//3 5//3 6//3

f 2//4 6//4 3//4

f 3//5 7//5 4//5

f 5//6 1//6 4//6

f 4//1 1//1 3//1

f 6//2 5//2 7//2

f 2//7 1//7 6//7

f 6//4 7//4 3//4

f 7//5 8//5 4//5

f 8//6 5//6 4//6

I have tried quite a few exaples of how to implement it online without 3rd party tools however everything I have tried renders incorrectly..

If someone would be kind enough to verify that this code is correct and tell me how to implement the data into opengl I would be eternally greatful.

Many Thanks

Doctrorzeus

I am a newbe to OpenGL but have been coding C/C++ for years now and have decided to try my hand at Opengl :) ..

I am currently trying to make my own obj model loader and viewer, have decided to ensure that I also learn as much as possible in regards to OpenGL I want to code the obj loader myself. Unfortunatly I am a bit confused about if I am doing it right and also how I would utilize the data once it is loaded into memory.. I have been reading LOADS of documentation on loading wavefront files however all of them seem to use 3rd party loaders and tools to do so and I want to do it all myself..

Here is the class I created that loads and stores the data:

#include <fstream>

#include <stdlib.h>

const unsigned int MAXVERTEXES = 10000;

const unsigned int MAXUVS = 10000;

const unsigned int MAXNORMALS = 10000;

const unsigned int MAXFACES = 100;

const unsigned int SCALER = 10;

class GraphicsStruc_Model

{

private:

bool IsModelLoaded;

unsigned int numVertexes, numUVS, numNormals, numFaces;

float VERTEXES[MAXVERTEXES][2];

float UVS[MAXUVS][2];

float NORMALS[MAXNORMALS][2];

unsigned int VERTEXES_INDEX[MAXFACES][2];

unsigned int UVS_INDEX[MAXFACES][2];

unsigned int NORMALS_INDEX[MAXFACES][2];

public:

GraphicsStruc_Model()

{

IsModelLoaded = false;

}

float GetVertex(int vertexNum, int xYZ)

{

return VERTEXES[vertexNum][xYZ]/SCALER;

}

float GetUV(int uvNum, int xYZ)

{

return UVS[uvNum][xYZ]/SCALER;

}

float GetNormal(int normalNum, int xYZ)

{

return NORMALS[normalNum][xYZ]/SCALER;

}

unsigned int GetFaces_Verticies(int faceNum, int xYZ)

{

return VERTEXES_INDEX[faceNum][xYZ];

}

unsigned int GetFaces_Uvs(int faceNum, int xYZ)

{

return UVS_INDEX[faceNum][xYZ];

}

unsigned int GetFaces_Normals(int faceNum, int xYZ)

{

return NORMALS_INDEX[faceNum][xYZ];

}

bool GetIsModelLoaded()

{

return IsModelLoaded;

}

unsigned int GetNumberVertextes()

{

return numVertexes;

}

unsigned int GetNumberNormals()

{

return numNormals;

}

unsigned int GetNumberFaces()

{

return numFaces;

}

bool LoadModel(char* modelLocation)

{

std::ifstream fileHandle;

std::string readStr;

unsigned int index, CountN, CountM, vertexCount, uvCount, normalCount, faceCount;

fileHandle.open(modelLocation);

if (fileHandle)

{

CountN = vertexCount = uvCount = normalCount = 0;

while (std::getline(fileHandle, readStr))

{

//Load Vertexes first

if (readStr.substr(0, 2) == "v ") //Vertexes

{

readStr.erase(0,2);

CountN = 0;

while (CountN < 3)

{

if (CountN == 2)

index = readStr.length();

else

index = readStr.find(" ", 0);

VERTEXES[vertexCount][CountN] = atof(readStr.substr(0, index).c_str());

readStr.erase(0, index+1);

++CountN;

}

++vertexCount;

}

else if (readStr.substr(0,2) == "vt") //UVS

{

readStr.erase(0,2);

CountN = 0;

while (CountN < 3)

{

if (CountN == 2)

index = readStr.length();

else

index = readStr.find(" ", 0);

UVS[uvCount][CountN] = atof(readStr.substr(0, index).c_str());

readStr.erase(0, index+1);

++CountN;

}

++uvCount;

}

else if (readStr.substr(0,2) == "vn") //NORMALS

{

readStr.erase(0,2);

CountN = 0;

while (CountN < 3)

{

if (CountN == 2)

index = readStr.length();

else

index = readStr.find(" ", 0);

NORMALS[normalCount][CountN] = atof(readStr.substr(0, index).c_str());

readStr.erase(0, index+1);

++CountN;

}

++normalCount;

}

else if (readStr.substr(0,2) == "f ") //FACES

{

readStr.erase(0,2);

CountN = CountM = 0;

while (CountN < 3)

{

while (CountM < 3)

{

if (CountM == 0) //VERTEXES

{

index = readStr.find("/", 0);

VERTEXES_INDEX[vertexCount][CountN] = atoi(readStr.substr(0, index).c_str());

}

else if (CountM == 1) //UVS

{

index = readStr.find("/", 0);

UVS_INDEX[vertexCount][CountN] = atoi(readStr.substr(0, index).c_str());

}

else if (CountM == 2) //NORMALS

{

index = readStr.find(" ", 0);

NORMALS_INDEX[vertexCount][CountN] = atoi(readStr.substr(0, index).c_str());

}

readStr.erase(0, index+1);

++CountM;

++faceCount;

}

++CountN;

}

}

}

fileHandle.close();

IsModelLoaded = true;

numVertexes = vertexCount;

numUVS = uvCount;

numNormals = normalCount;

numFaces = faceCount;

}

else

IsModelLoaded = false;

return IsModelLoaded;

}

};

And also the wavefront file I am trying to load (it should be a simple cube I explorted from Blender):

# Blender v2.67 (sub 1) OBJ File: ''

# www.blender.org

o Cube

v 1.000000 -1.000000 -1.000000

v 1.000000 -1.000000 1.000000

v -1.000000 -1.000000 1.000000

v -1.000000 -1.000000 -1.000000

v 1.000000 1.000000 -0.999999

v 0.999999 1.000000 1.000001

v -1.000000 1.000000 1.000000

v -1.000000 1.000000 -1.000000

vn 0.000000 -1.000000 0.000000

vn -0.000000 1.000000 0.000000

vn 1.000000 -0.000000 0.000001

vn -0.000000 -0.000000 1.000000

vn -1.000000 -0.000000 -0.000000

vn 0.000000 0.000000 -1.000000

vn 1.000000 0.000000 -0.000000

s off

f 1//1 2//1 3//1

f 5//2 8//2 7//2

f 1//3 5//3 6//3

f 2//4 6//4 3//4

f 3//5 7//5 4//5

f 5//6 1//6 4//6

f 4//1 1//1 3//1

f 6//2 5//2 7//2

f 2//7 1//7 6//7

f 6//4 7//4 3//4

f 7//5 8//5 4//5

f 8//6 5//6 4//6

I have tried quite a few exaples of how to implement it online without 3rd party tools however everything I have tried renders incorrectly..

If someone would be kind enough to verify that this code is correct and tell me how to implement the data into opengl I would be eternally greatful.

Many Thanks

Doctrorzeus