3D茶壶绘制_2021秋季《计算机图形学》_基于《计算机图形学(第四版)》D.H.&M.P.B.&W.R.C.

一、实验目的

  • 绘制3D茶壶,形状为传统的紫砂茶壶
    • 用光照模型着色
    • 用键盘实现旋转

二、实验环境

  • Visual Studio 2019
  • Windows 10

三、算法分析与设计

四、实验结果

在这里插入图片描述

旋转

在这里插入图片描述

五、附录

#include<iostream>
#include<GL/glut.h>
using namespace std;
static GLfloat xRotate = 0.0;
static GLfloat yRotate = 0.0;
static GLfloat controlValue = 10.0;
void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_SMOOTH);

    // 定义光源
    GLfloat light_position[] = { -100.0,100.0,100.0,1.0 };  // (点)光源的位置
    GLfloat light_ambient[] = { 0.1f,0.1f,0.1f,1.0f };  // 环境光
    GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };  // 漫反射光,白色
    GLfloat light_specular[] = { 0.9f,0.4f,0.6f,1.0f };  // 镜面反射光,稍浅的棕色
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);

    // 开启光源
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_DEPTH_TEST);

    // 定义壶的材质
    GLfloat mat_ambient[] = { 0.1f,0.1f,0.1f,1.0f };
    GLfloat mat_diffuse[] = { 0.4f,0.1f,0.2f,1.0f };  // 材质的漫反射光,棕色
    GLfloat mat_specular[] = { 0.8f,0.8f,0.8f,1.0f };  // 材质的镜面反射光,白色
    GLfloat mat_emission[] = { 0.0f,0.0f,0.0f,1.0f };  // 材质的幅射光,黑色
    GLfloat mat_shininess[] = { 5.0 };

    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);

 }

void display(void)
{
     // 清除之前的深度缓存
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glLoadIdentity();

     // 与显示相关的函数
     gluLookAt(20.0,20.0,10.0,0.0,0.0,0.0,0.0,1.0,0.0);
     glRotatef(xRotate, 1.0f, 0.0f, 0.0f);
     glRotatef(yRotate, 0.0f, 1.0f, 0.0f);
     glutSolidTeapot(50.0);

     glFlush();
     glutSwapBuffers();
}

void spinDisplay(void)
{
     xRotate = xRotate + 2.0;
     if (xRotate > 360)
         xRotate = xRotate - 360.0;
     glutPostRedisplay();
}

void reshape(int w, int h)
{
     glViewport(0, 0, (GLsizei)w, (GLsizei)h);
     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();

     if (w <= h)
         glOrtho(-200, 200, -200 * (GLfloat)h / (GLfloat)w, 200 * (GLfloat)h / (GLfloat)w, -100.0, 100.0);
     else
         glOrtho(-200 * (GLfloat)w / (GLfloat)h, 200 * (GLfloat)w / (GLfloat)h, -200, 200, -100.0, 100.0);
     glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();
}

void mySepcial(int key, int x, int y)
{
    switch (key)
    {
    case GLUT_KEY_DOWN:
    {
        xRotate = xRotate + 2.0;
        if (xRotate > 360)
            xRotate = xRotate - 360.0;
        glutPostRedisplay();
        break;
    }
    case GLUT_KEY_UP:
    {
        xRotate = xRotate - 2.0;
        if (xRotate > 360)
            xRotate = xRotate - 360.0;
        glutPostRedisplay();
        break;
    }
    case GLUT_KEY_LEFT:
    {
        yRotate = yRotate - 2.0;
        if (yRotate > 360)
            yRotate = yRotate - 360.0;
        glutPostRedisplay();
        break;
    }
    case GLUT_KEY_RIGHT:
    {
        yRotate = yRotate + 2.0;
        if (yRotate > 360)
            yRotate = yRotate - 360.0;
        glutPostRedisplay();
        break;
    }

    default:
        break;
    }
}

int main(int argc, char** argv)
{
     glutInit(&argc, argv);
     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
     glutInitWindowSize(960, 960);
     glutInitWindowPosition(100, 100);
     glutCreateWindow("OpenGL test");
     init();

     
     glutDisplayFunc(display);
     glutReshapeFunc(reshape);
     glutSpecialFunc(&mySepcial);

     glutMainLoop();
     return 0;
}


  • 9
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值