2014年6月12日 星期四

Week11

畫四顆球的原程式碼
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);
glTranslatef(0.5,-0.5,0);
glutSolidSphere(0.3,30,30);
glPopMatrix();
畫四顆球的改良程式碼
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;
float angleh=0;
float anglet=0;
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;
if(key=='2') angleX-=5;
if(key=='3') angleh+=5;
if(key=='4') angleh-=5;
if(key=='5') anglet+=5;
if(key=='6') anglet-=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,0.4,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();

glPushMatrix();//右臂
glTranslatef(0.3,0.05,0);
glutSolidSphere(0.15,30,30);
glPushMatrix();
glRotatef(angleh,0,0,1);
glTranslatef(0.25,0.05,0);
glScalef(3,1,1);
glutSolidCube(0.1);
glPopMatrix();
glPopMatrix();

glPushMatrix();//左臂
glTranslatef(-0.3,0.05,0);
glutSolidSphere(0.15,30,30);
glPushMatrix();
glRotatef(-angleh,0,0,1);
glTranslatef(-0.25,0.05,0);
glScalef(3,1,1);
glutSolidCube(0.1);
glPopMatrix();
glPopMatrix();

glPushMatrix();//右腳
glTranslatef(0.15,-0.25,0);
glutSolidSphere(0.1,30,30);
glPushMatrix();
glRotatef(anglet,1,0,0);
glTranslatef(0.02,-0.2,0);
glScalef(1,3,1);
glutSolidCube(0.1);
glPopMatrix();
glPopMatrix();

glPushMatrix();//左腳
glTranslatef(-0.15,-0.25,0);
glutSolidSphere(0.1,30,30);
glPushMatrix();
glRotatef(-anglet,1,0,0);
glTranslatef(-0.02,-0.2,0);
glScalef(1,3,1);
glutSolidCube(0.1);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}

int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glutMainLoop();
}


沒有留言:

張貼留言