计算机图形学第五次上机——真实感图形绘制(引入光照模型)&太阳系动画实现

 

    计算机图形学       课程实验报告

实验名称    真实感图形绘制    

 

一、实验目的

1.掌握光照模型的编程应用

2.能够编程模拟实现简单的太阳系

1. 用不同的着色和光照参数绘制茶壶:

i. 线框模型的茶壶

ii. 没有光照的固定颜色的茶壶

iii. 只有环境光,采用单一颜色的茶壶

iv. 只有环境光和漫反射光,采用Gouraud插值着色的茶壶

v. 有环境光、漫反射光和镜面高光,采用Gouraud插值着色的茶壶

vi. 有环境光、漫反射光和镜面高光,采用Phone插值着色的茶壶

2. 模拟简单的太阳系,太阳在中心,地球每365天绕太阳转一周,月球每年绕地球转12周。另外,地球每天24个小时绕它自己的轴旋转。

二、实验环境

1.codeblocks-17.12

2.Windows10 SDK 10.0.17134.0

三、实验内容

1茶壶绘制

1.1问题重述

 用不同的着色和光照参数绘制茶壶:

i. 线框模型的茶壶

ii. 没有光照的固定颜色的茶壶

iii. 只有环境光,采用单一颜色的茶壶

iv. 只有环境光和漫反射光,采用Gouraud插值着色的茶壶

v. 有环境光、漫反射光和镜面高光,采用Gouraud插值着色的茶壶

vi. 有环境光、漫反射光和镜面高光,采用Phone插值着色的茶壶

 

 

 

 

2.2实验结果

i. 线框模型的茶壶

ii. 没有光照的固定颜色的茶壶

iii. 只有环境光,采用单一颜色的茶壶

iv. 只有环境光和漫反射光,采用Gouraud插值着色的茶壶

v. 有环境光、漫反射光和镜面高光,采用Gouraud插值着色的茶壶

vi. 有环境光、漫反射光和镜面高光,采用Phone插值着色的茶壶

2.模拟太阳系

2.1问题重述

模拟简单的太阳系,太阳在中心,地球每365天绕太阳转一周,月球每年绕地球转12周。另外,地球每天24个小时绕它自己的轴旋转。

 

2.2运行结果

三、源代码

1.

#include<windows.h>

#include <GL/glut.h>

#include <stdio.h>

GLfloat d = 0.1;

GLfloat translateX = 0, translateY = 0;

int halfLength = 20;

GLfloat ratio;

class Point{

public:

    GLfloat x, y, z;

    Point(GLfloat x = 0.0, GLfloat y = 0.0, GLfloat z = 0.0){

        this->x = x;

        this->y = y;

        this->z = z;

    }

};

typedef Point Color;

int renderMode = 1;

GLfloat light_position[]={1.0,1.0,1.0,0.0};

GLfloat blackColor[]={0.0,0.0,0.0,1.0};

GLfloat greyColor[] = {0.5, 0.5, 0.5, 1.0};

GLfloat whiteColor[]={1.0,1.0,1.0,1.0};

GLfloat redColor[]={1.0,0.0,0.0,1.0};

 

GLfloat no_mat[] ={0.0,0.0,0.0,1.0};

GLfloat mat_ambient[] = {0.7,0.7,0.7,1.0};

GLfloat mat_diffuse[] = {0.1,0.5,0.8,1.0};

GLfloat mat_specular[] = {1.0,1.0,1.0,1.0};

GLfloat no_shininess[] = {0.0};

GLfloat low_shininess[] = {5.0};

GLfloat high_shininess[] = {100.0};

void init(){

    glClearColor(1.0, 1.0, 1.0, 1.0);

    //远光源照射

 

    glLightfv(GL_LIGHT0,GL_POSITION,light_position);

 

    //启用光照

    glEnable(GL_LIGHTING);

    glEnable(GL_LIGHT0);

    glShadeModel(GL_SMOOTH);

    //局部光源照射

//    GLfloat light_position1[] = {2.0,0.0,3.0,1.0};

//    glLightfv(GL_LIGHT1,GL_POSITION,light_position1);

//    glEnable(GL_LIGHTING);

//    glEnable(GL_LIGHT1);

 

    glEnable(GL_DEPTH_TEST);

}

