OpenGL立方体

直接画

#include <windows.h> 
#include <GL/glut.h> 
#include <stdio.h> 
#include <string> 
#include <iostream> 

// 绘制立方体


// 将立方体的八个顶点保存到一个数组里面 

static const float vertex_list[][3] =
{
    -0.5f, -0.5f, -0.5f,//0
    0.5f, -0.5f, -0.5f,//1
    -0.5f, 0.5f, -0.5f,//2
    0.5f, 0.5f, -0.5f,//3

    -0.5f, -0.5f, 0.5f,//4
    0.5f, -0.5f, 0.5f,//5
    -0.5f, 0.5f, 0.5f,//6
    0.5f, 0.5f, 0.5f,//7
};

// 将要使用的顶点的序号保存到一个数组里面 

static const GLint index_list[][3] =
{
    0,5,1,
    0,4,5,
    0,3,1,
    0,3,2,
    0,6,2,
    0,6,4,
    5,6,4,
    5,6,7,
    5,3,1,
    5,3,7,
    3,6,2,
    3,6,7,

};

// 绘制立方体

void DrawCube(void)
{
    int i, j;

    glBegin(GL_TRIANGLES);
    for (i = 0; i<12; ++i) // 12 三角形

    {
        for (j = 0; j<3; ++j) // 每个三角形3顶点

        {
            glVertex3fv(vertex_list[index_list[i][j]]);
        }
    }
    glEnd();
}

static float rotate = 0;
static int times = 0;

void renderScene(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glPushMatrix();

    /*glTranslatef(-0.2, 0, 0);*/ // 平移

    //glScalef(2, 1, 1);    // 缩放


    times++;
    if (times > 100)
    {
        times = 0;
    }

    if (times % 100 == 0)
    {
        rotate += 2;
    }

    glRotatef(rotate, 1, 0, 0);
    glRotatef(rotate, 0, 1, 0);

    glColor3f(0, 0, 1);

    DrawCube();

    glPopMatrix();
    glutSwapBuffers();
}

void main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(500, 500);
    glutCreateWindow("GLDemo");
    glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
    glutDisplayFunc(renderScene);
    glutIdleFunc(renderScene);
    glutMainLoop();
}

GL_TRIANGLE_STRIP方法

#include <windows.h> 
#include <GL/glut.h> 
#include <stdio.h> 
#include <string> 
#include <iostream> 

// 绘制立方体


// 将立方体的八个顶点保存到一个数组里面 

static const float vertex_list[][3] =
{
    -0.5f, -0.5f, -0.5f,//0
    0.5f, -0.5f, -0.5f,//1
    -0.5f, 0.5f, -0.5f,//2
    0.5f, 0.5f, -0.5f,//3

    -0.5f, -0.5f, 0.5f,//4
    0.5f, -0.5f, 0.5f,//5
    -0.5f, 0.5f, 0.5f,//6
    0.5f, 0.5f, 0.5f,//7
};

void display(void){


    glBegin(GL_TRIANGLE_STRIP);
    glVertex3fv(vertex_list[0]);
    glVertex3fv(vertex_list[1]);
    glVertex3fv(vertex_list[2]);
    glVertex3fv(vertex_list[3]);
    glVertex3fv(vertex_list[6]);
    glVertex3fv(vertex_list[7]);
    glVertex3fv(vertex_list[4]);
    glVertex3fv(vertex_list[5]);
    glVertex3fv(vertex_list[0]);
    glVertex3fv(vertex_list[1]);
    glEnd();
    glBegin(GL_TRIANGLE_STRIP);
    glVertex3fv(vertex_list[0]);
    glVertex3fv(vertex_list[2]);
    glVertex3fv(vertex_list[4]);
    glVertex3fv(vertex_list[6]);
    glEnd();
    glBegin(GL_TRIANGLE_STRIP);
    glVertex3fv(vertex_list[1]);
    glVertex3fv(vertex_list[3]);
    glVertex3fv(vertex_list[5]);
    glVertex3fv(vertex_list[7]);
    glEnd();
}
static float rotate = 0;
static int times = 0;

void renderScene(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glPushMatrix();

    /*glTranslatef(-0.2, 0, 0);*/ // 平移

    //glScalef(2, 1, 1);    // 缩放



    if (times > 100)
    {
        times = 0;
    }

    if (times % 100 == 0)
    {
        rotate += 0.3;
    }

    glRotatef(rotate, 0, 1, 0);
    glRotatef(rotate, 0, 1, 1);

    /*gluLookAt(0.0, 0.99, 0.10, 0.0, .0, 0.0, 0.0, 1.0, 0.0);*/

    glColor3f(0, 0, 1);

    display();

    glPopMatrix();
    glutSwapBuffers();
}

void main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(500, 500);
    glutCreateWindow("GLDemo");
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    glutDisplayFunc(renderScene);
    glutIdleFunc(renderScene);
    glutMainLoop();
}
原文地址:https://www.cnblogs.com/TTonly/p/10349953.html