#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);
glTranslatef(0.6,0,0);
glutSolidSphere(0.2,30,30);
glTranslatef(-1.2,0,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='a') angleX+=3;
if(key=='b') 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();
}
glPushMatrix();
glRotatef(angleX, 0,1,0);
glutWireTeapot(0.3);
glPushMatrix();
glTranslatef(0.6,0,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPushMatrix();
glTranslatef(-0.6,0,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPushMatrix();
glTranslatef(0,0.4,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPopMatrix();
#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);
glutWireTeapot(0.3);
glPushMatrix();
glTranslatef(0.5,0,0);
glutSolidSphere(0.2,30,30);
glTranslatef(0.3,0,0);
glScalef(1.2,0.8,1);
glutSolidCube(0.3);
glPopMatrix();
glPushMatrix();
glTranslatef(-0.5,0,0);
glutSolidSphere(0.2,30,30);
glTranslatef(-0.3,0,0);
glScalef(1.2,0.8,1);
glutSolidCube(0.3);
glPopMatrix();
glPushMatrix();
glTranslatef(0,0.4,0);
glutSolidSphere(0.2,30,30);
glTranslatef(0,0.3,0);
glScalef(0.8,1.2,1);
glutSolidCube(0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='a') angleX+=3;
if(key=='b') 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,anglel=0;
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.5,0,0);
glutSolidSphere(0.2,30,30);//左邊球
glPushMatrix();
glRotatef(anglel,0,0,1);
glTranslatef(0.3,0,0);
glScalef(1.2,0.8,1);
glutSolidCube(0.3);
glPopMatrix();
glPopMatrix();//裡面的移動 不影響外面
glPushMatrix();
glTranslatef(-0.5,0,0);
glutSolidSphere(0.2,30,30);//左邊球
glPushMatrix();
glRotatef(anglel,0,0,1);
glTranslatef(-0.3,0,0);
glScalef(1.2,0.8,1);
glutSolidCube(0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0,0.4,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=='b') angleX-=3;
if(key=='1')anglel+=3;
if(key=='2')anglel-=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,anglel=0;
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.5,0,0);
glutSolidSphere(0.2,30,30);//左邊球
glPushMatrix();
glRotatef(anglel,0,0,1);
glTranslatef(0.3,0,0);
glScalef(1.2,0.8,1);
glutSolidCube(0.3);
glPopMatrix();
glPopMatrix();//裡面的移動 不影響外面
glPushMatrix();
glTranslatef(-0.5,0,0);
glutSolidSphere(0.2,30,30);//左邊球
glPushMatrix();
glRotatef(anglel,0,0,1);
glTranslatef(-0.3,0,0);
glScalef(1.2,0.8,1);
glutSolidCube(0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0,0.4,0);
glutSolidSphere(0.2,30,30);//上面球
glPopMatrix();
glPopMatrix();
glPushMatrix();//left foot
glTranslatef(-0.2,-0.4,0);
glRotatef(anglel, 0,-1,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();
glRotatef(anglel, 0,0,-1);
glTranslatef(0,-0.3,0);
glScalef(0.8,1.2,1);
glutSolidCube(0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();//right foot
glTranslatef(0.2,-0.4,0);
glRotatef(anglel, 0,1,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();
glRotatef(anglel, 0,0,1);
glTranslatef(0,-0.3,0);
glScalef(0.8,1.2,1);
glutSolidCube(0.3);
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')anglel+=3;
if(key=='2')anglel-=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();
}





沒有留言:
張貼留言