void drawCoord(){

    glColor3f(1.0, 1.0, 1.0);

    glBegin(GL_LINES);

    glVertex2f(-halfLength, 0.0);

    glVertex2f(halfLength, 0.0);

    glVertex2f(0.0, -halfLength);

    glVertex2f(0.0, halfLength);

    glEnd();

}

void changeSize(GLsizei w, GLsizei h){

    glViewport(0.0, 0.0, w, h);

    if(h == 0)

        h = 1;

    ratio = (GLfloat)w / (GLfloat)h;

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    if(w < h)

        glOrtho(-halfLength, halfLength, -halfLength / ratio, halfLength /ratio, -halfLength, halfLength);

//        glFrustum(-halfLength, halfLength, -halfLength / ratio, halfLength /ratio, -10 * halfLength, 0.0);

    else

        glOrtho(-halfLength * ratio, halfLength * ratio, -halfLength, halfLength, -halfLength, halfLength);

//        glFrustum(-halfLength * ratio, halfLength * ratio, -halfLength, halfLength, -10 * halfLength, 0.0);

    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();

}

void renderScene(){

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();

    glEnable(GL_LIGHTING);

    switch (renderMode){

        case 1:

        case 2:

            glDisable(GL_LIGHTING);

            break;

        case 3:

            glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);

            glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, no_mat);

            glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, no_mat);

            glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, no_shininess);

//            glLightfv(GL_LIGHT0,GL_DIFFUSE,whiteColor);

            break;

        case 4:

            glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);

            glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);

            glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, no_mat);

            glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, no_shininess);

//            glLightfv(GL_LIGHT0,GL_SPECULAR,whiteColor);;

        case 5:

            glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);

            glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);

            glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);

            glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, low_shininess);

            break;

        case 6:

            glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);

            glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);

            glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);

            glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, high_shininess);

        default:;

 

 

 

    }

    glColor3f(1.0, 0, 0);

    glMatrixMode(GL_MODELVIEW);

    glTranslatef(0.0, 0.0, -5.0);

    //glRotatef(15.0, 1.0, 1.0, 1.0);

    printf("renderMode = %d\n", renderMode);

    if(renderMode == 1)

        glutWireTeapot(7.0);

    else

        glutSolidTeapot(7.0);

    glFlush();

}

void keyboardFunc(unsigned char key, int x, int y){

    switch(key){

        case '1':

        case '2':

        case '3':

        case '4':

        case '5':

        case '6':

            renderMode = key - '0';

            break;

        case 'q':

            exit(0);

        default:;

    }

    glutPostRedisplay();

}

int main(int argc, char *argv[]) {

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);

    glutInitWindowSize(500, 500);

    glutCreateWindow("template");

 

    init();

    glutDisplayFunc(renderScene);

    glutReshapeFunc(changeSize);

    glutKeyboardFunc(keyboardFunc);

    glutMainLoop();

 

    return 0;

}

 

2.

#include<windows.h>

#include <GL/glut.h>

#include <stdio.h>

GLfloat d = 0.1;

GLfloat translateX = 0, translateY = 0;

int halfLength = 70;

GLfloat ratio;

GLfloat sun2Earth = 40.0;

GLfloat earth2Moon = 10.0;

GLfloat earthAngle = 0.0, moonAngle = 0.0;

GLfloat sunSize = 15;

GLfloat earthSize = 5;

GLfloat moonSize = 2;

GLfloat sunColor[] = {1.0, 0.0, 0.0, 1.0};

GLfloat earthColor[] = {0.0, 0.0, 1.0, 1.0};

