计算机图形学——大作业

这篇具有很好参考价值的文章主要介绍了计算机图形学——大作业。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

计算机图形学大作业

一、实验要求

绘制一个简单的三维场景,可以是室内:卧室,办公室,教室,也可以是室外:运动场,公园等,加上光照效果,简单的纹理映射,透视投影;不能过于简单;可以加动画、鼠标和键盘交互。

    上交材料: project和word文档(具体内容展示,思路和心得)

二、实验思路

首先初始化窗口,定义摄像机坐标与视口坐标,利用实验二的知识内容绘制三维立体模型,定义每一个模型的坐标值以及利用变换函数实现相对位置的放置;利用实验三中的知识给整个场景进行灯光的设置;然后使用实验四中的代码实现模型材质的纹理贴图;最后进行鼠标与键盘的交互,实现鼠标可以控制摄像机的高度以及键盘的上下左右实现摄像机的移动。

三、实验代码

 文章来源地址https://www.toymoban.com/news/detail-514588.html

#include<windows.h>
#include<gl/glut.h>
#include<stdio.h>
#include<math.h>
#include<GL/glaux.h>
GLuint drawcube,drawsphere,drawteapot;

static float a=0.0;
static GLdouble eye=1.0;
static GLfloat locate=1.0;
static int lflag=GL_TRUE,rflag=GL_TRUE;

GLfloat planes[] = { 1.0,0.0,0.0,0.0 };
GLfloat planet[] = { 0.0,0.5,0.0,0.5 };

GLuint texture[8];    //创建一个纹理空间
AUX_RGBImageRec* LoadBMP(CHAR* Filename) //载入位图图像
{
    FILE* File = NULL;                             //文件句柄
    if (!Filename)                                //确保文件名已提供
    {
        return NULL;
    }
    File = fopen(Filename, "r");                   //尝试打开文件
    if (File)
    {
        fclose(File);                            //关闭文件
        return auxDIBImageLoadA(Filename);       //载入位图并返回指针
    }
    return NULL;                              //如果载入失败,返回NULL
}

int LoadGLTextures()  //载入位图并转换成纹理
{
    int Status = FALSE;                         //状态指示器
    AUX_RGBImageRec* TextureImage[8];         //创建纹理的存储空间
    memset(TextureImage, 0, sizeof(void*) * 8);//初始化
//载入位图,检查有无错误,如果位图没找到则退出
    char* image[] = {
        (char*)"texture.bmp",
        (char*)"texture1.bmp",
        (char*)"texture2.bmp",
        (char*)"texture3.bmp",
        (char*)"texture4.bmp",
        (char*)"texture5.bmp",
        (char*)"texture6.bmp",
        (char*)"texture7.bmp",
    };
    for (int i = 0; i < 8; i++)
    {
        if (TextureImage[i] = LoadBMP(image[i]))
        {
            Status = TRUE;
            glGenTextures(1, &texture[i]);
            //使用来自位图数据生成的纹理
            glBindTexture(GL_TEXTURE_2D, texture[i]);
            //指定二维纹理
            glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[i]->sizeX, TextureImage[i]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[i]->data);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        }
        if (TextureImage[i])               //纹理是否存在
        {
            if (TextureImage[i]->data)     //纹理图像是否存在
            {
                free(TextureImage[i]->data);      //释放纹理图像占用的内存
            }
            free(TextureImage[i]);        //释放图像结构
        }
    }

    return Status;                   //返回Status
}

