计算机图形学实验_透视茶壶源代码
- 格式:doc
- 大小:28.50 KB
- 文档页数:5
//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)
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()
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};
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);
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
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
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
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;}
//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
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 =
{
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轴向上
glutSwapBuffers();
}
int main(int argc,char*argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(480,480);
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;
{
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};
#include<windows.h>
#include<gl/glut.h>
#include<gl/gl.h>
using namespace std;
float fTranslate;
float fRotate;
float fScale=1.0f;//set inital scale value to 1.0f
int windowHandle=glutCreateWindow("Simple GLUT App");
glutDisplaunc(reshape);
glutKeyboardFunc(key);
glutIdleFunc(idle);
glutMainLoop();
return 0;
}
glMatrixMode(GL_PROJECTION);//select the projection matrix
glLoadIdentity();//reset the projection matrix
float whRatio=(GLfloat)width/(GLfloat)height;
if(bPersp)
glPopMatrix();
//leg2
glPushMatrix();
glTranslatef(-1.5,1,1.5);
Draw_Leg();
glPopMatrix();
//leg3
glPushMatrix();
glTranslatef(1.5,-1,1.5);
Draw_Leg();
glPopMatrix();
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();
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 '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;}