前言:我們知道在OpenGL中彤委,圖形都是通過一個(gè)或多個(gè)三角形來繪制的鞭铆,所以給圖形貼紋理,我們就需要把圖形拆分為多個(gè)三角形葫慎,通過三角形的各個(gè)頂點(diǎn)位置來算出紋理坐標(biāo)衔彻,有了紋理坐標(biāo),我們就可以給圖形貼紋理了偷办。
金字塔圖形的解析艰额,我們可以分為兩部分:
- 底部四邊形 = 三角形X + 三角形Y
- 頂點(diǎn)坐標(biāo) vApex, 如圖所示:
金字塔每個(gè)點(diǎn)的坐標(biāo)如下:
頂點(diǎn)坐標(biāo):
vApex(0.0, 1.0, 0.0)
vBackLeft(-1.0,-1.0,-1.0)
vBackRight(1.0,-1.0,-1.0)
vFrontLeft(-1.0,-1.0,1.0)
vFrontRight(1.0,-1.0,1.0)
三角形X的坐標(biāo):
vBackLeft(-1.0,-1.0,-1.0)
vBackRight(1.0,-1.0,-1.0)
vFrontRight(1.0,-1.0,1.0)
三角形Y的坐標(biāo):
vFrontLeft(-1.0,-1.0,1.0)
vBackLeft(-1.0,-1.0,-1.0)
vFrontRight(1.0,-1.0,1.0)
紋理坐標(biāo):
vApex(0.5, 1.0)
vBackLeft(0.0, 0.0)
vBackRight(1.0, 0.0)
vFrontLeft(0.0, 1.0)
vFrontRight(1.0, 1.0)
給圖形設(shè)置Mip貼圖的方法:
//設(shè)置mip貼圖基層
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_BASE_LEVEL,0);
//設(shè)置mip貼圖最?層
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_BASE_LEVEL,0);
經(jīng)過Mip貼圖的紋理過濾如下圖:
通過以上對(duì)圖形頂點(diǎn)坐標(biāo)的理解椒涯,我們用隧道案例代碼來加以解釋:
- 地板floorBatch幾何坐標(biāo)的計(jì)算:
GLfloat z;
floorBatch.Begin(GL_TRIANGLE_STRIP, 28, 1);
//Z表示深度柄沮,隧道的深度
for(z = 60.0f; z >= 0.0f; z -=10.0f)
{
floorBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
floorBatch.Vertex3f(-10.0f, -10.0f, z);
floorBatch.MultiTexCoord2f(0, 1.0f, 0.0f);
floorBatch.Vertex3f(10.0f, -10.0f, z);
floorBatch.MultiTexCoord2f(0, 0.0f, 1.0f);
floorBatch.Vertex3f(-10.0f, -10.0f, z - 10.0f);
floorBatch.MultiTexCoord2f(0, 1.0f, 1.0f);
floorBatch.Vertex3f(10.0f, -10.0f, z - 10.0f);
}
floorBatch.End();
- 天花板 ceilingBatch的幾何坐標(biāo)計(jì)算:
ceilingBatch.Begin(GL_TRIANGLE_STRIP, 28, 1);
for(z = 60.0f; z >= 0.0f; z -=10.0f)
{
ceilingBatch.MultiTexCoord2f(0, 0.0f, 1.0f);
ceilingBatch.Vertex3f(-10.0f, 10.0f, z - 10.0f);
ceilingBatch.MultiTexCoord2f(0, 1.0f, 1.0f);
ceilingBatch.Vertex3f(10.0f, 10.0f, z - 10.0f);
ceilingBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
ceilingBatch.Vertex3f(-10.0f, 10.0f, z);
ceilingBatch.MultiTexCoord2f(0, 1.0f, 0.0f);
ceilingBatch.Vertex3f(10.0f, 10.0f, z);
}
ceilingBatch.End();
- 左邊墻壁leftWallBatch的幾何坐標(biāo)計(jì)算:
leftWallBatch.Begin(GL_TRIANGLE_STRIP, 28, 1);
for(z = 60.0f; z >= 0.0f; z -=10.0f)
{
leftWallBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
leftWallBatch.Vertex3f(-10.0f, -10.0f, z);
leftWallBatch.MultiTexCoord2f(0, 0.0f, 1.0f);
leftWallBatch.Vertex3f(-10.0f, 10.0f, z);
leftWallBatch.MultiTexCoord2f(0, 1.0f, 0.0f);
leftWallBatch.Vertex3f(-10.0f, -10.0f, z - 10.0f);
leftWallBatch.MultiTexCoord2f(0, 1.0f, 1.0f);
leftWallBatch.Vertex3f(-10.0f, 10.0f, z - 10.0f);
}
leftWallBatch.End();
- 右邊rightWallBatch的幾何坐標(biāo)計(jì)算:
rightWallBatch.Begin(GL_TRIANGLE_STRIP, 28, 1);
for(z = 60.0f; z >= 0.0f; z -=10.0f)
{
rightWallBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
rightWallBatch.Vertex3f(10.0f, -10.0f, z);
rightWallBatch.MultiTexCoord2f(0, 0.0f, 1.0f);
rightWallBatch.Vertex3f(10.0f, 10.0f, z);
rightWallBatch.MultiTexCoord2f(0, 1.0f, 0.0f);
rightWallBatch.Vertex3f(10.0f, -10.0f, z - 10.0f);
rightWallBatch.MultiTexCoord2f(0, 1.0f, 1.0f);
rightWallBatch.Vertex3f(10.0f, 10.0f, z - 10.0f);
}
rightWallBatch.End();
總結(jié)类缤,通過上面對(duì)圖形拆解為多個(gè)三角形手素,計(jì)算每個(gè)頂點(diǎn)的坐標(biāo)和紋理坐標(biāo),我們就可以對(duì)圖形進(jìn)行貼紋理筋量,具體代碼實(shí)現(xiàn)請(qǐng)點(diǎn)這里隧道