Textured points in OpenGL ES 2.0?

為{幸葍}努か 提交于 2019-12-09 15:46:21

问题


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.

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.

My vertex shader :

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;
}

And my fragment shader :

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);
}

The texture in question is 16x16. I am able to successfully map this texture to other geometry, but for some reason not to points.

My platform is a Motorola Droid, running Android 2.2.


回答1:


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);
}



回答2:


I also had a similar problem - my sprites were just rendering as coloured squares but no texture was appearing.

I needed to add this:

glEnable( GL_POINT_SPRITE );

Texturing must not be enabled by default on all GPUs.



来源:https://stackoverflow.com/questions/3497068/textured-points-in-opengl-es-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!