ok im trying to load obj files and display them in opengl. exporting from lightwave gives them a format like this.
####
#
# OBJ File Generated by LightWave 3D(TM)
# LightWave 3D OBJ Export v2.1
#
####
# Object: 2
#
# Vertices: 8
# Points: 0
# Lines: 0
# Faces: 12
# Materials: 1
#
####
o 2
# Vertex list
v -0.60960 -0.60960 0.63500
v 0.60960 -0.60960 0.63500
v 0.60960 -0.60960 -0.63500
v -0.60960 -0.60960 -0.63500
v -0.60960 0.60960 0.63500
v 0.60960 0.60960 0.63500
v 0.60960 0.60960 -0.63500
v -0.60960 0.60960 -0.63500
# Face list
usemtl Default
f 4 2 1
f 4 3 2
f 5 1 2
f 6 5 2
f 3 6 2
f 7 6 3
f 5 4 1
f 5 8 4
f 8 5 6
f 7 8 6
f 8 3 4
f 8 7 3
# End of file
seems pretty easy, there are a few problems.
i have gotten it to read separate files that are like this:
# Vertices: 8
v -0.60960 -0.60960 0.63500
v 0.60960 -0.60960 0.63500
v 0.60960 -0.60960 -0.63500
v -0.60960 -0.60960 -0.63500
v -0.60960 0.60960 0.63500
v 0.60960 0.60960 0.63500
v 0.60960 0.60960 -0.63500
v -0.60960 0.60960 -0.63500
and this:
# Faces: 12
f 4 2 1
f 4 3 2
f 5 1 2
f 6 5 2
f 3 6 2
f 7 6 3
f 5 4 1
f 5 8 4
f 8 5 6
f 7 8 6
f 8 3 4
f 8 7 3
but if any lines are out of order or if i combine these into the same file like above nothing will load.
i kinda based my code on lesson 25.
here are the two loading functions for vertices and faces.
it only loads all triangles.
void readstr(FILE *f,char *string) // Reads A String From File (f)
{
do // Do This
{
fgets(string, 255, f); // Gets A String Of 255 Chars Max From f (File)
} while ((string[0] == '/') || (string[0] == '\n'));// Until End Of Line Is Reached
return; // Return
}
void objload(char *name,OBJECT *k) // Loads Object From File (name)
{
int ver; // Will Hold Vertice Count
float rx,ry,rz; // Hold Vertex X, Y & Z Position
FILE *filein; // Filename To Open
char oneline[255]; // Holds One Line Of Text (255 Chars Max)
filein = fopen(name, "rt"); // Opens The File For Reading Text In Translated Mode
readstr(filein,oneline); // Jumps To Code That Reads One Line Of Text From The File
sscanf(oneline, "# Vertices: %d\n", &ver); // Scans Text For "Vertices: ". Number After Is Stored In ver
k->verts=ver; // Sets Objects verts Variable To Equal The Value Of ver
objallocate(k,ver); // Jumps To Code That Allocates Ram To Hold The Object
for (int i=0;i<ver;i++) // Loops Through The Vertices
{
readstr(filein,oneline); // Reads In The Next Line Of Text
sscanf(oneline, "v %f %f %f", &rx, &ry, &rz); // Searches For 3 verteces, Floating Point Numbers, Store In rx,ry & rz
k->points.x = rx; // Sets Objects (k) points.x Value To rx
k->points.y = ry; // Sets Objects (k) points.y Value To ry
k->points.z = rz; // Sets Objects (k) points.z Value To rz
}
fclose(filein); // Close The File
if(ver>maxver) maxver=ver; // If ver Is Greater Than maxver Set maxver Equal To ver
} // Keeps Track Of Highest Number Of Vertices Used In Any Of The
void objfindfaces(char *name,FACES *f) // Loads Object From File (name)
{
int faces; // Will Hold face Count
float one,two,three; // Hold Vertices
FILE *filein; // Filename To Open
char oneline[255]; // Holds One Line Of Text (255 Chars Max)
filein = fopen(name, "rt"); // Opens The File For Reading Text In Translated Mode
readstr(filein,oneline); // Jumps To Code That Reads One Line Of Text From The File
sscanf(oneline, "# Faces: %d\n", &faces); // Scans Text For "Vertices: ". Number After Is Stored In ver
f->faces=faces; // Sets FACES faces Variable To Equal The Value Of faces
faceallocate(f,faces); // Jumps To Code That Allocates Ram To Hold The Object
for (int i=0;i<faces;i++) // Loops Through The faces
{
readstr(filein,oneline); // Reads In The Next Line Of Text
sscanf(oneline, "f %f %f %f", &one, &two, &three); // Searches For 3 vertices, Floating Point Numbers, Store In one,two & three
f->facenumber.one = one-1;
f->facenumber.two = two-1;
f->facenumber.three = three-1;
}
fclose(filein); // Close The File
}
what is my problem?
shouldn't i be able to read from the original file?
oh and my other questions.
what do i do about normals?
and textures?
thanks