计算机图形学实验 - 透视茶壶源代码

更新时间:2024-05-04 02:25:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

#include #include #include using namespace std;

float fTranslate; float fRotate;

float fScale=1.0f;//set inital scale value to 1.0f

bool bPersp=false; bool bAnim=false; bool bWire=false;

int wHeight=0; int wWidth=0;

//todo

//hint:some additional parameters may needed here when you operate the teapot

void Draw_Leg() {

glScalef(1,1,3);

glutSolidCube(1.0f); //glutWireCone(1.0f); }

//定义操作茶壶的操作参数 int tx=1; int ty=0; int tz=0;

int tangle=90;

//定义设置scale的参数 float sx=0.3f; float sy=0.3f; float sz=0.3f;

void Draw_Scene() {

glPushMatrix(); glTranslatef(0,0,5);

glRotatef(tangle,tx,ty,tz); // glutSolidTeapot(1);

glutSolidSphere(1.0f,10,10);

glPopMatrix();

glPushMatrix();

glTranslatef(0,0,3.5); glScalef(5,4,1); glutSolidCube(1.0); glPopMatrix(); //leg1

glPushMatrix();

glTranslatef(1.5,1,1.5); Draw_Leg(); glPopMatrix(); //leg2

glPushMatrix();

glTranslatef(-1.5,1,1.5); Draw_Leg(); glPopMatrix(); //leg3

glPushMatrix();

glTranslatef(1.5,-1,1.5); Draw_Leg(); glPopMatrix(); //leg4

glPushMatrix();

glTranslatef(-1.5,-1,1.5); Draw_Leg(); glPopMatrix(); }

void updateView(int width,int height) {

glViewport(0,0,width,height);//reset the current viewport

glMatrixMode(GL_PROJECTION);//select the projection matrix glLoadIdentity();//reset the projection matrix float whRatio=(GLfloat)width/(GLfloat)height; if(bPersp) {

//todo when 'p'operation ,hint:use function glupersPective } else

glOrtho(-3,3,-3,3,-100,100);

glMatrixMode(GL_MODELVIEW);//select the modelview matrix

}

void reshape(int width,int height) {

if(height==0)//prevent a divide by zero by {

height=1; }

wHeight=height; wWidth=width;

updateView(wHeight,wWidth); }

void idle() {

glutPostRedisplay(); }

float eye[]={0,0,8}; float center[]={0,0,0};

//todo:hint:you may need another array when you operate the teapot void key(unsigned char k,int x,int y) {

switch(k) {

case 27:

case 'q':{exit(0);break;}

case 'p':{bPersp=!bPersp;updateView(wHeight,wWidth);break;} case ' ':{bAnim=!bAnim;break;} case 'o':{bWire=!bWire;break;}

case 'a':{center[0]=center[0]+1;break;}//todo ,hint:eye[]and center[]are the keys to solve the problems

case 'd':{center[0]=center[0]-1;break;} case 'w':{center[2]=center[2]+1;break;} case 's':{center[2]=center[2]-1;break;} case 'z':{center[1]=center[1]+1;break;} case 'c':{center[1]=center[1]-1;break;}

case 'r':{center[0]=0;center[1]=0;center[2]=0;};//恢复原状 //茶壶相关操作 case 'j':{tx++;break;} case 'l':{tx--;break;}

case 'i':{ty++;break;} case 'k':{ty--;break;} case 'n':{tz++;break;} case 'm':{tz--;ty--;break;}

case 'g':{sx=sy=sz=sx+0.05;break;}//放大 case 'f':{sx=sy=sz=sx-0.05;break;}//缩小

} }

void redraw() {

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glLoadIdentity();

gluLookAt(eye[0],eye[1],eye[2],center[0],center[1],center[2],0,1,0);//场景(0,0,0)扥视点中心(0,5,50)Y轴向上 if(bWire) {

glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);

} else {

glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); }

glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING);

GLfloat white[]={1.0,1.0,1.0,1.0}; GLfloat light_pos[]={5,5,5,1};

glLightfv(GL_LIGHT0,GL_POSITION,light_pos); glLightfv(GL_LIGHT0,GL_AMBIENT,white); glEnable(GL_LIGHT0);

glRotatef(fRotate,0,0,1.0f);//ratate around y axis glRotatef(-90,1,0,0); glScalef(sx,sy,sz); Draw_Scene();

if(bAnim)

fRotate+=0.3f;

//todo hint:when you want to rotate the teepot you may like to add another line here =

glutSwapBuffers(); }

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

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(480,480);

int windowHandle=glutCreateWindow(\

glutDisplayFunc(redraw); glutReshapeFunc(reshape); glutKeyboardFunc(key); glutIdleFunc(idle); glutMainLoop(); return 0; }

本文来源:https://www.bwwdw.com/article/3oig.html

Top