PDA

View Full Version : Floor, Triangle strips and text coord



juglarx
06-21-2007, 02:45 PM
Hi everyone! i need your help again, i have a simple flat floor with tiles using triangel strips, but when i texturing it, just 1 row have te correct texture, the problem is with the text coord, i need a texture for 1 tile not a big texture for all the floor, anyone can help me with the correct text coord for a flat floor with tiles (each tiles has his own texture)

thanks in advance folks!!

oh!! here is te code that i use to generate the floor and the texture i'm using VBO

for(int x = 0;x<MAP_SIZE_X;x++){
for(int z = 0;z<MAP_SIZE_Z;z++){

verticesStd.push_back(x*size);
verticesStd.push_back(size);
verticesStd.push_back(z*size);

textCordStd.push_back(1.0);
textCordStd.push_back(0.0);

verticesStd.push_back(x*size);
verticesStd.push_back(size);
verticesStd.push_back((z*size) + size);
// text coord
textCordStd.push_back(0.0);
textCordStd.push_back(0.0);

verticesStd.push_back((x*size)+size);
verticesStd.push_back(size);
verticesStd.push_back(z*size);
// text coord
textCordStd.push_back(1.0);
textCordStd.push_back(1.0);

verticesStd.push_back((x*size) + size);
verticesStd.push_back(size);
verticesStd.push_back((z*size) + size);
// text coord
textCordStd.push_back(0.0);
textCordStd.push_back(1.0);

}
}here it's where i get the data from the

std::vector to a simple C array
vertices = new float[verticesStd.size()];
verticesSize = sizeof(vertices)*verticesStd.size();
if (!verticesStd.empty()) {
vertices = &verticesStd.front();

}


textCord = new float[textCordStd.size()];
textCordSize = sizeof(textCord)*textCordStd.size();
if (!textCordStd.empty()) {
textCord = &textCordStd.front();

}

// do the VBO stuff (maybe the map coord error is here)
glGenBuffersARB(1, &vboId);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboId);


glBufferDataARB(GL_ARRAY_BUFFER_ARB, verticesSize+textCordSize, 0, GL_STATIC_DRAW_ARB);

glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, verticesSize, vertices);

glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, verticesSize, textCordSize, textCord);

glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);here is the "codelet" render code for the floor and the textures

glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboId);

glActiveTextureARB( GL_TEXTURE0_ARB );
glEnable(GL_TEXTURE_2D);
glBindTexture( GL_TEXTURE_2D,1);

glTexCoordPointer(2, GL_FLOAT, 0,(void*)(verticesSize) );
glVertexPointer(3, GL_FLOAT, 0, 0);

glLockArraysEXT(0, verticesSize);
glDrawArrays(GL_TRIANGLE_STRIP, 0, verticesSize/12);
glUnlockArraysEXT();

k_szczech
06-21-2007, 11:10 PM
Change your texture coordinates from 0.0 and 1.0 to x+0.0, x+1.0, z+0.0 and z+1.0. This should do.

mfort
06-21-2007, 11:21 PM
Just went though your code and found:

vertices = new float[verticesStd.size()];
verticesSize = sizeof(vertices)*verticesStd.size();
if (!verticesStd.empty()) {
vertices = &verticesStd.front();
}1. memory leak (the float array is never released from memory when the vector is not empty (usual case)
2. verticesSize is incorrectly computed. Should be sizeof(float) or sizeof(*vertices), now you are multiplying size of pointer
3. The same applies to texcoord


glDrawArrays(GL_TRIANGLE_STRIP, 0, verticesSize/12);This will work only for one row.
Imagine how the first triangle of next row is rendered. The triangle strip means that the vertices of new triangle are connected to the
last triangle.
So there are 2 strangle very big triangles from last tile,previous row to the next row,first tile.If this is intentional then I'd play with culling to get rid of them.
The other way is to split the rows in separate runs of triangle strips.
Using display list is probably an option too, you can compile all the strips into one call to OpenGL.

mfort
06-21-2007, 11:29 PM
or try to replace GL_TRIANGLE_STRIP to GL_QUADS.
It has the same vertex arrangement and probably better for your tiles.

Olivier B.
06-22-2007, 02:57 AM
Defines your texcoord between 0 and 1 for each tiles or use GL_REPEAT.

juglarx
06-22-2007, 08:55 PM
tks to all for the response!
@mfort:i fix the memory leak and the correct size,that's ok now... i can render all the floor using tringle strips using

glDrawArrays(GL_TRIANGLE_STRIP, 0, verticesSize/12);but the problem is with the texture coord,here i put an image of the problem,
http://imagesjack.us/picture.php?p=5e4dc4cea7.jpg
can you help me to figure out what it's wrong ?
thanks in advance!!

kon
06-23-2007, 02:15 AM
since you want to render tiles don't use a strip, use quads! (because a corner for a tile has different texture coordinates for an adjacent tile)


verticesStd.push_back(x*size);
verticesStd.push_back(size);
verticesStd.push_back(z*size);
// text coord
textCordStd.push_back(0.0);
textCordStd.push_back(0.0);

verticesStd.push_back(x*size);
verticesStd.push_back(size);
verticesStd.push_back((z*size) + size);
// text coord
textCordStd.push_back(0.0);
textCordStd.push_back(1.0);

verticesStd.push_back((x*size) + size);
verticesStd.push_back(size);
verticesStd.push_back((z*size) + size);
// text coord
textCordStd.push_back(1.0);
textCordStd.push_back(1.0);

verticesStd.push_back((x*size)+size);
verticesStd.push_back(size);
verticesStd.push_back(z*size);
// text coord
textCordStd.push_back(1.0);
textCordStd.push_back(0.0);

mfort
06-24-2007, 11:46 PM
Definitely use quads (if you need mesh for lights)
or use one big quad over whole floor and set texture repeat.

I've found that the your tri.strip is wrong. Don't forget the tri.strip vertex is connected to last two. In your case the connection takes place
between last two vertices of previous tile and first vertex of new tile.
That is because you are drawing one very large tri.strip. (Maybe wanted to draw several tri.strips with 4 vertices, but your code doesn't do that).

See the last argument to

glDrawArrays(GL_TRIANGLE_STRIP, 0, verticesSize/12);It should be number of vertices to draw (in case of triangle strip it is number of vertices of one tri.strip)
Look what you give: verticesSize/12. You are giving number of bytes of vertex array divided by 12. Thats weird isn't it? You are bit lucky because 5*5*12*4 bytes / 12 = 100, which is number of vertices. So you are drawing very long triangle strip.
You can do that (See my first post, I thought you were doing it). You can prepare very long tri.strip in very special way, you must create degerate triangles at the end of each row, which are never rendered (too small,or culled away) and take care of transition of one tile to another. But it is tricky.