zoukankan      html  css  js  c++  java
  • 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();
    }
  • 相关阅读:
    课堂作业
    读书计划
    软件工程----11软件演化
    软件工程----10软件测试
    软件工程概论第五章--软件工程中的形式化方法
    软件工程概论第四章--需求工程
    软件工程概论第三章--软件项目管理
    软件工程概论第二章--软件过程
    软件工程概论第一章--概述
    在jsp里面如何用按钮跳转(转自http://oracleabc-126-com.iteye.com/blog/941739)自己留着学
  • 原文地址:https://www.cnblogs.com/TTonly/p/10349953.html
Copyright © 2011-2022 走看看