# Simple Polygon drawing issue

I have been creating a simple 3D game in OpenGL.

I am attempting to draw a large 6 sided box. I have broken it down into drawing the 6 sides as 6 separate polygons. I had initially defined all the vertices separately, but have no created functions to define these vertices instead. In doing so I have now broken it. :o

The top and bottom sides of the box draw with no problems. However the front/back and two sides will not draw.

I have attached the relevant code in the hope that someone may be able to spot where I am going wrong. I appreciate any help you can offer.

``````
void VerticesAlongX(int Distance, float StartValueX, float PositionY, float PositionZ, int Direction)
{
int i;
for (i = 0; i < Distance + 1; i++)
{
glVertex3f(StartValueX + i*(Direction), PositionY, PositionZ);
}
}

void VerticesAlongZ(int Distance, float PositionX, float PositionY, float StartValueZ, int Direction)
{
int i;
for (i = 0; i < Distance + 1; i++)
{
glVertex3f(PositionX, PositionY, StartValueZ + i*Direction);
}
}

void DrawBase()
{
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, ColourNormal);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specReflection);
glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, 30);

glMatrixMode(GL_MODELVIEW);

// Translate the whole scene out and into view
// This is the initial viewing transformation
glTranslatef(0.0f, 0.0f, -35.0f);

//Left side -- Not Working
//glBegin(GL_POLYGON);
//glNormal3f(-1.0f, 0.0f, 0.0f);
//VerticesAlongZ(8, -12.0f, -12.0f, -7.0f, 1);
//VerticesAlongZ(7, -12.0f, -11.0f, 0.0f, -1);
//glEnd();

//Right side -- Not working
//glBegin(GL_POLYGON);
//glNormal3f(1.0f, 0.0f, 0.0f);
//VerticesAlongZ(7, 12.0f, -12.0f, 0.0f, -1);
//VerticesAlongZ(7, 12.0f, -11.0f, -7.0f, 1);
//glEnd();

//Back -- Notworking
//glBegin(GL_POLYGON);
//glNormal3f(0.0f, 0.0f, -1.0f);
//VerticesAlongX(24, 12.0f, -12.0f, -7.0f, -1);
//VerticesAlongX(24, -12.0f, -11.0f, -7.0f, 1);
//glEnd();

//Bottom
glBegin(GL_POLYGON);
glNormal3f(0.0f, -1.0f, 0.0f);
VerticesAlongX(24, -12.0f, -12.0f, 0.0f, 1);
VerticesAlongZ(7, 12.0f, -12.0f, 0.0f, -1);
VerticesAlongX(24, 12.0f, -12.0f, -7.0f, -1);
VerticesAlongZ(7, -12.0f, -12.0f, -7.0f, 1);
glEnd();

//Top Side
glBegin(GL_POLYGON);
glNormal3f(0.0f, 1.0f, 0.0f);
VerticesAlongX(24, -12.0f, -11.0f, 0.0f, 1);
VerticesAlongZ(7, 12.0f, -11.0f, 0.0f, -1);
VerticesAlongX(24, 12.0f, -11.0f, -7.0f, -1);
VerticesAlongZ(7, -12.0f, -11.0f, -7.0f, 1);
glEnd();

}
``````

I should probably just explain that the VerticesAlongX and VerticesAlongZ functions just define a list of vertices along a straight line in either the x or z direction. I felt I did not need one in the y direction as there are no intermediary points along the y direction.

gl_cull enabled ?

A couple of easy tests to try using glPolygonMode. Set the PolygonMode to GL_POINT. Do you get the vertices you expected to get? If so, set the PolygonMode to GL_LINE. Do you get the polygon outlines you expected?

Thank you both, gl_cull is enabled as far as I can remember. I shall try changing to GL_POINT in the morning. Thank you both. I shall let you know my results. Cheers!

Hello,your code about the left side, i think,is not full correct.After you drew the top and bottom,if you want to draw left side,you should draw two lines ,one moves from(-12,-12,0)to(-12,-11,0),the another moves from(-12,-12,-7)to(-12,-11,-7).The Z line and the X line are already drawn by the top and bottom.
I think you should connect (-12,-12,-7)and(-12,-11,-7).

Using GL_POINT does not seem to be working either so the points don’t seem to be being drawn at all. (I left out the top and bottom while trying this so that I could see)

So your problems are more basic than the details of your box drawing routine. My guess is that the box is too large to fit into the clipping volume. Try scaling it down, i.e. use the same routine to draw your points, but put a glScalef (0.1, 0.1, 0.1) in front of it. Make sure you are translating the box to place it between the near and far clipping planes too.

I don’t think it’s a scaling problem as the top and bottom faces of the box draw with no trouble at all. I tried it anyway, but it didn’t work. I did however investigate more when changing the polygon mode to GL_POINTS. I found that only points along two of the sides of a rectangle were being drawn. But it was making the desired rectangle when in the normal polygon mode.

Ok I think I worked out the problem. I wasn’t closing the polygon, if that makes sense? Anyway it’s working now. Thank you for your help!