主要架構
#include <GL/glut.h>
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ///2種模式
glutSwapBuffers(); ///交換畫面buffer便會顯示
}
int main(int argc,char** argv){ ///進階的main()參數(個數,字串)
glutInit(&argc,argv); ///進階參數,初始GLUT
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); ///開啟兩種模式
glutCreateWindow("Week03");
glutDisplayFunc(display); ///呼叫display函式
glutMainLoop(); ///主要迴圈
}
視窗名字沒顯示:
glutInitDisplayMode(GL_DOUBLE | GLUT_DEPTH);
§GLUT_DOUBLE 少了UT
視窗名字顯示:
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
display函式內
glClearColor(1,1,0,1); ///設定背景顏色(R,G,B,A):黃(用來指定Clear的顏色)
§需在glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);之前
glColor3f(0,1,0); ///標示顏色:綠
glutSolidTeapot(0.3); ///繪製茶壺
------------------------------------------------------------------------------------------------------
學習 glutMouseFunc(Mouse); glutMotionFunc(motion);
新增標頭檔
#include <stdio.h>
新增Mouse():
void Mouse(int botton ,int state, int x, int y){ ///按鈕、狀態、x軸、y軸
if(state == GLUT_DOWN) printf("glVertex2f(%f ,%f);\n", (x - 150)/150.0, -(y - 150)/150.0);
}
main內需增加
glutMouseFunc(Mouse);
結果:
新增Motion()
整個程式外面宣告float teapotX =0, teapotY = 0;
display()內需增加:
glPushMatrix(); ///備份矩陣、glTranslatef(x,y,z); ///移動參數、glPopMatrix(); ///還原矩陣
新增void Motion():
void Motion(int x, int y){
teapotX = (x - 150) / 150.0; teapotY = - (y - 150) / 150.0;
printf("%d %d\n",x, y);
glutPostRedisplay(); ///貼上標籤,有空記得重畫畫面Redisplay
}
main()內需增加:
glutMotionFunc(Motion);
結果:
沒有留言:
張貼留言