用z和x來轉動茶壺
在上方、左方和右方加圓球
用push和pop把每個圓球都包起來
在左右加上手臂(長方體)
轉動手臂(錯誤的方法)
轉動手臂(正確的方法)
加上大腿,轉動的中心在大腿上方
掛上小腿
程式碼內收
--茶壺和3個圓球--
#include <GL/glut.h>
float angleX=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angleX, 0,1,0);
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(0.4,0,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPushMatrix();
glTranslatef(-0.4,0,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPushMatrix();
glTranslatef(0,0.3,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='z') angleX+=3;
if(key=='x') angleX-=3;
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("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glutMainLoop();
}
--轉動的手臂--
#include <GL/glut.h>
float angleX=0, angle1=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angleX, 0,1,0);
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(0.4,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();
glRotatef(angle1, 0,0,1);
glTranslatef(0.4,0,0);
glScalef(1.5,0.5,0.5);
glutSolidCube(0.4);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.4,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();
glRotatef(angle1, 0,0,-1);
glTranslatef(-0.4,0,0);
glScalef(1.5,0.5,0.5);
glutSolidCube(0.4);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0,0.3,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='z') angleX+=3;
if(key=='x') angleX-=3;
if(key=='c') angle1+=3;
if(key=='v') angle1-=3;
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("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glutMainLoop();
}
--大腿+小腿--
#include <GL/glut.h>
float angleX=0, angle1=0,angle2=0, angle3=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angleX, 0,1,0);
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(0.4,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix(); //左手
glRotatef(angle1, 0,0,1);
glTranslatef(0.4,0,0);
glScalef(1.5,0.5,0.5);
glutSolidCube(0.4);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.4,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix(); //右手
glRotatef(angle1, 0,0,-1);
glTranslatef(-0.4,0,0);
glScalef(1.5,0.5,0.5);
glutSolidCube(0.4);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0.15,0.05,0);
glRotatef(angle2, 1,0,0);
glTranslatef(0,-1.0/2,0);
glPushMatrix();//左大腿
glScalef(0.65,1.0,0.65);
glutSolidCube(0.4);
glPopMatrix();
glPushMatrix(); //左小腿
glTranslatef(0,0.08,0);
glRotatef(angle3, 1,0,0);
glTranslatef(0,-1.0/2,0);
glScalef(0.6,1.0,0.6);
glutSolidCube(0.4);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.15,0.05,0);
glRotatef(angle2, 1,0,0);
glTranslatef(0,-1.0/2,0);
glPushMatrix(); //右大腿
glScalef(0.65,1.0,0.65);
glutSolidCube(0.4);
glPopMatrix();
glPushMatrix(); //右小腿
glTranslatef(0,0.08,0);
glRotatef(angle3, 1,0,0);
glTranslatef(0,-1.0/2,0);
glScalef(0.6,1.0,0.6);
glutSolidCube(0.4);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0,0.3,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='a') angleX+=3;
if(key=='d') angleX-=3;
if(key=='z') angle1+=3;
if(key=='x') angle1-=3;
if(key=='c') { angle2+=3; angle3+=0.03;}
if(key=='v') { angle2-=3; angle3-=0.03;}
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("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glutMainLoop();
}
--轉動的手臂--
#include <GL/glut.h>
float angleX=0, angle1=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angleX, 0,1,0);
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(0.4,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();
glRotatef(angle1, 0,0,1);
glTranslatef(0.4,0,0);
glScalef(1.5,0.5,0.5);
glutSolidCube(0.4);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.4,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();
glRotatef(angle1, 0,0,-1);
glTranslatef(-0.4,0,0);
glScalef(1.5,0.5,0.5);
glutSolidCube(0.4);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0,0.3,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='z') angleX+=3;
if(key=='x') angleX-=3;
if(key=='c') angle1+=3;
if(key=='v') angle1-=3;
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("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glutMainLoop();
}
--大腿+小腿--
#include <GL/glut.h>
float angleX=0, angle1=0,angle2=0, angle3=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angleX, 0,1,0);
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(0.4,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix(); //左手
glRotatef(angle1, 0,0,1);
glTranslatef(0.4,0,0);
glScalef(1.5,0.5,0.5);
glutSolidCube(0.4);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.4,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix(); //右手
glRotatef(angle1, 0,0,-1);
glTranslatef(-0.4,0,0);
glScalef(1.5,0.5,0.5);
glutSolidCube(0.4);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0.15,0.05,0);
glRotatef(angle2, 1,0,0);
glTranslatef(0,-1.0/2,0);
glPushMatrix();//左大腿
glScalef(0.65,1.0,0.65);
glutSolidCube(0.4);
glPopMatrix();
glPushMatrix(); //左小腿
glTranslatef(0,0.08,0);
glRotatef(angle3, 1,0,0);
glTranslatef(0,-1.0/2,0);
glScalef(0.6,1.0,0.6);
glutSolidCube(0.4);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.15,0.05,0);
glRotatef(angle2, 1,0,0);
glTranslatef(0,-1.0/2,0);
glPushMatrix(); //右大腿
glScalef(0.65,1.0,0.65);
glutSolidCube(0.4);
glPopMatrix();
glPushMatrix(); //右小腿
glTranslatef(0,0.08,0);
glRotatef(angle3, 1,0,0);
glTranslatef(0,-1.0/2,0);
glScalef(0.6,1.0,0.6);
glutSolidCube(0.4);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0,0.3,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='a') angleX+=3;
if(key=='d') angleX-=3;
if(key=='z') angle1+=3;
if(key=='x') angle1-=3;
if(key=='c') { angle2+=3; angle3+=0.03;}
if(key=='v') { angle2-=3; angle3-=0.03;}
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("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glutMainLoop();
}








沒有留言:
張貼留言