Week11 上課筆記
小考T-R-T轉動
主題:階層,轉動,骨架
主題:鍵盤按鍵
主題一 :
利用茶壺來當作身體與手臂
///////
#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 );///(3)往右移動, 掛在身體的右肩
glRotatef(angle, 0,0,1);///(2)旋轉
glTranslatef(0.28, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glutSolidTeapot( 0.2 );///手臂
glPopMatrix();
glutSwapBuffers();
angle++;
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11 TRT");
glutIdleFunc(display);///Now!!!!
glutDisplayFunc(display);
glutMainLoop();
}
//////
把手臂分為上手臂與下手臂 並 加上左邊部分 (1)
//////////
#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 );///(3)往右移動, 掛在身體的右肩
glRotatef(angle, 0,0,1);///(2)旋轉
glTranslatef(0.28, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glutSolidTeapot( 0.2 );///上手臂
glPushMatrix();///右下手臂
glTranslatef(0.2, 0,0 );///(3)往右移動, 掛在身體的右肘
glRotatef(angle, 0,0,1);///(2)旋轉
glTranslatef(0.28, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glutSolidTeapot( 0.2 );///上手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();///左邊的手臂
glTranslatef(-0.2, 0,0 );///(3)往左移動, 掛在身體的左肩
glRotatef(angle, 0,0,1);///(2)旋轉
glTranslatef(-0.28, 0,0);///(1)往左推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glutSolidTeapot( 0.2 );///上手臂
glPushMatrix();///左下手臂
glTranslatef(-0.2, 0,0 );///(3)往左移動, 掛在身體的左肘
glRotatef(angle, 0,0,1);///(2)旋轉
glTranslatef(-0.28, 0,0);///(1)往左推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glutSolidTeapot( 0.2 );///上手臂
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
angle++;
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11 TRT");
glutIdleFunc(display);///Now!!!!
glutDisplayFunc(display);
glutMainLoop();
}
//////////
原本為自動旋轉 (2)
更改為利用滑鼠操控旋轉角度
/////
#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 );///(3)往右移動, 掛在身體的右肩
glRotatef(angle, 0,0,1);///(2)旋轉
glTranslatef(0.2, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(0,255,0);///綠色
glutSolidCube( 0.3 );///上手臂
glPushMatrix();///右下手臂
glTranslatef(0.1, 0,0 );///(3)往右移動, 掛在身體的右肘
glRotatef(angle, 0,0,1);///(2)旋轉
glTranslatef(0.2, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glutSolidCube( 0.3 );///上手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();///左邊的手臂
glTranslatef(-0.2, 0,0 );///(3)往左移動, 掛在身體的左肩
glRotatef(-angle, 0,0,1);///(2)旋轉
glTranslatef(-0.2, 0,0);///(1)往左推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(0,0,255);///藍色
glutSolidCube( 0.3 );///上手臂
glPushMatrix();///左下手臂
glTranslatef(-0.1, 0,0 );///(3)往左移動, 掛在身體的左肘
glRotatef(-angle, 0,0,1);///(2)旋轉
glTranslatef(-0.2, 0,0);///(1)往左推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glutSolidCube( 0.3 );///上手臂
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
///angle++;
}
void motion (int x, int y)
{
angle= x; ///now2!!
display(); /// now2!!
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11 TRT");
glutIdleFunc(display);///Now!!!!
glutDisplayFunc(display);
glutMotionFunc(motion);///now2!!!
glutMainLoop();
}
/////
利用鍵盤控制可旋轉的關節
///
#include <GL/glut.h>
float angle[20]; ///now3
int jointID=1; ///now3!!! ///1,2,3,4
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(255,255,255);///白色
glutSolidTeapot( 0.3 );///身體
glPushMatrix();///右邊的手臂
glTranslatef(0.2, 0,0 );///(3)往右移動, 掛在身體的右肩
glRotatef(angle[1], 0,0,1);///(2)旋轉
glTranslatef(0.2, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(0,255,0);///綠色
glutSolidCube( 0.25 );///上手臂
glPushMatrix();///右下手臂
glTranslatef(0.05, 0,0 );///(3)往右移動, 掛在身體的右肘
glRotatef(angle[2], 0,0,1);///(2)旋轉
glTranslatef(0.1, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glRotatef(90,0,0.5,0);
glutSolidCone(0.1,0.5,30,30);///下手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();///左邊的手臂
glTranslatef(-0.2, 0,0 );///(3)往左移動, 掛在身體的左肩
glRotatef(-angle[3], 0,0,1);///(2)旋轉
glTranslatef(-0.2, 0,0);///(1)往左推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(0,0,255);///藍色
glutSolidCube( 0.25 );///上手臂
glPushMatrix();///左下手臂
glTranslatef(-0.05, 0,0 );///(3)往左移動, 掛在身體的左肘
glRotatef(-angle[4], 0,0,1);///(2)旋轉
glTranslatef(-0.1, 0,0);///(1)往左推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glRotatef(-90,0,0.5,0);
glutSolidCone(0.1,0.5,30,30);///下手臂
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
///angle++;
}
void motion (int x, int y)///now2!!!
{
angle[jointID] = x; ///now3!!!
display(); /// now2!!
}
void keyboard(unsigned char key,int x, int y)///now3!!!
{
if(key=='1') jointID=1; ///NOW3!!!
if(key=='2') jointID=2; ///NOW3!!!
if(key=='3') jointID=3; ///NOW3!!!
if(key=='4') jointID=4; ///NOW3!!!
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11 TRT");
glutIdleFunc(display);///Now!!!!
glutDisplayFunc(display);
glutMotionFunc(motion);///now2!!!
glutKeyboardFunc(keyboard);///NOW3!!!
glutMainLoop();
}
///
優化程式碼
滑鼠控制時不會有順移的情況
////
#include <GL/glut.h>
float angle[20]; ///now3
int jointID=1; ///now3!!! ///1,2,3,4
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(255,255,255);///白色
glutSolidTeapot( 0.3 );///身體
glPushMatrix();///右邊的手臂
glTranslatef(0.2, 0,0 );///(3)往右移動, 掛在身體的右肩
glRotatef(angle[1], 0,0,1);///(2)旋轉
glTranslatef(0.2, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(0,255,0);///綠色
glutSolidCube( 0.25 );///上手臂
glPushMatrix();///右下手臂
glTranslatef(0.05, 0,0 );///(3)往右移動, 掛在身體的右肘
glRotatef(angle[2], 0,0,1);///(2)旋轉
glTranslatef(0.1, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glRotatef(90,0,0.5,0);
glutSolidCone(0.1,0.5,30,30);///下手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();///左邊的手臂
glTranslatef(-0.2, 0,0 );///(3)往左移動, 掛在身體的左肩
glRotatef(-angle[3], 0,0,1);///(2)旋轉
glTranslatef(-0.2, 0,0);///(1)往左推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(0,0,255);///藍色
glutSolidCube( 0.25 );///上手臂
glPushMatrix();///左下手臂
glTranslatef(-0.05, 0,0 );///(3)往左移動, 掛在身體的左肘
glRotatef(-angle[4], 0,0,1);///(2)旋轉
glTranslatef(-0.1, 0,0);///(1)往左推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glRotatef(-90,0,0.5,0);
glutSolidCone(0.1,0.5,30,30);///下手臂
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
///angle++;
}
int oldX,oldY;
void mouse(int button, int state, int x, int y)///Now4!!!!
{///當mouse按下去時,要記下!! 要記得要讓它有作用!!
oldX = x; oldY = y;///Now5!!!!
}
void motion (int x, int y)///now2!!!
{
angle[jointID] += x -oldX;///Now4!!!!///Now5!!!!
oldX = x;///Now5!!!!
display(); /// now2!!
}
void keyboard(unsigned char key,int x, int y)///now3!!!
{
if(key=='1') jointID=1; ///NOW3!!!
if(key=='2') jointID=2; ///NOW3!!!
if(key=='3') jointID=3; ///NOW3!!!
if(key=='4') jointID=4; ///NOW3!!!
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11 TRT");
glutIdleFunc(display);///Now!!!!
glutDisplayFunc(display);
glutMotionFunc(motion);///now2!!!
glutMouseFunc(mouse);///Now5!!!!
glutKeyboardFunc(keyboard);///NOW3!!!
glutMainLoop();
}
///
原本為自動旋轉 (2)
更改為利用滑鼠操控旋轉角度
/////
#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 );///(3)往右移動, 掛在身體的右肩
glRotatef(angle, 0,0,1);///(2)旋轉
glTranslatef(0.2, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(0,255,0);///綠色
glutSolidCube( 0.3 );///上手臂
glPushMatrix();///右下手臂
glTranslatef(0.1, 0,0 );///(3)往右移動, 掛在身體的右肘
glRotatef(angle, 0,0,1);///(2)旋轉
glTranslatef(0.2, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glutSolidCube( 0.3 );///上手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();///左邊的手臂
glTranslatef(-0.2, 0,0 );///(3)往左移動, 掛在身體的左肩
glRotatef(-angle, 0,0,1);///(2)旋轉
glTranslatef(-0.2, 0,0);///(1)往左推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(0,0,255);///藍色
glutSolidCube( 0.3 );///上手臂
glPushMatrix();///左下手臂
glTranslatef(-0.1, 0,0 );///(3)往左移動, 掛在身體的左肘
glRotatef(-angle, 0,0,1);///(2)旋轉
glTranslatef(-0.2, 0,0);///(1)往左推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glutSolidCube( 0.3 );///上手臂
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
///angle++;
}
void motion (int x, int y)
{
angle= x; ///now2!!
display(); /// now2!!
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11 TRT");
glutIdleFunc(display);///Now!!!!
glutDisplayFunc(display);
glutMotionFunc(motion);///now2!!!
glutMainLoop();
}
利用鍵盤控制可旋轉的關節
///
#include <GL/glut.h>
float angle[20]; ///now3
int jointID=1; ///now3!!! ///1,2,3,4
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(255,255,255);///白色
glutSolidTeapot( 0.3 );///身體
glPushMatrix();///右邊的手臂
glTranslatef(0.2, 0,0 );///(3)往右移動, 掛在身體的右肩
glRotatef(angle[1], 0,0,1);///(2)旋轉
glTranslatef(0.2, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(0,255,0);///綠色
glutSolidCube( 0.25 );///上手臂
glPushMatrix();///右下手臂
glTranslatef(0.05, 0,0 );///(3)往右移動, 掛在身體的右肘
glRotatef(angle[2], 0,0,1);///(2)旋轉
glTranslatef(0.1, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glRotatef(90,0,0.5,0);
glutSolidCone(0.1,0.5,30,30);///下手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();///左邊的手臂
glTranslatef(-0.2, 0,0 );///(3)往左移動, 掛在身體的左肩
glRotatef(-angle[3], 0,0,1);///(2)旋轉
glTranslatef(-0.2, 0,0);///(1)往左推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(0,0,255);///藍色
glutSolidCube( 0.25 );///上手臂
glPushMatrix();///左下手臂
glTranslatef(-0.05, 0,0 );///(3)往左移動, 掛在身體的左肘
glRotatef(-angle[4], 0,0,1);///(2)旋轉
glTranslatef(-0.1, 0,0);///(1)往左推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glRotatef(-90,0,0.5,0);
glutSolidCone(0.1,0.5,30,30);///下手臂
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
///angle++;
}
void motion (int x, int y)///now2!!!
{
angle[jointID] = x; ///now3!!!
display(); /// now2!!
}
void keyboard(unsigned char key,int x, int y)///now3!!!
{
if(key=='1') jointID=1; ///NOW3!!!
if(key=='2') jointID=2; ///NOW3!!!
if(key=='3') jointID=3; ///NOW3!!!
if(key=='4') jointID=4; ///NOW3!!!
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11 TRT");
glutIdleFunc(display);///Now!!!!
glutDisplayFunc(display);
glutMotionFunc(motion);///now2!!!
glutKeyboardFunc(keyboard);///NOW3!!!
glutMainLoop();
}
優化程式碼
滑鼠控制時不會有順移的情況
////
#include <GL/glut.h>
float angle[20]; ///now3
int jointID=1; ///now3!!! ///1,2,3,4
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(255,255,255);///白色
glutSolidTeapot( 0.3 );///身體
glPushMatrix();///右邊的手臂
glTranslatef(0.2, 0,0 );///(3)往右移動, 掛在身體的右肩
glRotatef(angle[1], 0,0,1);///(2)旋轉
glTranslatef(0.2, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(0,255,0);///綠色
glutSolidCube( 0.25 );///上手臂
glPushMatrix();///右下手臂
glTranslatef(0.05, 0,0 );///(3)往右移動, 掛在身體的右肘
glRotatef(angle[2], 0,0,1);///(2)旋轉
glTranslatef(0.1, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glRotatef(90,0,0.5,0);
glutSolidCone(0.1,0.5,30,30);///下手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();///左邊的手臂
glTranslatef(-0.2, 0,0 );///(3)往左移動, 掛在身體的左肩
glRotatef(-angle[3], 0,0,1);///(2)旋轉
glTranslatef(-0.2, 0,0);///(1)往左推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(0,0,255);///藍色
glutSolidCube( 0.25 );///上手臂
glPushMatrix();///左下手臂
glTranslatef(-0.05, 0,0 );///(3)往左移動, 掛在身體的左肘
glRotatef(-angle[4], 0,0,1);///(2)旋轉
glTranslatef(-0.1, 0,0);///(1)往左推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glRotatef(-90,0,0.5,0);
glutSolidCone(0.1,0.5,30,30);///下手臂
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
///angle++;
}
int oldX,oldY;
void mouse(int button, int state, int x, int y)///Now4!!!!
{///當mouse按下去時,要記下!! 要記得要讓它有作用!!
oldX = x; oldY = y;///Now5!!!!
}
void motion (int x, int y)///now2!!!
{
angle[jointID] += x -oldX;///Now4!!!!///Now5!!!!
oldX = x;///Now5!!!!
display(); /// now2!!
}
void keyboard(unsigned char key,int x, int y)///now3!!!
{
if(key=='1') jointID=1; ///NOW3!!!
if(key=='2') jointID=2; ///NOW3!!!
if(key=='3') jointID=3; ///NOW3!!!
if(key=='4') jointID=4; ///NOW3!!!
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11 TRT");
glutIdleFunc(display);///Now!!!!
glutDisplayFunc(display);
glutMotionFunc(motion);///now2!!!
glutMouseFunc(mouse);///Now5!!!!
glutKeyboardFunc(keyboard);///NOW3!!!
glutMainLoop();
}
沒有留言:
張貼留言