Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 5278

# Draw a 8 Sided Prism using OPENGL

Hi All,

I am doing a 8 and 7 sided polygon using Openg.But i need all the sided to be of a same width.But when i draw using the GL_POLYGON i dont get all the sided of the polygon with the same width.

Below is the program i did to draw the 8 sided polygon.Can anybody help me to find the angles i need to use like the x,y,z or cn anybody tell me is there any formula to find the angle.And can i also know how to do a fraw a 7 sided polygon

glColor4f(1,0,0,0);

glBegin (GL_POLYGON);// Draw the First from square,Square 1.
glVertex3f(-0.5,-1,1.5);
glVertex3f(0.5,-1,1.5);
glVertex3f(0.5,1,1.5);
glVertex3f(-0.5,1,1.5);
glEnd();

glColor4f(1,1,0,0);
glBegin (GL_POLYGON);// Draw the right from the front the square.
glVertex3f(0.5,-1,1.5);
glVertex3f(1,-1,0.5);
glVertex3f(1,1,0.5);
glVertex3f(0.5,1,1.5);
glEnd();

glColor4f(0,0,1,0);
glBegin (GL_POLYGON);// Draw the straight square from right.
glVertex3f(1,-1,0.5);
glVertex3f(1,-1,-0.5);
glVertex3f(1,1,-0.5);
glVertex3f(1,1,0.5);
glEnd();

glColor4f(0,1,1,0);
glBegin (GL_POLYGON);
glVertex3f(1,-1,-0.5);
glVertex3f(0.5,-1,-1.5);
glVertex3f(0.5,1,-1.5);
glVertex3f(1,1,-0.5);
glEnd();

glColor4f(0,1,0,0);
glBegin (GL_POLYGON);// Draw the behind square.
glVertex3f(0.5,-1,-1.5);
glVertex3f(-0.5,-1,-1.5);
glVertex3f(-0.5,1,-1.5);
glVertex3f(0.5,1,-1.5);
glEnd();

glColor4f(1,1,1,0);
glBegin (GL_POLYGON);//draw the left suare.
glVertex3f(-0.5,-1,-1.5);
glVertex3f(-1,-1,-0.5);
glVertex3f(-1,1,-0.5);
glVertex3f(-0.5,1,-1.5);
glEnd();

glColor4f(0,1,1,0);
glBegin (GL_POLYGON);// Draw the square next to the left..
glVertex3f(-1,-1,-0.5);
glVertex3f(-1,-1,0.5);
glVertex3f(-1,1,0.5);
glVertex3f(-1,1,-0.5);
glEnd();

glColor4f(1,0,.6,1);
glBegin (GL_POLYGON);// Draw the square next to the left..
glVertex3f(-1,-1,0.5);
glVertex3f(-0.5,-1,1.5);
glVertex3f(-0.5,1,1.5);
glVertex3f(-1,1,0.5);
glEnd();

Thanks
Hari
0
srihari1986
• 2
2 Solutions

Commented:
8-sided can be done as 'two pyramids' or a diamond:  two points (top and bottom) at the ends, four center points as the cross section in the middle.  And a pyramid can be done quickly by taking a cube and 'pinching' all four vertices at the 'top' of the cube together in the center.

T

C
C
C
C

B

(not sure if you can visualize, dependin on how that outputs!)

-d
0

Commented:
Considering that the title says "prism" I feel obligated to post this (the definition of a prism):
http://en.wikipedia.org/wiki/Prism_%28geometry%29

So an 8-sided prism would have two hexagons at each end, and six 4-sided faces connecting them, and a 7-sided prism would have two pentagons at each end with five 4-sided faces connecting them.

The simplest way to draw a regular polygon with openGL would be to calculate the apothem (http://mathforum.org/dr.math/faq/formulas/faq.regpoly.html).  Once you've calculated the apothem, drawing it is simple:

glPushMatrix();
glBegin(GL_POLYGON);
for(int i = 0; i < num_sides; i++)
{
glBegin(GL_POLYGON);
glVertex3f(0 - (length_of_side / 2), -apothem, 0.0);
glRotatef(360.0 / num_sides, 0.0, 0.0, 1.0); //I'm not sure if this statement will work inside a glBegin/glEnd pair
}
glEnd();
glPopMatrix();

If that glRotate doesn't work (I'd hoped it would, it'd save you from having to do a lot of calculation), the above link should get you the formulas you need.
0

Author Commented:
Hi According to the for loop given by you , i idont see any changes in the value for this glVertex3f(0 - (length_of_side / 2), -apothem, 0.0); statement when the for loop is executed.

Is there any simpler way i can calculate the apothem , if i see the formu given in the website i am going nuts.

Regards
Hari
0

Commented:
try basing off this code for drawing a circle using lines:
http://steinsoft.net/index.php?site=Programming/Code%20Snippets/OpenGL/no5
or even better, this builds in a circle given a number of sides, exactly what you wanted:
http://mindfuck.de-brauwer.be/articles/circle/index.php

-d
0

## Featured Post

• 2
Tackle projects and never again get stuck behind a technical roadblock.