Jump to content
• Advertisement

# OpenGL OpenGL Drawing Polygons

This topic is 5008 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

I am writing a small prototype clone of Space Invaders for my OpenGL class and am having trouble drawing the ship model using Polygons and Quads. I have XYZ vertexs in Ship_Verts, and I have in Ship_Edges, the set of Vertices needed to draw that particular polygon. The problem is I have to draw each polygon in the order from farthest to nearest one. Or else If i draw a polygon that is suppose to behind another then it will overlap it. I tried setting the normals,so that the polygon faces just one direction but that won't didn't help. Is that the only way i can do it, setup to draw each polygon from farthest to nearest?
[SOURCE]
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>

float Ship_Verts[16][3] = {{-1,1,-1},{-1,0,-1},{-1,0,1},{-1,1,1},{1,1,-1},{1,0,-1},{1,0,1},{1,1,1},{-0.5,0.5,1},{-0.5,0,1},{-0.5,0,1.5},{-0.5,0.5,1.5},{0.5,0.5,1},{0.5,0,1},{0.5,0,1.5},{0.5,0.5,1.5}};
int Ship_Edges[11][4] = {{3,2,1,0},{3,2,6,7},{0,4,5,1},{7,6,5,4},{0,3,7,4},{11,8,9,10},{11,10,14,15},{15,14,13,12},{9,13,14,10},{8,11,15,12},{8,12,13,9}};
int Ship_Norms[11][3] = {{-1,0,0},{0,0,1},{1,0,0},{0,0,-1},{0,1,0},{-1,0,0},{0,0,1},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1}};
float Ship_Colors[11][3] = {{0,1,0},{1,0,0},{0,1,0},{0,1,1},{1,1,1},{1,1,.5},{0,0,1},{0,0,1},{0,0,1},{0,0,1},{0,0,1}};

/* A general OpenGL initialization function.  Sets all of the initial parameters. Taken from Nehe.Gamedev.net */
void InitGL(GLsizei Width, GLsizei Height)	// We call this right after our OpenGL window is created.
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);	// This Will Clear The Background Color To Black
glClearDepth(1.0);				// Enables Clearing Of The Depth Buffer
glShadeModel(GL_SMOOTH);			// Enables Smooth Color Shading
glMatrixMode(GL_PROJECTION);
glLoadIdentity();				// Reset The Projection Matrix
gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);	// Calculate The Aspect Ratio Of The Window
glMatrixMode(GL_MODELVIEW);
}

//Resize Function, taken from Nehe.Gamedev.Net
void ReSizeGLScene(GLsizei Width, GLsizei Height)
{
if (Height==0)				// Prevent A Divide By Zero If The Window Is Too Small
Height=1;

glViewport(0, 0, Width, Height);		// Reset The Current Viewport And Perspective Transformation

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(10.0 , 10.0, -10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

//Draw Ship
void draw_ship(void)
{
glColor3f(1.0, .5 , 1.0);
for(int a = 0; a < 3; a++)
{	glBegin(GL_POLYGON);
glColor3fv(&Ship_Colors[a][0]);
glNormal3iv(&Ship_Norms[a][0]);
glVertex3fv(&Ship_Verts[Ship_Edges[a][0]][0]);
glVertex3fv(&Ship_Verts[Ship_Edges[a][1]][0]);
glVertex3fv(&Ship_Verts[Ship_Edges[a][2]][0]);
glVertex3fv(&Ship_Verts[Ship_Edges[a][3]][0]);
glEnd();
}
}

void drawScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, .5 , 1.0);
for(int a = 0; a < 11; a++)
{	glBegin(GL_POLYGON);
glColor3fv(&Ship_Colors[a][0]);
glNormal3iv(&Ship_Norms[a][0]);
glVertex3fv(&Ship_Verts[Ship_Edges[a][0]][0]);
glVertex3fv(&Ship_Verts[Ship_Edges[a][1]][0]);
glVertex3fv(&Ship_Verts[Ship_Edges[a][2]][0]);
glVertex3fv(&Ship_Verts[Ship_Edges[a][3]][0]);
glEnd();
}
glutSwapBuffers();
}

int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitWindowSize(500, 500);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("Space Invaders");
//glutMouseFunc(mouse);
//glutKeyboardFunc(keyboard);
glutDisplayFunc(drawScene);
//glutIdleFunc(drawScene);
glutReshapeFunc(&ReSizeGLScene);
InitGL(640, 480);
glutMainLoop();
return(0);
}
[/SOURCE]

#### Share this post

##### Share on other sites
Advertisement
u need to enable depth testing

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

#### Share this post

##### Share on other sites
Sweet, Thanks alot.

-tn

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
frob
17
3. 3
4. 4
5. 5
• Advertisement

• 20
• 13
• 14
• 76
• 22
• ### Forum Statistics

• Total Topics
632140
• Total Posts
3004373

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!