Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Polygin using C , OpenGL

Posted on 2005-04-12
2
Medium Priority
?
635 Views
Last Modified: 2008-02-01
Hi Guys,

I am actually using C to crete a 8 sided polygon.I have doen the below programming to create the polygon and it work.

glPushMatrix();  

     glColor4f(0, 0, 1.0, 0);
     glBegin (GL_POLYGON);
     glVertex3f (-0.66,-0.88,0.65);
     glVertex3f (0.08,-0.81,1.03);
     glVertex3f (0.07,0.85,0.98);
     glVertex3f (-0.67,0.78,0.60);
     glEnd();
     
     glColor4f(0, 1, 1.0, 0);
    glBegin (GL_POLYGON);
     glVertex3f (0.08,-0.81,1.03);
    glVertex3f  (0.83,-0.83,0.66);
     glVertex3f (0.81,0.82,0.61);
    glVertex3f  (0.07,0.85,0.98);
   glEnd();


   glColor4f(1, 0, 1.0, 0);
    glBegin (GL_POLYGON);
     glVertex3f (0.83,-0.83,0.66);
    glVertex3f  (1.01,-0.93,-0.18);
     glVertex3f (1.00,0.73,-0.23);
     glVertex3f (0.81,0.82,0.61);

    glEnd();


  glColor4f(0, 1, 0.0, 0);
    glBegin (GL_POLYGON);
     glVertex3f (1.01,-0.93,-0.18);
     glVertex3f (0.50,-1.02,-0.86);
     glVertex3f (0.49,0.64,-0.91);
     glVertex3f (1.00,0.73,-0.23);

    glEnd();



  glColor4f(1, 1, 0.0, 1);
    glBegin (GL_POLYGON);
     glVertex3f (0.50,-1.02,-0.86);
     glVertex3f (-0.32,-1.04,-0.87);
     glVertex3f (-0.33,0.61,-0.92);
     glVertex3f (0.49,0.64,-0.91);

    glEnd();

  glColor4f(1, 1, 1.0, 1);
    glBegin (GL_POLYGON);
     glVertex3f (-0.32,-1.04,-0.87);
     glVertex3f (-0.84,-0.98,-0.19);
     glVertex3f (-0.85,0.68,-0.24);
     glVertex3f (-0.33,0.61,-0.92);

    glEnd();

  glColor4f(1, 0, 0.0, 1);
    glBegin (GL_POLYGON);
     glVertex3f (-0.84,-0.98,-0.19);
     glVertex3f (-0.66,-0.88,0.65);
     glVertex3f (-0.67,0.78,0.60);
     glVertex3f (-0.85,0.68,-0.24);

    glEnd();

   
      glPopMatrix();

Is there any way i can use a for loop or anything to create this.As it seems that the codes are veery big.Do u have any idea.


Regards
Hari
0
Comment
Question by:srihari1986
2 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13764909
Appear to be 2 piramides joined by the base (I am guessing, really)

You can use either Triangle Fans or Triangle Strips to have a shorter code. Have a look to this tutorial. (Notice that OpenGL prefers to work with triangles rather than 4 sided polygons):
http://www.falloutsoftware.com/tutorials/gl/gl3.htm
0
 
LVL 22

Accepted Solution

by:
NovaDenizen earned 2000 total points
ID: 13767614
I'm skipping the colors, but I'm sure you can expand this idea and create a color table.
Create an array with all your vertices, like this:

float octahedron_verts[][3] = {
    { -0.66,-0.88,0.65 },
    { 0.08,-0.81,1.03 },
    ...and so on
}

int octahedron_faces[] = {
    0, 1, 2, 3, -1,
    1, 0, 4, 5, -1, // just making it up from here
....
    11, 12, 13, 14, -2,   // -2 marks end of face list.
}

Then to draw your polyhedron:
    int i=0;
    glBegin (GL_POLYGON);
    while (octahedron_faces[i] != -2)
         if (octahedron_faces[i] == -1) {
             glEnd();
             glBegin(GL_POLYGON);
         } else {
             glVertex3fv (octahedron_verts[octahedron_faces[i]]);
        }
        i++;
    }
    glEnd();

There's another way to specify indices of pre-specified vertices in arrays, but I can't recall it right now.  It's basically the same complexity as this code here.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question