void reshape(int w, int h)
{

    glViewport(0,0,(GLsizei)w,(GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90,w/h,0.5,1000);  //透视效果
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(80,100,300,0,0,0,0,10,0);//设置观察点

}
void init1() //初始化
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    //画出正方体显示列表
    drawcube = glGenLists(1);
    glNewList(drawcube, GL_COMPILE);
    glutSolidCube(1);
    glEndList();

    //画出茶壶显示列表
    drawteapot = glGenLists(1);
    glNewList(drawteapot, GL_COMPILE);
    glutSolidTeapot(1);
    glEndList();


    //设置普通灯光照0位置及参数;
    GLfloat position0[] = { 30,5,30,1 };
    GLfloat light0s[] = { 0.10,0.10,0.10,0.0 };
    GLfloat light0d[] = { 0.6,0.7,0.7 };
    GLfloat light0a[] = { 0.9,0.9,0.9 };

    glLightfv(GL_LIGHT0, GL_POSITION, position0);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light0s);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light0d);
    glLightfv(GL_LIGHT0, GL_AMBIENT, light0a);

    //设置探照灯光照1位置及参数
    GLfloat position1[] = { -60,40,0,1 };
    GLfloat light1s[] = { 1.0,1.0,1.0,1.0 };
    GLfloat light1d[] = { 0.06,0.1,0.1 };
    GLfloat light1a[] = { 0.91,0.99,0.96 };
    GLfloat  direction[] = { 0,-60,0,1 };
    glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, direction);
    glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 30.0);
    glLightfv(GL_LIGHT1, GL_POSITION, position1);
    glLightfv(GL_LIGHT1, GL_SPECULAR, light1s);
    glLightfv(GL_LIGHT1, GL_DIFFUSE, light1d);
    glLightfv(GL_LIGHT1, GL_AMBIENT, light1a);

    glEnable(GL_LIGHTING);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHT1);

    LoadGLTextures();

}

