2014年6月6日 星期五

背景顏色、旋轉運鏡改變遠近


改變背景顏色
旋轉運鏡時遠近的變化
特別注意要一樣
#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>
float angleX =0,angle2=0,angle3=0,angle4=0,angle5=0,angle6=0,angle7=0,angle8=0;
float eyex=0.2,eyey=0.2,eyez=0.4,objx=0,objy=0,objz=0,upx=0,upy=1,upz=0;
float thesize=1;
void timerRotateShow(int t);
void keyboard(unsigned char key, int x, int y)
{
 if(key=='4') angleX+=5;
 if(key=='6') angleX-=5;
 if(key=='9') angle2+=5;
 if(key=='3') angle2-=5;
 if(key=='7') angle3+=5;
 if(key=='1') angle3-=5;
 if(key=='i') angle4+=5;
 if(key=='o') angle4-=5;
 if(key=='y') angle5+=5;
 if(key=='u') angle5-=5;
 if(key=='k') angle6+=5;
 if(key=='l') angle6-=5;
 if(key=='h') angle7+=5;
 if(key=='j') angle7-=5;
 if(key=='/') angle8+=5;
 if(key=='*') angle8-=5;
 if(key==' ') {
eyex-=0.03,eyey-=0.02;
}
else if(key=='`'){
glutTimerFunc(0,timerRotateShow,0);
}
 glutPostRedisplay();//請電腦重新畫一次畫面
}
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(10,timerRotateShow,t+1);
eyex=cos(t/100.0*3.14);
eyey-=0.005;
eyez=sin(t/100.0*3.14);
thesize+=0.01;
glutPostRedisplay();
}

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 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);
glScalef(0.8,0.8,0.8);
glRotatef(angleX, 0,1,0);
glutSolidTeapot(0.3);
 
glPushMatrix();//右肩 
glTranslatef(0.3,0,0);
glutSolidSphere(0.25,20,20);
glPushMatrix();//右手臂
glRotatef(angle2, 0,0,1);
glTranslatef(0.3,0,0);
glScalef(0.5,0.2,0.2);
glutSolidCube(1);
glPopMatrix();  
glPopMatrix();
glPushMatrix();//左肩 
glTranslatef(-0.3,0,0);
glutSolidSphere(0.25,20,20);
glPushMatrix();//左手臂
glRotatef(angle3, 0,0,1);
glTranslatef(-0.3,0,0);
glScalef(0.5,0.2,0.2);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPushMatrix();//左大腿 
glRotatef(angle4, 1,0,0);
glTranslatef(0.15,-0.4,0);
glScalef(0.2,0.5,0.2);
glutSolidCube(1);
glPushMatrix();//左小腿 
glTranslatef(0,-0.1,0);
glRotatef(angle6, 1,0,0);
glTranslatef(0,-0.8,0);
glScalef(0.8,0.9,0.8);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPushMatrix();//右大腿 
glRotatef(angle5, 1,0,0);
glTranslatef(-0.15,-0.4,0);
glScalef(0.2,0.5,0.2);
glutSolidCube(1);
glPushMatrix();//右小腿 
glTranslatef(0,-0.1,0);
glRotatef(angle7, 1,0,0);
glTranslatef(0,-0.8,0);
glScalef(0.8,0.9,0.8);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPushMatrix();//頭 

glRotatef(angle8, 0,1,0);
glTranslatef(0,0.5,0);
glutSolidSphere(0.3,30,30);
   glPushMatrix();
glTranslatef(0,0,-0.3);
glutSolidSphere(0.05,20,20);
glPopMatrix();
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();
}


沒有留言:

張貼留言