Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (538) Games in Android Showcase (132) games submitted by our members Games in WIP (600) 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
 Calculate Texture coordinate in a 3d terrain grid done with GL_TRIANGLE_STRIP  (Read 1006 times) 0 Members and 1 Guest are viewing this topic.
alesky

Junior Devvie

Medals: 3
Exp: 15 years

mmm....

 « Posted 2013-07-29 17:47:06 »

Hi guys
i have create my simple terrain generator that calculate vertex and normal

vertex are all at same distance the only difference consist in the height that is generated random

this is my algorithm

 1  2  3  4  5  6  7  8  9  10  11  12  13 `   private void drawPlan(Vertex[][] vertex, Vector[][] normals, GL2 gl) {      for(int z = 0; z < zSubdivision-1; z++) {         gl.glBegin(GL2.GL_TRIANGLE_STRIP);         for(int x = 0; x < xSubdivision ; x++) {            gl.glNormal3f(normals[x][z].x, normals[x][z].y, normals[x][z].z);            gl.glVertex3f(vertex[x][z].x, vertex[x][z].y, vertex[x][z].z);                             gl.glNormal3f(normals[x][z+1].x, normals[x][z+1].y, normals[x][z+1].z);            gl.glVertex3f(vertex[x][z+1].x, vertex[x][z+1].y, vertex[x][z+1].z);                                           }         gl.glEnd();      }   }`

now i would like to add texture and calculate correctly the coordinate of my texture
is there already a know algorithm that calculate for plan draw with triangles the texture coordinate?

i suppose that the a texture must enter correctly in 8 triangles draw as hire bottom and so repeated for all the group of 8 trinagles
 1  2  3  4  5 `x--x--x| / | / |x--x--x| / | / |x--x--x   `

but because the triangle strip is calculated by a double cycle that consider only z and z+1 for each strip
i have to calculate texture coordinate for first half of image in the first cycle and after second half in the second cycle and repeat

is this right any advice ?
there is a better way to do it?
thanks

Redocdam

Senior Devvie

Medals: 17

 « Reply #1 - Posted 2013-07-30 16:40:47 »

Edit: I really hope my beautiful ASCII art displays properly. If not, then pretend those are quads.

I'm probably missing your problem in its entirety, so you may need to provide a little more of an explanation if the issue is more complicated.

With that, I'll provide what information I can:
Keep in mind texture coordinates are subjective to the texture being used. So you'll want to keep them consistent with the orientation of the texture.
Since you're doing terrain tiling, I'll assume you're tiling quads and distorting the height

Texture coordinates range from 0-1, regardless of the model size. Also, keep in mind the OpenGL texture coordinates start at the lower left and increase to the upper right. So (0,0,) represents the lower left corner of the image and (1,1) is the upper right coordinate.

So really all you have to do is start apply that to your model. If you think about your tiles as a quad (instead of two triangles), then you know the coordinates would be:
 1  2  3  4  5  6  7 `(0,1)   (1,1) _______ |       ||       ||       ||_______|(0,0)   (1,0) `

So if we think about how to apply this to a quad made of multiple quads (I believe 8 in your case), then we just need to divide 1 by the number of quads in a certain direction. That would tell you the increment for a given UV coordinate in a particular direction.

For Example:
If you're putting 8 quads together to form your tile, then you can calculate the UV increment with 1/8 (=0.125). Then the current vertex texture coordinate would just be a matter of multiplying 0.125 by the vertex position in the quad (not the actual OpenGL position).

So let's say we want to figure out the U value for one of the 4th vertices from the left.
 1  2  3  4  5  6  7 ` _______ _______ _______ _______ _______ _______ _______ _______|       |       |       |       |       |       |       |       ||       |       |       |       |       |       |       |       ||       |       |       |       |       |       |       |       ||_______|_______|_______|_______|_______|_______|_______|_______|                        ^                We want this one`

Then U = 0.125 * 3.
Of course you would do the same thing with the V coordinate, but you need to make sure the the lowest vertices on the tiles are the 0 value.

Unwrapping
The above method will indeed work, but it will cause some stretching of the image. However, I still suggest you to try the above method first, as you may not even notice any distortion. The stretching will be a factor of the difference in vertex height.

If you want an accurate UV mapping, then you're going to need to apply some unwrap methods. Now, I've never actually written model unwrapping, but for this case it should be a pretty strait forward method to calculate the length of a vertex in an X or Y direction and then weight it accordingly on a 0-1 scale.
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.
 rwatson462 (29 views) 2014-12-15 09:26:44 Mr.CodeIt (20 views) 2014-12-14 19:50:38 BurntPizza (40 views) 2014-12-09 22:41:13 BurntPizza (76 views) 2014-12-08 04:46:31 JscottyBieshaar (37 views) 2014-12-05 12:39:02 SHC (50 views) 2014-12-03 16:27:13 CopyableCougar4 (47 views) 2014-11-29 21:32:03 toopeicgaming1999 (114 views) 2014-11-26 15:22:04 toopeicgaming1999 (102 views) 2014-11-26 15:20:36 toopeicgaming1999 (30 views) 2014-11-26 15:20:08
 LiquidNitrogen 33x basil_ 31x HeroesGraveDev 27x Rayvolution 27x BurntPizza 19x kevglass 17x appel 17x kpars 16x gouessej 14x Riven 13x Gibbo3771 13x Spasi 11x Ecumene 11x SHC 10x NegativeZero 9x pitbuller 9x
 Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27Resources for WIP games2014-08-01 16:20:17Resources for WIP games2014-08-01 16:19:50List of Learning Resources2014-07-31 16:29:50
 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