OpenGL使用顶点数组进行图形的绘制

#include <OpenGL/glu.h>
static GLfloat spin = 0.0;
#define PI 3.1415926
GLint circle_points = 100;
static GLint vertices[] =
{
    100, 200, 0,
    100, 100, 0,
    200, 100, 0,
    200, 200, 0
};

static GLfloat colors[] =
{
    1.0, 0.2, 0.2, 0.2, 0.2, 1.0, 0.8, 1.0, 0.2,
    0.75, 0.75, 0.75,
    0.35, 0.35, 0.35,
    0.5, 0.5, 0.5
};

static GLubyte allIndices[] = { 0, 1, 2, 0, 2, 3};

void init(void)
{
    glClearColor (0.0, 0.0, 0.0, 0.0);
    glShadeModel (GL_SMOOTH);

     // a. 启用或叫激活数组, 有8种同类要启用的数组GL_COLOR_ARRAY,GL_VERTEX_ARRAY
    glEnableClientState(GL_COLOR_ARRAY);
    glEnableClientState(GL_VERTEX_ARRAY);
    
    // b. 把数据放入到数组中
    glColorPointer(3, GL_FLOAT, 0, colors);
    glVertexPointer(3, GL_INT, 0, vertices);
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    
    // c. 用这些数据绘制几何图形, 这句话的调用会导致客户端的数据发送到了服务端
    //glDrawArrays (GL_TRIANGLE_FAN, 0, 6); // 是仅仅按照数组的顺序进行访问,索引的控制可以更灵活
    glDrawElements(GL_TRIANGLE_FAN, 24, GL_UNSIGNED_BYTE, allIndices);
    
    //glRotatef(spin, 0.0, 0.0, 1.0);
    //glColor3f(1.0, 1.0, 0.0);
    //glRectf(-25.0, -25.0, 25.0, 25.0);
    //glutWireCube(20);
    //glCirclefN();
    glutSwapBuffers();
}

void reshape(int w, int h)
{
    glViewport (0, 0, (GLsizei) w  , (GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D (0, (GLdouble) w, 0.0, (GLdouble) h);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}


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

int main(int argc, char ** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize (500, 500);
    glutInitWindowPosition (100, 100);
    glutCreateWindow (argv[0]);
    init ();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMouseFunc(mouse);
    glutMainLoop();
}



                
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值