1.上傳作業
2.機器人Robot
3.關節轉動T-R-T
4.期末作品
5.T-R-T考試
//--------------------------------------------------
2.機器人
一樣先將freeglut&五個咒語設定好
先將兩個茶壺做出來
然後將其旋轉成自轉和公轉


程式碼為:
#include <GL/glut.h>
float rotX=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glutSolidTeapot(0.3);
glRotatef(rotX,0,0,1);///自轉 轉了
glTranslatef(0.5,0,0); 移動
glRotatef(rotX,0,0,1);///公轉 再轉
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y)
{
rotX=x;///滑鼠動量
glutPostRedisplay();///再播放
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Robot");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
//--------------------------------
進階
將期移動在旋轉
做出像關節的樣子


程式碼為
#include <GL/glut.h>
float rotX=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glutSolidTeapot(0.2);
glTranslatef(0.33,0.05,0);
glRotatef(rotX,0,0,1);
glTranslatef(0.25,0,0);
glutSolidTeapot(0.2);
glTranslatef(0.33,0.05,0);
glRotatef(rotX,0,0,1);
glTranslatef(0.25,0,0);
glutSolidTeapot(0.2);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y)
{
rotX=x;
glutPostRedisplay();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Robot");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
//-----------------------------------


程式碼
#include <GL/glut.h>
float rotX=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glPushMatrix();
glutWireCube(0.3);///身體
glPushMatrix();
glTranslatef(0,0.2,0);
glutWireCube(0.1);///頭
glPopMatrix();
glPushMatrix();///右邊
glTranslatef(0.15,0,0);
glRotatef(rotX,0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///上臂
glTranslatef(0.05,0,0);
glRotatef(rotX,0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///下臂
glTranslatef(0.05,0,0);
glRotatef(rotX,0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///右手
glPopMatrix();
glPushMatrix();///左邊
glTranslatef(-0.15,0,0);
glRotatef(-rotX,0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///上臂
glTranslatef(-0.05,0,0);
glRotatef(-rotX,0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///下臂
glTranslatef(-0.05,0,0);
glRotatef(-rotX,0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///左手
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y)
{
rotX=x;
glutPostRedisplay();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Robot");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}

沒有留言:
張貼留言