void draw()  //绘制
{
    if(lflag)
    glEnable(GL_LIGHT0);
    if(rflag)
    glEnable(GL_LIGHT1);
    glClearColor(0.51,0.40,0.5,0.0); //背景色
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glRotatef(a,0.0,1.0,0.0);
    glScaled(eye,eye,eye);
    glTranslatef(0,locate,0);
    glPushMatrix();

    //房子地面贴图
    glPushMatrix();//
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glShadeModel(GL_SMOOTH);

    glBindTexture(GL_TEXTURE_2D, texture[4]);
    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
    glTexGenfv(GL_S, GL_OBJECT_LINEAR, planes);
    glTexGenfv(GL_T, GL_OBJECT_LINEAR, planet);
    glEnable(GL_TEXTURE_GEN_S);
    glEnable(GL_TEXTURE_GEN_T);//

    //房子地面
    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(1.0,1.0,1.0,0.0);
    glTranslatef(0,-20,0);
    glScalef(147.5,1,147.5);
    glRotatef(90, 1, 0, 0);
    glCallList(drawcube);
    glColor4f(0.0,1.0,0.0,0.0);
    glutSolidCube(1);

    glDisable(GL_TEXTURE_2D);//
    glPopMatrix();//

    //房顶贴图
    glPushMatrix();//
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glShadeModel(GL_SMOOTH);

    glBindTexture(GL_TEXTURE_2D, texture[7]);
    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
    glTexGenfv(GL_S, GL_OBJECT_LINEAR, planes);
    glTexGenfv(GL_T, GL_OBJECT_LINEAR, planet);
    glEnable(GL_TEXTURE_GEN_S);
    glEnable(GL_TEXTURE_GEN_T);//

    //房顶
    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT, GL_AMBIENT);
    glColor4f(1.0, 1.0, 1.0, 0.0);
    glTranslatef(0, 75, 0);
    glScalef(147.5, 1, 147.5);
    glRotatef(90, 1, 0, 0);
    glCallList(drawcube);
    glColor4f(0.0, 1.0, 0.0, 0.0);
    glutSolidCube(1);

    glDisable(GL_TEXTURE_2D);//
    glPopMatrix();//


    //草坪贴图
    glPushMatrix();//
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glShadeModel(GL_SMOOTH);

    glBindTexture(GL_TEXTURE_2D, texture[6]);
    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
    glTexGenfv(GL_S, GL_OBJECT_LINEAR, planes);
    glTexGenfv(GL_T, GL_OBJECT_LINEAR, planet);
    glEnable(GL_TEXTURE_GEN_S);
    glEnable(GL_TEXTURE_GEN_T);//

    //草坪
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();
    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(1.0,1.0,1.0,0.5);
    glTranslatef(0,-22,0);
    glScalef(700,2,700);
    glRotatef(90, 1, 0, 0);
    glCallList(drawcube);
    glColor4f(0.0,1.0,0.0,0.5);
    glutSolidCube(1);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();
    glDisable(GL_TEXTURE_2D);//
    glPopMatrix();//

    //土地
    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(210.0/255,105.0/255,30.0/255,0.5);
    glTranslatef(0,-32,0);
    glScalef(700,20,700);
    glCallList(drawcube);
    glColor4f(0.0,1.0,0.0,0.5);
    glutSolidCube(1);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    //墙贴图
    glPushMatrix();//
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glShadeModel(GL_SMOOTH);

    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
    glTexGenfv(GL_S, GL_OBJECT_LINEAR, planes);
    glTexGenfv(GL_T, GL_OBJECT_LINEAR, planet);
    glEnable(GL_TEXTURE_GEN_S);
    glEnable(GL_TEXTURE_GEN_T);//

    //墙1
    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(1.0,1.0,1.0,0.0);
    glTranslatef(75,0,0);
    glScalef(2,150,150);
    glRotatef(90, 0, 1, 0);
    glCallList(drawcube);
    glColor4f(0.0,1.0,0.0,0.0);
    glutSolidCube(1);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    //墙2
    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(1.0,1.0,1.0,0.0);
    glTranslatef(-75,0,0);
    glScalef(2,150,150);
    glRotatef(90,0,1,0);
    glCallList(drawcube);
    glColor4f(0.0,1.0,0.0,0.0);
    glutSolidCube(1);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    //墙3
    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(1.0,1.0,1.0,0.0);
    glTranslatef(0,0,-75);
    glScalef(150,150,2);
    glCallList(drawcube);
    glColor4f(0.0,1.0,0.0,0.0);
    glutSolidCube(1);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    //墙4
    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(1.0,1.0,1.0,0.0);
    glTranslatef(-45,0,75);
    glScalef(60,150,2);
    glCallList(drawcube);
    glColor4f(1.0,1.0,1.0,0.0);
    glutSolidCube(1);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    //墙5
    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(1.0,1.0,1.0,0.0);
    glTranslatef(45,0,75);
    glScalef(60,150,2);
    glCallList(drawcube);
    glColor4f(1.0,1.0,1.0,0.0);
    glutSolidCube(1);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    //墙6
    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(1.0,1.0,1.0,0.0);
    glTranslatef(0,60,75);
    glScalef(30,30,2);
    glCallList(drawcube);
    glColor4f(1.0,1.0,1.0,0.0);
    glutSolidCube(1);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    glDisable(GL_TEXTURE_2D);//
    glPopMatrix();//
    
    //门贴图
    glPushMatrix();//
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glShadeModel(GL_SMOOTH);

    glBindTexture(GL_TEXTURE_2D, texture[1]);
    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
    glTexGenfv(GL_S, GL_OBJECT_LINEAR, planes);
    glTexGenfv(GL_T, GL_OBJECT_LINEAR, planet);
    glEnable(GL_TEXTURE_GEN_S);
    glEnable(GL_TEXTURE_GEN_T);//

    //门
    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(1,1,1,0.0);
    glTranslatef(15,0,85);
    glScalef(2,90,20);
    glRotatef(90,0,1,0);
    glCallList(drawcube);
    glColor4f(0.0,1.0,0.0,0.0);
    glutSolidCube(1);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    glDisable(GL_TEXTURE_2D);//
    glPopMatrix();//

    //墙柱贴图
    glPushMatrix();//
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glShadeModel(GL_SMOOTH);

    glBindTexture(GL_TEXTURE_2D, texture[2]);
    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
    glTexGenfv(GL_S, GL_OBJECT_LINEAR, planes);
    glTexGenfv(GL_T, GL_OBJECT_LINEAR, planet);
    glEnable(GL_TEXTURE_GEN_S);
    glEnable(GL_TEXTURE_GEN_T);//

    //墙柱
    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(1.0,1.0,1.0,1.0);
    glTranslatef(75,0,-75);
    glScalef(10,150,10);
    glCallList(drawcube);
    glColor4f(0.0,1.0,0.0,0.0);
    glutSolidCube(1);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(1.0,1.0,1.0,1.0);
    glTranslatef(-75,0,-75);
    glScalef(10,150,10);
    glCallList(drawcube);
    glColor4f(0.0,1.0,0.0,0.0);
    glutSolidCube(1);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(1.0,1.0,1.0,1.0);
    glTranslatef(75,0,75);
    glScalef(10,150,10);
    glCallList(drawcube);
    glColor4f(0.0,1.0,0.0,0.0);
    glutSolidCube(1);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(1.0,1.0,1.0,1.0);
    glTranslatef(-75,0,75);
    glScalef(10,150,10);
    glCallList(drawcube);
    glColor4f(0.0,1.0,0.0,0.0);
    glutSolidCube(1);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    glDisable(GL_TEXTURE_2D);//
    glPopMatrix();//

    //树干贴图
    glPushMatrix();//
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glShadeModel(GL_SMOOTH);

    glBindTexture(GL_TEXTURE_2D, texture[3]);
    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
    glTexGenfv(GL_S, GL_OBJECT_LINEAR, planes);
    glTexGenfv(GL_T, GL_OBJECT_LINEAR, planet);
    glEnable(GL_TEXTURE_GEN_S);
    glEnable(GL_TEXTURE_GEN_T);//

    //树干
    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(1,1,1,1.0);
    glTranslatef(-120,0,120);
    glScalef(15,120,15);
    glRotatef(90, 0, 1, 1);
    glCallList(drawcube);
    glColor4f(0.0,1.0,0.0,0.0);
    glutSolidCube(1);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();
    glDisable(GL_TEXTURE_2D);//
    glPopMatrix();//

    //白 枕头
    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(1.0,1.0,0.9,0.0);
    glTranslatef(-62.5,-7,-67);
    glScalef(18,5,9);
    glCallList(drawcube);
    glColor4f(0.0,1.0,0.0,0.0);
    glutSolidCube(1);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    //床
    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(0.51,0.40,0.01,0.0);
    glTranslatef(-70,-15,-70);
    glScalef(3,10,3);
    glCallList(drawcube);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(0.51,0.40,0.01,0.0);
    glTranslatef(-55,-15,-70);
    glScalef(3,10,3);
    glCallList(drawcube);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(0.51,0.40,0.01,0.0);
    glTranslatef(-70,-15,-40);
    glScalef(3,10,3);
    glCallList(drawcube);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(0.51,0.40,0.01,0.0);
    glTranslatef(-55,-15,-40);
    glScalef(3,10,3);
    glCallList(drawcube);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    glPushMatrix();//床板
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(0.51,0.40,0.01,0.0);
    glTranslatef(-62.5,-10,-55);
    glScalef(17,5,32);
    glCallList(drawcube);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    glPushMatrix();//红 被子
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(1.0,0.0,0.0,0.0);
    glTranslatef(-62.5,-7,-50);
    glScalef(18,5,25);
    glCallList(drawcube);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

  //绘制四个桌腿:
    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(0, 0, 0, 0.0);
    glTranslatef(20,-10,-20);
    glScalef(1,20,1);
    glCallList(drawcube);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(0, 0, 0, 0.0);
    glTranslatef(-20,-10,-20);
    glScalef(1,20,1);
    glCallList(drawcube);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(0, 0, 0, 0.0);
    glTranslatef(-20,-10,20);
    glScalef(1,20,1);
    glCallList(drawcube);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(0, 0, 0, 0.0);
    glTranslatef(20,-10,20);
    glScalef(1,20,1);
    glCallList(drawcube);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();


    //桌面贴图
    glPushMatrix();//
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glShadeModel(GL_SMOOTH);

    glBindTexture(GL_TEXTURE_2D, texture[5]);
    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
    glTexGenfv(GL_S, GL_OBJECT_LINEAR, planes);
    glTexGenfv(GL_T, GL_OBJECT_LINEAR, planet);
    glEnable(GL_TEXTURE_GEN_S);
    glEnable(GL_TEXTURE_GEN_T);//

  //画出桌面:
    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(1,1,1,0);
    glScalef(50,1,50);
    glRotatef(90, 1, 0, 0);
    glCallList(drawcube);
    glColor4f(0.39,0.30,0.1,0.0);
    glutSolidCube(1);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();
    glDisable(GL_TEXTURE_2D);//
    glPopMatrix();//


 //绘制两边台阶;
    glPushMatrix();//左边第一级台阶
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(0.51,0.40,0.2,1.0);
    glTranslatef(-32,-12,0);
    glScalef(10,4,50);
    glCallList(drawcube);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(0.1,0.50,0.51,0.0);
    glutSolidCube(1);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    glPushMatrix();// 右边第一级台阶
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(0.51,0.40,0.2,1.0);
    glTranslatef(32,-12,0);
    glScalef(10,4,50);
    glCallList(drawcube);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(0.1,0.50,0.51,0.0);
    glutSolidCube(1);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();


    glPushMatrix();//左边第二级台阶
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(0.5,0.40,0.3,0.0);
    glTranslatef(-34.5,-8,0);
    glScalef(5,4,50);
    glCallList(drawcube);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(0.1,0.50,0.51,0.0);
    glutSolidCube(1);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    glPushMatrix();//右边第二级台阶
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(0.5,0.40,0.3,0.0);
    glTranslatef(34.5,-8,0);
    glScalef(5,4,50);
    glCallList(drawcube);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(0.1,0.50,0.51,0.0);
    glutSolidCube(1);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    //桌面上摆放物品,茶壶
    glPushMatrix();//茶壶
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(0.1,0.1,0.1,0.0);
    glTranslatef(0,8,0);
    glScalef(5,10,5);
    glCallList(drawteapot);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    GLUquadricObj *pObj;
    glPushMatrix();//第一个杯子  左边
    pObj=gluNewQuadric();
    gluQuadricNormals(pObj,GLU_SMOOTH);
    glTranslatef(-8,6,-8);
    glRotatef(90,1,0,0);
    gluCylinder(pObj, 1.50f, 1.0f,5.0f, 26, 13);
    glPopMatrix();

    glPushMatrix();  //第2个杯子 右边
    glTranslatef(8,6,8);
    glRotatef(90,1,0,0);
    gluCylinder(pObj, 1.50f, 1.0f,5.0f, 26, 13);
    glPopMatrix();
    glPushMatrix();  //第3个杯子
    glTranslatef(-8,6,8);
    glRotatef(90,1,0,0);
    gluCylinder(pObj, 1.50f, 1.0f,5.0f, 26, 13);
    glPopMatrix();
    glPushMatrix();  //第4个杯子
    glTranslatef(8,6,-8);
    glRotatef(90,1,0,0);
    gluCylinder(pObj, 1.50f, 1.0f,5.0f, 26, 13);
    glPopMatrix();
    glPushMatrix();  //桌面上垫圈
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor4f(0.21,0.21,0.21,0.0);

    pObj= gluNewQuadric();
    //gluQuadricDrawStyle(pObj3,GLU_LINE);
    glTranslatef(-8,1,-8);
    glRotatef(90,1,0,0);
    gluDisk(pObj, 0.50f,  3.0f, 10, 10);
    glPopMatrix();

    glPushMatrix();  //桌面上垫圈

    pObj = gluNewQuadric();
    glTranslatef(8,1,8);
    glRotatef(90,1,0,0);
    gluDisk(pObj, 0.50f,  3.0f, 30, 30);
    glPopMatrix();
    glPushMatrix();  //桌面上垫圈

    pObj = gluNewQuadric();
    glTranslatef(-8,1,8);
    glRotatef(90,1,0,0);
    gluDisk(pObj, 0.50f,  3.0f, 30, 30);
    glPopMatrix();
    glPushMatrix();  //桌面上垫圈

    pObj = gluNewQuadric();
    glTranslatef(8,1,-8);
    glRotatef(90,1,0,0);
    gluDisk(pObj, 0.50f,  3.0f, 30, 30);
    glPopMatrix();
    
    glPushMatrix(); //月亮
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor3f(1.0,1.0,0.0);//moon
    glTranslatef(200,180,200);
    glutSolidSphere(35.0,20,20);
    glCallList(drawteapot);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    //外地面贴图-树叶
    glPushMatrix();//
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glShadeModel(GL_SMOOTH);

    glBindTexture(GL_TEXTURE_2D, texture[6]);
    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
    glTexGenfv(GL_S, GL_OBJECT_LINEAR, planes);
    glTexGenfv(GL_T, GL_OBJECT_LINEAR, planet);
    glEnable(GL_TEXTURE_GEN_S);
    glEnable(GL_TEXTURE_GEN_T);//

    glPushMatrix();  //树叶
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glColor3f(0.0,100.0/255,0.0);//leaf
    glTranslatef(-120,90,120);
    glutSolidSphere(50.0,5,5);
    glCallList(drawteapot);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    glDisable(GL_TEXTURE_2D);//
    glPopMatrix();//

    glPushMatrix();//哑铃球
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT, GL_AMBIENT);
    glColor3f(0.0,0.0, 0.0);
    glTranslatef(-62.5, -15, 10);
    glutSolidSphere(4.0, 12, 10);
    glCallList(drawteapot);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();
    glPushMatrix();//哑铃球
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT, GL_AMBIENT);
    glColor3f(0.0, 0.0, 0.0);
    glTranslatef(-62.5, -15, 30);
    glutSolidSphere(4.0, 12, 10);
    glCallList(drawteapot);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    //哑铃棍
    glPushMatrix();
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT, GL_AMBIENT);
    glColor4f(0, 0, 0, 1.0);
    glTranslatef(-62.5, -15, 20);
    glScalef(1.5, 1.5, 20);
    glCallList(drawcube);
    glColor4f(0.0, 1.0, 0.0, 0.0);
    glutSolidCube(1);
    glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();

    glPopMatrix();
    glutSwapBuffers();
}
void NormalKeysProc(unsigned char key,int x,int y) //退出按键
{
    if(key==27) exit(0);
}
void SpecialKeys(int key, int x, int y)  //按键功能
{
    if (key == GLUT_KEY_LEFT)
    {
        a += 1.0;
        glutPostRedisplay();
    }
    if (key == GLUT_KEY_RIGHT)
    {
        a -= 1.0;
        glutPostRedisplay();
    }
    if (key == GLUT_KEY_DOWN)
    {
        eye -= 0.05;
        glutPostRedisplay();
    }
    if (key == GLUT_KEY_UP)
    {
        eye += 0.05;
        glutPostRedisplay();
    }
   
}
void MouseFunc(int button, int state, int x, int y) {
    switch (state) {
    case GLUT_UP:
        switch (button) {
        case GLUT_LEFT_BUTTON:
            locate += 5.0;
            glutPostRedisplay();
            break;
        case GLUT_RIGHT_BUTTON:
            locate -= 5.0;
            glutPostRedisplay();
            break;
        }
    }
}
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
    glutInitWindowSize(800,600);
    glutInitWindowPosition(100,100);
    glutCreateWindow("finally"); 
    init1();
    glutDisplayFunc(draw);
    glutReshapeFunc(reshape);
    glutIdleFunc(draw);
    glutKeyboardFunc(NormalKeysProc);
    glutSpecialFunc(SpecialKeys);
    glutMouseFunc(MouseFunc);
    glutMainLoop();
    return 0;
}

