Graphics - 行者无疆 始于足下 - 行走,思考,在路上

opengl 第一篇

还是蛮漂亮的。

linux的opengl的开发配置可以参见这篇文章。还是蛮简单的。开发环境和程序的完成大概用了一下午的时间。不过我现在还不太opengl和mesa, freeglut之间到底是啥关系。不管了。先用着吧。archlinux下的开发配置非常简单:

sudo pacman -S freeglut mesa

 源代码如下:

#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>


#define PI 3.1415926
#define R 1
#define r (sqrt(3) / 3 * R)

void redraw()
{
    double origin[] = {0.0, 0.0};
    double colorMode1[6][3] = {       //六角星最外层顶点各自的颜色
        {1, 0, 0},
        {1, 1, 0},
        {0, 1, 0},
        {0, 1, 1},
        {0, 0, 1},
        {1, 0, 1}
    };

    double colorMode2[6][3] = {       //六角星内层顶点各自的颜色
        {1, 0.5, 0},
        {0.5, 1, 0},
        {0, 1, 0.5},
        {0, 0.5, 1},
        {0.5, 0, 1},
        {1, 0, 0.5}
    };
   
    glClear(GL_COLOR_BUFFER_BIT);
    int i;
    for (i = 0; i < 6; ++i)          //  把这个凹多边形分解成为六个菱形旋转变换组合而成,通过给每个顶点设定不同的颜色达到颜色渐变的效果
    {
        glBegin(GL_POLYGON);
        glColor3f(colorMode1[i][0], colorMode1[i][1], colorMode1[i][2]);
        glVertex2d((R * cos(PI / 2 + (i * PI) / 3)), R * sin(PI / 2 + (i * PI) / 3));
        glColor3f(colorMode2[(i + 5) % 6][0], colorMode2[(i + 5) % 6][1], colorMode2[(i + 5) % 6][2]);
        glVertex2d(r * cos(PI / 3 + (i * PI) / 3), r * sin(PI / 3 + (i * PI) / 3));
        glColor3f(1.0f, 1.0f, 1.0f);
        glVertex2dv(origin);
        glColor3f(colorMode2[i][0], colorMode2[i][1], colorMode2[i][2]);
        glVertex2d(r * cos(2 * PI / 3 + (i * PI) /3), r * sin(2 * PI / 3 + (i * PI) / 3));
        glEnd();
    }
    glutSwapBuffers();
}

int main (int argc,  char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    int windowHandle
        = glutCreateWindow("Simple GLUT App");

    glutDisplayFunc(redraw);

    glutMainLoop();

    return 0;
}
 

编译:

gcc -I/usr/include -L/usr/local/lib -L/usr/lib -lglut -lGLU -lGL -lX11 -lXext -lXmu -lXi -lm filename.c -o filename

 




Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee