OpenGL入门示例5——黑色背景绘制彩色旋转立方体

#include<GL\glut.h>
#include<windows.h>		//for exit() and Sleep()
static float xrot = 0.0; 
static float yrot = 0.0; 
static float zrot = 0.0; 


void cube()
{
    glBegin(GL_QUADS);
        glColor3f(1.0,1.0,0.0);            
        glVertex3f( 1.0, 1.0,-1.0);        
        glColor3f(0.0,1.0,0.0);            
        glVertex3f(-1.0, 1.0,-1.0);        
        glColor3f(0.0,1.0,1.0);            
        glVertex3f(-1.0, 1.0, 1.0);        
        glColor3f(1.0,1.0,1.0);            
        glVertex3f( 1.0, 1.0, 1.0);        
                                        
        glColor3f(1.0,0.0,1.0);            
        glVertex3f( 1.0,-1.0, 1.0);        
        glColor3f(0.0,0.0,1.0);            
        glVertex3f(-1.0,-1.0, 1.0);        
        glColor3f(0.0,0.0,0.0);            
        glVertex3f(-1.0,-1.0,-1.0);        
        glColor3f(1.0,0.0,0.0);            
        glVertex3f( 1.0,-1.0,-1.0);        
                                        
        glColor3f(1.0,1.0,1.0);            
        glVertex3f( 1.0, 1.0, 1.0);        
        glColor3f(0.0,1.0,1.0);            
        glVertex3f(-1.0, 1.0, 1.0);        
        glColor3f(0.0,0.0,1.0);            
        glVertex3f(-1.0,-1.0, 1.0);        
        glColor3f(1.0,0.0,1.0);            
        glVertex3f( 1.0,-1.0, 1.0);        
                                        
        glColor3f(1.0,0.0,0.0);            
        glVertex3f( 1.0,-1.0,-1.0);        
        glColor3f(0.0,0.0,0.0);            
        glVertex3f(-1.0,-1.0,-1.0);        
        glColor3f(0.0,1.0,0.0);            
        glVertex3f(-1.0, 1.0,-1.0);        
        glColor3f(1.0,1.0,0.0);            
        glVertex3f( 1.0, 1.0,-1.0);        
                                        
        glColor3f(0.0,1.0,1.0);            
        glVertex3f(-1.0, 1.0, 1.0);        
        glColor3f(0.0,1.0,0.0);            
        glVertex3f(-1.0, 1.0,-1.0);        
        glColor3f(0.0,0.0,0.0);            
        glVertex3f(-1.0,-1.0,-1.0);        
        glColor3f(0.0,0.0,1.0);            
        glVertex3f(-1.0,-1.0, 1.0);        
                                        
        glColor3f(1.0,1.0,0.0);            
        glVertex3f( 1.0, 1.0,-1.0);        
        glColor3f(1.0,1.0,1.0);            
        glVertex3f( 1.0, 1.0, 1.0);        
        glColor3f(1.0,0.0,1.0);            
        glVertex3f( 1.0,-1.0, 1.0);        
        glColor3f(1.0,0.0,0.0);            
        glVertex3f( 1.0,-1.0,-1.0);        
    glEnd();                            
}

void display(void)
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(0, 0, -5);
    glRotatef(xrot, 1, 0, 0);
    glRotatef(yrot, 0, 1, 0);
    glRotatef(zrot, 0, 0, 1);
    //glPolygonMode(GL_FRONT, GL_LINE);
    cube();
    xrot = xrot + 1;
    yrot = yrot + 1;
    zrot = zrot + 1;

    glutSwapBuffers();
	Sleep(10);
}

void reshape(int w, int h)
{
    if(h==0) h = 1;
    glViewport(0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (GLfloat)w/(GLfloat)h, 0.1, 100.0);
    glMatrixMode(GL_MODELVIEW);

}
void init(int width, int height )
{
    if(height == 0) height = 1;
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClearDepth(1.0);
    glDepthFunc(GL_LESS);
    glEnable(GL_DEPTH_TEST);
    glShadeModel(GL_SMOOTH);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (GLfloat)width/(GLfloat)height, 1, 100.0);
    glMatrixMode(GL_MODELVIEW);
}

void keyboard(unsigned char key, int w, int h)
{
    if(key == 'f') // 进入全屏
        glutFullScreen();
    if(key == 'F') // 退出全屏
    {
        glutReshapeWindow(640, 480); // 设置窗口大小(不能用初始化的函数)
        glutPositionWindow(400, 100); // 设置窗口位置(不能用初始化的函数)
    }
    if(key == 27) // ESC退出程序
        exit(0);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowPosition(400, 100);
    glutInitWindowSize(640, 480);
    glutCreateWindow("Spin cube");

    glutDisplayFunc(display);
    glutIdleFunc(display);
    
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);
    init(640, 480);
    glutMainLoop();
    return 0;
}

运行结果:



