hopittyhop
hopittyhop

Reputation: 119

Multi-colored Icosahedron in OpenGL using Glut

I'm drawing an Icosahedron, but I can't seem to color every face of it, so it can be something like "multi-colored" Icosahedron.

How can I achieve this?

Here is the code so far:

#include <freeglut.h>

void init();
void display(void);
void centerOnScreen();
void drawObject();

//  define the window position on screen
int window_x;
int window_y;

//  variables representing the window size
int window_width = 480;
int window_height = 480;

//  variable representing the window title
char *window_title = "Sample OpenGL FreeGlut App";

bool mouseDown = false;

float xrot = 0.0f;
float yrot = 0.0f;

float xdiff = 0.0f;
float ydiff = 0.0f;



static GLfloat spin = 0.0;
#define X .525731112119133606
#define Z .850650808352039932

//-----------------------------------------------------------------------

#define X .525731112119133606 
#define Z .850650808352039932

static GLfloat vdata[12][3] = {
    { -X, 0.0, Z },{ X, 0.0, Z },{ -X, 0.0, -Z },{ X, 0.0, -Z },
    { 0.0, Z, X },{ 0.0, Z, -X },{ 0.0, -Z, X },{ 0.0, -Z, -X },
    { Z, X, 0.0 },{ -Z, X, 0.0 },{ Z, -X, 0.0 },{ -Z, -X, 0.0 }
};
static GLuint tindices[20][3] = {
    { 0,4,1 },{ 0,9,4 },{ 9,5,4 },{ 4,5,8 },{ 4,8,1 },
    { 8,10,1 },{ 8,3,10 },{ 5,3,8 },{ 5,2,3 },{ 2,7,3 },
    { 7,10,3 },{ 7,6,10 },{ 7,11,6 },{ 11,0,6 },{ 0,1,6 },
    { 6,1,10 },{ 9,0,11 },{ 9,11,2 },{ 9,2,5 },{ 7,2,11 } };
int i;



//-------------------------------------------------------------------------
//  Set OpenGL program initial state.
//-------------------------------------------------------------------------
void init()
{
    //  Set the frame buffer clear color to black. 
    glClearColor(0.2, 0.3, 0.4, 0.5);
}

//-------------------------------------------------------------------------
//  This function is passed to glutDisplayFunc in order to display 
//  OpenGL contents on the window.
//-------------------------------------------------------------------------
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    gluLookAt(
        0.0f, 0.0f, 3.0f,
        0.0f, 0.0f, 0.0f,
        0.0f, 1.0f, 0.0f);

    glRotatef(xrot, 1.0f, 0.0f, 0.0f);
    glRotatef(yrot, 0.0f, 1.0f, 0.0f);

    drawObject();

    glFlush();
    glutSwapBuffers();
}

//-------------------------------------------------------------------------
//  Draws our object.
//-------------------------------------------------------------------------
void drawObject()
{
    //  Draw Icosahedron

glBegin(GL_TRIANGLES);    
for (i = 0; i < 20; i++) {    
   /* color information here */ 
   glVertex3fv(&vdata[tindices[i][0]][0]); 
   glVertex3fv(&vdata[tindices[i][1]][0]); 
   glVertex3fv(&vdata[tindices[i][2]][0]); 
}
glEnd();
}

//-------------------------------------------------------------------------
//  This function sets the window x and y coordinates
//  such that the window becomes centered
//-------------------------------------------------------------------------
void centerOnScreen()
{
    window_x = (glutGet(GLUT_SCREEN_WIDTH) - window_width) / 2;
    window_y = (glutGet(GLUT_SCREEN_HEIGHT) - window_height) / 2;
}

void resize(int w, int h)
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glViewport(0, 0, w, h);

    gluPerspective(45.0f, 1.0f * w / h, 1.0f, 100.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void idle()
{
    if (!mouseDown)
    {
        xrot += 0.3f;
        yrot += 0.4f;
    }

    glutPostRedisplay();
}

void mouse(int button, int state, int x, int y)
{
    if (button == GLUT_LEFT_BUTTON & state == GLUT_DOWN)
    {
        mouseDown = true;

        xdiff = x - yrot;
        ydiff = -y + xrot;
    }
    else
        mouseDown = false;
}

void mouseMotion(int x, int y)
{
    if (mouseDown)
    {
        yrot = x - xdiff;
        xrot = y + ydiff;

        glutPostRedisplay();
    }
}

//-------------------------------------------------------------------------
//  Program Main method.
//-------------------------------------------------------------------------
void main(int argc, char **argv)
{
    //  Connect to the windowing system + create a window
    //  with the specified dimensions and position
    //  + set the display mode + specify the window title.
    glutInit(&argc, argv);
    centerOnScreen();
    glutInitWindowSize(window_width, window_height);
    glutInitWindowPosition(window_x, window_y);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutCreateWindow(window_title);

    //  Set OpenGL program initial state.
    init();

    // Set the callback functions
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(mouseMotion);
    glutReshapeFunc(resize);

    //  Start GLUT event processing loop
    glutMainLoop();
}

Upvotes: 2

Views: 1219

Answers (1)

user1118321
user1118321

Reputation: 26395

The problem is that in your drawObject() function, you never set the color of the vertices. You only draw them. You can set them using glColor4f(red, green, blue, alpha);. You will need to have either a list of the colors you want for the sides, or have a way to calculate them in the loop where you draw each vertex. If you call glColor4f() at the start of the loop, that will use the same color for all 3 vertices.

For example, you could update your drawObject() method to look like this:

void drawObject()
{
    //  Draw Icosahedron
    glBegin(GL_TRIANGLES);    
    for (i = 0; i < 20; i++) {
        // Set the color for this face
        glColor4f(cdata[i].red, cdata[i].green, cdata[i].blue, cdata[i].alpha);

        // Set the vertices of this face
        glVertex3fv(&vdata[tindices[i][0]][0]); 
        glVertex3fv(&vdata[tindices[i][1]][0]); 
        glVertex3fv(&vdata[tindices[i][2]][0]); 
    }
    glEnd();
}

You'll need to have an array of face colors, something like this:

typedef struct RGBAColor {
    float red;
    float green;
    float blue;
    float alpha;
} RGBAColor;

RGBAColor cdata[] = {
    { 1.0, 0.0, 0.0, 1.0 }, // Red
    ... etc. for whatever colors you want the faces to be
};

Upvotes: 1

Related Questions