四、实验结果与心得

计算机图形学——大作业计算机图形学——大作业计算机图形学——大作业计算机图形学——大作业计算机图形学——大作业计算机图形学——大作业

 

本次作业,对模型的坐标值的计算进行放置比较复杂,其次还有赋予材质贴图时也出现了各式各样的毛病,比如进行一个贴图的赋予时,就会给整个场景赋予相同的贴图;之后进行修改代码,最后可以给每一个模型进行赋予材质,但也只有几个面的贴图铺张是正确的显示,目前尚未解决。通过此次大作业,对图形学中三维可编程绘制流水线已经有部分的了解与学习。虽然过程中有各式各样的困难但也是逐个击破,对今后的学习也是有了较大的帮助。

 

 

 

到了这里,关于计算机图形学——大作业的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【计算机图形学01】坐标变换

             将坐标变换为标准化设备坐标,接着再转化为屏幕坐标的过程通常是分步进行的,也就是类似于流水线那样子。在流水线中,物体的顶点在最终转化为屏幕坐标之前还会被变换到多个坐标系统(Coordinate System)。将物体的坐标变换到几个 过渡 坐标系(Intermediate Coor

    2024年02月10日
    浏览(40)
  • 计算机图形学 期末复习笔记

    目录 第一章-导论 1. 计算机图形学的定义 2. 计算机图形学的应用领域 2.1 计算机图形学与其他学科的关系 3. 图形显示器的发展及其工作原理 3.1 阴极射线管(CRT) 3.2 随机扫描显示器 3.3 直视存储管显示器 3.4 光栅扫描显示器 4. 图形软件标准的形成 5. 三维图形渲染管线 第二章

    2024年02月12日
    浏览(47)
  • 计算机图形学(三) -- 3D 变换

    同样引入齐次坐标: 3D 点 = ( x , y , z , 1 ) T (x, y, z, 1)^T ( x , y , z , 1 ) T 3D 向量 = ( x , y , z , 0 ) T (x, y, z, 0)^T ( x , y , z , 0 ) T 通常, ( x , y , z , w ) (x, y, z, w) ( x , y , z , w ) (w != 0) 表示一个坐标为 ( x / w , y / w , z / w ) (x/w, y/w, z/w) ( x / w , y / w , z / w ) 的 3D 点 用一个 4x4 的矩阵来表示

    2024年02月08日
    浏览(41)
  • 【计算机图形学】曲线和曲面

    模块5 曲线和曲面 一 实验目的 编写曲线和曲面的算法 二 实验内容 1 :绘制Bezier曲线,并采用自行设计输入和交互修改数据点的方式。 实验结果如下图所示: 第一步:输入特征多边形的顶点个数,并按照顺序输入顶点的坐标。 第二步:点击左键生成bezier曲线(白色部分)和

    2024年02月06日
    浏览(44)
  • 计算机图形学 | 变换与观察

    华中科技大学《计算机图形学》课程 MOOC地址:计算机图形学(HUST) 回顾几何阶段 整体流程: 这其中存在3种变换: 坐标系的变换 模型本身的运动 观察者的运动 几何变换 以上各种变换都可以通过以下变换的复合来计算: 平移 比例 旋转 对称 错切 图形的几何变换是指对图

    2023年04月27日
    浏览(44)
  • 计算机组成原理 作业5

    题量: 21 满分: 100  作答时间: 03-23 09:45 至 03-29 23:59                                    91.7 分 1. (单选题, 3分)计算机的存储器采用分级存储体系的主要目的是________。 A. 便于读写数据 B. 便于系统升级 C. 解决存储容量、价格和存取速度之间的矛盾 D. 减小机箱的体积 2. 

    2023年04月23日
    浏览(66)
  • 【计算机图形学】三维图形投影和消隐(三视图构造)

    模块4-1 三维图形投影和消隐 一 实验目的 编写三维图形各种变换的投影算法 二 实验内容 1 :自行选择三维物体(不能选长方体),建立坐标系,给定点的三维坐标值,建立边表结构。完成三视图。 实验结果如下图所示: 左上显示为主视图,右上显示为侧视图,右下显示为

    2024年02月01日
    浏览(92)
  • 最全计算机图形学面试资料整理

    渲染管线又称渲染流水线,它是图形图像从数据一步一步形成最终输出的画面所要经历的各种操作过程。 物体坐标系(本地坐标系)Local Space 或 Model Space 世界坐标系 World Space 观察者坐标系(摄像机坐标系) View Space 裁剪空间 Clipping Space 屏幕空间 Screen Space 其中前四个矩阵之

    2023年04月15日
    浏览(42)
  • 计算机图形学-GAMES101-3

    缩放变换示意图 图形每个像素对应坐标的计算公式 写成缩放矩阵的形式 非均匀缩放 镜面变换示意图 图形每个像素对应坐标的计算公式 写成镜像矩阵的形式 切变变换示意图 写成切片矩阵的形式 旋转变换示意图 旋转默认以原点为旋转中心,旋转方式为逆时针旋转。 旋转矩

    2024年02月06日
    浏览(39)
  • 【计算机图形学】习题课:Viewing

    如果这篇文章对你有帮助,欢迎点赞与收藏~ Global (or World) Frame Transformations : Transformations are applied relative to a fixed global/world coordinate frame. When multiple transformations are applied, they are composed in the same order that they are applied to the point or object. Local (or Body or Object) Frame Transformations : Trans

    2024年01月18日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包