PDA

View Full Version : How To Triangle Strip?



nomad
05-17-2002, 02:09 AM
Hi,

I have loaded an 8bit grayscale .raw file into an array. The size of the .raw file is 1024x1024, which is the same size as my array. I am using one dimensional array.

I have already implemented my heightmapped terrain using GL_QUADS, but would like to use GL_TRIANGLE_STRIP instead, so my question is:

How do you grab the correct triangle-strip values from my array?

thanks!

Robbo
05-17-2002, 03:54 AM
1..2..3..4..5..6..7
1.*..*..*..*..*..*..*
.
2.*..*..*..*..*..*..*
.
3.*..*..*..*..*..*..*
.
4.*..*..*..*..*..*..*
.

For strips, you want the vertices as follows:

(1, 1),
(1, 2),
(2, 1),
(2, 2),
etc.

Notice a pattern here?

glBegin ( GL_TRIANGLE_STRIP)
for ( y = 0; y < ( Height - 1 ); y++ )
{
for ( x = 0; x < Width; x++ )
{
glVertex ..... (x, y)
glVertex ..... (x, y+1)
}
}
glEnd ();



[This message has been edited by Robbo (edited 05-17-2002).]

nomad
05-17-2002, 09:54 PM
thanks robbo, but i means how do you access your array...i mean, when i load it to my 1024x1024 uint array, how do i access, say, (1,2), or (2,1)...

any more help guys? please? thanks!

Penance
05-18-2002, 04:35 AM
i think the equation would be something like

(width * height) + (offset)
width is the width of your image (duh) and height is the row you want to access. Offset is going to be the x component you're searching for.

so if you had an 8x8 image (64 member array) and you wanted the member that would be at [3][2] if it were a 2d array, you would take

8 x 2 + (3) which would yield 19.
so yourArray[19] is what you're after
if you sketch out an 8x8 graph and label each box starting at 0, you can see what I mean.

Robbo
05-18-2002, 06:38 AM
Also, depending on your application, if you know your array will be fixed size, yeilds:

Value = x + ( y << 10 );

Quicker than a multiply - some compilers will optimise for this anyway.

However, I wouldn't personally advise setting things up for fixed images sizes, unless you always knows its going to be 2^n. In which case, use x + ( y << n ).

nomad
05-19-2002, 03:50 AM
ok, i did that, but now my terrain is drawing the whole world in wrong order!... http://www.opengl.org/discussion_boards/ubb/frown.gif.

it is like the code is reading the x and y values of my .raw file in backward! but when i used GL_QUADS, it draws just fine...

is the problem in .raw file? is .raw file saved in opposite direction? or is the problem in my array?

Robbo
05-20-2002, 10:15 AM
Ugh! Lots of possibiles. You can flip your heightfield, or you can generate the vertices in the opposite direction - it all depends on the values you are getting (assuming you are doing some kind of lerp between two points to get the patch vertex x\z positions).

Miguel_dup1
05-20-2002, 10:24 AM
I wrote a sequence of terrain demos. One uses QUADS, and then there is another one that uses QUAD_STRIP and TRIANGLE_STRIP.

Go to http://141.217.140.88/miguel/ and get them...

I hope this help... http://www.opengl.org/discussion_boards/ubb/smile.gif