计算机图形学——大作业

本文涉及的产品
云数据库 Tair(兼容Redis),内存型 2GB
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
简介: 计算机图形学大作业一、实验要求绘制一个简单的三维场景,可以是室内:卧室,办公室,教室,也可以是室外:运动场,公园等,加上光照效果,简单的纹理映射,透视投影;不能过于简单;可以加动画、鼠标和键盘交互。 上交材料: project和word文档(具体内容展示,思路和心得)二、实验思路首先初始化窗口,定义摄像机坐标与视口坐标,利用实验二的知识内容绘制三维立体模型,定义每一个模型的坐标值以及利用变换函数实现相对位置的放置;利用实验三中的知识给整个场景进行灯光的设置;然后使用实验四中的代码实...

 计算机图形学大作业

 

一、实验要求

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

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

二、实验思路

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

三、实验代码

#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;
}

image.gif

四、实验结果与心得

image.gif编辑image.gif编辑image.gif编辑image.gif编辑image.gif编辑image.gif编辑

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


相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
3月前
|
机器学习/深度学习 算法 数据挖掘
机器学习第五次作业
机器学习第五次作业包含三个题目。第一题实现高斯贝叶斯分类和高斯朴素贝叶斯分类,分别用于判断瓜的好坏。第二题使用EM算法对数据进行聚类,输出两个类别的均值、方差和先验概率。第三题内容未完整展示。
73 0
机器学习第五次作业
|
5月前
【随机过程】课后作业 1-条件概率期望密度
本文提供了随机过程课后作业的解答示例,涉及泊松分布、卡方分布等概率论概念的计算题。
55 3
|
2月前
|
算法 调度
基于遗传模拟退火混合优化算法的车间作业最优调度matlab仿真,输出甘特图
车间作业调度问题(JSSP)通过遗传算法(GA)和模拟退火算法(SA)优化多个作业在并行工作中心上的加工顺序和时间,以最小化总完成时间和机器闲置时间。MATLAB2022a版本运行测试,展示了有效性和可行性。核心程序采用作业列表表示法,结合遗传操作和模拟退火过程,提高算法性能。
|
3月前
|
机器学习/深度学习 计算机视觉 Python
坐标下降求解人脸表示(机器学习作业04)
这是机器学习第四次作业的解答,包含两道题目。第一题涉及数学推导,给出了详细的求解过程。第二题则通过 Python 代码实现图像处理,包括读取单张图片和批量读取文件夹中的图片,并将图像数据转换为矩阵形式。
19 0
坐标下降求解人脸表示(机器学习作业04)
|
5月前
|
算法
算法设计与分析作业
这篇文章是关于算法设计与分析的作业,其中包含了两个算法实现:一个是使用分治算法实现的十进制大整数相乘(包括加法、减法和乘法函数),并进行了正确性和健壮性测试;另一个是使用快速排序思想实现的分治查找第K小元素的程序,并分析了其平均和最坏时间复杂度。
算法设计与分析作业
|
机器学习/深度学习 算法 Java
基于多动作深度强化学习的柔性车间调度研究(Matlab代码实现)
基于多动作深度强化学习的柔性车间调度研究(Matlab代码实现)
257 0
|
Web App开发 调度 Windows
开源代码分享(8)—大规模电动汽车时空耦合双层优化调度(附matlab代码)
本文研究了发电机、电动汽车和风能的协同优化调度问题。提出了一种新颖的双层优化方法,用于解决在风能存在的情况下,电动汽车充放电负荷在时间和空间领域的调度问题。在输电系统中,上层优化协调了电动汽车、热发电机和基本负荷,考虑了风能因素,优化了电动汽车在时间域内的负荷时段。在配电系统中,下层优化则对电动汽车负荷的位置进行空间调度。通过对一个拥有10台发电机的输电网和一个IEEE 33节点的配电网的电力系统基准进行评估,评估了提出的双层优化策略的性能。分析了电价曲线、电动汽车普及率以及电动汽车负荷位置等因素的影响。
|
消息中间件 算法 调度
基于GWO灰狼优化的生产线工件工序调度优化matlab仿真,仿真输出优化收敛曲线和工序调度甘特图
基于GWO灰狼优化的生产线工件工序调度优化matlab仿真,仿真输出优化收敛曲线和工序调度甘特图
144 0
基于GWO灰狼优化的生产线工件工序调度优化matlab仿真,仿真输出优化收敛曲线和工序调度甘特图
|
算法 数据挖掘 调度
【优化调度】基于改进遗传算法的公交车调度排班优化的研究与实现(Matlab代码实现)
【优化调度】基于改进遗传算法的公交车调度排班优化的研究与实现(Matlab代码实现)
151 0
|
机器学习/深度学习 算法 决策智能
人工蜂群优化及其在资源管理中的应用(Matlab代码实现)
人工蜂群优化及其在资源管理中的应用(Matlab代码实现)