貼上11行程式碼
#include <GL/glut.h>
void display()
{
glClearColor(1.0,1.0,0.9,1.0);///清背景的鵝黃色
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week04 keyboard mouse motion");
glutDisplayFunc(display);
glutMainLoop();
}可以旋轉
#include <GL/glut.h>
float angle = 0;///旋轉角度
float s = 1;///縮放的比例
void display()
{
glClearColor(1.0,0.0,0.9,1.0);///清背景的鵝黃色
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
///glRotatef(angle,0,0,1);
glScalef(s,s,s);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y){
///angle = x;
s = 1 = (x-150)/150.0;
display();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week04 keyboard mouse motion");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
可以縮放大小
開新檔案 week04_keyboard_mouse_motion
貼上11行程式碼
#include <GL/glut.h>
#include <stdio.h>//加上這行
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.3);
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();
}按下按鍵會印出 再加上程式碼
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);
}
glutMouseFunc(mouse); glutMotionFunc(motion);float angle = 0,oldX = 0;void display() { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); glRotatef(angle,0,0,1); glutSolidTeapot(0.3); glPopMatrix(); glutSwapBuffers(); }可以旋轉茶壺
下載範例交換程式碼旋轉中心會不一樣
貼上前一個程式碼刪掉keyboard motion mouseglutIdleFunc(display);///加這行有空就重設畫面angle++會自動旋轉#include <GL/glut.h> #include <stdio.h> float angle = 0,oldX = 0; void display() { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); glTranslatef(0.8,0,0);///放到右邊去 glRotatef(angle++,0,0,1);///轉動中的 glScalef(0.3,0.3,0.3);///小小的 glutSolidTeapot(0.3);///茶壺 glPopMatrix(); glutSwapBuffers(); } int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH); glutCreateWindow("week04 keyboard mouse motion"); glutDisplayFunc(display); glutIdleFunc(display);///加這行有空就重設畫面 glutMainLoop(); }glClearColor(1.0,1.0,0.9,1.0);///背景 glColor3f(0,1,0);///綠色的 更改顏色更換程式碼順序
開新檔案複製剛才程式碼改變兩行交換glRotatef(angle++,0,0,1); glTranslatef(0.8,0,0);///放到右邊去
沒有留言:
張貼留言