Little Endian
Little Endian

Reputation: 830

Using glColor* with OpenGL version 4.4

I am in the process of updating our ancient OpenGL code. It is a Windows system and I am using GLEW. The OpenGL version is 4.4 (previously, the way the context was created limited us to 1.1). The code base is large and so I want to update it in stages (i.e. get everything working now on a version higher than 1.1 with minimal work). So far, I found only one break. Transparency no longer works. I suspect this may be due to glColor* or glTexEnv* being deprecated. I tried to request a specific version of OpenGL but was not successful. Can you tell me what I am doing wrong in context creation or what changes I can make to the draw code to get transparency to work? (Other help appreciated of course.)

Here is the context creation code (error checking removed for readability):

PIXELFORMATDESCRIPTOR pfd = {
    /*WORD  nSize*/           sizeof(PIXELFORMATDESCRIPTOR),
    /*WORD  nVersion*/        1,
    /*DWORD dwFlags*/         PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL,
    /*BYTE  iPixelType*/      PFD_TYPE_RGBA,
    /*BYTE  cColorBits*/      24,
    /*BYTE  cRedBits*/        0,
    /*BYTE  cRedShift*/       0,
    /*BYTE  cGreenBits*/      0,
    /*BYTE  cGreenShift*/     0,
    /*BYTE  cBlueBits*/       0,
    /*BYTE  cBlueShift*/      0,
    /*BYTE  cAlphaBits*/      8,
    /*BYTE  cAlphaShift*/     0,
    /*BYTE  cAccumBits*/      0,
    /*BYTE  cAccumRedBits*/   0,
    /*BYTE  cAccumGreenBits*/ 0,
    /*BYTE  cAccumBlueBits*/  0,
    /*BYTE  cAccumAlphaBits*/ 0,
    /*BYTE  cDepthBits*/      16,
    /*BYTE  cStencilBits*/    0,
    /*BYTE  cAuxBuffers*/     0,
    /*BYTE  iLayerType*/      PFD_MAIN_PLANE,
    /*BYTE  bReserved*/       0,
    /*DWORD dwLayerMask*/     0,
    /*DWORD dwVisibleMask*/   0,
    /*DWORD dwDamageMask*/    0
};
int nPixelFormat = ChoosePixelFormat(hDC, &pfd);
SetPixelFormat(hDC, nPixelFormat, &pfd);

hRC_ = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC_);

MsgUtil::TraceWin("version: %s", glGetString(GL_VERSION)); // Output: version: 4.4.0

glewInit();

GLint attribs[] = {
    WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
    WGL_CONTEXT_MINOR_VERSION_ARB, 0,
    WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
    0
};

HGLRC CompHRC = wglCreateContextAttribsARB(hDC, 0, attribs);
if (CompHRC && wglMakeCurrent(hDC, CompHRC)){
hRC_ = CompHRC;
}

MsgUtil::TraceWin("version: %s", glGetString(GL_VERSION)); // Output: version: 4.4.0

Even after requesting version 3.0, glGetString(GL_VERSION) returns 4.4.0. I draw to a framebuffer and then use glReadPixels() to write to a bitmap (though I don't think that is especially relevant here).

Here is the framebuffer code (stays bound for lifetime of context):

glGenFramebuffers(1, &defaultFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);

glGenRenderbuffers(1, &colorRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, framebufferWidth, framebufferHeight);

glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer);

glGenRenderbuffers(1, &depthRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, framebufferWidth, framebufferHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer);

Here is the draw code:

glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(1.0f, 1.0f, 1.0f, alpha);

glBindTexture(GL_TEXTURE_2D, texture_id);

glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);  
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);

// Turn theImage (a CImage) into a texture
HBITMAP hbitmap = NewCreateDib24(*window_dc_, MAX_DELTA, MAX_DELTA);
CBitmap* bitmap = CBitmap::FromHandle(hbitmap);
CDC* memDC = new CDC();
memDC->CreateCompatibleDC(window_dc_);
CBitmap* pOldBitmap = memDC->SelectObject(bitmap);
theImage.StretchBlt(*memDC, 0, 0, MAX_DELTA, MAX_DELTA, SRCCOPY);
LPBYTE tempbitsMem = new BYTE[bm_info.bmiHeader.biSizeImage];
bitmap->GetBitmapBits(bm_info.bmiHeader.biSizeImage, (LPVOID)tempbitsMem);

glTexImage2D(GL_TEXTURE_2D, 0, 3, MAX_DELTA, MAX_DELTA, 0, GL_BGR, GL_UNSIGNED_BYTE, tempbitsMem);

// Draw texture     
glBegin(GL_QUADS);
glTexCoord2d(tx.x_, 1.0 - tx.y_);
glVertex2d(px.x_, px.y_);
glTexCoord2d(t0.x_, 1.0 - t0.y_);
glVertex2d(p0.x_, p0.y_ );
glTexCoord2d(ty.x_, 1.0 - ty.y_);
glVertex2d(py.x_, py.y_);
glTexCoord2d(txy.x_, 1.0 - txy.y_);
glVertex2d(pxy.x_, pxy.y_); 
glEnd();

The texture is now applied with full opacity, regardless of the alpha value sent to glColor4f(). However, if I change the RGB values, then that is still reflected in the output.

Upvotes: 0

Views: 773

Answers (1)

SurvivalMachine
SurvivalMachine

Reputation: 8356

You should ask for a compatibility profile instead of a core profile.

Upvotes: 2

Related Questions