建個GLUT專案再貼上10 行程式碼外加一行新的
#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();
}
多加個小方塊
#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()
///glRotatef()
///glTranslatef()
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();
}
TRT
把手的軸心放在正中心(1)
#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()
///glRotatef()
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();
}
.png)
旋轉(2)
#include<GL/glut.h>
float angle=45;
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()
glRotatef(angle,0,0,1);///(2)
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();
}
改整體位置(3)
#include<GL/glut.h>
float angle=45;
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(angle,0,0,1);///(2)
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();
}

藉由滑鼠旋轉手
#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重畫畫面
}
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(angle,0,0,1);///(2)
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");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
再加一個手臂也可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重畫畫面
}
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(angle,0,0,1);///(2)
glTranslatef(-0.3,-0.4,0);///(1)把軸心放在正中心
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,0);///綠
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");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
兩隻手都可旋轉
#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重畫畫面
}
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(angle,0,0,1);///(2)
glTranslatef(-0.3,-0.4,0);///(1)把軸心放在正中心
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,0);///綠
glRectf(0.7,0.5,1.0,0.3);///下手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();///左邊
glTranslatef(-0.3,0.4,0);///(3)整體移位
glRotatef(angle,0,0,1);///(2)
glTranslatef(0.3,-0.4,0);///(1)把軸心放在正中心
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,0);///綠
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");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}

用鍵盤按鍵指定要旋轉哪一部分
#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;///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();///請GLUT重畫畫面
}
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(angle[0],0,0,1);///(2)
glTranslatef(-0.3,-0.4,0);///(1)把軸心放在正中心
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)
glTranslatef(-0.7,-0.4,0);///(1)把軸心放在正中心
glColor3f(0,1,0);///綠
glRectf(0.7,0.5,1.0,0.3);///下手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();///左邊
glTranslatef(-0.3,0.4,0);///(3)整體移位
glRotatef(angle[2],0,0,1);///(2)
glTranslatef(0.3,-0.4,0);///(1)把軸心放在正中心
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)
glTranslatef(0.7,-0.4,0);///(1)把軸心放在正中心
glColor3f(0,1,0);///綠
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");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}
沒有留言:
張貼留言