GLfloat moonColor[] = {1.0, 1.0, 1.0, 1.0};

 

GLfloat whiteLight[] = {0.5f, 0.5f, 0.5f, 1.0f};

GLfloat sourceLight[] = {0.8f, 0.8f, 0.8f, 1.0f};

GLfloat lightPos[] = {0.0f, 0.0f, 0.0f, 1.0f};

 

class Point{

public:

    GLfloat x, y, z;

    Point(GLfloat x = 0.0, GLfloat y = 0.0, GLfloat z = 0.0){

        this->x = x;

        this->y = y;

        this->z = z;

    }

};

typedef Point Color;

void init(){

    glEnable(GL_LIGHTING);

    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, whiteLight);

    glLightfv(GL_LIGHT0, GL_DIFFUSE, sourceLight);

    glLightfv(GL_LIGHT0, GL_POSITION, lightPos);

    glEnable(GL_LIGHT0);

 

    glEnable(GL_COLOR_MATERIAL);

    glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

    glEnable(GL_DEPTH_TEST);

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

    glShadeModel(GL_SMOOTH);

}

void drawCoord(){

    glColor3f(1.0, 1.0, 1.0);

    glBegin(GL_LINES);

    glVertex2f(-halfLength, 0.0);

    glVertex2f(halfLength, 0.0);

    glVertex2f(0.0, -halfLength);

    glVertex2f(0.0, halfLength);

    glEnd();

}

void changeSize(GLsizei w, GLsizei h){

    glViewport(0.0, 0.0, w, h);

    if(h == 0)

        h = 1;

    ratio = (GLfloat)w / (GLfloat)h;

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    if(w < h)

        glOrtho(-halfLength, halfLength, -halfLength / ratio, halfLength /ratio, -halfLength, halfLength);

//        glFrustum(-halfLength, halfLength, -halfLength / ratio, halfLength /ratio, -10 * halfLength, 0.0);

    else

        glOrtho(-halfLength * ratio, halfLength * ratio, -halfLength, halfLength, -halfLength, halfLength);

//        glFrustum(-halfLength * ratio, halfLength * ratio, -halfLength, halfLength, -10 * halfLength, 0.0);

    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();

}

void renderScene(){

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();

    glMatrixMode(GL_MODELVIEW);

    glRotatef(80.0, 1.0, 1.0, 1.0);

//    drawCoord();

    glColor3fv(sunColor);

    glDisable(GL_LIGHTING);

    glutSolidSphere(sunSize, 50, 50);

    glEnable(GL_LIGHTING);

    glRotatef(earthAngle, 0.0, 0.0, 1.0);

    glTranslatef(sun2Earth, 0.0, 0.0);

    glColor3fv(earthColor);

    glutSolidSphere(earthSize, 50, 50);

 

    glRotatef(moonAngle, 0.0, 0.0, 1.0);

    glTranslatef(earth2Moon, 0.0, 0.0);

    glColor3fv(moonColor);

    glutSolidSphere(moonSize, 50, 50);

    glLoadIdentity();

 

    earthAngle += d;

    moonAngle += 12 * d;

 

    glutSwapBuffers();

    glutPostRedisplay();

}

 

void mySpecialKeyFunc(int key, int x, int y){

    switch (key){

        case GLUT_KEY_UP:

            d += 0.01;

            break;

        case GLUT_KEY_DOWN:

            d -= 0.01;

            break;

        default:;

    }

}

void myKeyboardFunc(unsigned char key, int x, int y){

    if(key == 'q'){

        exit(0);

    }

}

int main(int argc, char *argv[]) {

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutInitWindowSize(500, 500);

    glutCreateWindow("template");

 

    init();

    glutDisplayFunc(renderScene);

    glutReshapeFunc(changeSize);

    glutSpecialFunc(mySpecialKeyFunc);

    glutKeyboardFunc(myKeyboardFunc);

    glutMainLoop();

 

    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值