sullysunday
sullysunday

Reputation: 61

How to render a texture on a triangle strip?

I'm creating an android side scrolling game using the libgdx library. I am using immediateModeRenderer20 in GL_TRIANGLE_STRIP mode to render 2D triangle strips that scroll infinitely. The renderering works fine, I have figured out how to use solid colors, gradients and alternating patterns on the strip.

Is there any way to render a triangle strip but overlay it with a .png or a Texture or something like that?

I have looked into the texCoord(...) method in the immediateModeRenderer20 docs but I haven't found any solid examples on how to use it.

If anyone needs any code snippets or images, let me know.

Upvotes: 2

Views: 1226

Answers (1)

kacpr
kacpr

Reputation: 392

Yes, it's possible, I've recently attempted the same.

The loop for rendering it looks simply:

texture.bind();
immediateModeRenderer20.begin(camera().combined, GL20.GL_TRIANGLE_STRIP);
immediateModeRenderer20.color(new Color(1, 1, 1, 1));
immediateModeRenderer20.texCoord(textureCoordinate.x, textureCoordinate.y);
immediateModeRenderer20.vertex(point.x, point.y, 0f);
immediateModeRenderer20.end();

But the important thing is that you build your texture coordinates to match your triangles. In my case I would draw a rope like this one:

https://i.sstatic.net/yk6x6.jpg

from a texture of a straight rope. To texture each triangle you will need texture coordinates x and y - remember that textures use different coordinates: from 0.0 to 1.0 for both x and y.

https://i.sstatic.net/9chiQ.jpg

So your triangle vertex will need textureCoord value of:

  1. x: 0.0, y: 0.0
  2. x: 0.0, y: 1.0
  3. x: triangle length, y: 0.0
  4. x: triangle length, y: 1.0

and so on.

Upvotes: 1

Related Questions