2023年2月22日 星期三

sheeba - week02 - 點線面色彩

 GLUT程式 點線面色彩

Step01-1

File-New-Project,GLUT專案,選桌面,week02_GLUT_first 第一個專案的名字。在檔案總管裡,把桌面-葉正聖老師的 freeglut壓縮檔點開,裡面的freeglut資料夾複製到桌面,再把 lib的libfreeglut.a複製成libglut32.a 便可在CodeBlocks繼續,完成。

複製程式碼10行
#include <GL/glut.h>
void display()
{
glutSolidTeapot( 0.3 );
glutSwapBuffers();
}

int main(int argc, char *argv[])
  
    glutInit(&argc, argv); ///把 GLUT 開起來
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    ///上面這行,把顯示的模式設定好!
glutCreateWindow("GLUT Shapes"); ///要開視窗
glutDisplayFunc(display); ///要顯示的對應函式
glutMainLoop(); ///最後用 main迴圈,壓在最後面
}

Step01-2

有了基礎的10行程式碼,我們嘗試加上色彩 glColor3f(r,g,b) 比如說 glColor3f(1,1,0) 是黃色。再配合不同大小的茶壼,便能看到比較多變化的程式


Step02-1

今天的第二個重點是頂點Vertex, 對應的程式是 glVertex2f(x,y); 
有3個頂點,包在 glBegin(GL_POLYGON) 及 glEnd() 中間, 便能繪製出3角形。
座標要注意要分開、不能共線,才畫得出來。


Step02-2

其實每一個頂點,都可以有自己的色彩, 也就是一行裡,同時寫2行的程式 glColor3f(r,g,b); glVertex2f(x,y) 配合往右一個TAB縮排,方便理解

glBegin(GL_POLYGON);///開始畫多邊形
        glColor3f(1,0,0); glVertex2f(0,1);///頂點Vertex,紅,綠,藍
        glColor3f(0,1,0); glVertex2f(-1,-1);
        glColor3f(0,0,1); glVertex2f(1,-1);
glEnd();///結束

Step02-3

畫很多個圓

void myCircle(float r,float x,float y)
{
    glBegin(GL_POLYGON);///開始畫多邊形
        for(float a = 0;a<2*3.1415926; a+=0.01)
        {
            glVertex2f(r*cos(a)+x,r*sin(a)+y);
        }
glEnd();///結束
}
void display()
{
glColor3f(1,0,1); myCircle(0.6,0,0);///正中間

glColor3f(1,0,0); myCircle(0.3,0.5,0.5);///右上角
glColor3f(0,1,0); myCircle(0.3,-0.5,0.5);///左上角
glColor3f(1,1,0); myCircle(0.3,-0.5,-0.5);///左下角
glColor3f(0,0,1); myCircle(0.3,0.5,-0.5);///右下角

glutSwapBuffers();///請GLUT把畫面swap送到顯示的地方
}



沒有留言:

張貼留言