1. gluLookAt()做視角
程式碼:
float eyex=0.3,eyey=0.2,eyez=0.4,objx=0,objy=0,objz=0,upx=0,upy=1,upz=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
gluLookAt(eyex,eyey,eyez,objx,objy,objz,upx,upy,upz);
glRotatef(angleX, 0,1,0);
glPushMatrix();//右肩
glTranslatef(0.25, 0.3, 0);
glutSolidSphere(0.1, 30, 30);
glPushMatrix();//右臂
glRotatef(angle2, 0, 0, 1);
glTranslatef(0.2, 0, 0);
glScalef(0.5, 0.1, 0.1);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();//右手
glRotatef(angle2, 0, 0, 1);
glTranslatef(0.5, 0, 0);
glutSolidSphere(0.1, 30, 30);
glPopMatrix();
glPopMatrix();
glPushMatrix();//頭
glTranslatef(0, 0.55, 0);
glutSolidSphere(0.2, 30, 30);
glPopMatrix();
glPushMatrix();//左肩
glTranslatef(-0.25, 0.3, 0);
glutSolidSphere(0.1, 30, 30);
glPushMatrix();//左臂
glRotatef(-angle2, 0, 0, 1);
glTranslatef(-0.2, 0, 0);
glScalef(0.5, 0.1, 0.1);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();//左手
glRotatef(-angle2, 0, 0, 1);
glTranslatef(-0.5, 0, 0);
glutSolidSphere(0.1, 30, 30);
glPopMatrix();
glPopMatrix();
glPushMatrix();//左大腿
glTranslatef(-0.1, -0.15, 0);
glRotatef(angle3, 1, 0, 0);
glTranslatef(0, -0.3, 0);
glPushMatrix();
glScalef(0.13, 0.6, 0.13);
glutSolidCube(1);
glPopMatrix();
glTranslatef(-0.01, -0.3, 0);//左腳掌
glutSolidSphere(0.1, 30, 30);
glPopMatrix();
glPushMatrix();//右大腿
glTranslatef(0.1, -0.15, 0);
glRotatef(-angle3, 1, 0, 0);
glTranslatef(0, -0.3, 0);
glPushMatrix();
glScalef(0.13, 0.6, 0.13);
glutSolidCube(1);
glPopMatrix();
glTranslatef(0, -0.3, 0);//右腳掌
glutSolidSphere(0.1, 30, 30);
glPopMatrix();
glPushMatrix(); //身體
glTranslatef(0, 0.1, 0);
glScalef(0.4, 0.6, 0.2);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
2. 使用空白鍵做運鏡
程式碼:
float angleX =0, angle2 = 0, angle3 = 0;
float eyex=0.3,eyey=0.2,eyez=0.4,objx=0,objy=0,objz=0,upx=0,upy=1,upz=0; *將宣告往前提*
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;//全身的轉動
if(key=='2') angleX-=5;//全身的轉動
if(key=='3') angle2+=5;//手的轉動
if(key=='4') angle2-=5;//手的轉動
if(key=='5') angle3+=5;//腳的轉動
if(key=='6') angle3-=5;//腳的轉動
if(key=='0') //控制運鏡轉動
{
eyex-=0.02;
eyey-=0.02;
}
glutPostRedisplay();
}
3. 解決破圖
程式碼:
void myLight()
{
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;//全身的轉動
if(key=='2') angleX-=5;//全身的轉動
if(key=='3') angle2+=5;//手的轉動
if(key=='4') angle2-=5;//手的轉動
if(key=='5') angle3+=5;//腳的轉動
if(key=='6') angle3-=5;//腳的轉動
if(key=='0')
{
eyex-=0.02;
eyey-=0.02;
}
else if(key=='a')
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2,2,-2,2,-10,10);
glMatrixMode(GL_MODELVIEW);
}
glutPostRedisplay();
}
4. 邊旋轉邊放大,背景變色
程式碼:
#include <stdlib.h>#include <GL/glut.h>
#include <math.h>
float angleX =0, angle2 = 0, angle3 = 0;
float eyex=0.3,eyey=1.2,eyez=0.4,objx=0,objy=0,objz=0,upx=0,upy=1,upz=0;
float thesize=1;
void timerRotateShow(int t)
{
if(t%10==0) glClearColor(rand()%255/255.0,rand()%255/255.0,rand()%255/255.0,rand()%255/255.0);
glutTimerFunc(30,timerRotateShow,t+1);
eyex=cos(t/100.0*3.1415);
eyez=sin(t/100.0*3.1415);
eyey-=0.005;
thesize+=0.05;
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;//全身的轉動
if(key=='2') angleX-=5;//全身的轉動
if(key=='3') angle2+=5;//手的轉動
if(key=='4') angle2-=5;//手的轉動
if(key=='5') angle3+=5;//腳的轉動
if(key=='6') angle3-=5;//腳的轉動
if(key=='0')
{
eyex-=0.02;
eyey-=0.02;
}
else if(key=='a')
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2,2,-2,2,-10,10);
glMatrixMode(GL_MODELVIEW);
}
else if(key=='z')
{
glutTimerFunc(0,timerRotateShow,0);
}
glutPostRedisplay();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
gluLookAt(eyex,eyey,eyez,objx,objy,objz,upx,upy,upz);
glScalef(thesize,thesize,thesize);
glRotatef(angleX, 0,1,0);
glPushMatrix();//右肩
glTranslatef(0.25, 0.3, 0);
glutSolidSphere(0.1, 30, 30);
glPushMatrix();//右臂
glRotatef(angle2, 0, 0, 1);
glTranslatef(0.2, 0, 0);
glScalef(0.5, 0.1, 0.1);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();//右手
glRotatef(angle2, 0, 0, 1);
glTranslatef(0.5, 0, 0);
glutSolidSphere(0.1, 30, 30);
glPopMatrix();
glPopMatrix();
glPushMatrix();//頭
glTranslatef(0, 0.55, 0);
glutSolidSphere(0.2, 30, 30);
glPopMatrix();
glPushMatrix();//左肩
glTranslatef(-0.25, 0.3, 0);
glutSolidSphere(0.1, 30, 30);
glPushMatrix();//左臂
glRotatef(-angle2, 0, 0, 1);
glTranslatef(-0.2, 0, 0);
glScalef(0.5, 0.1, 0.1);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();//左手
glRotatef(-angle2, 0, 0, 1);
glTranslatef(-0.5, 0, 0);
glutSolidSphere(0.1, 30, 30);
glPopMatrix();
glPopMatrix();
glPushMatrix();//左大腿
glTranslatef(-0.1, -0.15, 0);
glRotatef(angle3, 1, 0, 0);
glTranslatef(0, -0.3, 0);
glPushMatrix();
glScalef(0.13, 0.6, 0.13);
glutSolidCube(1);
glPopMatrix();
glTranslatef(-0.01, -0.3, 0);//左腳掌
glutSolidSphere(0.1, 30, 30);
glPopMatrix();
glPushMatrix();//右大腿
glTranslatef(0.1, -0.15, 0);
glRotatef(-angle3, 1, 0, 0);
glTranslatef(0, -0.3, 0);
glPushMatrix();
glScalef(0.13, 0.6, 0.13);
glutSolidCube(1);
glPopMatrix();
glTranslatef(0, -0.3, 0);//右腳掌
glutSolidSphere(0.1, 30, 30);
glPopMatrix();
glPushMatrix(); //身體
glTranslatef(0, 0.1, 0);
glScalef(0.4, 0.6, 0.2);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2,2,-2,2,-10,10);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();
}
沒有留言:
張貼留言