I'm trying to make a program showing a red rotating cube in the background, overlayed with a textured quad.
The texture is a simple 24-bit bitmap of the words "Hello World" in black over a white background. I want the white background to be transparent so that the cube can be seen behind the overlay. The image loader checks the value of each pixel and adds the relevant alpha value to convert the image into a 32-bit bitmap.
At the moment, my program displays the overlay with black text but a red background, same colour as the cube. Below is the code used for the initial texture set up:
if (bitmap->Load("test.bmp")) {
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, 3, bitmap->GetWidth(), bitmap->GetHeight(),
0, GL_RGBA, GL_UNSIGNED_BYTE, bitmap->GetPixelData());
}
And this is the whole of my display function, in case anything is interfering with anything else.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40, 1, 0.1, 27.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glTranslatef(0.0, 0.0, -1.1);
glRotatef(angle, 1.0, 1.0, 0.0);
glutSolidCube(0.1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 640, 480, 0.0, -1.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDisable(GL_CULL_FACE);
glClear(GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_QUADS);
glTexCoord2d(0.0, 0.0); glVertex2f(0.0, 0.0);
glTexCoord2d(1.0, 0.0); glVertex2f(320.0, 0.0);
glTexCoord2d(1.0, 1.0); glVertex2f(320.0, 240.0);
glTexCoord2d(0.0, 1.0); glVertex2f(0.0, 240.0);
glEnd();
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
glFlush();
glutSwapBuffers();
The default texture environment is GL_MODULATE which mixes in the current color (red from your cube) with the incoming texel value.
Switch to GL_DECAL or do a glColor3ub(255,255,255) before you render your text.
Related
Trying to complete a basic texture map to surface using OpenGL and SOIL but I am not generating anything.
GLuint textureID[5];
glutInitWindowPosition(0, 50);
windowID[0] = glutCreateWindow("orthogonal projection, cubes");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-400, 400, -400, 400, -500, 500);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glutKeyboardFunc(Keyboard);
glutDisplayFunc(DrawWindowOne);
textureID[0] = SOIL_load_OGL_texture("assets/faceA.png",
SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_INVERT_Y);
void DrawWindowOne()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, 250, 250);
glMatrixMode(GL_MODELVIEW);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureID[0]);
glBegin(GL_QUADS);
glNormal3f(0.0, 0.0, 1.0); // front face
glTexCoord2f(0.0, 0.0); glVertex3f(-a,-a, a);
glTexCoord2f(0.0, 1.0); glVertex3f(-a, a, a);
glTexCoord2f(1.0, 1.0); glVertex3f( a, a, a);
glTexCoord2f(1.0, 0.0); glVertex3f( a,-a, a);
glEnd();
glDisable(GL_TEXTURE_2D);
}
The face draws in blue, however, and I get no texture. I have a second window, where apart from position the only differance is that I am using Frustrum as opposed to Orthogonal
windowID[1] = glutCreateWindow("Perspective projection using glFrustum");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-60, 60, -60, 60, 60, 200);
gluLookAt(0, 0, 120, 0, 0, 0, 0, 1, 0);
and the texture draws fine.
The problem was that I was loading the textures all at once, where it appears they need to be loaded after each window is initialized to be available for that windows draw code.
#pragma region Initialise Window One
glutInitWindowPosition(0, 50);
windowID[1] = glutCreateWindow("orthogonal projection, cubes");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-400, 400, -400, 400, -500, 500);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glutKeyboardFunc(Keyboard);
glutDisplayFunc(DrawWindowOne);
LoadTextures();
#pragma endregion
#pragma region Initialise Window Two
glutInitWindowPosition(0, 450);
windowID[1] = glutCreateWindow("Perspective projection using glFrustum, ellipsoids");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-60, 60, -60, 60, 60, 200);
gluLookAt(0, 0, 120, 0, 0, 0, 0, 1, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glutKeyboardFunc(Keyboard);
glutDisplayFunc(DrawWindowTwo);
LoadTextures();
#pragma endregion
I'm trying to render a texture with part opaque color and other part with transparency.
This is my draw function for the object:
void drawHighGrass(){
glDisable(GL_LIGHTING);
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor4f(1.0, 1.0, 1.0, 1.0);
glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texturas[HIGH_GRASS]);
glPushMatrix();
//glTranslatef(1000, 0, 1000);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(0, 0, 0);
glTexCoord2f(1.0f, 0.0f); glVertex3f(100, 0, 0);
glTexCoord2f(1.0f, 1.0f); glVertex3f(100, 40, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f(0, 40, 0);
glEnd();
glPopMatrix();
glDisable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
glDepthMask(GL_TRUE);
glEnable(GL_LIGHTING);
}
The problem is that in the transparent part it's showing solid white. I can make the texture transparent by using glColor4f(1.0, 1.0, 1.0, 0.5) but that's not what I want because it makes the entire texture transparent and not only the transparent part.
I've checked, my texture files is a PNG with transparency.
Restating the solution here so others can find it easily.
Your rendering code seems to be correct, so what seems to have been the problem was the texture loading code. When loading a texture, you must be sure that you are passing in the correct flags for the internal texture pixel format (GL_RGBA8, GL_RGBA16, etc.) as well as the source image pixel format (GL_RGBA or GL_BGRA, etc.).
I'm trying to understand how to load a texture in OpenGL and I wrote this very simple code:
GLuint texture;
void loadTexture() {
GLubyte data[] = { 255,0,0,
0,255,0,
0,255,0,
255,0,0 };
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
int chk = gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGB, 2, 2, GL_RGB, GL_UNSIGNED_BYTE, data );
if (chk!=0)
printf("error code = %d\n",chk);
else
printf("success\n");
}
I use loadTexture() to load the texture in memory. The texture, in this sample, is extremely simple, but at the moment it doesn't matter.
void drawTexturedSquare() {
glEnable( GL_TEXTURE_2D );
glBegin (GL_QUADS);
glTexCoord2f (0.0, 0.0);
glNormal3f(0, 0, 1);
glVertex3f (0.0, 0.0, 0.0);
glTexCoord2f (1.0, 0.0);
glNormal3f(0, 0, 1);
glVertex3f (10.0, 0.0, 0.0);
glTexCoord2f (1.0, 1.0);
glNormal3f(0, 0, 1);
glVertex3f (10, 10, 0.0);
glTexCoord2f (0.0, 1.0);
glNormal3f(0, 0, 1);
glVertex3f (0.0, 10, 0.0);
glEnd ();
glDisable( GL_TEXTURE_2D);
}
I would like to apply this simple texture to a square. I call function drawTexturedSquare() from inside a draw() function where I already called
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
The problem is that this is the result I get
while I expected the square to be green in main diagonal (upper-left to lower-right) and red is secondary diagonal (upper-right to lower-left). May someone explain me why?
Besides, every time I run the program I get a different result:
I do not understand where this blue comes out...May someone help me?
Each row of your data needs to be 4-byte aligned. Either pad each row with 0 0, or use a RGBA texture.
If you don't want to do the aforementioned, you can use:
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
The reason your color is changing is that since you don't pad your rows correctly, OpenGL reads past the end of the array.
You may want to try GL_NEAREST for GL_TEXTURE_MIN_FILTER and GL_TEXTURE_MAG_FILTER
As it is right now, OpenGL is interpolating between the colors, creating a gradient (since your texture is being stretched from 2x2 to however big your screen is)
This doesn't explain why you're getting different results for your texture each time though.
I try to share a HPBUFFERARB between two classes : TGLForm and TGLForm2.
(I tried FBO but having an old Borland Builder 6 version I can't manage using FBO)
My goal is to display the same buffer in two openGL windows.
So I declared outside of the first Form this object :
struct GLRenderToTexture
{
struct
{
HDC hdc;
HGLRC hGlRc;
HPBUFFERARB hBuffer;
PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB;
PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB;
PFNWGLCREATEPBUFFERARBPROC wglCreatePbufferARB;
PFNWGLGETPBUFFERDCARBPROC wglGetPbufferDCARB;
PFNWGLQUERYPBUFFERARBPROC wglQueryPbufferARB;
PFNWGLDESTROYPBUFFERARBPROC wglDestroyPbufferARB;
PFNWGLRELEASEPBUFFERDCARBPROC wglReleasePbufferDCARB;
PFNWGLBINDTEXIMAGEARBPROC wglBindTexImageARB;
PFNWGLRELEASETEXIMAGEARBPROC wglReleaseTexImageARB;
} wgl;
unsigned int texture; // the texture we're going to render to
};
GLRenderToTexture RTT;
I intialize it so as to have the same pixel format as the first GLForm :
void __fastcall TGLForm::FormCreate(TObject *Sender)
{
ghDC = GetDC(Handle);
if (!bSetupPixelFormat(ghDC)) Close();
ghRC = wglCreateContext(ghDC);
wglMakeCurrent(ghDC, ghRC);
InitializeGL();
int pixelFormats;
int intAttrs[32] ={WGL_RED_BITS_ARB,8,WGL_GREEN_BITS_ARB,8,WGL_BLUE_BITS_ARB,8,WGL_ALPHA_BITS_ARB,8,WGL_DRAW_TO_PBUFFER_ARB, GL_TRUE,WGL_BIND_TO_TEXTURE_RGBA_ARB, GL_TRUE,WGL_SUPPORT_OPENGL_ARB,GL_TRUE,WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,WGL_DOUBLE_BUFFER_ARB,GL_FALSE,0}; // 0 terminate the list
unsigned int numFormats = 0;
// get an acceptable pixel format to create the PBuffer with
if (RTT.wgl.wglChoosePixelFormatARB(ghDC, intAttrs, NULL, 1, &pixelFormats, &numFormats)==FALSE)
AnsiString error = AnsiString().sprintf("wglChoosePixelFormatARB returned %i", GetLastError()); // GetLastError will tell us why it failed
//Set some p-buffer attributes so that we can use this p-buffer as a 2d texture target
const int attributes[]= {WGL_TEXTURE_FORMAT_ARB, WGL_TEXTURE_RGBA_ARB, // p-buffer will have RBA texture format
WGL_TEXTURE_TARGET_ARB, WGL_TEXTURE_2D_ARB, 0}; // Of texture target will be GL_TEXTURE_2D
// the size of the PBuffer must be the same size as the texture
RTT.wgl.hBuffer= RTT.wgl.wglCreatePbufferARB(ghDC, pixelFormats, ClientWidth, ClientHeight, attributes);
RTT.wgl.hdc= RTT.wgl.wglGetPbufferDCARB(RTT.wgl.hBuffer);
RTT.wgl.hGlRc= wglCreateContext(RTT.wgl.hdc);
wglMakeCurrent(NULL,NULL);
}
Here is my first DrawScene : the "PaintGL()" drawing is perfectly drawn on this form :
void TGLForm::DrawSceneForm1()
{
wglMakeCurrent(ghDC, ghRC);
ClientWidth = 1920;
ClientHeight = 1080;
// create a texture to use as the backbuffer
glGenTextures(1, &RTT.texture);
glBindTexture(GL_TEXTURE_2D, RTT.texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// make sure this is the same color format as the screen
glTexImage2D(GL_TEXTURE_2D, 0, 4, ClientWidth, ClientHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
// switch to the texture context
wglMakeCurrent(RTT.wgl.hdc, RTT.wgl.hGlRc);
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE);
glClear(GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_TEXTURE_2D);
// switch back to the screen context
wglMakeCurrent(ghDC, ghRC);
wglShareLists(ghRC, RTT.wgl.hGlRc);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE);
glClear(GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, ClientWidth, ClientHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
wglMakeCurrent(RTT.wgl.hdc, RTT.wgl.hGlRc);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, RTT.texture);
PaintGL();
glDisable(GL_TEXTURE_2D);
wglMakeCurrent(ghDC, ghRC);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, RTT.texture);
RTT.wgl.wglBindTexImageARB(RTT.wgl.hBuffer, WGL_FRONT_LEFT_ARB);
glBegin(GL_QUADS);
glColor4ub(255,255,255,255);
glTexCoord2f (0.0, 0.0); glVertex2f (-1.0, -1.0);
glTexCoord2f (1.0, 0.0); glVertex2f (1.0, -1.0);
glTexCoord2f (1.0, 1.0); glVertex2f (1.0, 1.0);
glTexCoord2f (0.0, 1.0); glVertex2f (-1.0, 1.0);
glEnd();
RTT.wgl.wglReleaseTexImageARB(RTT.wgl.hBuffer, WGL_FRONT_LEFT_ARB);
glDisable(GL_TEXTURE_2D);
glFlush();
SwapBuffers(ghDC);
wglMakeCurrent(NULL,NULL);
}
And here is my second GLForm's DrawScene : the problem is that I only see the colored quad but this QUAD is not textured, or the texture is empty :
void TGLForm2::DrawSceneForm2()
{
wglMakeCurrent(ghDC2, ghRC2);
ClientWidth = 1920;
ClientHeight = 1080;
wglShareLists(RTT.wgl.hGlRc, ghRC2);
if (wglShareLists(RTT.wgl.hGlRc,ghRC2) == FALSE)
SCmsgError(AnsiString().sprintf("wglShareLists returned %i", GetLastError()));
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE); //ARC
glClear(GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, ClientWidth, ClientHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, RTT.texture);
RTT.wgl.wglBindTexImageARB(RTT.wgl.hBuffer, WGL_FRONT_LEFT_ARB);
glBegin(GL_QUADS);
glColor4ub(200,200,200,200);
glTexCoord2f (0.0, 0.0); glVertex2f (-1.0, -1.0);
glTexCoord2f (1.0, 0.0); glVertex2f (1.0, -1.0);
glTexCoord2f (1.0, 1.0); glVertex2f (1.0, 1.0);
glTexCoord2f (0.0, 1.0); glVertex2f (-1.0, 1.0);
glEnd();
RTT.wgl.wglReleaseTexImageARB(RTT.wgl.hBuffer, WGL_FRONT_LEFT_ARB);
glDisable(GL_TEXTURE_2D);
glFlush();
SwapBuffers(ghDC);
}
=> How may I check if this texture is empty or not ?
export it to a bitmap and check it ?
=> the wglShareLists in the DrawSceneForm2 returns an error with GetLastError :
Error 6 : ERROR_INVALID_HANDLE The handle is invalid.
=> Does somebody see what is wrong in this wglShareList or in my code ?
When calling wglShareLists, the context must not be current. Preferrably share before you do anything else. Sharing contexts will share anything created thereafter just fine. The best thing is to create all contexts that need to be shared at startup. If you use WGL_ARB_create_context, then you can even do this atomically within the creation call.
If you can't for some reason (though, why?) then wglMakeCurrent(0,0); first (you do the opposite in your code, you make the context current just before sharing).
I had a similar problem where :
wglShareLists returns 0
GetLastError() returns 3221684311 (0xc0070057)
It turns out you cant do much with the hglrc2 (2nd parameter passed into wglShareLists) before you call wglShareLists. In my case I created, and glUseProgram a shader, and then tried wglShareLists resulting in the errors shown above. Moving wglShareLists to immediately after wglCreateContext(hDC) of 2nd RC worked. I was able to share textures across the 2 contexts.
I've tried to research this on Google but there doesn't appear to me to be any coherent simple answers. Is this because it's not simple, or because I'm not using the correct keywords?
Nevertheless, this is the progress I've made so far.
Created 8 vertices to form 2 squares.
Created a texture with a 200 bit alpha value (so, about 80% transparent).
Assigned the same texture to each square, which shows correctly.
Noticed that when I use a texture with 255 alpha, it appears brighter.
The init is something like the following:
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, textureIds);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
int i, j;
GLubyte pixel;
for (i = 0; i < TEXTURE_HEIGHT; i++)
{
for (j = 0; j < TEXTURE_WIDTH; j++)
{
pixel = ((((i & 0x8) == 0) ^ ((j & 0x8) == 0)) * 255);
texture[i][j][0] = pixel;
texture[i][j][1] = pixel;
texture[i][j][2] = pixel;
texture[i][j][3] = 200;
}
}
glBindTexture(GL_TEXTURE_2D, textureIds[0]);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGBA,
TEXTURE_WIDTH, TEXTURE_HEIGHT,
0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
This is somewhat similar to the code snippet from page 417 in the book, OpenGL Programming Guide, and creates a check pattern.
And then, the display function contains...
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
// Use model view so that rotation value is literal, not added.
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
// ... translation, etc ...
glBindTexture(GL_TEXTURE_2D, textureIds[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, +1.0, 0.0); // top left
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, -1.0, 0.0); // bottom left
glTexCoord2f(1.0, 1.0); glVertex3f(+1.0, -1.0, 0.0); // bottom right
glTexCoord2f(1.0, 0.0); glVertex3f(+1.0, +1.0, 0.0); // top right
glEnd();
// not neccecary to repeat, just good practice
glBindTexture(GL_TEXTURE_2D, textureIds[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-0.5, +1.0, -1.0); // top left
glTexCoord2f(0.0, 1.0); glVertex3f(-0.5, -1.0, -1.0); // bottom left
glTexCoord2f(1.0, 1.0); glVertex3f(+1.5, -1.0, -1.0); // bottom right
glTexCoord2f(1.0, 0.0); glVertex3f(+1.5, +1.0, -1.0); // top right
glEnd();
glFlush();
glDisable(GL_TEXTURE_2D);
glPopMatrix();
SwapBuffers();
So, this renders a 2nd square in the background; I can see this but it looks like they're being blended with the background (I assume this because they are darker with 200 bit alpha than 255 bit) instead of the texture behind...
As you can see, no transparency... How can I fix this?
So the other answer which was here but was deleted mentioned this - Generally, for alpha blending to work correctly you need to sort the objects from far to near in the coordinate system of the camera.
This is why your polygons are blended with the background. You can confirm that this is indeed the problem by disabling the depth test. Without depth test all the fragments are displayed and you'll be able to see the alpha blending.
More on this in this page.