.obj reader
Has anyone created a program that can load up .obj models? And want to share it? Also, can anyone tell me what the g " " section of a .obj model does? And the f # # # s below it?
- + -
Sploof
- + - Sploof
look at
http://www.royriggs.com/obj.html
http://www.neutralzone.org/home/faqsys/docs/obj~1.spe
regards
lunasol
http://www.royriggs.com/obj.html
http://www.neutralzone.org/home/faqsys/docs/obj~1.spe
regards
lunasol
All around the world i''ve got an army of spys, diving in deep ocean with superatomic submarines, listening to all global communications with hightech satellite. When they intercept keywords like "3d" or "OpenGL" or "file format", they write to me a report. My best spy is google. You can reach him at http://www.google.com
:p
lunasol
:p
lunasol
LOL. Sorry, i was looking for "File Spesficiation"(spelling, I spelled it right when I searched).
My informants aren''t too bright.
- + -
Sploof
My informants aren''t too bright.
- + -
Sploof
- + - Sploof
Alright, now i've got one that _should_ work. But it doesn't. I can't really explain the problem, since i'm not finding one. It compiles, and decides not to show anything. I've put in glPushMatrix, glPopMatrix, and glFlush because i thought that one of them might possibly do something. It didn't work, but I left them anyway.
typedef struct tagTEMPVERT
{
float x, y, z;
} TEMPVERT;
typedef struct tagONE
{
float x, y, z;
} ONE;
typedef struct tagTHREE
{
float x, y, z;
} TWO;
typedef struct tagTHRE
{
float x, y, z;
} THREE;
typedef struct tagFACE
{
ONE one;
TWO two;
THREE three;
int onet;
int twot;
int threet;
} FACE;
typedef struct tagCUBE
{
int numface;
TEMPVERT tempvert[50];
FACE face[200];
} SECTOR;
SECTOR cube;
// Read one string of a file
int readstr(FILE *f,char *string)
{
int type_variable=0;
// Type Variables:
// 1= v: Vertex
// 2= f: Face
do
{
fgets(string, 255, f);
// Type of command
if(string[0] == 'v ')
type_variable=1;
if(string[0] == 'f ')
type_variable=2;
} while ((string[0] == '#') || (string[0] == '\n'));
return type_variable;
}
// Load the Information for a .OBJ Model
void BuildModel(char *objname)
{
float x, y, z;
int one,two,three;
int type=0;
int face=1,vert=0;
FILE *filein;
char oneline[40];
filein = fopen(objname, "rt"); // File To Load Data From
do
{
type=readstr(filein,oneline);
if(type==1) // If It's a Vertex Command
{
vert=vert+1;
sscanf(oneline, "%f %f %f %f %f", &x, &y, &z); // Not sure, but I DO know it helps
cube.tempvert[vert].x = x; // Save the X Vertex
cube.tempvert[vert].y = y; // Save the Y Vertex
cube.tempvert[vert].z = z; // Save the Z Vertex
if(vert==3) // Triangle Loop
{
face=face+1; // Increase the triangle name
vert=0; // Reset vertex number
}
}
if(type==2) // If It's A Face Command
{
sscanf(oneline, "%f %f %f %f %f", &one, &two, &three); // Not sure, but I DO know it helps
cube.face[face].onet = one; // Save the X Vertex
cube.face[face].twot = two; // Save the Y Vertex
cube.face[face].threet = three; // Save the Z Vertex
}
} while(type!=0);
// Final Variables
cube.numface=face; // Total Faces
// Placing the Vertexs in the Faces
for(int loop=0;loop {
// The First Vertex
cube.face[loop].one.x=cube.tempvert[cube.face[loop].onet].x;
cube.face[loop].one.y=cube.tempvert[cube.face[loop].onet].y;
cube.face[loop].one.z=cube.tempvert[cube.face[loop].onet].z;
cube.face[loop].two.x=cube.tempvert[cube.face[loop].twot].x;
cube.face[loop].two.y=cube.tempvert[cube.face[loop].twot].y;
cube.face[loop].two.z=cube.tempvert[cube.face[loop].twot].z;
cube.face[loop].three.x=cube.tempvert[cube.face[loop].threet].x;
cube.face[loop].three.y=cube.tempvert[cube.face[loop].threet].y;
cube.face[loop].three.z=cube.tempvert[cube.face[loop].threet].z;
}
fclose(filein);
}
// Simple Stuff to Clear up my Model Code, Builds Each Indivigual Model
void LoadModels()
{
BuildModel("models/cube.obj");
return;
}
// Shows the Model
void ShowModel()
{
for(int loop=0;loop {
glBegin(GL_POLYGON);
glVertex3f(cube.face[loop].one.x,
cube.face[loop].one.y,
cube.face[loop].one.z);
glVertex3f(cube.face[loop].two.x,
cube.face[loop].two.y,
cube.face[loop].two.z);
glVertex3f(cube.face[loop].three.x,
cube.face[loop].three.y,
cube.face[loop].three.z);
glEnd();
}
}
// Graphics
void Draw (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();
glPushMatrix();
// Viewing
glTranslated(0.0f,0.0f,-6.0f);
glTranslated(camx,camy,0.0f);
glColor3d(0.3,0.3,0.3);
glColor3f(1.0,0.0,1.0);
// Loaded Model
ShowModel();
glTranslated(-camx,-camy,0.0f);
// Cursor
glTranslated(-x,y,0.0f);
glBegin(GL_TRIANGLES);
glColor3d(0.0f,1.0f,0.0f);
glVertex3f( 0.0f, 0.1f, 0.0f);
glVertex3f(-0.1f,-0.1f, 0.0f);
glVertex3f( 0.1f,-0.1f, 0.0f);
glEnd();
glPopMatrix();
glFlush();
}
(These, of cource, are the functions used to try to load a model. Oh, and if you want to test it out, you'll need a .obj model called "cube.obj" in the "models/" directory. You'll also need "LoadModels();" in your Init function.
(Edit: forgot my structors)
- + -
Sploof
Edited by - Ionwizard on September 19, 2001 4:46:31 PM
typedef struct tagTEMPVERT
{
float x, y, z;
} TEMPVERT;
typedef struct tagONE
{
float x, y, z;
} ONE;
typedef struct tagTHREE
{
float x, y, z;
} TWO;
typedef struct tagTHRE
{
float x, y, z;
} THREE;
typedef struct tagFACE
{
ONE one;
TWO two;
THREE three;
int onet;
int twot;
int threet;
} FACE;
typedef struct tagCUBE
{
int numface;
TEMPVERT tempvert[50];
FACE face[200];
} SECTOR;
SECTOR cube;
// Read one string of a file
int readstr(FILE *f,char *string)
{
int type_variable=0;
// Type Variables:
// 1= v: Vertex
// 2= f: Face
do
{
fgets(string, 255, f);
// Type of command
if(string[0] == 'v ')
type_variable=1;
if(string[0] == 'f ')
type_variable=2;
} while ((string[0] == '#') || (string[0] == '\n'));
return type_variable;
}
// Load the Information for a .OBJ Model
void BuildModel(char *objname)
{
float x, y, z;
int one,two,three;
int type=0;
int face=1,vert=0;
FILE *filein;
char oneline[40];
filein = fopen(objname, "rt"); // File To Load Data From
do
{
type=readstr(filein,oneline);
if(type==1) // If It's a Vertex Command
{
vert=vert+1;
sscanf(oneline, "%f %f %f %f %f", &x, &y, &z); // Not sure, but I DO know it helps
cube.tempvert[vert].x = x; // Save the X Vertex
cube.tempvert[vert].y = y; // Save the Y Vertex
cube.tempvert[vert].z = z; // Save the Z Vertex
if(vert==3) // Triangle Loop
{
face=face+1; // Increase the triangle name
vert=0; // Reset vertex number
}
}
if(type==2) // If It's A Face Command
{
sscanf(oneline, "%f %f %f %f %f", &one, &two, &three); // Not sure, but I DO know it helps
cube.face[face].onet = one; // Save the X Vertex
cube.face[face].twot = two; // Save the Y Vertex
cube.face[face].threet = three; // Save the Z Vertex
}
} while(type!=0);
// Final Variables
cube.numface=face; // Total Faces
// Placing the Vertexs in the Faces
for(int loop=0;loop
// The First Vertex
cube.face[loop].one.x=cube.tempvert[cube.face[loop].onet].x;
cube.face[loop].one.y=cube.tempvert[cube.face[loop].onet].y;
cube.face[loop].one.z=cube.tempvert[cube.face[loop].onet].z;
cube.face[loop].two.x=cube.tempvert[cube.face[loop].twot].x;
cube.face[loop].two.y=cube.tempvert[cube.face[loop].twot].y;
cube.face[loop].two.z=cube.tempvert[cube.face[loop].twot].z;
cube.face[loop].three.x=cube.tempvert[cube.face[loop].threet].x;
cube.face[loop].three.y=cube.tempvert[cube.face[loop].threet].y;
cube.face[loop].three.z=cube.tempvert[cube.face[loop].threet].z;
}
fclose(filein);
}
// Simple Stuff to Clear up my Model Code, Builds Each Indivigual Model
void LoadModels()
{
BuildModel("models/cube.obj");
return;
}
// Shows the Model
void ShowModel()
{
for(int loop=0;loop
glBegin(GL_POLYGON);
glVertex3f(cube.face[loop].one.x,
cube.face[loop].one.y,
cube.face[loop].one.z);
glVertex3f(cube.face[loop].two.x,
cube.face[loop].two.y,
cube.face[loop].two.z);
glVertex3f(cube.face[loop].three.x,
cube.face[loop].three.y,
cube.face[loop].three.z);
glEnd();
}
}
// Graphics
void Draw (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();
glPushMatrix();
// Viewing
glTranslated(0.0f,0.0f,-6.0f);
glTranslated(camx,camy,0.0f);
glColor3d(0.3,0.3,0.3);
glColor3f(1.0,0.0,1.0);
// Loaded Model
ShowModel();
glTranslated(-camx,-camy,0.0f);
// Cursor
glTranslated(-x,y,0.0f);
glBegin(GL_TRIANGLES);
glColor3d(0.0f,1.0f,0.0f);
glVertex3f( 0.0f, 0.1f, 0.0f);
glVertex3f(-0.1f,-0.1f, 0.0f);
glVertex3f( 0.1f,-0.1f, 0.0f);
glEnd();
glPopMatrix();
glFlush();
}
(These, of cource, are the functions used to try to load a model. Oh, and if you want to test it out, you'll need a .obj model called "cube.obj" in the "models/" directory. You'll also need "LoadModels();" in your Init function.
(Edit: forgot my structors)
- + -
Sploof
Edited by - Ionwizard on September 19, 2001 4:46:31 PM
- + - Sploof
September 20, 2001 03:18 AM
Difficult to say what is going wrong... Have you put traces in the showModel function? If the trace are ok, you are sure that the loading code is ok...
Have you set up lighting?
Another thing : in Draw() function, as you work always with the top matrix on the stack, you can remove pop and push matrix. But keep glFlush (it forces all the command to be send in the pipeline)
regards,
lunasol
Have you set up lighting?
Another thing : in Draw() function, as you work always with the top matrix on the stack, you can remove pop and push matrix. But keep glFlush (it forces all the command to be send in the pipeline)
regards,
lunasol
Honestly, I had never heard of tracing before. Do you mean ray-tracing(ick)? Also, lighting really makes a difference? I wasn''t aware of that.
- + -
Sploof
- + -
Sploof
- + - Sploof
I think i''ve been misunderstood : When i talk about traces i talk neither raytracing nor any rendring techniques. I talk about debugging
like putting printf in your code or using debug tools.
Lighting is very important in ogl! Unless you setup at least one light all the vertices will remain black...
Another thing : are you sure that the camera is looking toward your model?
hope that help
lunasol

Lighting is very important in ogl! Unless you setup at least one light all the vertices will remain black...
Another thing : are you sure that the camera is looking toward your model?
hope that help
lunasol
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement