Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Newbie Polygon Texturing Question  (Read 1478 times)
0 Members and 1 Guest are viewing this topic.
Offline counterp

Senior Member


Medals: 11



« Posted 2012-12-28 04:44:16 »

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

Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #1 - Posted 2012-12-28 04:56:45 »

  • 1) Are you using OpenGL rendering or Java2D?
  • 2) Do you want the texture to be zig-zagging like the polygon, or drawn normally on the polygon like it's a cookie-cutter?
  • 3) 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

Offline counterp

Senior Member


Medals: 11



« Reply #2 - Posted 2012-12-28 05:02:26 »

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

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
            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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #3 - Posted 2012-12-28 05:16:02 »

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

(
triangles = sides-2
for future reference in case you didn't already know)

Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #4 - Posted 2012-12-28 05:18:06 »

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)

Offline counterp

Senior Member


Medals: 11



« Reply #5 - Posted 2012-12-28 05:19:35 »

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)
Offline davedes
« Reply #6 - Posted 2012-12-28 05:21:19 »

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:

Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #7 - Posted 2012-12-28 05:21:54 »

So I have to reuse vertices, it can 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)

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

Offline counterp

Senior Member


Medals: 11



« Reply #8 - Posted 2012-12-28 05:22:52 »

So I have to reuse vertices, it can 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)

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
Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #9 - Posted 2012-12-28 05:24:26 »

So I have to reuse vertices, it can 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)

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

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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline davedes
« Reply #10 - Posted 2012-12-28 05:30:10 »

@davedes that's what the code I posted does, I just wasn't sure if I was taking the long way about doing it
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

Offline counterp

Senior Member


Medals: 11



« Reply #11 - Posted 2012-12-28 05:31:45 »

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

One small step at a time!  Smiley
Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #12 - Posted 2012-12-28 05:34:36 »

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

But it's easier to learn to use VBOs than to learn to use shaders. Huh

Offline davedes
« Reply #13 - Posted 2012-12-28 05:52:41 »

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

One small step at a time!  Smiley
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. Smiley


Quote
But it's easier to learn to use VBOs than to learn to use shaders
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.

Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #14 - Posted 2012-12-28 06:01:13 »

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. Shocked Shocked Shocked Shocked Shocked Shocked Shocked

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

Offline counterp

Senior Member


Medals: 11



« Reply #15 - Posted 2012-12-28 14:08:46 »

These tutorials are great; they're explained well..  Cheesy
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (18 views)
2014-07-29 18:09:19

Riven (13 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (31 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (41 views)
2014-07-23 21:16:32

Riven (28 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (60 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!