PDA

View Full Version : OpenGL doesn't render behind blended texture tris



DanZaidan
09-12-2017, 08:19 AM
Hello everybody, new member here!
After a few years developing games using engines, I'm super happy to dive into lower level programming!

I came across an unexpected behavior and would appreciate if anyone could help me out:
I'm rendering an image and a rectangle behind it:
2478

Here's the code (so far, so good):


glClearColor(1.f, 0.f, 1.f, 0.f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);


glBindTexture(GL_TEXTURE_2D, texture_handle);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bitmap);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glEnable(GL_TEXTURE_2D);

glBegin(GL_TRIANGLES);
glColor3d(1.f, 1.f, 1.f);

glTexCoord2f(0.f, 0.f);
glVertex3f(-0.4f, -0.4f, -0.5f);
glTexCoord2f(1.f, 0.f);
glVertex3f(0.2f, -0.4f, -0.5f);
glTexCoord2f(1.f, 1.f);
glVertex3f(0.2f, 0.2f, -0.5f);

glTexCoord2f(0.f, 0.f);
glVertex3f(-0.4f, -0.4f, -0.5f);
glTexCoord2f(0.f, 1.f);
glVertex3f(-0.4f, 0.2f, -0.5f);
glTexCoord2f(1.f, 1.f);
glVertex3f(0.2f, 0.2f, -0.5f);

glEnd();


glBegin(GL_TRIANGLES);
glColor3d(1.f, 1.f, 0.f);

glVertex3f(-0.3f, -0.3f, 0.5f);
glVertex3f(0.3f, -0.3f, 0.5f);
glVertex3f(0.3f, 0.3f, 0.5f);

glVertex3f(-0.3f, -0.3f, 0.5f);
glVertex3f(-0.3f, 0.3f, 0.5f);
glVertex3f(0.3f, 0.3f, 0.5f);

glEnd();


However, when I enable blending, the image renders correctly but the rectangle disappears completely!
2479

Code:


glClearColor(1.f, 0.f, 1.f, 0.f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);



glBindTexture(GL_TEXTURE_2D, texture_handle);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bitmap);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glEnable(GL_TEXTURE_2D);

glEnable (GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);


glBegin(GL_TRIANGLES);
glColor3d(1.f, 1.f, 1.f);

glTexCoord2f(0.f, 0.f);
glVertex3f(-0.4f, -0.4f, -0.5f);
glTexCoord2f(1.f, 0.f);
glVertex3f(0.2f, -0.4f, -0.5f);
glTexCoord2f(1.f, 1.f);
glVertex3f(0.2f, 0.2f, -0.5f);

glTexCoord2f(0.f, 0.f);
glVertex3f(-0.4f, -0.4f, -0.5f);
glTexCoord2f(0.f, 1.f);
glVertex3f(-0.4f, 0.2f, -0.5f);
glTexCoord2f(1.f, 1.f);
glVertex3f(0.2f, 0.2f, -0.5f);

glEnd();


glBegin(GL_TRIANGLES);
glColor3d(1.f, 1.f, 0.f);

glVertex3f(-0.3f, -0.3f, 0.5f);
glVertex3f(0.3f, -0.3f, 0.5f);
glVertex3f(0.3f, 0.3f, 0.5f);

glVertex3f(-0.3f, -0.3f, 0.5f);
glVertex3f(-0.3f, 0.3f, 0.5f);
glVertex3f(0.3f, 0.3f, 0.5f);

glEnd();


What am I missing? :confused:

Thanks so much! :)
Dan Zaidan

DanZaidan
09-12-2017, 09:01 AM
Tried calling glDisable(GL_BLEND); before the non-textured triangles.
Now it displays it where the other texture doesn't render... :confused:

2480



glClearColor(1.f, 0.f, 1.f, 0.f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);



glBindTexture(GL_TEXTURE_2D, texture_handle);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bitmap);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glEnable(GL_TEXTURE_2D);

glEnable (GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);


glBegin(GL_TRIANGLES);
glColor3d(1.f, 1.f, 1.f);

glTexCoord2f(0.f, 0.f);
glVertex3f(-0.4f, -0.4f, -0.5f);
glTexCoord2f(1.f, 0.f);
glVertex3f(0.2f, -0.4f, -0.5f);
glTexCoord2f(1.f, 1.f);
glVertex3f(0.2f, 0.2f, -0.5f);

glTexCoord2f(0.f, 0.f);
glVertex3f(-0.4f, -0.4f, -0.5f);
glTexCoord2f(0.f, 1.f);
glVertex3f(-0.4f, 0.2f, -0.5f);
glTexCoord2f(1.f, 1.f);
glVertex3f(0.2f, 0.2f, -0.5f);

glEnd();

glDisable(GL_BLEND);

glBegin(GL_TRIANGLES);
glColor3d(1.f, 1.f, 0.f);

glVertex3f(-0.3f, -0.3f, 0.5f);
glVertex3f(0.3f, -0.3f, 0.5f);
glVertex3f(0.3f, 0.3f, 0.5f);

glVertex3f(-0.3f, -0.3f, 0.5f);
glVertex3f(-0.3f, 0.3f, 0.5f);
glVertex3f(0.3f, 0.3f, 0.5f);

glEnd();