( 傳送門:https://2018graphicsa.blogspot.tw/2018/03/week-01_67.html )
𝄞第一個GLUT程式𝄞
1.開啟一個新的GLUT專案->將程式碼全部刪除 (要開始自己寫程式囉!
2.輸入以下程式碼:
#include <stdio.h>
#include <GL/glut.h>//使用較高級的GLUT
int main(int argc, char **argv)
{
for(int i=0;i<argc;i++)
{
printf("argv[%d]; %s\n", i, argv[i]);
}
}
♪主程式後的括弧,裡面空白或(int argc, char **argv)或(int argc, char *argv[])意思是一樣的,只是儲存方式不同
♪argc表示輸入參數的個數,argv儲存所有的參數
4.貼上在week02_mytrangle->bin->Debug
5.開啟命令字元,輸入cd C:\Users\user\Desktop\week02_TEA\bin\Debug按下Enter
接著輸入week02_TEA.exe 後面輸入字串,並會列出每一個字以及換行
♪cd=Change Directory:"切換目錄到"的意思
𝄞茶壺程式𝄞
5.開啟命令字元,輸入cd C:\Users\user\Desktop\week02_TEA\bin\Debug按下Enter
接著輸入week02_TEA.exe 後面輸入字串,並會列出每一個字以及換行
♪cd=Change Directory:"切換目錄到"的意思
𝄞茶壺程式𝄞
1.將程式碼全部刪除 (使用同一個檔案
2.輸入以下程式碼:
2.輸入以下程式碼:
#include <stdio.h>
#include <GL/glut.h>
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT);//清理畫面
glutSolidTeapot(0.3);//大小為0.3的茶壺
glutSwapBuffers();//交換double buffers來顯示畫出的畫面
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);//初始Initialize你的glut參數設定
glutInitDisplayMode(GLUT_DOUBLE);//顯示模式:double buffers
glutCreateWindow("Week02 Hello World Triangle");
{
glClear(GL_COLOR_BUFFER_BIT);//清理畫面
glutSolidTeapot(0.3);//大小為0.3的茶壺
glutSwapBuffers();//交換double buffers來顯示畫出的畫面
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);//初始Initialize你的glut參數設定
glutInitDisplayMode(GLUT_DOUBLE);//顯示模式:double buffers
glutCreateWindow("Week02 Hello World Triangle");
//建立視窗,視窗名可以自己更改,打中文有時候會出現亂碼要注意!
glutDisplayFunc(display);//顯示函式display()用來畫圖的
glutMainLoop();//主要的迴圈,用來控制程式
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,0,0);
//Color3f:3表示3個參數,f表示float浮點數(0.0~1.0)
//glColor3ub(248,199,20);//用RGB調整茶壺的顏色
//ub:unsigned char byte(0~255)
glutSolidTeapot(0.3);
glutSwapBuffers();
}
♪glcolor3f 和glcolor3ub同樣是填色的意思,只是儲存方式不同

4.改變背景顏色
void display()
{
glClearColor(0,1,1,1);//背景顏色
glClear(GL_COLOR_BUFFER_BIT);
glColor3ub(248,199,20);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
♪glClearColor必須寫在 glClear上面(先執行),否則一開始會顯示不出來
♪glClearColor有4個參數,前三個是RGB,第4個是α透明度
glutSolidTeapot(0.3);
glutSwapBuffers();
}
♪glClearColor必須寫在 glClear上面(先執行),否則一開始會顯示不出來
♪glClearColor有4個參數,前三個是RGB,第4個是α透明度
1.加上頂點座標和顏色
{
glClearColor(199/255.0,209/255.0,224/255.0,1);//背景顏色
glClear(GL_COLOR_BUFFER_BIT);//清畫面
glBegin(GL_POLYGON);//開始畫
glColor3ub(135,248,239);//color
glVertex2f(-1,-1);//Vertex:頂點,2f(x座標,y座標)
glColor3ub(250,253,130);
glVertex2f(+1,-1);
glColor3ub(201,130,253);
glVertex2f(0,1);
glEnd();//結束畫






沒有留言:
張貼留言