第一個程式碼( 使用codeblocks寫出一個矩形 )
#include<gl/glut.h>
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///具有3D深度
glRectf(0.5, 0.5, -0.5, -0.5); ///繪制一個矩形
glutSwapBuffers();///
}
int main(int argc, char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);///有3D功能
glutInitWindowSize(600,600); ///視窗大小
glutCreateWindow("week13 rect TRT"); ///專案
glutDisplayFunc(Display); ///設置當前窗口的顯示回調函數
glutMainLoop(); ///結束
return 0;
}
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///具有3D深度
glColor3f(1,1,1);///白色
glRectf(0.3, 0.5, -0.3, -0.2); ///繪制一個矩形(白色)
glPushMatrix();///備份矩陣
glTranslatef(0.3,0.5,0);///掛在0.5的地方 3.
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();
///
///
glColor3f(0,1,0);///綠色
glRectf(0.8,0.5,1.1,0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();///有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);///身體瘦身(對比1)
glPushMatrix();
glTranslatef(0.3, 0.5, 0);
///glRotatef(angle, 0, 0, 1);///旋轉
glTranslatef(-0.3, -0.4, 0);///旋轉中心放正中心
glColor3f(1, 0, 0);
glRectf(0.3, 0.5, 0.8, 0.3);///上手臂
glPushMatrix();
glTranslatef(0.8, 0.4, 0);///把下手肘掛在關節上
glRotatef(angle, 0, 0, 1);///旋轉
glTranslatef(-0.8, -0.4, 0);///把下手肘的旋轉中心放正中心
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");
glutMouseFunc(mouse);
glutMotionFunc(motion);
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();}void display(){glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );glColor3f(1, 1, 1);glRectf(0.3, 0.5, -0.3, -0.2);///身體瘦身(對比1)glPushMatrix();///右半邊glTranslatef(0.3, 0.5, 0);glRotatef(angle, 0, 0, 1);///旋轉glTranslatef(-0.3, -0.4, 0);///旋轉中心放正中心glColor3f(1, 0, 0);glRectf(0.3, 0.5, 0.8, 0.3);///右上手臂glPushMatrix();glTranslatef(0.8, 0.4, 0);///把下手肘掛在關節上glRotatef(angle, 0, 0, 1);///旋轉glTranslatef(-0.8, -0.4, 0);///把下手肘的旋轉中心放正中心glColor3f(0, 1, 0);glRectf(0.8, 0.5, 1.1, 0.3);///右下手肘glPopMatrix();glPopMatrix();glPushMatrix();///左半邊glTranslatef(-0.3, 0.5, 0);glRotatef(angle, 0, 0, 1);///旋轉glTranslatef(+0.3, -0.4, 0);///旋轉中心放正中心glColor3f(1, 0, 0);glRectf(-0.3, 0.5, -0.8, 0.3);///左上手臂glPushMatrix();glTranslatef(-0.8, 0.4, 0);///把下手肘掛在關節上glRotatef(angle, 0, 0, 1);///旋轉glTranslatef(+0.8, -0.4, 0);///把下手肘的旋轉中心放正中心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");glutMouseFunc(mouse);glutMotionFunc(motion);glutDisplayFunc(display);glutMainLoop();}
放上兩隻手後也能一起旋轉




沒有留言:
張貼留言