2014年5月1日 星期四

第十一周作業

#include <GL/glut.h>
float angleX=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
               glRotatef(angleX ,0,1,0);
                  glutTeapot(0.3);
      glPopMatrix();
glutSwapBuffers(); 
}
void keyboard (unsigned char key , int x,int y)
{
 if(key=='a') angleX+=3;
 if(key=='b') angleX-=3;
 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);
  
}
int main()
{
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
 glutCreateWindow("week11");
 glutDisplayFunc(display);
 glutKeyboardFunc(keyboard);
 myLight();
 glutMainLoop();
}
第一方法(比較不好的)

#include <GL/glut.h>
float angleX=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angleX ,0,1,0);
glutWireTeapot(0.3);
glTranslatef(0.4,0,0);
glutSolidSphere(0.2,30,30);
glTranslatef(-0.8,0,0);
glutSolidSphere(0.2,30,30);
glTranslatef(0.4,0.4,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glutSwapBuffers(); 
}
void keyboard (unsigned char key , int x,int y)
{
 if(key=='a') angleX+=3;
 if(key=='b') angleX-=3;
 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);
  
}
int main()
{
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
 glutCreateWindow("week11");
 glutDisplayFunc(display);
 glutKeyboardFunc(keyboard);
 myLight();
 glutMainLoop();
}
第二種方法比較好

#include <GL/glut.h>
float angleX=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angleX ,0,1,0);
glutWireTeapot(0.3);
glPushMatrix();
glTranslatef(0.4,0,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPushMatrix();
glTranslatef(-0.4,0,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPushMatrix();
glTranslatef(0,0.4,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPopMatrix();
glutSwapBuffers(); 
}
void keyboard (unsigned char key , int x,int y)
{
 if(key=='a') angleX+=3;
 if(key=='b') angleX-=3;
 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);
  
}
int main()
{
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
 glutCreateWindow("week11");
 glutDisplayFunc(display);
 glutKeyboardFunc(keyboard);
 myLight();
 glutMainLoop();
}

#include <GL/glut.h>
float angleX=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angleX ,0,1,0);
glutWireTeapot(0.3);
glPushMatrix();
glTranslatef(0.4,0,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPushMatrix();
glTranslatef(-0.4,0,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPushMatrix();
glTranslatef(0,0.4,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPushMatrix();
glTranslatef(0.6,0,0);
glRotatef(90,0,0,1);
glScalef(1,1.5,0.4);
glutSolidCube(0.3);
glPopMatrix();
glPushMatrix();
glTranslatef(-0.6,0,0);
glRotatef(90,0,0,1);
glScalef(1,1.5,0.4);
glutSolidCube(0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers(); 
}
void keyboard (unsigned char key , int x,int y)
{
 if(key=='a') angleX+=3;
 if(key=='b') angleX-=3;
 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);
  
}
int main()
{
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
 glutCreateWindow("week11");
 glutDisplayFunc(display);
 glutKeyboardFunc(keyboard);
 myLight();
 glutMainLoop();
}

我還試著加上腳了,覺得非常有趣




#include <GL/glut.h>
float angleX=0,angle1=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angleX ,0,1,0);
glutWireTeapot(0.3);
glPushMatrix();
glTranslatef(0.4,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();
glRotatef(angle1 ,0,0,1);
glTranslatef(0.3,0,0);
glScalef(2,0.5,0.5);
glutSolidCube(0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.4,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();
glRotatef(angle1 ,0,0,-1);
glTranslatef(-0.3,0,0);
glScalef(2,0.5,0.5);
glutSolidCube(0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0,0.4,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPushMatrix();
glTranslatef(0.2,-0.25,0);
glRotatef(angle1,0,0,1);
glTranslatef(0,-0.45/2,0);
glScalef(0.2,0.45,0.12);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2,-0.25,0);
glRotatef(angle1,0,0,1);
glTranslatef(0,-0.45/2,0);
glScalef(0.2,0.45,0.12);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glutSwapBuffers(); 
}
void keyboard (unsigned char key , int x,int y)
{
 if(key=='a') angleX+=3;
 if(key=='b') angleX-=3;
 if(key=='1') angle1+=3;
 if(key=='2') angle1-=3; 
 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);
  
}
int main()
{
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
 glutCreateWindow("week11");
 glutDisplayFunc(display);
 glutKeyboardFunc(keyboard);
 myLight();
 glutMainLoop();
}




#include <GL/glut.h>
float angleX=0,angle1=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angleX ,0,1,0);
glutWireTeapot(0.3);
glPushMatrix();
glTranslatef(0.4,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();
glRotatef(angle1 ,0,0,1);
glTranslatef(0.3,0,0);
glScalef(2,0.5,0.5);
glutSolidCube(0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.4,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();
glRotatef(angle1 ,0,0,-1);
glTranslatef(-0.3,0,0);
glScalef(2,0.5,0.5);
glutSolidCube(0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0,0.4,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPushMatrix();//右腳 
glTranslatef(0.2,-0.25,0);
glRotatef(angle1,0,0,1);
glTranslatef(0,-0.45/2,0);
glPushMatrix();
glScalef(0.2,0.45,0.12);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();//右小腿 
glTranslatef(0,-0.18,0);
glRotatef(angle1,0,0,1);
glTranslatef(0,-0.45/2,0);
glScalef(0.2,0.45,0.12);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPushMatrix();//左腿 
glTranslatef(-0.2,-0.25,0);
glRotatef(angle1,0,0,1);
glTranslatef(0,-0.45/2,0);
glPushMatrix();
glScalef(0.2,0.45,0.12);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();//左小腿 
glTranslatef(0,-0.18,0);
glRotatef(angle1,0,0,1);
glTranslatef(0,-0.45/2,0);
glScalef(0.2,0.45,0.12);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers(); 
}
void keyboard (unsigned char key , int x,int y)
{
 if(key=='a') angleX+=3;
 if(key=='b') angleX-=3;
 if(key=='1') angle1+=3;
 if(key=='2') angle1-=3; 
 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);
  
}
int main()
{
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
 glutCreateWindow("week11");
 glutDisplayFunc(display);
 glutKeyboardFunc(keyboard);
 myLight();
 glutMainLoop();
}










沒有留言:

張貼留言