Skip to main content
gamedev.net
🔒 Locked

Problems drawing a model

Started by joshualimm 4 replies 1.8k views
Original Post
joshualimm
joshualimm
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:

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();
	}
}


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!
joshualimm
joshualimm
Hi! Sorry bout that. Anyway, here's a downsized image, but it's clearly a problem of shading -> normals. :(

Anyway, here it is:

Image Hosted by 4FreeImageHost.com

Even having glShadeModel(GL_SMOOTH) on doesn't seem to give me the right result.

Please help, thanks!
Fiddler
Fiddler
I think that you need to re-normalise your normals when using glScale. Try gLEnable with GL_AUTO_NORMALS as a parameter (I am not sure if this is the correct name, a quick search should give the answer).

In a side note, moving the for-loop inside the glBegin - glEnd pair will give a significant performance boost.
[OpenTK: C# OpenGL 4.4, OpenGL ES 3.0 and OpenAL 1.1. Now with Linux/KMS support!]
evanofsky
evanofsky
Also, try loading a different model. Maybe the normals are loaded wrong in the first place. (I'm having trouble with that as well...)
Kalidor
Kalidor
Quote:
Original post by Fiddler
I think that you need to re-normalise your normals when using glScale. Try gLEnable with GL_AUTO_NORMALS as a parameter (I am not sure if this is the correct name, a quick search should give the answer).
I'm pretty sure that's the problem as well. The correct state to enable is GL_NORMALIZE (or GL_RESCALE_NORMAL since it's a uniform scale, it may be slightly faster). GL_AUTO_NORMAL is for creating normals when using evaluators.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.