Vertex Arrays and Texture Maps

I as you will guess am new to OpenGL coding.

Unlike the oter questions about Vertex arrays, don’t have a problem with displaying a cube on screen, the problem I have is placing a texture map upon the damn thing.

Although I clould do what other people seem to do, and display 6 quads, I lave always attempted to optimise my code.

I am starting to assume that I cannot place a texture map on the cube without some sort of texture array ?

If that is the case, then
1: what format does it take.

2: How do I map a different image onto each face (without resorting to 6 separate quads, each separately mapped)

I know I’m running before I can walk too well, but I come from a background of heavily optimised 68000 assembler.

Regards,

Kilby…

Hi Kilby,

If you were on ST, Falcon or Amiga, you’ll find that you have to forget almost everything you learnt about optimization…

I was coding on ST/Falcon (Demomaker / -ABSTRACT-) and am now coding on PC using VC++ (I have even stopped assembler !).

For your problem, yes you have to use a texture array (look at glTexCoordPointer).
Unfortunately, you can not change a texture while using vertex arrays. What you can do is merge all your textures in a single big one and play with the texture coordinates.

If you want more details, I am listening !

Regards.

Eric

Hi,

Well, when I sat 68000, I should also include Z80, 6800 and 6502 as well, though there was a lot of time spent on the Amiga & ST (ex Silents (UK), SAE & others)

I just can’t bare to see brute force methods being used, I like small smart code.

Thanks for the info,

I was playing around with glTexCoordPointer last night and managed to get cube texture mapped to some extent.

The new problem I have is now getting my cube mapped the with the correct rexture coords.

I assume I need 4 sets of texturecoords for each face ?

I seem to be suffering from 2 faces mapped correctly and the other 4 faces having a 1 dimentional texture.

I assume the lack of detail on this is the reason that most tutorials produce a cube with s translated quads

Regards,
Kilby…

Hust a comment to say that I seem to have found the problem,

It seems to be the way that glDrawElements works.

If I use the method in the red book (Example 2-11) the texture coords screw up

But if I just create use an array with all the vertex data in it glDrawArrays does the job properly.

Is there a simple method of getting texture mapping working with glDrawElements ?

Thanks

Kilby…

You need a vertex array and a texcoord array which both have the same number of elements.The you enable the two arrays with glVertexArray and glTexCoordArray and now if you draw with glDrawelements and say for example that you want to draw the vrtex in element 1 of your vertex array it´ll use element 1 of your texcoord array for that vertex…

P.s.:You can do everything with glDrawElements…Q3 uses ONLY glDrawElements
(at least for 98% of all primitives)

HTH,XBTC!

[This message has been edited by XBCT (edited 07-19-2000).]

Hey XBCT…

can you show how can i use a glDrawElements ??? (just a little example)
coz i can’t make it works.

thanx

see ya

[This message has been edited by Blza (edited 07-19-2000).]

Thanks for the info I have now got the cube correctly mapped.

However the problem stems from my addiction to being a memory miser.

What was happening was that I was reusing the vertex data, and the texture data was also being reused

Though this means that for large models with many many shared vertexes that you cannot reuse the vertex data via indeces ;(

Still one useful side effect is I managed to get a tolerable 3D texture effect.

Thanx

Kilby…

Sorry for my absence…
Blazka:I think there are tons of tutorials out there about Vertex arrays +glDrawElements.So do I really have to explain it here?If so just drop a note…

Kilby:Yes that´s a problem:You really can not use the same vertex with different texcoords.BUT it speeds things really up if you use glLockArraysEXT+VertexArrays ´cause if you do multiple passes on the same geometry you´ll have to transform the vertices only once.This is the main advantage of Vertex arrays in my opinion…

Greets,XBTC!

Unfortunately, you can not change a texture while using vertex arrays. What you can do is merge all your textures in a single big one and play with the texture coordinates.
You can so change textures while using vertex arrays… I have used vertex arrays which places different textures on each face of a cube…

My problem was that the top and bottom texture Coords get messed up… That was using glDrawElements…

I’ll try it using DrawArrays, i’ll se how that turns out…

by the way, i was using glInterleavedArrays which simplify things down a lot… But the texture coords are difficult to understand…

Heres the semo of the vertex arrays…
http://www.ompac.net.au/~troyfa/hello.zip

The top and bottom texture Coords are all messed up… heres the code…

#define xyz 30.0
#define uv 1.0
#define u0 0.0
//
static LPCTSTR AppName = “Hello World!”;
//
static GLfloat Cube = {
uv, uv, -xyz, xyz, -xyz,
u0, uv, xyz, xyz, -xyz,
u0, u0, xyz, -xyz, -xyz,
uv, u0, -xyz, -xyz, -xyz,
u0, uv, -xyz, xyz, xyz,
uv, uv, xyz, xyz, xyz,
uv, u0, xyz, -xyz, xyz,
u0, u0, -xyz, -xyz, xyz
};
//
static GLuint DrawCube = {
4, 5, 6, 7,
5, 1, 2, 6,
0, 1, 5, 4, //-------Messed up texture
1, 0, 3, 2,
0, 4, 7, 3,
7, 6, 2, 3 //-------Messed up texture
};
//
char* bitmaps[ 6 ] = {
“xray.bmp”,
“lightning.bmp”,
“fall.bmp”,
“coins.bmp”,
“sand.bmp”,
“eye.bmp”
};
//
void Init( void )
{
for( int t = 0; t < 6; t++ )
Textures[ t ] = glWin_LoadBMPTexture( bitmaps[ t ] );

glEnable( GL_TEXTURE_2D );
glEnable( GL_DEPTH_TEST );
glEnable( GL_CULL_FACE );
glFrontFace( GL_CW );
glClearColor( 0.2f, 0.2f, 0.7f, 0.0f );
glInterleavedArrays( GL_T2F_V3F, 0, Cube );

}
//
//
//-----Inside Draw function---------->>>
//
for( int t = 0, offset = 0; t < 6; t++, offset += 4 )
{
glBindTexture( GL_TEXTURE_2D, Textures[ t ] );
glDrawElements( GL_QUADS, 4, GL_UNSIGNED_INT, &DrawCube[ offset ] );
}
//
//--------End Draw function------->>>>>

Does anyone know how to fix the textures ??

You do see why the textures are messed up don’t you? On those sides you have multiple points using the exact same texture coordinates. And the only way to fix it (and still use glDrawElements), is by duplicating some vertices in the vertex array. Each duplicate having an alternate texture coordinate.