1.利用 glRectf 畫出圖案
1.使用下列程式碼:
#include <GL/glut.h>void display(){glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glRectf(0.5,0.5,-0.5,-0.5);//四邊形RectangleglutSwapBuffers();}int main(int argc , char **argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);glutInitWindowSize(600,600);glutCreateWindow("Week13 rect TRT");glutDisplayFunc(display);glutMainLoop();}
2.畫出白色身體及紅手臂
#include <GL/glut.h>
void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(1,0,0);
glRectf(0.5,0.5,-0.5,-0.5);//白色身體
glPushMatrix();///要轉動之物體得放在push pop內
//glTranslatef(x, y, z);
//glRotatef( angle, 0,0,1 );
//glTranslatef(x2, y2, z2);
glColor3f(1,0,0);
glRectf(0.5,0.5,1.0,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();
}
3.使手臂可以隨滑鼠轉動
#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();//重畫畫面}void display(){glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glColor3f(1,1,1);glRectf(0.5,0.5,-0.5,-0.5);//白色身體glPushMatrix();///要轉動之物體得放在push pop內glTranslatef(0.5, 0.5, 0);glRotatef( angle, 0,0,1 );//2.旋轉他glTranslatef(-0.5,-0.4, 0);//1.把旋轉中心放中心點glColor3f(1,0,0);glRectf(0.5,0.5,1.0,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);glutMouseFunc(mouse);/////mouseglutMotionFunc(motion);//配合motionglutMainLoop();}
4.兩個關節
#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();//重畫畫面}void display(){glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glColor3f(1,1,1);glRectf(0.3,0.5,-0.3,-0.2);//白色身體glPushMatrix();///要轉動之物體得放在push pop內glTranslatef(0.3, 0.5, 0);glRotatef( angle, 0,0,1 );//2.旋轉他glTranslatef(-0.5,-0.4, 0);//1.把旋轉中心放中心點glColor3f(1,0,0);glRectf(0.5,0.5,0.8,0.3);//紅手臂glPushMatrix();glTranslatef(0.8,0.4,0);//3.把下手肘掛在關節上glRotatef(angle,0,0,1);//2.旋轉glTranslatef(-0.8,-0.4,0);//1.把下手肘的旋轉中心,放在正中心glColor3f(0,1,0);//綠色的glRectf(0.8,0.5,1.1,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");glutDisplayFunc(display);glutMouseFunc(mouse);/////mouseglutMotionFunc(motion);//配合motionglutMainLoop();}
5.兩個關節兩個手臂
#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();//重畫畫面
}
void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glRectf(0.3,0.5,-0.3,-0.2);//白色身體
glPushMatrix();///right
glTranslatef(0.3, 0.5, 0);
glRotatef( angle, 0,0,1 );//2.旋轉他
glTranslatef(-0.3,-0.4, 0);//1.把旋轉中心放中心點
glColor3f(1,0,0);
glRectf(0.3,0.5,0.8,0.3);//紅手臂
glPushMatrix();
glTranslatef(0.8,0.4,0);//3.把下手肘掛在關節上
glRotatef(angle,0,0,1);//2.旋轉
glTranslatef(-0.8,-0.4,0);//1.把下手肘的旋轉中心,放在正中心
glColor3f(0,1,0);//綠色的
glRectf(0.8,0.5,1.1,0.3);//下手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();///left
glTranslatef(-0.3, 0.5, 0);
glRotatef( angle, 0,0,1 );//2.旋轉他
glTranslatef(0.3,-0.4, 0);//1.把旋轉中心放中心點
glColor3f(1,0,0);
glRectf(-0.3,0.5,-0.8,0.3);//左手臂
glPushMatrix();
glTranslatef(-0.8,0.4,0);//3.把下手肘掛在關節上
glRotatef(angle,0,0,1);//2.旋轉
glTranslatef(0.8,-0.4,0);//1.把左下手肘的旋轉中心,放在正中心
glColor3f(0,1,0);//綠色的
glRectf(-0.8,0.5,-1.1,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");
glutDisplayFunc(display);
glutMouseFunc(mouse);/////mouse
glutMotionFunc(motion);//配合motion
glutMainLoop();
}
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();//重畫畫面
}
void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glRectf(0.3,0.5,-0.3,-0.2);//白色身體
glPushMatrix();///right
glTranslatef(0.3, 0.5, 0);
glRotatef( angle, 0,0,1 );//2.旋轉他
glTranslatef(-0.3,-0.4, 0);//1.把旋轉中心放中心點
glColor3f(1,0,0);
glRectf(0.3,0.5,0.8,0.3);//紅手臂
glPushMatrix();
glTranslatef(0.8,0.4,0);//3.把下手肘掛在關節上
glRotatef(angle,0,0,1);//2.旋轉
glTranslatef(-0.8,-0.4,0);//1.把下手肘的旋轉中心,放在正中心
glColor3f(0,1,0);//綠色的
glRectf(0.8,0.5,1.1,0.3);//下手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();///left
glTranslatef(-0.3, 0.5, 0);
glRotatef( angle, 0,0,1 );//2.旋轉他
glTranslatef(0.3,-0.4, 0);//1.把旋轉中心放中心點
glColor3f(1,0,0);
glRectf(-0.3,0.5,-0.8,0.3);//左手臂
glPushMatrix();
glTranslatef(-0.8,0.4,0);//3.把下手肘掛在關節上
glRotatef(angle,0,0,1);//2.旋轉
glTranslatef(0.8,-0.4,0);//1.把左下手肘的旋轉中心,放在正中心
glColor3f(0,1,0);//綠色的
glRectf(-0.8,0.5,-1.1,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");
glutDisplayFunc(display);
glutMouseFunc(mouse);/////mouse
glutMotionFunc(motion);//配合motion
glutMainLoop();
}
6.各個關節進行轉動
#include <GL/glut.h>float angle[20],oldx=0;int angleID=0;void keyboard(unsigned char key,int x,int y){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);oldx=x;glutPostRedisplay();//重畫畫面}void display(){glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glColor3f(1,1,1);glRectf(0.3,0.5,-0.3,-0.2);//白色身體glPushMatrix();///rightglTranslatef(0.3, 0.5, 0);glRotatef( angle[0], 0,0,1 );//2.旋轉他glTranslatef(-0.3,-0.4, 0);//1.把旋轉中心放中心點glColor3f(1,0,0);glRectf(0.3,0.5,0.8,0.3);//紅手臂glPushMatrix();glTranslatef(0.8,0.4,0);//3.把下手肘掛在關節上glRotatef(angle[1],0,0,1);//2.旋轉glTranslatef(-0.8,-0.4,0);//1.把下手肘的旋轉中心,放在正中心glColor3f(0,1,0);//綠色的glRectf(0.8,0.5,1.1,0.3);//下手臂glPopMatrix();glPopMatrix();glPushMatrix();///leftglTranslatef(-0.3, 0.5, 0);glRotatef( angle[2], 0,0,1 );//2.旋轉他glTranslatef(0.3,-0.4, 0);//1.把旋轉中心放中心點glColor3f(1,0,0);glRectf(-0.3,0.5,-0.8,0.3);//左手臂glPushMatrix();glTranslatef(-0.8,0.4,0);//3.把下手肘掛在關節上glRotatef(angle[3],0,0,1);//2.旋轉glTranslatef(0.8,-0.4,0);//1.把左下手肘的旋轉中心,放在正中心glColor3f(0,1,0);//綠色的glRectf(-0.8,0.5,-1.1,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");glutKeyboardFunc(keyboard);glutDisplayFunc(display);glutMouseFunc(mouse);/////mouseglutMotionFunc(motion);//配合motionglutMainLoop();}

沒有留言:
張貼留言