4
\$\begingroup\$

Using the libgdx framework, I've figured out how to render a texture to a point sprite. The problem is, it renders the entire texture to the point sprite, where I only want a small part of it (since it's an isometric tile image).

Here's a snippet from some demo code I wrote...

create()
{
    renderer = new ImmediateModeRenderer();

    tiles = Gdx.graphics.newTexture(
        Gdx.files.internal("data/tiles2.png"),
        TextureFilter.MipMap,
        TextureFilter.Linear,
        TextureWrap.ClampToEdge,
        TextureWrap.ClampToEdge);

    Gdx.gl.glClearColor(0.6f, 0.7f, 0.9f, 1);

    Gdx.gl.glEnable(GL10.GL_TEXTURE_2D);
    Gdx.gl.glEnable(GL11.GL_POINT_SPRITE_OES);

    Gdx.gl11.glTexEnvi(
        GL11.GL_POINT_SPRITE_OES, 
        GL11.GL_COORD_REPLACE_OES,
        GL11.GL_TRUE);

    Gdx.gl10.glPointSize(s);

    tiles.bind();
}

render()
{
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    renderer.begin(GL10.GL_POINTS);

    // render 3 point sprites at various 3d points
    renderer.vertex(-.1f, 0, -.1f);
    renderer.vertex(0, 0, 0);
    renderer.vertex(.1f, 0, .1f);

    // ... more vertices here if needed (one for each sprite) ...

    renderer.end();
}
\$\endgroup\$
  • \$\begingroup\$ Update: As a work around, I had to split my texture images into separate files (loaded individually). This adds a bit of loading time, but it's ok with a few images (worried about when I have 100 textures though...). A caveat I didn't foresee is that OpenGL ES (<2.0) requires that images be power of 2, and point sprites must be square! \$\endgroup\$ – Nick Bolton Mar 12 '11 at 1:48
4
\$\begingroup\$

With OpenGL ES 1.x, i gess there is no solution. With OpenGL ES 2.x, you can use gl_PointCoord in fragment shader to select a part of your texture.

A typical PointSprite Fragment shader:

#ifdef GL_ES
precision highp float;
#endif 
uniform sampler2D tex0;

void main() 
{
    gl_FragColor = texture2D(tex0, gl_PointCoord);
}

So, you can modify it for 16 square textures in one texture:

    gl_FragColor = texture2D(tex0, gl_PointCoord*0.25+displacement);

One last word, if you are developing for mobile devices, keep you fragment very short and don't use branch.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.