Solved

# Draw a 8 Sided Prism using OPENGL

Posted on 2005-04-03
Medium Priority
5,117 Views
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
Question by:srihari1986
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 17

Expert Comment

ID: 13697324
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

LVL 6

Accepted Solution

guitaristx earned 900 total points
ID: 13697849
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 Comment

ID: 13699307
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

LVL 17

Assisted Solution

davebytes earned 600 total points
ID: 13701053
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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
###### Suggested Courses
Course of the Month8 days, 6 hours left to enroll