Link to home
Start Free TrialLog in
Avatar of srihari1986
srihari1986

asked on

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
Avatar of davebytes
davebytes
Flag of United States of America image

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
ASKER CERTIFIED SOLUTION
Avatar of guitaristx
guitaristx

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of srihari1986
srihari1986

ASKER

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial