Original Post
hi! I've created a function that's responsible of loading in a text-file-based model, and another function to draw the whole model. Well, I dare say that all the vertices position have been correctly imported, since the function successfully draws the model. Problem is, there's something wrong with the way the model is being shaded. Can someone help? I'm attaching the data structure and 2 functions that I've created: and btw, just something to note: The model's data (vertices/triangles/uv/normals) have been stored correctly. The format of a model file is like: Right now, I'm suspecting that there's something wrong with the way I've loaded my model, or the way I'm drawing the model. But if the need arise, I can attach the whole program, including a sample model. Please help, thanks!
numVerts
numTrigs
(vertex_1)[uv_1]
..
(vertex_n)[uv_n]
[empty line]
(trig_index_1) [normal_1)
..
(trig_index_n) [normal_n]
// Structure to store model information
typedef struct
{
UINT numVertices;
UINT numFaces;
UINT **faces;
float **vertices;
float **uvs;
float **normals;
} model_t;
// Loads a text file model
model_t *LoadModel(char *filename)
{
FILE *fp = fopen(filename, "r");
model_t *model = (model_t*)malloc(sizeof(model_t));
char data[128];
// Get number of vertices
fgets(data, 128, fp);
sscanf(data, "%d", &model->numVertices);
printf("Num vertices: %d\n", model->numVertices);
// Get number of triangles
fgets(data, 128, fp);
sscanf(data, "%d", &model->numFaces);
printf("Num Triangles: %d\n", model->numFaces);
// Allocate memory for UV, vertices and normals
model->vertices = (float**)malloc(sizeof(float*)*model->numVertices);
model->faces = (UINT**)malloc(sizeof(UINT*)*model->numFaces);
model->uvs = (float**)malloc(sizeof(float*)*model->numVertices);
model->normals = (float**)malloc(sizeof(float*)*model->numFaces);
// Read Vertices and UVs
int i;
for (i=0; i<model->numVertices; ++i)
{
model->vertices = (float*)malloc(3*sizeof(float));
model->uvs = (float*)malloc(2*sizeof(float));
fgets(data, 128, fp);
sscanf(data, "(%f, %f, %f) [%f, %f]",
&model->vertices[0],
&model->vertices[1],
&model->vertices[2],
&model->uvs[0],
&model->uvs[1]
);
}
fgets(data, 128, fp);
// Read face indexes and normals
for (i=0; i<model->numFaces; ++i)
{
model->faces = (UINT*)malloc(3*sizeof(UINT));
model->normals = (float*)malloc(3*sizeof(float));
fgets(data, 128, fp);
sscanf(data, "(%d, %d, %d) [%f, %f, %f]",
&model->faces[0],
&model->faces[1],
&model->faces[2],
&model->normals[0],
&model->normals[1],
&model->normals[2]
);
}
fclose(fp);
return model;
}
// Draw model using Triangles
void DrawTrigModel(model_t *model)
{
int i;
glScalef(.1,.1,.1);
for (i=0; i<model->numFaces; i++)
{
//glBegin(GL_TRIANGLES);
glBegin(GL_LINES);
glNormal3fv(model->normals);
glVertex3fv(model->vertices[model->faces[0]]);
glVertex3fv(model->vertices[model->faces[1]]);
glVertex3fv(model->vertices[model->faces[2]]);
glEnd();
}
}
