按照前幾週的方式,先開啟
上網搜尋OpenGV2.1找到這個'網站
下載win32-vs2008.exe版本
安裝到C:\OpenCV2.1目錄
freeglut3.0.0 MinGW
解壓縮後將libfreeglut.a檔複製並改名為libglut32.a
到codeblock裡開啟glut 並儲存 #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);
glutDisplayFunc(display);
glutMainLoop();
}
即可印出
若要模擬人體的左右手臂,即可修改為
#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.28, 0,0);
glColor3f(255,0,0);
glutSolidTeapot(0.2);///上手臂
glPushMatrix();
glTranslatef(0.2,0,0);
glRotatef(angle,0,0,1);
glTranslatef(0.28, 0,0);
glColor3f(255,0,0);
glutSolidTeapot(0.2);///下手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();///左手臂
glTranslatef(-0.2,0,0);
glRotatef(-angle,0,0,1);
glTranslatef(-0.28, 0,0);
glColor3f(255,0,0);
glutSolidTeapot(0.2);///上手臂
glPushMatrix();
glTranslatef(-0.2,0,0);
glRotatef(-angle,0,0,1);
glTranslatef(-0.28, 0,0);
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);
glutDisplayFunc(display);
glutMainLoop();
}
若想改成滑鼠手動控制,須加上以下程式碼(紅字)
#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.28, 0,0);
glColor3f(255,0,0);
glutSolidTeapot(0.2);///上手臂
glPushMatrix();
glTranslatef(0.2,0,0);
glRotatef(angle,0,0,1);
glTranslatef(0.28, 0,0);
glColor3f(255,0,0);
glutSolidTeapot(0.2);///下手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();///左手臂
glTranslatef(-0.2,0,0);
glRotatef(-angle,0,0,1);
glTranslatef(-0.28, 0,0);
glColor3f(255,0,0);
glutSolidTeapot(0.2);///上手臂
glPushMatrix();
glTranslatef(-0.2,0,0);
glRotatef(-angle,0,0,1);
glTranslatef(-0.28, 0,0);
glColor3f(255,0,0);
glutSolidTeapot(0.2);///下手臂
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
//angle++; ///去掉自動轉動
}
void motion(int x,int y) ///NOW2
{
angle=x; ///NOW2
display(); ///NOW2
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week11 TRT");
glutIdleFunc(display);
glutDisplayFunc(display); ///NOW
glutMotionFunc(motion); ///NOW2
glutMainLoop();
}
2.鍵盤按鍵
若要改為鍵盤按鍵控制各關節 ,及修改程式碼為
#include <GL/glut.h>
float angle[20];
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(angle[1],0,0,1);
glTranslatef(0.28, 0,0);
glColor3f(255,0,0);
glutSolidTeapot(0.2);///上手臂
glPushMatrix();
glTranslatef(0.2,0,0);
glRotatef(angle[2],0,0,1);
glTranslatef(0.28, 0,0);
glColor3f(255,0,0);
glutSolidTeapot(0.2);///下手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();///左手臂
glTranslatef(-0.2,0,0);
glRotatef(-angle[3],0,0,1);
glTranslatef(-0.28, 0,0);
glColor3f(255,0,0);
glutSolidTeapot(0.2);///上手臂
glPushMatrix();
glTranslatef(-0.2,0,0);
glRotatef(-angle[4],0,0,1);
glTranslatef(-0.28, 0,0);
glColor3f(255,0,0);
glutSolidTeapot(0.2);///下手臂
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
//angle++;
}
void motion(int x,int y)///NOW2
{
angle[jointID]=x;
display();///NOW2
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='1') jointID=1;
if(key=='2') jointID=2;
if(key=='3') jointID=3;
if(key=='4') jointID=4;
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week11 TRT");
glutIdleFunc(display);
glutDisplayFunc(display);///NOW
glutMotionFunc(motion);///NOW2
glutKeyboardFunc(keyboard);
glutMainLoop();
}
即可用鍵盤控制各關節
但速度過快,需再修改程式碼
#include <GL/glut.h>
float angle[20];
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(angle[1],0,0,1);
glTranslatef(0.28, 0,0);
glColor3f(255,0,0);
glutSolidTeapot(0.2);///上手臂
glPushMatrix();
glTranslatef(0.2,0,0);
glRotatef(angle[2],0,0,1);
glTranslatef(0.28, 0,0);
glColor3f(255,0,0);
glutSolidTeapot(0.2);///下手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();///左手臂
glTranslatef(-0.2,0,0);
glRotatef(-angle[3],0,0,1);
glTranslatef(-0.28, 0,0);
glColor3f(255,0,0);
glutSolidTeapot(0.2);///上手臂
glPushMatrix();
glTranslatef(-0.2,0,0);
glRotatef(-angle[4],0,0,1);
glTranslatef(-0.28, 0,0);
glColor3f(255,0,0);
glutSolidTeapot(0.2);///下手臂
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
//angle++;
}
int oldX,oldY;
void mouse(int button,int state,int x,int y)
{
oldX=x,oldY=y;
}
void motion(int x,int y)///NOW2
{
angle[jointID]+=x-oldX;
oldX=x;
display();///NOW2
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='1') jointID=1;
if(key=='2') jointID=2;
if(key=='3') jointID=3;
if(key=='4') jointID=4;
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week11 TRT");
glutIdleFunc(display);
glutDisplayFunc(display);///NOW
glutMotionFunc(motion);///NOW2
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
把左右腿一起加上去
#include <GL/glut.h>
float angle[20];
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(angle[1],0,0,1);
glTranslatef(0.28, 0,0);
glColor3f(255,0,0);
glutSolidTeapot(0.2);///上手臂
glPushMatrix();
glTranslatef(0.2,0,0);
glRotatef(angle[2],0,0,1);
glTranslatef(0.28, 0,0);
glColor3f(255,0,0);
glutSolidTeapot(0.2);///下手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();///左手臂
glTranslatef(-0.2,0,0);
glRotatef(-angle[3],0,0,1);
glTranslatef(-0.28, 0,0);
glColor3f(255,0,0);
glutSolidTeapot(0.2);///上手臂
glPushMatrix();
glTranslatef(-0.2,0,0);
glRotatef(-angle[4],0,0,1);
glTranslatef(-0.28, 0,0);
glColor3f(255,0,0);
glutSolidTeapot(0.2);///下手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();///左邊的大腿
glTranslatef(-0.18, -0.1,0 );///(3)往左移動, 掛在身體的左肩
glRotatef(-angle[3], 0,0,1);///(2)旋轉
glTranslatef(0, -0.2,0);///(1)往左推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glutSolidTeapot( 0.2 );///上手臂
glPushMatrix();///左下小腿
glTranslatef(0, -0.1,0 );///(3)往左移動, 掛在身體的左肘
glRotatef(-angle[4], 0,0,1);///(2)旋轉
glTranslatef(0, -0.2,0);///(1)往左推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glutSolidTeapot( 0.2 );///下小腿
glPopMatrix();
glPopMatrix();
glPushMatrix();///右邊的大腿
glTranslatef(+0.18, -0.1,0 );///(3)往左移動, 掛在身體的左肩
glRotatef(-angle[3], 0,0,1);///(2)旋轉
glTranslatef(0, -0.2,0);///(1)往左推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glutSolidTeapot( 0.2 );///上手臂
glPushMatrix();///左下小腿
glTranslatef(0, -0.1,0 );///(3)往左移動, 掛在身體的左肘
glRotatef(-angle[4], 0,0,1);///(2)旋轉
glTranslatef(0, -0.2,0);///(1)往左推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glutSolidTeapot( 0.2 );///下小腿
glPopMatrix();
glPopMatrix();22
glutSwapBuffers();
//angle++;
}
int oldX,oldY;
void mouse(int button,int state,int x,int y)
{
oldX=x,oldY=y;
}
void motion(int x,int y)///NOW2
{
angle[jointID]+=x-oldX;
oldX=x;
display();///NOW2
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='1') jointID=1;
if(key=='2') jointID=2;
if(key=='3') jointID=3;
if(key=='4') jointID=4;
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week11 TRT");
glutIdleFunc(display);
glutDisplayFunc(display);///NOW
glutMotionFunc(motion);///NOW2
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
沒有留言:
張貼留言