Newbie Polygon Texturing Question

When texturing a polygon, what order do I have to follow exactly? Say I have this polygon

And I have a rectangular texture (of the same height, so the only thing changing is the x component)

How do I achieve this? I have vertices 1-10, what order do I use them to texture the polygon?

At the moment I’m breaking it up into quads and texturing those quads (using only a part of the texture) using these groups of vertices in this order:

1, 2, 4, 3

3, 4, 6, 5

5, 6, 8, 7

etc

    1. Are you using OpenGL rendering or Java2D?
    1. Do you want the texture to be zig-zagging like the polygon, or drawn normally on the polygon like it’s a cookie-cutter?
    1. Why the weird shape?

I’l assume you’re using OpenGL. If you aren’t, ignore this

Firstly, render in triangles. It’s better. Don’t ask why to avoid getting off-topic (there will be people who have long explanations for this)
Secondly, render in whatever order you want as long as the triangles cover the whole shape. Just make sure the texture coords are rendered in the same order.

Other than that, I can’t help more until you answer Question #2

It’s OpenGL and the texture should be zig zagging.

I have the effect working, it’s just not very efficient:

				float difY = tile.top() - tile.bottom();
				int offZ = off % 360;
				float o_ = 0;
				for (int z = offZ; z <= 360 + offZ; ) {
					float x_ = (float) (Math.sin(z * DEG_2_RAD) * 2 * PIXEL_X);

					gl.glTexCoord2f(tile.left(), tile.bottom() + difY * (z - offZ) / 360);
					gl.glVertex2f(l + o_, b + h_interval * (z - offZ) / 360); // bottom-left
					gl.glTexCoord2f(tile.right(), tile.bottom() + difY * (z - offZ) / 360);
					gl.glVertex2f(r + o_, b + h_interval * (z - offZ) / 360); // bottom-right

					z += 30;
					o_ = x_;

					gl.glTexCoord2f(tile.right(), tile.bottom() + difY * (z - offZ) / 360);
					gl.glVertex2f(r + x_, b + h_interval * (z - offZ) / 360); // top-right
					gl.glTexCoord2f(tile.left(), tile.bottom() + difY * (z - offZ) / 360);
					gl.glVertex2f(l + x_, b + h_interval * (z - offZ) / 360); // top-left
				}

(h_interval is the height of a tile, l is the left x coordinate of a tile, r is the right x coordinate of a tile, b is the bottom y coordinate of a tile, offZ is just a number i’m incrementing after each render, to make the effect move)

I’m trying to get a ‘mirage’ effect (tiles waving back and forth using sine. also i’m sure there’s a way better way of doing this, if you know of it point it out)

edit: how do I get the smallest number of triangles (not reuse vertices) is my question I guess, in response to your edit

In response to your edit (which was in response to my edit :P), the smallest number of triangles is 8.

([icode]triangles = sides-2[/icode] for future reference in case you didn’t already know)

You could use shaders and add a blur or more the vertices around etc.

(By the way, I’ve never used shaders, just thought it might be worth mentioning)

So I have to reuse vertices, it can’t just be one fluid motion, going from vertex to vertex. i.e. in my original drawing, I have to texture the triangles:

1, 2, 4
1, 3, 4
3, 4, 6
3, 5, 6
5, 6, 8
5, 7, 8
7, 8, 10
7, 9, 10

(I’m calling glVertex2f 24 times. When there are only 10 vertices)

Your tiles are all quads, right?

As you are rendering them, just displace the X position of every other row of vertices.

Since an image says a thousand words:

You could use GL_TRIANGLE_STRIP which lets you reuse 2 vertices each time resulting in 10 glVertex2f() calls

Interesting. Sounds like what I was looking for

@davedes that’s what the code I posted does, I just wasn’t sure if I was taking the long way about doing it

You would need to change the order you rendered your vertices though, as it uses the vertices passed through glVertex2f() calls in the order:

1,2,3
2,3,4
3,4,5
4,5,6

etc.

EDIT: You only need to swap 3&4 and 7&8 then it should be fine.

[quote=“counterp,post:9,topic:40550”]
That’s probably one of the easiest ways if your code already relies on fixed function pipeline, glVertex, and all that deprecated jazz.

If you were using the programmable pipeline, it would be achieved with shaders. Potentially much more efficient, and much more flexibility for other effects (blurs, vignettes, etc). Further reading here.

EDIT: Worth mentioning, you can use shaders alongside immediate mode, if you’re feeling ambitious. :wink:

Yes I’ll eventually learn about these things, thanks for the link.

One small step at a time! :slight_smile:

But it’s easier to learn to use VBOs than to learn to use shaders. ???

If your end goal is to learn graphics programming and OpenGL, I wouldn’t spend too long on immediate mode (glBegin/glEnd). It teaches bad practices and doesn’t really help you understand what’s going on under the hood.

Take this thread, for example. You’ve learned how to solve the problem, but only by using deprecated code. When you switch to the programmable pipeline, you wil probably want to re-learn how to solve the problem using different techniques, be it for performance or greater flexibility (i.e. FBOs and fragment shaders).

Shaders are really not hard to learn and will give you much more flexibility and power – it just takes a bit of reading and experimentation. The hardest part is the boilerplate (ShaderProgram, SpriteBatch, etc), which you can avoid with a library like LibGDX, or the minimal lwjgl-basics API.

But that’s just my two cents, as somebody who was once in the same boat that you’re in now. :slight_smile:

[quote]But it’s easier to learn to use VBOs than to learn to use shaders
[/quote]
What part of shaders is hard to learn? In my experience the actual GLSL is fairly simple, given you aren’t doing anything too crazy.

The hard part is setting them up, not writing the actual shader. As soon as I get my setup code working I’ll be fine.

Your tutorials have been a great help for me getting something ready to use shaders, and after I ignore all the debug stuff, I can actually see what it’s doing.

I’ll probably have shaders working by tomorrow if I can be bothered finding my lwjgl_util.jar

EDIT: Followed the GLSL sandbox link. :o :o :o :o :o :o :o

Lots of complex code with a not very exciting effect (I’m sure it’s a very complex effect though).

These tutorials are great; they’re explained well… :smiley: