2014年4月11日 星期五

多拉A夢

#include <GL/glut.h>
float angle=0;
void display()
{
glEnable(GL_DEPTH_TEST);//要3D的深度測試
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清空畫面 
    glPushMatrix();
    glRotatef(angle,0,1,0);//對整個多拉A夢做旋轉 
   
    glPushMatrix();
glTranslatef(0,0.3,0);//往上移動
glColor3f(0,0,1);
glutSolidSphere(0.3,30,30);//頭


glColor3f(1,1,1);//白色
glTranslatef(0,-0.1,0.1);
glutSolidSphere(0.25,30,30);//臉
glPopMatrix();

glPushMatrix();
glColor3f(1,1,1);
//glutSolidSphere(0.2,30,30);//身體 
glPopMatrix();
glPopMatrix();

glutSwapBuffers();
}
void idle()
{
angle++;
glutPostRedisplay(); //要去Re-Display重新顯示 
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE| GLUT_DEPTH);
glutCreateWindow("week08");
glutDisplayFunc(display);
glutIdleFunc(idle);//執行某個函式 

glutMainLoop();
}

加入打光片段程式碼
const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
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_AMBIENT,  light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);



#include <GL/glut.h>
float angle=0;
void eye()
{
glPushMatrix();
glColor3f(1,1,1);
glutSolidSphere(0.1,20,20);//先畫白色的眼球 
glTranslatef(0,0,0.1);// 動一點點試試看
  glColor3f(0,0,0);//再畫瞳孔(黑)
glutSolidSphere(0.02,10,10);
glPopMatrix();  
}
void head()
{
glPushMatrix();
glTranslatef(0,0.3,0);//往上移動
glColor3f(0,0,1);
glutSolidSphere(0.3,30,30);//頭

glColor3f(1,1,1);
glTranslatef(0,-0.1,0.1);
glutSolidSphere(0.25,30,30);//臉
glPopMatrix();
glPushMatrix();
glColor3f(1,1,1);
//glutSolidSphere(0.2,30,30);身體
glPopMatrix();

void display()
{
glEnable(GL_DEPTH_TEST);//要3D的深度測試
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清空畫面 
    glPushMatrix();
    glRotatef(angle,0,1,0);//對整個多拉A夢做旋轉 
    head();
    glTranslatef(0,0,0.6);//往外慢慢移
    eye();
    /*ushMatrix();
glTranslatef(0,0.3,0);//往上移動
glColor3f(0,0,1);
glutSolidSphere(0.3,30,30);//頭

glColor3f(1,1,1);//白色
glTranslatef(0,-0.1,0.1);
glutSolidSphere(0.25,30,30);//臉*/
glPopMatrix();

glPushMatrix();
glColor3f(1,1,1);
//glutSolidSphere(0.2,30,30);//身體 
glPopMatrix();
glPopMatrix();

glutSwapBuffers();
}
void idle()
{
angle++;
glutPostRedisplay(); //要去Re-Display重新顯示 
}
const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
int main()
{
glutInitDisplayMode(GLUT_DOUBLE| GLUT_DEPTH);
glutCreateWindow("week08");
glutDisplayFunc(display);
glutIdleFunc(idle);//執行某個函式 

glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);

    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

glutMainLoop();

}


沒有留言:

張貼留言