本週主題:
a. 鍵盤、滑鼠、motion
b. 移動、旋轉、縮放
c. 矩陣
1. GLUT程式1:縮放
將上週程式做調整,加入glScalef()可以做圖形的縮放操作
#include <GL/glut.h>
float s=1; ///縮放比例
void display(){
glClearColor(0.5,1.0,1.0,1.0); ///清背景的水藍色
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); ///清背景
glPushMatrix();
glScalef(s,s,s);
glutSolidTeapot(0.5);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y){
s = 1 +(x-150)/150.0;
display();
}
int main(int argc,char*argv[]){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week04 mouse glScalef");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
float s=1; ///縮放比例
void display(){
glClearColor(0.5,1.0,1.0,1.0); ///清背景的水藍色
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); ///清背景
glPushMatrix();
glScalef(s,s,s);
glutSolidTeapot(0.5);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y){
s = 1 +(x-150)/150.0;
display();
}
int main(int argc,char*argv[]){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week04 mouse glScalef");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
2. GLUT程式2:獲取鍵盤按鍵+鼠標當前位於畫面座標值
使用glutKeyboardFunc()可以獲取鍵盤按鍵與滑鼠座標...等資訊
#include <GL/glut.h>
#include <stdio.h>
void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.5);
glutSwapBuffers();
}
void keyboard(unsigned char key,int x,int y){
printf("key: %c x: %d y: %d\n" ,key,x,y);
}
int main(int argc, char *argv[]){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week04 keyboard mouse motion");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
#include <GL/glut.h>
#include <stdio.h>
float angle = 0, oldX = 0;
void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle,0,0,1);
glutSolidTeapot(0.5);
glPopMatrix();
glutSwapBuffers();
}
void mouse(int button,int state, int x,int y){
oldX = x; ///當mouse按下時,紀錄位置
}
void motion(int x,int y){
angle += (x-oldX);
oldX = x;
display();
}
void keyboard(unsigned char key,int x,int y){
printf("key: %c x: %d y: %d\n",key,x,y);
}
int main(int argc, char *argv[]){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week04 keyboard mouse motion");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
4. 範例程式碼:
5. 自轉與公轉
note:由後往前理解物件改變
a. 自轉
glTranslatef() -> glRotatef() -> glScalef():
調整拉伸->物件自轉->移動物件
調整拉伸->物件自轉->移動物件
b. 公轉
glRotatef() -> glTranslatef() -> glScalef():
調整拉伸->移動物件->物件以中心公轉
調整拉伸->移動物件->物件以中心公轉
6. GLUT程式4:將茶壺移至右方,並使其自轉
a. 加入glutIdleFunc();讓每一次的loop都可以更新畫面
b. 加入glTranslatef()、glRotatef()、glScalef()
#include <GL/glut.h>
#include <stdio.h>
float angle = 0;
void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.8,0,0);
glRotatef(angle+=0.01,0,0,1);
glScalef(0.3, 0.3, 0.3);
glutSolidTeapot(0.5);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char *argv[]){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week04 translate rotate scale");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
7. GLUT程式5:茶壺移離中心,並使其以中心公轉
對上面的程式碼調整glTranslatef()、glRotatef()、glScalef()的次序,詳細可參考5.自轉與公轉
#include <GL/glut.h>
#include <stdio.h>
float angle = 0;
void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle+=0.01,0,0,1);
glTranslatef(0.8,0,0);
glScalef(0.3, 0.3, 0.3);
glutSolidTeapot(0.5);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char *argv[]){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week04 translate rotate scale");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
Do you ever get a little bit tired of lifeLike you're not really happy but you don't wanna dieLike you're hanging by a thread but you gotta survive'Cause you gotta survive
A little bit tired of tryin' to care when I don'tA little bit tired of quick repairs to copeA little bit tired of sinkin'There's water in my boatI'm barely breathin'Tryna stay afloatSo I got these quick repairs to copeGuess I'm just broken and broke
----Numb Little Bug , Em Beihold
沒有留言:
張貼留言