1.先複習上禮拜的TRT程式先貼上10行程式後加入正方形的程式
#include <GL/glut.h>
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glRectf(0.5,0.5,-0.5,-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();
return 0;
}
1-2.加上TRT跟顏色並把旋轉部分註解掉
加上的程式碼為
glPushMatrix();
///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.2);
glPopMatrix();
整體程式碼
#include <GL/glut.h>
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glRectf(0.5,0.5,-0.5,-0.5);
glPushMatrix();
///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.2);
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();
return 0;
1-3把紅色手臂傾斜45度
#include <GL/glut.h>
float angle=45, oldX=0;
void mouse(int button, int stste, 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();
glTranslatef(0.5, 0.5, 0);
glRotatef(angle, 0, 0, 1);
glTranslatef( -0.5, -0.4, 0);
glColor3f(1,0,0);
glRectf(0.5,0.5,1.0,0.2);
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();
return 0;
}
2-1新增mouse函式 可用滑鼠操作
#include <GL/glut.h>
float angle=45;
float oldX=0;///舊X座標
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();
glRotatef(angle,0,0,1);
glTranslatef(-0.5,-0.4,0);
glColor3f(1,0,0);
glRectf(0.5,0.5,1.0,0.3);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{///進階的main函式
glutInit( &argc, argv);///初始化
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH);///兩倍交換避免畫面閃爍+3D深度
// glutInitWindowSize(600, 600);
glutCreateWindow("week13_rect");///建視窗
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc( display );///畫圖函式
glutMainLoop();///主要迴圈
}
3-1
#include <GL/glut.h>
float angle=45;
float oldX=0;///舊X座標
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();///右半邊
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);///(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();///左半邊
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);///(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)
{///進階的main函式
glutInit( &argc, argv);///初始化
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH);///兩倍交換避免畫面閃爍+3D深度
// glutInitWindowSize(600, 600);
glutCreateWindow("week13_rect");///建視窗
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc( display );///畫圖函式
glutMainLoop();///主要迴圈
}
3.2
#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)///mouse按下去
{
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);///四邊形Rectangle,表白色身體
glPushMatrix();
glTranslatef(0.3, 0.5, 0);///等下要掛到0.5,0.5
glRotatef(angle[0], 0, 0, 1);///Z軸,(2)旋轉45度
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();
glTranslatef(-0.3, 0.5, 0);///等下要掛到0.5,0.5
glRotatef(angle[2], 0, 0, 1);///Z軸,(2)旋轉45度
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();///2被交換的buffer
}
int main(int argc,char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(600,600);
glutCreateWindow("Week13 rect TRT");
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
沒有留言:
張貼留言