#include #include "stdafx.h" #define GLUT_DISABLE_ATEXIT_HACK #include //#pragma comment(lib, "glut32.lib") GLfloat AngleX;//旋转向量 GLfloat AngleY; void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); //这个函数其实就是对接下来要做什么进行一下声明 GL_MODELVIEW 模型视图 glLoadIdentity(); glPushMatrix();// 提供了相应的接口 { glRotatef(AngleX, 1.0f, 0.0f, 0.0f); glRotatef(AngleY, 0.0f, 1.0f, 0.0f); glBegin(GL_POLYGON); //前表面 glColor3f(1.0f,1.0f,1.0f);//颜色设置为白色 glVertex3f(50.0f, 50.0f, 50.0f); glColor3f(1.0f,1.0f,0.0f);//颜色设置为黄色 glVertex3f(50.0f, -50.0f, 50.0f); glColor3f(1.0f,0.0f,0.0f);//颜色设置为红色 glVertex3f(-50.0f, -50.0f, 50.0f); glColor3f(1.0f,0.0f,1.0f);//颜色设置为品红色 glVertex3f(-50.0f, 50.0f, 50.0f); glEnd(); glBegin(GL_POLYGON); //后表面 glColor3f(0.0f, 1.0f, 1.0f);//颜色设置为青色 glVertex3f(50.0f, 50.0f, -50.0f); glColor3f(0.0f, 1.0f, 0.0f);//颜色设置为绿色 glVertex3f(50.0f, -50.0f, -50.0f); glColor3f(0.0f, 0.0f, 0.0f);//颜色设置黑色 glVertex3f(-50.0f, -50.0f, -50.0f); glColor3f(0.0f, 0.0f, 1.0f);//颜色设置为蓝色 glVertex3f(-50.0f, 50.0f, -50.0f); glEnd(); glBegin(GL_POLYGON); //右表面 glColor3ub((GLubyte)255, (GLubyte)255, (GLubyte)255);//颜色设置为白色 glVertex3f(50.0f, 50.0f, 50.0f); glColor3f(0.0f, 1.0f, 1.0f);//颜色设置为青色 glVertex3f(50.0f, 50.0f, -50.0f); glColor3f(0.0f, 1.0f, 0.0f);//颜色设置为绿色 glVertex3f(50.0f, -50.0f, -50.0f); glColor3ub((GLubyte)255, (GLubyte)255, (GLubyte)0);//颜色设置为黄色 glVertex3f(50.0f, -50.0f, 50.0f); glEnd(); glBegin(GL_POLYGON); //左表面 glColor3d(0.0, 0.0, 1.0);//颜色设置为蓝色 glVertex3f(-50.0f, 50.0f, -50.0f); glColor3f(0.0f, 0.0f, 0.0f);//颜色设置黑色 glVertex3f(-50.0f, -50.0f, -50.0f); glColor3ub((GLubyte)255, (GLubyte)0, (GLubyte)0);//颜色设置为红色 glVertex3f(-50.0f, -50.0f, 50.0f); glColor3f(1.0f, 0.0f, 1.0f);//颜色设置为品红色 glVertex3f(-50.0f, 50.0f, 50.0f); glEnd(); glBegin(GL_POLYGON); //上表面 glColor3d(0.0, 1.0, 1.0);//颜色设置为青色 glVertex3f(50.0f, 50.0f, -50.0f); glColor3d(1.0, 1.0, 1.0);//颜色设置为白色 glVertex3f(50.0f, 50.0f, 50.0f); glColor3d(1.0, 0.0, 1.0);//颜色设置为品红色 glVertex3f(-50.0f, 50.0f, 50.0f); glColor3d(0.0, 0.0, 1.0);//颜色设置为蓝色 glVertex3f(-50.0f, 50.0f, -50.0f); glEnd(); glBegin(GL_POLYGON); //下表面 glColor3f(0.0f, 1.0f, 0.0f);//颜色设置为绿色 glVertex3f(50.0f, -50.0f, -50.0f); glColor3ub((GLubyte)255, (GLubyte)255, (GLubyte)0);//颜色设置为黄色 glVertex3f(50.0f, -50.0f, 50.0f); glColor3f(1.0f, 0.0f, 0.0f);//颜色设置为红色 glVertex3f(-50.0f, -50.0f, 50.0f); glColor3f(0.0f, 0.0f, 0.0f);//颜色设置黑色 glVertex3f(-50.0f, -50.0f, -50.0f); glEnd(); } glPopMatrix(); glutSwapBuffers(); } void reshape(int w, int h) { GLfloat aspect = (GLfloat)w / (GLfloat)h; GLfloat nRange = 100.0f; glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); //将当前矩阵指定为投影模式 glLoadIdentity(); //设置三维投影区 if (w 355.0f) { AngleX = 0.0f; } if (AngleX 355.0f) AngleY = 0.0f; if (AngleY < 0.0f) { AngleY = 355.0f; } glutPostRedisplay(); } void init() { AngleX = 45.0f; AngleY = 315.0f; glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glEnable(GL_DEPTH_TEST); //初始化OpenGL glEnable(GL_DITHER); //抖动是激活的。 glShadeModel(GL_SMOOTH);//两点间颜色有过渡效果 } void main(int argc, char* argv[]) { glutInit(&argc;, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); //使用双缓存 使用深度缓存。 glutInitWindowSize(480, 480); glutCreateWindow("OpenGL颜色立方体"); glutReshapeFunc(reshape); //窗口改变的时候调用的函数 glutDisplayFunc(display); glutSpecialFunc(key_board); //函数注册鼠标响应事件 init(); glutMainLoop(); }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值