图形学笔记1

Posted 工大教务处

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图形学笔记1相关的知识,希望对你有一定的参考价值。

转载自http://www.cnblogs.com/wangshide/archive/2012/04/30/2477131.html

/*
*  
*  彩色旋转立方体
*  1. 定义六个面,同时定义每个顶点的颜色(与顶点坐标相对应)
*  2. 启用 GL_SMOOTH 方式渲染
*
*
*/
#include <stdio.h>
#include<GL\\glut.h>
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();
}

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("");

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

  

以上是关于图形学笔记1的主要内容,如果未能解决你的问题,请参考以下文章

虎书学习笔记5:图形学基础数学(三维隐式曲面三维参数曲面)

虎书学习笔记1:图形学基础数学(集合映射逆映对数三角学)

三维图形学课程笔记,3D建模与游戏开发方向(unity3d,c4d,3dsmax, maya的对比与联系)

三维图形学课程笔记,3D建模与游戏开发方向(unity3d,c4d,3dsmax, maya的对比与联系)

三维图形学课程笔记,3D建模与游戏开发方向(unity3d,c4d,3dsmax, maya的对比与联系)

Real - time Rendering 实时计算机图形学