我相信我的问题很琐碎,我正在使用OpenGL ES 2.0绘制简单的2D场景。
我具有延伸整个屏幕的背景纹理,以及在屏幕上特定位置绘制的花朵的另一种纹理(或者说我说精灵?)。
因此,为什么我能想到的琐碎事是调用glDrawArrays
两次,一次调用背景纹理的顶点,另一次调用花纹理的顶点。
那是正确的方法吗?如果是这样,那是否意味着我要打10朵花,要打glDrawArrays
10次?
那混合呢?如果我想将花朵与背景融合在一起,我既需要背景色又需要花朵像素色怎么办,而两次平局可能不成问题?
还是有可能一口气完成?如果是这样,我如何创建一个着色器,该着色器现在是否正在处理背景纹理顶点或花纹理顶点?
还是有可能一口气完成?
一幅绘制的问题在于,着色器需要知道当前顶点是背景顶点(而不是使用背景纹理颜色)还是花顶点(而不是使用花纹理颜色),并且我不知道该怎么做它。
这是我使用一次绘制调用来绘制背景图像的方法,该图像将整个屏幕拉伸,并且花朵的大小以一半为中心。
- (void)renderOnce {
//... set program, clear color..
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, backgroundTexture);
glUniform1i(backgroundTextureUniform, 2);
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, flowerTexture);
glUniform1i(flowerTextureUniform, 3);
static const GLfloat allVertices[] = {
-1.0f, -1.0f, // background texture coordinates
1.0f, -1.0f, // to draw in whole screen
-1.0f, 1.0f, //
1.0f, 1.0f,
-0.5f, -0.5f, // flower texture coordinates
0.5f, -0.5f, // to draw half screen size
-0.5f, 0.5f, // and centered
0.5f, 0.5f, //
};
// both background and flower texture coords use the whole texture
static const GLfloat backgroundTextureCoordinates[] = {
0.0f, 0.0f,
1.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
};
static const GLfloat flowerTextureCoordinates[] = {
0.0f, 0.0f,
1.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
};
glVertexAttribPointer(positionAttribute, 2, GL_FLOAT, 0, 0, allVertices);
glVertexAttribPointer(backgroundTextureCoordinateAttribute, 2, GL_FLOAT, 0, 0, backgroundTextureCoordinates);
glVertexAttribPointer(flowerTextureCoordinateAttribute, 2, GL_FLOAT, 0, 0, flowerTextureCoordinates);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
您有两种选择:
第二种方法显然是更好和正确的方法。要了解如何做,请在这里查看我的遮阳篷
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句