Saturday, April 14, 2012

Hello World with OpenGL and GLUT

So I followed the spin shape function (somewhat modified) from the OpenGL Programming Guide, a.k.a. the Red Book, and followed Chapters 1 and 2.

What it does is spin a square slowly. The original Red Book version spun really fast. And since I'm using a MacBook Pro, which only has a trackpad, the spinning pauses on Right-Click instead of Middle-Click.



Then, I modified the program to spin a 5-vertex polygon instead:



While the rest more or less follows the guide, here's the code that adds a vertex, reduces spin speed and changes button used:


void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
    glRotatef(spin, 0.0, 0.0, 1.0);
    glColor3f(0.1, .5, .6);
    glBegin(GL_POLYGON);
        glVertex2f(-25, 25);
        glVertex2f(-25, 25);
        glVertex2f(0, 30);
        glVertex2f(25, 25);
        glVertex2f(25, -25);
    glEnd();
    glPopMatrix();
    glutSwapBuffers();
}

void spinDisplay()
{
    spin = spin + .002;
    if (spin > 360)
        spin = spin -360;
    glutPostRedisplay();
}

void mouse(int button, int state, int x, int y)
{
    switch (button)
    {
        case GLUT_LEFT_BUTTON:
            if (state == GLUT_DOWN)
                glutIdleFunc(spinDisplay);
            break;
        case GLUT_RIGHT_BUTTON:
            if (state == GLUT_DOWN)
                glutIdleFunc(NULL);
            break;
        default:
            break;
    }  
}

No comments:

Post a Comment