texture coordinate array enabled but texture mapping not?

I swear this worked for me before, but now I’m crashing(read: access violation). What is the best way to enable/disable texture mapping when using glDrawElements?

What I had been doing is enabling the texture coordinate array at program initialization, then just enabling/disabling texture mapping as I needed it. This seemed to work fine. What appears to be the issue now is that glDrawElements crashes if the texture coordinate vertex pointer is not initialized with something. It seems to me that, if texture mapping is not enabled, glDrawElements should just ignore the texture coordinate array. Why does it not do this? Do I need to enable/disable the texture coordinate array each time I enable/disable texture mapping?

J

Given that you have your texture loaded and binded, you only need
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_TEXTURE_COORD_ARRAY );
glTexCoordPointer(YourTexCoordArray);

that’s it. Anyway, read the documentation to understand what’s all this for.

coco -

it’s not that I don’t understand what each piece does. What I’m asking is if(and if so, why?) the driver, every time glDrawElements is called, transfers the texture coordinate array from system memory to video memory, even if texture mapping is turned off. This appears to be how it behaves, and I don’t understand why.

I understand how to do texturemapping with glDrawElements. I am not asking how to do that. I am trying to minimize the number of state changes, which in this case means avoiding many glEnable/DisableClientState(GL_TEXTURE_COORD_ARRAY);

J

You shouldn’t NEED to glDisableClientState() if you don’t want texture mapping. glDisable(GL_TEXTURE_2D) should do it.

However, you may still have to allocate memory for that texture array. I think I remember doing this, and it seems like ogl looks at that array whether texturing is enabled or not, and if you haven’t allocated memory for it, well, the crash is obvious.

Also, don’t forget to allocate the memory BEFORE passing the pointer to glTexCoordPointer().

Zeno

Zeno -

I agree that I shouldn’t have to. However, the fact that I get an access violation makes me believe I do need to. If I enable the texture coordinate array, then make a call to glDrawElements without calling glTexCoordPointer() I get an access violation. Texture mapping is turned off at this point.

However, if texture mapping is turned off and I have previously called glTexCoordPointer(), it works fine. I wouldn’t get an access violation if it wasn’t trying to access the array, correct?

I guess what I want to know is if the driver is transferring the texture coordinates each time, even if texture mapping is turned off. If this is the case, I’m assuming it’s more efficient to disable the texture coordinate array before calling glDrawElements. Otherwise, I have no problem inserting a call to glTexCoordPointer() just to avoid the crash.

(all memory is allocated and good)

J

Sorry.
Well you do have to do both:
-glDisableClientState(GL_TEXTURE_COORD_ARRAY) // to tell gldrawelements to ignore the texcoord array
and
-glDisable(GL_TEXTURE_2D) // to tell the rasterizer that the polys dont have textures.

I think this is the only way to do it.

coco -

This is what I have resigned to. It just seems like it’d be really easy to add an “if” to the driver code to check if texture mapping is enabled. Perhaps one of the nVidia folks can shed light on this…?

J

like others have said u haveto disable any unused? client states included both the texture_coord thingees if you’re using multitexture and have had them enabled before

drumminj - the answer to your question is in the “how expensive are redundant state changes” thread

The driver cannot ignore that texture array, as that behaviour would not conform to the OpenGL spec. Specifically, OpenGL has the notion of current color, normal, texture coordinates, etc. All vertex array commands have to update these values if the array is enabled. In other words, if the color array is enabled and DrawElements is called, when DrawElements returns the current color should be set to the color specified by the last index drawn.

Disabling TEXTURE_2D only tells us to not turn on the texture. It tells us nothing about whether you have chosen to supply us with texture coordinates.

If TEXTURE_2D (and 1D, and 3D, and cubemap) are all disabled, but TEXTURE_COORD_ARRAY is enabled, we will still access your texture coordinate array and send those values to the HW.

Sure, we could detect that, but the amount of logic required would be inordinate, and the OpenGL spec doesn’t tell us we have to, so we don’t.

  • Matt