|Step 01|
1.照慣例下載《freeglut》並確認可執行
1.1修改《main.cpp》內的程式碼為下,右圖為執行結果
```
#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("week_13 rect TRT");
glutDisplayFunc(display);
glutMainLoop();
}
```
|Step 01-2|
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();
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("week_13 rect TRT");
glutDisplayFunc(display);
glutMainLoop();
}
```
|Step 01-3|
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(-0.3, -0.4, 0); //旋轉軸設定
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("week_13 rect TRT");
glutDisplayFunc(display);
glutMainLoop();
}
```
1.將其旋轉(以Z軸)
```
#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();
glRotatef(angle, 0, 0, 1); //設置旋轉(角度, x, y, z)
glTranslatef(-0.3, -0.4, 0);
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("week_13 rect TRT");
glutDisplayFunc(display);
glutMainLoop();
}
```
|Step 01-5|
1.將小矩形掛回右上角
```
#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); //位置設定
glRotatef(angle, 0, 0, 1);
glTranslatef(-0.3, -0.4, 0);
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("week_13 rect TRT");
glutDisplayFunc(display);
glutMainLoop();
}
```
|Step 01-6|
1.能以滑鼠操控其旋轉
```
#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);
glRotatef(angle, 0, 0, 1);
glTranslatef(-0.3, -0.4, 0);
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("week_13 rect TRT");
glutMouseFunc(mouse); //呼叫滑鼠函式
glutMotionFunc(motion); //呼叫移動函式
glutDisplayFunc(display);
glutMainLoop();
}
```
|Step 02|
1.1多新增一小矩形(圖中綠色)
```
///WEEK_13_RECT
#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();
}
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);
glRotatef(angle, 0, 0, 1);
glTranslatef(-0.3, -0.4, 0);
glColor3f(1, 0, 0);
glRectf(0.3, 0.5, 0.7, 0.3);
glPushMatrix();
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("week_13 rect TRT");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
```
```
///WEEK_13_RECT
#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();
}
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);
//glRotatef(angle, 0, 0, 1); //暫時註解
glTranslatef(-0.3, -0.4, 0);
glColor3f(1, 0, 0);
glRectf(0.3, 0.5, 0.7, 0.3);
glPushMatrix();
glTranslatef(0.7, 0.4, 0);
glRotatef(angle, 0, 0, 1);
glTranslatef(-0.7, -0.4, 0);
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("week_13 rect TRT");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
```
|Step 03|
1.1將右邊倆矩形複製到左方(改X軸)
1.修改角度參數,讓其有獨立關節,能以鍵盤切換
```
///WEEK_13_RECT_MANY
#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();
}
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);
glRotatef(angle, 0, 0, 1);
glTranslatef(-0.3, -0.4, 0);
glColor3f(1, 0, 0);
glRectf(0.3, 0.5, 0.7, 0.3);
glPushMatrix();
glTranslatef(0.7, 0.4, 0);
glRotatef(angle, 0, 0, 1);
glTranslatef(-0.7, -0.4, 0);
glColor3f(0, 1, 0);
glRectf(0.7, 0.5, 1.0, 0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.3, 0.4, 0);
glRotatef(angle, 0, 0, 1);
glTranslatef(0.3, -0.4, 0);
glColor3f(1, 0, 0);
glRectf(-0.3, 0.5, -0.7, 0.3);
glPushMatrix();
glTranslatef(-0.7, 0.4, 0);
glRotatef(angle, 0, 0, 1);
glTranslatef(0.7, -0.4, 0);
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("week_13 rect TRT");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
```
|Step 03-1|
```
///WEEK_13_RECT_MANY
#include <GL/glut.h>
float angle[20], oldX=0; //設定20種關節
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.5);
glPushMatrix();
glTranslatef(0.3, 0.4, 0);
glRotatef(angle[0], 0, 0, 1); //設定關節編號
glTranslatef(-0.3, -0.4, 0);
glColor3f(1, 0, 0);
glRectf(0.3, 0.5, 0.7, 0.3);
glPushMatrix();
glTranslatef(0.7, 0.4, 0);
glRotatef(angle[1], 0, 0, 1); //設定關節編號
glTranslatef(-0.7, -0.4, 0);
glColor3f(0, 1, 0);
glRectf(0.7, 0.5, 1.0, 0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.3, 0.4, 0);
glRotatef(angle[2], 0, 0, 1); //設定關節編號
glTranslatef(0.3, -0.4, 0);
glColor3f(1, 0, 0);
glRectf(-0.3, 0.5, -0.7, 0.3);
glPushMatrix();
glTranslatef(-0.7, 0.4, 0);
glRotatef(angle[3], 0, 0, 1); //設定關節編號
glTranslatef(0.7, -0.4, 0);
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("week_13 rect TRT");
glutKeyboardFunc(keyboard); //呼叫鍵盤函式
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
```
沒有留言:
張貼留言