
NO.01 如前幾周所打的程式,之後上課內容的基礎皆為下列所示:
#include<GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week03 Mouse操作");
glutDisplayFunc(display);
glutMainLoop();
}
NO.03 新增顏色程式碼
glClearColor(1,1,0,1); 指定把背景clear為黃色 ,glColor3f(0,1,0); 標示茶壺顏色
#include<GL/glut.h>
void display()
{
glClearColor(1,1,0,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0,1,0);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week03 Mouse操作");
glutDisplayFunc(display);
glutMainLoop();
}
NO.04 下圖為解釋各程式碼的差異

NO.05 加入滑鼠的功能程式碼 (取其X軸及Y軸之座標)
#include<GL/glut.h>
void display()
{
glClearColor(1,1,0,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0,1,0);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
#include<stdio.h>
void mouse(int button,int state,int x,int y)
{
if(state==GLUT_DOWN) printf("glVertex2f(%f, %f);\n",(x-150)/150.0,-(y-150)/150.0);
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week03 Mouse操作");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
}
NO.06 加入下圖程式碼茶壺便可隨滑鼠移動
沒有留言:
張貼留言