第17週 期末作品展示
1. gluLookAt( eyeX, eyeY, eyeZ, objX, objY, objZ, upX, upY, upZ);
1.1. 裝 glut DevPak, 把你的期末作品開起來, 試著用 gluLookAt()來改改看攝影機的位子, 像是 45度俯視看你的臉
換色
程式內碼:
#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>
float angleX=0;
float angle1=0;
float eyeX=0.1,eyeY=0.5,eyeZ=0.5, cX=0,cY=0,cZ=0, upX=0,upY=1,upZ=0;
void timer(int t)
{
glutTimerFunc(30,timer,t+1);
eyeX=cos(t/18.0);
eyeZ=sin(t/18.0);
eyeY-=0.01;
if(t%2==0)
glClearColor( (rand()%255)/255.0, (rand()%255)/255.0 , (rand()%255)/255.0,0);
glutPostRedisplay();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
gluLookAt(eyeX,eyeY,eyeZ, cX,cY,cZ, upX,upY,upZ);
glRotatef(angleX, 0, 1, 0);
glutWireTeapot(0.3);
glPushMatrix();//右邊
glTranslatef(0.4, 0, 0);
glutSolidSphere(0.2, 30, 30);
glPushMatrix();
glRotatef(angle1, 1, 1, 1);
glTranslatef(0.3, 0, 0);
glScalef(1.5, 1, 1);
glutSolidCube(0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();//左邊
glTranslatef(-0.4, 0, 0);
glutSolidSphere(0.2, 30, 30);
glPushMatrix();
glRotatef(angle1, 1, 1, 1);
glTranslatef(-0.3, 0, 0);
glScalef(1.5, 1, 1);
glutSolidCube(0.3);
glPopMatrix();
glPopMatrix();
//階層性關節轉動(T-R-T)
glPushMatrix();//右大腿
glTranslatef(0.2, -0.2, 0);
glRotatef(angle1, 0, 0, 1);
glTranslatef(0,-0.36/2,0);
glPushMatrix();
glScalef(1,1.8,1);
glutSolidCube(0.2);
glPopMatrix();
glPushMatrix();//右小腿
glTranslatef(0,-0.18,0);
glRotatef(angle1, 0,0,1);
glTranslatef(0,-0.36/2,0);
glScalef(0.2,0.36,0.2);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPushMatrix();//左大腿
glTranslatef(-0.2, -0.2, 0);
glRotatef(angle1, 0, 0, 1);
glTranslatef(0,-0.36/2,0);
glPushMatrix();
glScalef(1,1.8,1);
glutSolidCube(0.2);
glPopMatrix();
glPushMatrix();//左小腿
glTranslatef(0,-0.18,0);
glRotatef(angle1, 0,0,1);
glTranslatef(0,-0.36/2,0);
glScalef(0.2,0.36,0.2);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPushMatrix();//頭
glTranslatef(0, 0.4, 0);
glRotatef(angle1, 0, 0, 1);
glTranslatef(0,-0.36/2,0);
glScalef(0.2, 0.25, 0.2);
glutSolidSphere(0.8, 30, 30);
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;
if (key==' ') eyeX+=0.05;
if (key=='o')
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2,2,-2,2,-2,2);
glMatrixMode(GL_MODELVIEW);
}
else if(key=='c')
glutTimerFunc(0,timer,0);
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("weed11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glutMainLoop();
}


沒有留言:
張貼留言