glEnable(GL_TEXTURE_2D) conflicts with primitives blending?

I have a program rendering transparent .tga files with no problem. But it fails when I try to make it render additional transparent squares. Here’s the original working code structure:

void glInit()
{
    glClearColor( 0.0f, 0.0f, 1.0f, 0.0f );
    glShadeModel(GL_FLAT);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
    glDisable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
}

void Render()
{
    glClear(GL_COLOR_BUFFER_BIT );
    RenderTGA();
}

If I add a function rendering a square in Render() behind RenderTGA(), it won’t work. The function just has glBegin(), four lines of glVertex3f(), and glEnd(). But If I wrap the function with glDisable(GL_TEXTURE_2D) and glEnable(GL_TEXTURE_2D), the square is drawn.

I don’t understand why, and the current solution might not be the best solution. Please help me! thanks.

Why do you have depth testing off? turn it on

It sounds like you need to order your primitives from front to back. (order in which you render them)

I don’t remember if it closer first farthest last or the other way around but I’m sure someone can fill me in. (or try it both ways)

I did not realy understand what square do you need to reder? is it textured square? If yes? than you should assign texture coords too, else the square will be not correct.

And here is some thing - when you don’t assign texture coords OpenGL do it. Your texture have alpha values, so if the coords are not correct the square can have the alpha 0 everywhere. That’s because it not rendering.

to des:
I just want to draw a simple red square by using four glVertex3f().

to Mars_9999:
I can’t even draw the square and the textures if I turn the depth test on.

to darkrappey:
I try to draw the square first in Render(), but it doesn’t show up.

Ok, 1st is your winding order correct? If not change it or change glFrontFace().

2nd which are you using? glOrtho or gluPerspective? Make sure your near/far planes are correct.

3rd what/how are you setting up to draw this one quad?

If all is setup correctly you shouldn’t have to turn off depth testing to see it.

you should disable texture to draw simple red quad. You sad? when you turn off texture the quad is drawing. This the right way.

des allready answered your question:

if the coords are not correct the square can have the alpha 0 everywhere. That’s because it not rendering.
And then you wrote:

I just want to draw a simple red square by using four glVertex3f().
Then why do you enable GL_TEXTURE_2D?

Draw a quad:

glDisable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
  glVertex3f(...);
  glVertex3f(...);
  glVertex3f(...);
  glVertex3f(...);
glEnd();

Draw a textured quad:

glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
  glTexCoord2f(...);
  glVertex3f(...);
  glTexCoord2f(...);
  glVertex3f(...);
  glTexCoord2f(...);
  glVertex3f(...);
  glTexCoord2f(...);
  glVertex3f(...);
glEnd();

If you don’t use glTexCoord then all your vertices will be at the same point on texture, and therefore you will render just one texel of texture. If it happen to have alpha value = 0 then you won’t see it.

thanks, guys,

I tried to draw both textures and primitive squares in the same time in my program. The textures have no problem (of course with GL_TEXTURE_2D enabled). But the square is not drawn until I disable GL_TEXTURE_2D (but the textures will have problems now). I am confused why both of them can no exist in the same time, and I believe what des said is the answer.

if the coords are not correct the square can have the alpha 0 everywhere. That’s because it not rendering.
With GL_TEXTURE_2D enabled, OpenGL expects coords for each vertex, but there is none for the square.

So I have to disable GL_TEXTURE_2D before drawing the squares, and enable it back before drawing textures.

I’m not sure, but perhaps you misunderstood the texture management.

When you draw your quad, OpenGL need to know if it must use the texture to fill your quad, or not. There is no conflict : it is yes or it is no. But it changes from a quad to another one, inside the draw pass.

glDisable/glEnable GL_TEXTURE_2D is used for this : just to say to OpenGL “You will need the texture for this quad”.

This is not only for initialisation: you can use it in live. To be true, you must use it in live in fact, like this:

// For my textured quad :
glEnable (GL_TEXTURE_2D);
renderTexturedQuad ();

// And now, I do not use textures anymore, then
glDisable (GL_TEXTURE_2D);
renderColoredQuad ();