1.先下載freeglut壓縮檔
2.將壓縮檔解壓縮並複製檔案位置
3.將程式碼修改:
#include <GL/glut.h>
void display(){
glutSolidTeapot(0.3);
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutMainLoop();
}
4.跑程式碼
二.從glut專案中變出滑鼠並且做出mouse互動
1.將程式碼修改
增加程式碼:
#include <math.h>
#include <stdio.h>
#include <GL/glut.h>
void mouse(int button, int state, int x, int y)
{
printf("%d%d%d%d\n",button,state,x,y);
}
2.跑程式碼
三.將滑鼠點擊跟茶壺顏色變換做連結
1.將程式碼修改
增加程式碼:
glColor3f(1,x/300.0,y/300.0);///1->red,x/300.0->green,y/300.0->blue
glutPostRedisplay();
2.注意:不可使用glutdisplay();
3.跑程式碼
四.滑鼠進行拖曳時,茶壺顏色也會隨著變化
1.將程式碼修改
增加程式碼:
void motion(int x,int y)
{
printf("%d %d\n",x,y);
glColor3f(1,x/500.0,y/500.0);
glutPostRedisplay();
}
2.跑程式碼
五.做出圓形,並且滑鼠點擊時顏色會有所變化
1.將程式碼修改
void mouse(int button, int state, int x, int y)
{
if(state==GLUT_DOWN)printf("glVertex2f(%.2f, %.2f);\n",(x-250)/250.0,-(y-250)/250.0);
}
和
void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
for(float angle=0;angle<3.1415926*2;angle+=0.001)
{
glVertex2f(cos(angle),sin(angle));
}
glEnd();
glutSwapBuffers();
}
2.跑程式碼
3.對sin和cos改變可以修改圖形





沒有留言:
張貼留言