2014年5月2日 星期五

轉動茶壺

*複習上周課程:讓茶壺轉動
程式碼:
#include <GL/glut.h>

float angleX =0;
void keyboard(unsigned char key, int x, int y)  //鍵盤控制
{
if(key=='1') angleX+=5;
if(key=='2') angleX-=5;
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();
glRotatef(angleX, 0,1,0);
glutSolidTeapot(0.3);
  glPopMatrix();
  glutSwapBuffers();
}

int main()
{
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
  glutCreateWindow("week11");

  glutDisplayFunc(display);
  glutKeyboardFunc(keyboard);

myLight();
  glutMainLoop();
}


新增:畫出三個球,分別在茶壺的左、右、上
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.5, 0, 0);
glutSolidSphere(0.3, 30, 30);
glTranslatef(-0.5, 0.5, 0);
glutSolidSphere(0.3, 30, 30);
glTranslatef(-0.5, -0.5, 0);
glutSolidSphere(0.3, 30, 30);
  glPopMatrix();
  glutSwapBuffers();
}


新增:畫出四個球,分別在茶壺的左、右、上以及右下
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.5, 0, 0);
glutSolidSphere(0.3, 30, 30);
  glPopMatrix();
  glPushMatrix();
glTranslatef(-0.5, 0, 0);
glutSolidSphere(0.3, 30, 30);
  glPopMatrix();
  glPushMatrix();
glTranslatef(0, 0.5, 0);
glutSolidSphere(0.3, 30, 30);
  glPopMatrix();
  glPushMatrix();
glTranslatef(0.5, -0.5, 0);
glutSolidSphere(0.3, 30, 30);
  glPopMatrix();
  glPopMatrix();
  glutSwapBuffers();
}


程式碼:
#include <GL/glut.h>

float angleX =0, angle2 = 0, angle3 = 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;
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();
glRotatef(angleX, 0,1,0);
glutWireTeapot(0.3);
 
  //右手 
  glPushMatrix();
  glTranslatef(0.4, 0, 0);
  glutSolidSphere(0.2, 30, 30);
  glPushMatrix();
glRotatef(angle2, 0, 0, 1);
glTranslatef(0.2, 0, 0);
glScalef(0.5, 0.2, 0.2);
  glutSolidCube(1);
  glPopMatrix();
glPopMatrix();
 
  //頭 
  glPushMatrix();
  glTranslatef(0, 0.5, 0);
  glutSolidSphere(0.25, 30, 30);
  glPopMatrix();
 
  //左手 
  glPushMatrix();
  glTranslatef(-0.4, 0, 0);
  glutSolidSphere(0.2, 30, 30);
  glPushMatrix();
glRotatef(angle2, 0, 0, 1);
glTranslatef(-0.2, 0, 0);
glScalef(0.5, 0.2, 0.2);
  glutSolidCube(1);
  glPopMatrix();
glPopMatrix();
//左大腿 

 
glPushMatrix();
glTranslatef(-0.1, -0.2, 0);
glRotatef(angle3, 0, 0, 1);
glTranslatef(0, -0.2, 0);
glPushMatrix();
glScalef(0.15, 0.4, 0.15);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
 

//右大腿 
glPushMatrix();
glTranslatef(0.1, -0.2, 0);
glRotatef(-angle3, 0, 0, 1);
glTranslatef(0, -0.2, 0);
glPushMatrix();
glScalef(0.15, 0.4, 0.15);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPopMatrix();
  glutSwapBuffers();
}

int main()
{
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
  glutCreateWindow("week11");

  glutDisplayFunc(display);
  glutKeyboardFunc(keyboard);

  myLight();
  glutMainLoop();
}


沒有留言:

張貼留言