歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> OpenGL超級寶典學習筆記——其他圖元

OpenGL超級寶典學習筆記——其他圖元

日期:2017/3/1 9:32:28   编辑:Linux編程

四邊形

OpenGL的GL_QUADS圖元,可以指定4個點畫出四邊形。這些多邊形具有順時針環繞的方向。PS:四邊形的四個角必須位於同一個平面

GL_QUADS_STRIP圖元可以畫四邊形帶。

通用多邊形

GL_POLYGONS可以使用指定的所有點繪制一個多邊形。PS:這些點必須在一個平面上

使用點畫方式,填充多邊形

開啟點畫模式

glEnagle(GL_POLYGON_STIPPLE);

設置填充的模型

glPolygonStipple(pBitmap);

pBitmap指定了一塊數據區域。然後多邊形就使用pBitmap(GLubyte*)所指定的模式進行填充。pBitmap指向的緩沖區是32*32的位模式。首先讀入的是最高有效位(MSB)這與點畫的方式剛好相反。

一個多邊形的點畫模式,篝火的圖像。這個篝火圖像的數據用32*32的數組存儲

// Bitmap of campfire
GLubyte fire[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x01, 0xf0,
0x00, 0x00, 0x07, 0xf0, 0x0f, 0x00, 0x1f, 0xe0,
0x1f, 0x80, 0x1f, 0xc0, 0x0f, 0xc0, 0x3f, 0x80,
0x07, 0xe0, 0x7e, 0x00, 0x03, 0xf0, 0xff, 0x80,
0x03, 0xf5, 0xff, 0xe0, 0x07, 0xfd, 0xff, 0xf8,
0x1f, 0xfc, 0xff, 0xe8, 0xff, 0xe3, 0xbf, 0x70,
0xde, 0x80, 0xb7, 0x00, 0x71, 0x10, 0x4a, 0x80,
0x03, 0x10, 0x4e, 0x40, 0x02, 0x88, 0x8c, 0x20,
0x05, 0x05, 0x04, 0x40, 0x02, 0x82, 0x14, 0x40,
0x02, 0x40, 0x10, 0x80, 0x02, 0x64, 0x1a, 0x80,
0x00, 0x92, 0x29, 0x00, 0x00, 0xb0, 0x48, 0x00,
0x00, 0xc8, 0x90, 0x00, 0x00, 0x85, 0x10, 0x00,
0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00 };

數組的第一行存儲的是圖像最後一行的數據,數組的最後一行存儲的是圖像的第一行數據。點畫模式只能用於簡單的多邊形填充,因為其不會隨多邊形的變換而變換。示例如下

//篝火圖像
GLubyte fire[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x01, 0xf0,
0x00, 0x00, 0x07, 0xf0, 0x0f, 0x00, 0x1f, 0xe0,
0x1f, 0x80, 0x1f, 0xc0, 0x0f, 0xc0, 0x3f, 0x80,
0x07, 0xe0, 0x7e, 0x00, 0x03, 0xf0, 0xff, 0x80,
0x03, 0xf5, 0xff, 0xe0, 0x07, 0xfd, 0xff, 0xf8,
0x1f, 0xfc, 0xff, 0xe8, 0xff, 0xe3, 0xbf, 0x70,
0xde, 0x80, 0xb7, 0x00, 0x71, 0x10, 0x4a, 0x80,
0x03, 0x10, 0x4e, 0x40, 0x02, 0x88, 0x8c, 0x20,
0x05, 0x05, 0x04, 0x40, 0x02, 0x82, 0x14, 0x40,
0x02, 0x40, 0x10, 0x80, 0x02, 0x64, 0x1a, 0x80,
0x00, 0x92, 0x29, 0x00, 0x00, 0xb0, 0x48, 0x00,
0x00, 0xc8, 0x90, 0x00, 0x00, 0x85, 0x10, 0x00,
0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00 };

