🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Modified Lesson 10 Code

Started by
0 comments, last by lc_overlord 17 years, 11 months ago
Hello Everyone. I was trying to get my head tightly wrapped around the Lesson 10 code, and I decided the best way would be to modify it a bit. So, I changed some of the functions to allow polygons to be drawn instead of just triangles. The problem is, trying to draw anything other than a triangle results in black, tryangle shaped holes in the polygon. Here is the setup code I changed:

void SetupWorld()
{
	float x, y, z, u, v;
	int numpolygons, maxverts, t;
	FILE *filein;
	char oneline[255];
	filein = fopen("data/world.spm", "rt");				// File To Load World Data From

	readstr(filein,oneline);
	sscanf(oneline, "NUMPOLLIES %d\n", &numpolygons);

	sector1.polygon = new POLYGON[numpolygons];
	sector1.numpolygons = numpolygons;
	for (int loop = 0; loop < numpolygons; loop++)
	{
	         readstr(filein,oneline);
	         sscanf(oneline, "VERTS %d\n", &maxverts);
             sector1.polygon[loop].maxverts = maxverts;
        for (int vert = 0; vert < maxverts; vert++)
		{
			readstr(filein,oneline);
			sscanf(oneline, "%f %f %f %f %f %i", &x, &y, &z, &u, &v, &t);
			sector1.polygon[loop].vertex[vert].x = x;
			sector1.polygon[loop].vertex[vert].y = y;
			sector1.polygon[loop].vertex[vert].z = z;
			sector1.polygon[loop].vertex[vert].u = u;
			sector1.polygon[loop].vertex[vert].v = v;
            sector1.polygon[loop].t = t;
		}
	}
	fclose(filein);
	return;
} 
And the drawing code:

int DrawGLScene(GLvoid)									// Here's Where We Do All The Drawing
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear The Screen And The Depth Buffer
	glLoadIdentity();									// Reset The View

	GLfloat x_m, y_m, z_m, u_m, v_m;
	GLfloat xtrans = -xpos;
	GLfloat ztrans = -zpos;
	GLfloat ytrans = -walkbias-0.25f;
	GLfloat sceneroty = 360.0f - yrot;

	int numpolygons;

	glRotatef(lookupdown,1.0f,0,0);
	glRotatef(sceneroty,0,1.0f,0);
	
	glTranslatef(xtrans, ytrans, ztrans);
	
	numpolygons = sector1.numpolygons;
	
	// Process Each Triangle
	for (int loop_m = 0; loop_m < numpolygons; loop_m++)
	{
        glBindTexture(GL_TEXTURE_2D, textures[sector1.polygon[loop_m].t].texID);
        glBegin(GL_POLYGON);
        for(int loop_v = 0; loop_v < sector1.polygon[loop_m].maxverts;loop_v++){
			glNormal3f( 0.0f, 0.0f, 1.0f);
			x_m = sector1.polygon[loop_m].vertex[loop_v].x;
			y_m = sector1.polygon[loop_m].vertex[loop_v].y;
			z_m = sector1.polygon[loop_m].vertex[loop_v].z;
			u_m = sector1.polygon[loop_m].vertex[loop_v].u;
			v_m = sector1.polygon[loop_m].vertex[loop_v].v;
			glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);}
			
		glEnd();
	}
	return TRUE;										// Everything Went OK
} 
What did I do wrong? Thanks for any help, Biubid_boy.
Advertisement
I don't know, the only thing i can think of is that it draws one vertic to little for every polygon, can't directly say where the bug is though, maybe maxverts is wrong or that you just don't load enough vertics per poly.

When rendering meshes like this it's a lot faster and easier if you use one type of primitive, like triangles, tristrips might be faster but they are a bit more complex to render.
Also, group the triangles according to the texture, since glBindTexture does take alot of time and should not be switched for evey polygon.

Sort of like this pseudo code
For each texture  {   glBindTexture   glBegin(GL_TRIANGLES);    for each polygon that uses this texture      {        Render triangle      }     glEnd();  }

This topic is closed to new replies.

Advertisement