今天在教幫茶壺畫手臂,旋轉
畫完右邊的,試著把左邊的也畫出來
把右邊的複製下來,貼成第二段
然後將x座標改成負的
今天的程式碼 ↓
--------------------------------------------------------------------------------------------------------------------------
#include <GL/glut.h>float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(255,255,255);
glutSolidTeapot( 0.3 );///身體
glPushMatrix();///開始畫右邊手臂
glTranslatef(0.2, 0, 0);///右肩
glRotatef(angle, 0,0,1);
glTranslatef(0.15, 0,0);
glColor3f(1,0,0);///紅色
glutSolidTeapot( 0.15 );///右手臂
glPushMatrix();///畫右邊手軸
glTranslatef(0.2, 0, 0);///右手肘
glRotatef(angle, 0,0,1);
glTranslatef(0.15, 0,0);
glColor3f(1,0,0);///紅色
glutSolidTeapot( 0.15 );
glPopMatrix();
glPopMatrix();///要來畫左邊
glPushMatrix();///開始畫左邊手臂
glTranslatef(-0.2, 0, 0);///左肩
glRotatef(angle, 0,0,1);
glTranslatef(-0.15, 0,0);
glColor3f(1,0,0);///紅色
glutSolidTeapot( 0.15 );///左手臂
glPushMatrix();///畫左邊手軸
glTranslatef(-0.2, 0, 0);///左手肘
glRotatef(angle, 0,0,1);
glTranslatef(-0.15, 0,0);
glColor3f(1,0,0);///紅色
glutSolidTeapot( 0.15 );
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void motion(int x, int y)///讓手臂隨著滑鼠動
{
angle=x;
display();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow("06160795");
glutIdleFunc(display);
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
--------------------------------------------------------------------------------------------------------------------------
可是這樣手臂的轉動會很奇怪,所以對手臂轉動的程式碼進行修改
--------------------------------------------------------------------------------------------------------------------------

void motion(int x, int y)
{
angle += (x- oldX);/// 算每次新的跟舊的相減的移動幅度
oldX=x;
display();
}
void mouse (int botton, int state, int x, int y)
{
oldX=x; oldY=y;
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow("06160795");
glutIdleFunc(display);
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutMainLoop();
}
--------------------------------------------------------------------------------------------------------------------------
可是這樣手臂的轉動會很奇怪,所以對手臂轉動的程式碼進行修改
--------------------------------------------------------------------------------------------------------------------------

void motion(int x, int y)
{
angle += (x- oldX);/// 算每次新的跟舊的相減的移動幅度
oldX=x;
display();
}
void mouse (int botton, int state, int x, int y)
{
oldX=x; oldY=y;
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow("06160795");
glutIdleFunc(display);
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutMainLoop();
}
--------------------------------------------------------------------------------------------------------------------------
利用鍵盤讓每個關節可以獨自動
--------------------------------------------------------------------------------------------------------------------------
#include <GL/glut.h>
float angle1=0, angle2=0, angle3=0, angle4=0;/// 配合鍵盤
int jointID=1;///配合鍵盤
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(255,255,255);
glutSolidTeapot( 0.3 );///身體
glPushMatrix();///開始畫右邊手臂
glTranslatef(0.2, 0, 0);///右肩
glRotatef(angle1, 0,0,1);
glTranslatef(0.15, 0,0);
glColor3f(1,0,0);///紅色
glutSolidTeapot( 0.15 );///右手臂
glPushMatrix();///畫右邊手軸
glTranslatef(0.2, 0, 0);///右手肘
glRotatef(angle2, 0,0,1);
glTranslatef(0.15, 0,0);
glColor3f(1,0,0);///紅色
glutSolidTeapot( 0.15 );
glPopMatrix();
glPopMatrix();///要來畫左邊
glPushMatrix();///開始畫左邊手臂
glTranslatef(-0.2, 0, 0);///左肩
glRotatef(-angle3, 0,0,1);
glTranslatef(-0.15, 0,0);
glColor3f(1,0,0);///紅色
glutSolidTeapot( 0.15 );///左手臂
glPushMatrix();///畫左邊手軸
glTranslatef(-0.2, 0, 0);///左手肘
glRotatef(-angle4, 0,0,1);
glTranslatef(-0.15, 0,0);
glColor3f(1,0,0);///紅色
glutSolidTeapot( 0.15 );
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int oldX,oldY;
void motion(int x, int y)
{
if(jointID==1) angle1 += (x-oldX);///NOW3 用滑鼠mouse來控制angle ///NOW6 配合鍵盤
if(jointID==2) angle2 += (x-oldX);///NOW3 用滑鼠mouse來控制angle ///NOW6 配合鍵盤
if(jointID==3) angle3 += (x-oldX);///NOW3 用滑鼠mouse來控制angle ///NOW6 配合鍵盤
if(jointID==4) angle4 += (x-oldX);///NOW3 用滑鼠mouse來控制angle ///NOW6 配合鍵盤
oldX=x;
display();
}
void mouse (int botton, int state, int x, int y)
{
oldX=x; oldY=y;
}
void keyboard(unsigned char key, int x, int y)///NOW6 配合鍵盤
{
if(key=='1') jointID=1;///NOW6 配合鍵盤
if(key=='2') jointID=2;///NOW6 配合鍵盤
if(key=='3') jointID=3;///NOW6 配合鍵盤
if(key=='4') jointID=4;///NOW6 配合鍵盤
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow("06160795");
glutIdleFunc(display);
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);///配合鍵盤
glutMainLoop();
}



沒有留言:
張貼留言