update: 2012/05/01
reference:
OpenGL: A Primer, 3/e
基本幾何圖形之二: 線
A. 說明:
延續之前文章: I touchs: OpenGL: 基本幾何圖形之一 的專案.
-----------------------------------------------------------------------
B. 畫線前置處理:
1. 開啓 draw.h 檔案, 修改如下:
....
//@add: drawing
void drawPoints(); // 畫點
void drawLines(); // 畫線
2. 開啓 draw.cpp 檔案, 修改如下:
....
// 畫線
void drawLines()
{
// Clear the buffer
glClear(GL_COLOR_BUFFER_BIT);
// Let us begin drawing
// Ok we are done drawing
// Write this out to the screen
glFlush();
}
3. 開啓 main.cpp 檔案, 修改如下:
....
int main(int argc, char * argv[])
{
...
// Set the callback function, will be called as needed
//glutDisplayFunc(drawPoints); // 畫點
glutDisplayFunc(drawLines); // 畫線
....
}
....
-----------------------------------------------------------------------
C. 畫線段(Lines)
1. 開啓 draw.cpp 檔案, 修改如下:
....
// 畫線
void drawLines()
{
// Clear the buffer
glClear(GL_COLOR_BUFFER_BIT);
//@add: attributes for line
glLineWidth(5.0); // default: 1.0
// Let us begin drawing
glBegin(GL_LINES);
// Specify the points
glColor3f(1.0, 1.0, 1.0); // Set the color state: white
glVertex2f(-0.5, -0.5);
glColor3f(1.0, 0.0, 0.0); // Set the color state: red
glVertex2f(-0.5, 0.5);
glColor3f(0.0, 1.0, 0.0); // Set the color state: green
glVertex2f(0.5, 0.5);
glColor3f(0.0, 0.0, 1.0); // Set the color state: blue
glVertex2f(0.5, -0.5);
// Ok we are done drawing
glEnd();
// Write this out to the screen
glFlush();
}
2. 編譯並執行:
-----------------------------------------------------------------------
D. 畫線條(Line Strip)
1. 開啓 draw.cpp 檔案, 修改如下:
....
void drawLines()
{
// Clear the buffer
glClear(GL_COLOR_BUFFER_BIT);
//@add: attributes for line
glLineWidth(5.0); // default: 1.0
// Let us begin drawing
//glBegin(GL_LINES);
glBegin(GL_LINE_STRIP);
// Specify the points
glColor3f(1.0, 1.0, 1.0); // Set the color state: white
glVertex2f(-0.5, -0.5);
glColor3f(1.0, 0.0, 0.0); // Set the color state: red
glVertex2f(-0.5, 0.5);
glColor3f(0.0, 1.0, 0.0); // Set the color state: green
glVertex2f(0.5, 0.5);
glColor3f(0.0, 0.0, 1.0); // Set the color state: blue
glVertex2f(0.5, -0.5);
// Ok we are done drawing
glEnd();
// Write this out to the screen
glFlush();
}
2. 編譯並執行:
E. 畫線圈(Line Loop)
1. 開啓 draw.cpp 檔案, 修改如下:
....
void drawLines()
{
// Clear the buffer
glClear(GL_COLOR_BUFFER_BIT);
//@add: attributes for line
glLineWidth(5.0); // default: 1.0
// Let us begin drawing
//glBegin(GL_LINES);
//glBegin(GL_LINE_STRIP);
glBegin(GL_LINE_LOOP);
// Specify the points
glColor3f(1.0, 1.0, 1.0); // Set the color state: white
glVertex2f(-0.5, -0.5);
glColor3f(1.0, 0.0, 0.0); // Set the color state: red
glVertex2f(-0.5, 0.5);
glColor3f(0.0, 1.0, 0.0); // Set the color state: green
glVertex2f(0.5, 0.5);
glColor3f(0.0, 0.0, 1.0); // Set the color state: blue
glVertex2f(0.5, -0.5);
// Ok we are done drawing
glEnd();
// Write this out to the screen
glFlush();
}
2. 編譯並執行:
F. 點畫(Stipple)
1. 設定直線目前點畫模式:
void glLineStipple(GLint factor, GLshort pattern);
a. pattern 參數: 由 1 或 0 組成的 16-bit 值, 從低位元開始算起, 來對特定直線
的每一個像素進行點畫處理. 如果位元值為 1 就畫出此像素,
否則就不會畫.
b. factor 參數: 代表重複因子. 如果 pattern 中出現了 3 個 1, 並且 factor 為 2,
那麼就會擴展成 6 個連續的 1.
c. 例如: glLineStipple(1, Ox3F07);
pattern 為: Ox3F07, 其二進制形式為: 0011111100000111, 所以它所畫出的
直線為: (從低位元開始) 先連續畫 3 個像素, 然後連續 5 個像素留空, 再連續
畫 6 個像素, 最後 2 個像素留空. 如果 factor 是 2, 那麼這個模式將被擴展為:
先連續畫 6 個像素, 然後連續 10 個像素留空, 再連續畫 12 個像素, 最後 4 個
像素留空.
d. 如果沒有啟用點畫功能, OpenGL 會把 pattern 當作 OxFFFF, factor 當成 1.
2. 開啓 draw.cpp 檔案, 修改如下:
....
void drawLines()
{
// Clear the buffer
glClear(GL_COLOR_BUFFER_BIT);
//@add: attributes for line
glLineWidth(5.0); // default: 1.0
//@add: enable features
glEnable(GL_LINE_STIPPLE);
//glLineStipple(1, 0xffff); // default
//glLineStipple(1, 0x3F07);
glLineStipple(3, 0xcccc);
// Let us begin drawing
//glBegin(GL_LINES);
//glBegin(GL_LINE_STRIP);
glBegin(GL_LINE_LOOP);
// Specify the points
glColor3f(1.0, 1.0, 1.0); // Set the color state: white
glVertex2f(-0.5, -0.5);
glColor3f(1.0, 0.0, 0.0); // Set the color state: red
glVertex2f(-0.5, 0.5);
glColor3f(0.0, 1.0, 0.0); // Set the color state: green
glVertex2f(0.5, 0.5);
glColor3f(0.0, 0.0, 1.0); // Set the color state: blue
glVertex2f(0.5, -0.5);
// Ok we are done drawing
glEnd();
//@add: disable features
glDisable(GL_LINE_STIPPLE);
// Write this out to the screen
glFlush();
}
3. 編譯並執行:
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。