Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: OpenGL doesn't render behind blended texture tris

  1. #1
    Newbie Newbie
    Join Date
    Sep 2017
    Posts
    2

    OpenGL doesn't render behind blended texture tris

    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:
    Click image for larger version. 

Name:	opengl_01.jpg 
Views:	19 
Size:	28.8 KB 
ID:	2478

    Here's the code (so far, so good):
    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);
     
                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!
    Click image for larger version. 

Name:	opengl_02.jpg 
Views:	16 
Size:	28.5 KB 
ID:	2479

    Code:
    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?

    Thanks so much!
    Dan Zaidan

  2. #2
    Newbie Newbie
    Join Date
    Sep 2017
    Posts
    2
    Tried calling glDisable(GL_BLEND); before the non-textured triangles.
    Now it displays it where the other texture doesn't render...

    Click image for larger version. 

Name:	opengl_03.jpg 
Views:	10 
Size:	31.6 KB 
ID:	2480

    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();
     
                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();

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •