2014年4月11日 星期五

多拉A夢

#include<GL/glut.h>
float angle;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle,0,1,0);
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();
}

int main()
{
  glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week08");
glutDisplayFunc(display);
glutIdleFunc(idle);
glutMainLoop();
}
要有打光效果

#include<GL/glut.h>
float angle;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle,0,1,0);
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();
}
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 };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
int main()
{
  glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week08");
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);
glutDisplayFunc(display);
glutIdleFunc(idle);

glutMainLoop();  
}

沒有留言:

張貼留言