Step 1 : 四邊形
1.1開啟專案 ,命名為week13_rect_TRT
1.2 新增四邊形當身體
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glRectf( 0.3,0.5,-0.3,-0.5);///四邊形
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(600,600);///改視窗大小
glutCreateWindow("week13_rect_TRT");
glutDisplayFunc(display);
glutMainLoop();
}
1.3 增加手臂
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glRectf( 0.3,0.5, -0.3,-0.5);
glPushMatrix();
glColor3f(1,0,0);///紅色
glRectf(0.3,0.5,0.7,0.3);///右邊手臂
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(600,600);
glutCreateWindow("week13_rect_TRT");
glutDisplayFunc(display);
glutMainLoop();
}
1.4 調整手臂軸心
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glRectf( 0.3,0.5, -0.3,-0.5);
glPushMatrix();
glTranslatef(-0.3,-0.4,0);///(1)把手臂選轉中心放世界中心
///將原本軸心(0.3,0.4) 拉到(0,0) 變成負號
glColor3f(1,0,0);
glRectf(0.3,0.5,0.7,0.3);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(600,600);
glutCreateWindow("week13_rect_TRT");
glutDisplayFunc(display);
glutMainLoop();
}
1.5 手臂旋轉45度
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glRectf( 0.3,0.5, -0.3,-0.5);
glPushMatrix();
glRotatef(45,0,0,1);///(2)旋轉 對Z軸轉45度
glTranslatef(-0.3,-0.4,0);///(1)把手臂選轉中心放世界中心
glColor3f(1,0,0);
glRectf(0.3,0.5,0.7,0.3);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(600,600);
glutCreateWindow("week13_rect_TRT");
glutDisplayFunc(display);
glutMainLoop();
}
1.6 調整手臂到右上角
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glRectf( 0.3,0.5, -0.3,-0.5);
glPushMatrix();
glTranslatef(0.3,0.4,0);///(3)手臂調整到要的位置
glRotatef(45,0,0,1);///(2)旋轉 對Z軸轉
glTranslatef(-0.3,-0.4,0);///(1)把手臂選轉中心放世界中心
glColor3f(1,0,0);
glRectf(0.3,0.5,0.7,0.3);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(600,600);
glutCreateWindow("week13_rect_TRT");
glutDisplayFunc(display);
glutMainLoop();
}
step 2 : 使用mouse、motion
2.1 增加mouse指令
#include <GL/glut.h>
float angle=45 , oldx=0;///設定選轉角度變數
void mouse(int button , int state , int x ,int y) ///用滑鼠點擊
{
oldx=x;
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glRectf( 0.3,0.5, -0.3,-0.5);
glPushMatrix();
glTranslatef(0.3,0.4,0);///
glRotatef(angle,0,0,1);///旋轉 對Z軸轉 ///角度轉angle度
glTranslatef(-0.3,-0.4,0);///把手臂選轉中心放世界中心
glColor3f(1,0,0);
glRectf(0.3,0.5,0.7,0.3);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(600,600);
glutCreateWindow("week13_rect_TRT");
glutMouseFunc(mouse);///mouse用
glutDisplayFunc(display);
glutMainLoop();
}
2.2 增加motion指令
#include <GL/glut.h>
float angle=45 , oldx=0;
void mouse(int button , int state , int x ,int y)
{
oldx=x;
}
void motion (int x, int y)
{
angle+=(x-oldx); ///每次轉換要減去原本角度
oldx=x;///舊的變新的
glutPostRedisplay();///請glut重新re display
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glRectf( 0.3,0.5, -0.3,-0.5);
glPushMatrix();
glTranslatef(0.3,0.4,0);///
glRotatef(angle,0,0,1);///旋轉 對Z軸轉
glTranslatef(-0.3,-0.4,0);///把手臂選轉中心放世界中心
glColor3f(1,0,0);
glRectf(0.3,0.5,0.7,0.3);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(600,600);
glutCreateWindow("week13_rect_TRT");
glutMouseFunc(mouse);
glutMotionFunc(motion);///motion用
glutDisplayFunc(display);
glutMainLoop();
}
step 3 : 新增關節
3.1 開新專案 ,命名為week13_rect_TRT_TRT
3.2 複製week13_rect_TRT程式碼 貼上去剛剛新開的專案
3.3 貼上程式碼
///week13_rect_TRT_TRT
#include <GL/glut.h>
float angle=0 , oldx=0;
void mouse(int button , int state , int x ,int y)
{
oldx=x;
}
void motion (int x, int y)
{
angle+=(x-oldx);
oldx=x;
glutPostRedisplay();///請glut重新re display
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glRectf( 0.3,0.5, -0.3,-0.5);
glPushMatrix();
glTranslatef(0.3,0.4,0);///
glRotatef(angle,0,0,1);///旋轉 對Z軸轉
glTranslatef(-0.3,-0.4,0);///把手臂選轉中心放世界中心
glColor3f(1,0,0);
glRectf(0.3,0.5,0.7,0.3);///右手臂
glPushMatrix();
glTranslatef(0.7,0.4,0);///(3)拉回到右上角
glRotatef(angle,0,0,1);///(2)旋轉角度
glTranslatef(-0.7,-0.4,0);///(1)將軸心放到世界中心
glColor3f(0,1,10);
glRectf(0.7 , 0.5 , 1.0 , 0.3);///右上手臂
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(600,600);
glutCreateWindow("week13_rect_TRT_TRT");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
step 4 : 貼上左手
4.1 開新專案 ,命名為week13_rect_many_TRT
4.2 複製week13_rect_TRT程式碼 貼上去剛剛新開的專案
4.3貼上程式碼
///week13_rect_many_TRT
#include <GL/glut.h>
float angle=0 , oldx=0;
void mouse(int button , int state , int x ,int y)
{
oldx=x;
}
void motion (int x, int y)
{
angle+=(x-oldx);
oldx=x;
glutPostRedisplay();///請glut重新re display
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glRectf( 0.3,0.5, -0.3,-0.5);
glPushMatrix();
glTranslatef(0.3,0.4,0);///
glRotatef(angle,0,0,1);///旋轉 對Z軸轉
glTranslatef(-0.3,-0.4,0);///把手臂選轉中心放世界中心
glColor3f(1,0,0);
glRectf(0.3,0.5,0.7,0.3);///右手臂
glPushMatrix();
glTranslatef(0.7,0.4,0);;///(3)拉回到右上角
glRotatef(angle,0,0,1);///(2)旋轉角度
glTranslatef(-0.7,-0.4,0);///(1)將軸心放到世界中心
glColor3f(0,1,10);
glRectf(0.7 , 0.5 , 1.0 , 0.3);///右上手臂
glPopMatrix();
glPopMatrix();
///左手跟右手的X軸相反,所以要加負號
glPushMatrix();
glTranslatef(-0.3,0.4,0);///
glRotatef(angle,0,0,1);///旋轉 對Z軸轉
glTranslatef(0.3,-0.4,0);///把手臂選轉中心放世界中心
glColor3f(1,0,0);
glRectf(-0.3,0.5,-0.7,0.3);///左手臂
glPushMatrix();
glTranslatef(-0.7,0.4,0);///(3)拉回到左上角
glRotatef(angle,0,0,1);///(2)旋轉角度
glTranslatef(0.7,-0.4,0);///(1)將軸心放到世界中心
glColor3f(0,1,10);
glRectf(-0.7 , 0.5 , -1.0 , 0.3);///左上手臂
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(600,600);
glutCreateWindow("week13_rect_many_TRT");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
step 5 : 新增更多角度
5.1 新增keyboard函數
///week13_rect_many_TRT
#include <GL/glut.h>
float angle[4] , oldx=0;///angle變陣列以致可以放更多選轉角度
int angleID=0;///0號關節 1號關節
void keyboard(unsigned char key , int x , int y)///根據按數字鍵多少決定第幾個關節要轉動
{ ///按數字鍵決定要改變哪個angleID
if (key=='0') angleID=0;
if (key=='1') angleID=1;
if (key=='2') angleID=2;
if (key=='3') angleID=3;
}
void mouse(int button , int state , int x ,int y)
{
oldx=x;
}
void motion (int x, int y)
{
angle[angleID]+=(x-oldx);///angle變成陣列
oldx=x;
glutPostRedisplay();///請glut重新re display
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glRectf( 0.3,0.5, -0.3,-0.5);
glPushMatrix();
glTranslatef(0.3,0.4,0);///
glRotatef(angle[0],0,0,1);///旋轉 對Z軸轉 ///angle[0] 第一個值
glTranslatef(-0.3,-0.4,0);///把手臂選轉中心放世界中心
glColor3f(1,0,0);
glRectf(0.3,0.5,0.7,0.3);///右手臂
glPushMatrix();
glTranslatef(0.7,0.4,0);;///(3)拉回到右上角
glRotatef(angle[1],0,0,1);///(2)旋轉角度 ///angle[1] 第二個值
glTranslatef(-0.7,-0.4,0);///(1)將軸心放到世界中心
glColor3f(0,1,10);
glRectf(0.7 , 0.5 , 1.0 , 0.3);///右上手臂
glPopMatrix();
glPopMatrix();
///左手跟右手的X軸相反,所以要加負號
glPushMatrix();
glTranslatef(-0.3,0.4,0);///
glRotatef(angle[2],0,0,1);///旋轉 對Z軸轉 ///angle[2] 第三個值
glTranslatef(0.3,-0.4,0);///把手臂選轉中心放世界中心
glColor3f(1,0,0);
glRectf(-0.3,0.5,-0.7,0.3);///左手臂
glPushMatrix();
glTranslatef(-0.7,0.4,0);///(3)拉回到左上角
glRotatef(angle[3],0,0,1);///(2)旋轉角度 ///angle[3] 第四個值
glTranslatef(0.7,-0.4,0);///(1)將軸心放到世界中心
glColor3f(0,1,10);
glRectf(-0.7 , 0.5 , -1.0 , 0.3);///左上手臂
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(600,600);
glutCreateWindow("week13_rect_many_TRT");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
沒有留言:
張貼留言