OpenGL ES 2.0中的纹理点? [英] Textured points in OpenGL ES 2.0?
问题描述
我正在尝试在OpenGL ES 2.0中为粒子系统实现纹理化的点(例如,点精灵).我遇到的问题是所有点均渲染为实心黑色正方形,而不是正确映射纹理.
I'm trying to implement textured points (e.g. point sprites) in OpenGL ES 2.0 for a particle system. Problem I'm having is the points all render as solid black squares, rather than having the texture properly mapped.
我已验证gl_PointCoord实际上返回的x/y值从0.0到1.0,它将在整个纹理上进行映射.虽然texture2D调用似乎总是返回黑色.
I have verified that gl_PointCoord is in fact returning x/y values from 0.0 to 1.0, which would map across the entire texture. The texture2D call always seems to return black though.
我的顶点着色器:
attribute vec4 aPosition;
attribute float aAlpha;
attribute float aSize;
varying float vAlpha;
uniform mat4 uMVPMatrix;
void main() {
gl_PointSize = aSize;
vAlpha = aAlpha;
gl_Position = uMVPMatrix * aPosition;
}
还有我的片段着色器:
precision mediump float;
uniform sampler2D tex;
varying float vAlpha;
void main () {
vec4 texColor = texture2D(tex, gl_PointCoord);
gl_FragColor = vec4(texColor.rgb, texColor.a * vAlpha);
}
有问题的纹理为16x16.我能够成功地将此纹理映射到其他几何,但是由于某些原因未指向点.
The texture in question is 16x16. I am able to successfully map this texture to other geometry, but for some reason not to points.
我的平台是运行Android 2.2的Motorola Droid.
My platform is a Motorola Droid, running Android 2.2.
推荐答案
您选择纹理作为活动纹理并将其绑定-就像通常一样.但是您必须将纹理单元统一传递给着色器程序.因此,您可以将gl_PointCoord
用作片段着色器中的纹理坐标.
You select your texture as active and bind it - like normally. But you must pass your texture unit as uniform to the shader program. So you can use the gl_PointCoord
as texture coordinates in your fragment shader.
glUniform1i(MY_TEXTURE_UNIFORM_LOCATION, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, yourTextureId);
glEnable(GL_TEXTURE_2D);
glDrawArrays(GL_POINTS, 0, pointCount);
glDisable(GL_TEXTURE_2D);
您的片段着色器应如下所示:
And your fragment shader should look like:
uniform sampler2D texture;
void main ( )
{
gl_FragColor = texture2D(texture, gl_PointCoord);
}
这篇关于OpenGL ES 2.0中的纹理点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!