static void RenderScene()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 0.0f);
//開啟多邊形點畫填充
glEnable(GL_POLYGON_STIPPLE);
//設置填充模式
glPolygonStipple(fire);
glBegin(GL_POLYGON);
glVertex2f(0.0f, 0.0f);
glVertex2f(50.0f, 0.0f);
glVertex2f(50.0f, 50.0f);
glVertex2f(0.0f, 50.0f);
glEnd();

glFlush();
}

源碼:https://github.com/sweetdark/openglex/tree/master/polygon

多邊形的創建規則:多邊形不能被扭曲,邊也不能交叉。指定的一個多邊形的頂點必須在一個面上。多邊形必須是凸多邊形。

凹多邊形及邊界

直接畫凹多邊形在OpenGL中是非法的,但可以使用多個多邊形的拼接來實現繪制凹多邊形。當我們使用填充模式的時候,看不出這些由多個多邊形拼接起來的凹多邊形的內部線條。但是當我們使用多邊形畫線模式glPolygonMode(GL_FRONT, GL_LINE)的時候會出現這些內部的線。此時我們可以通過OpenGL提供的glEdageFlag()來處理這些邊的問題。當這個函數接受true值時,接下來的頂點都將作為多邊形邊界線的起點。例子:


//五角星的8個點
static GLfloat points[16] =
{
-50.0f, 50.0f,
0.0f, 125.0f,
50.0f, 50.0f,
125.0f, 0.0f,
50.0f, -50.0f,
0.0f, -125.0f,
-50.0f, -50.0f,
-125.0f, 0.0f
};

static GLboolean bFlag = true;

static void RenderScene()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 0.0f);
glPolygonMode(GL_FRONT, GL_LINE);
glPolygonMode(GL_BACK, GL_LINE);
glBegin(GL_TRIANGLES);
//五角星的內部兩個三角形不是邊界
glEdgeFlag(bFlag);
glVertex2f(points[0], points[1]);
glVertex2f(points[4], points[5]);
glVertex2f(points[12], points[13]);

glVertex2f(points[4], points[5]);
glVertex2f(points[8], points[9]);
glVertex2f(points[12], points[13]);
glEdgeFlag(true);

glVertex2f(points[0], points[1]);
glVertex2f(points[2], points[3]);
glEdgeFlag(bFlag);
glVertex2f(points[4], points[5]);

glVertex2f(points[4], points[5]);
glEdgeFlag(true);
glVertex2f(points[8], points[9]);
glVertex2f(points[6], points[7]);

glVertex2f(points[8], points[9]);
glVertex2f(points[10], points[11]);
glEdgeFlag(bFlag);
glVertex2f(points[12], points[13]);

glVertex2f(points[12], points[13]);
glEdgeFlag(true);
glVertex2f(points[0], points[1]);
glVertex2f(points[14], points[15]);

glEnd();
glutSwapBuffers();
}

bFlag=true和bFlag=false的區別:

OpenGL超級寶典 第4版 中文版PDF+英文版+源代碼 見 http://www.linuxidc.com/Linux/2013-10/91413.htm

OpenGL編程指南(原書第7版)中文掃描版PDF 下載 http://www.linuxidc.com/Linux/2012-08/67925.htm

OpenGL 渲染篇 http://www.linuxidc.com/Linux/2011-10/45756.htm

Ubuntu 13.04 安裝 OpenGL http://www.linuxidc.com/Linux/2013-05/84815.htm

OpenGL三維球體數據生成與繪制【附源碼】 http://www.linuxidc.com/Linux/2013-04/83235.htm

Ubuntu下OpenGL編程基礎解析 http://www.linuxidc.com/Linux/2013-03/81675.htm

如何在Ubuntu使用eclipse for c++配置OpenGL http://www.linuxidc.com/Linux/2012-11/74191.htm

更多《OpenGL超級寶典學習筆記》相關知識 見 http://www.linuxidc.com/search.aspx?where=nkey&keyword=34581

Copyright © Linux教程網 All Rights Reserved