使用glColor *和OpenGL 4.4版

时间:2014-02-19 19:03:39

标签: windows opengl glew openglcontext

我正在更新我们的古老OpenGL代码。这是一个Windows系统,我正在使用GLEW。 OpenGL版本是4.4(之前,创建上下文的方式将我们限制为1.1)。代码库很大,所以我想分阶段更新它(即现在所有工作都在高于1.1的版本上工作,只需要很少的工作)。到目前为止,我发现只有一次休息。透明度不再有效。我怀疑这可能是由于glColor *或glTexEnv *被弃用。我试图请求特定版本的OpenGL,但没有成功。你能告诉我在上下文创建中我做错了什么,或者我可以对绘图代码做些什么改变以使透明度有效? (当然还有其他帮助。)

以下是上下文创建代码(为了便于阅读而删除了错误检查):

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

即使在请求3.0版之后,glGetString(GL_VERSION)也会返回4.4.0。我绘制到一个帧缓冲区,然后使用glReadPixels()写入一个位图(虽然我不认为这在这里特别相关)。

这是帧缓冲区代码(在上下文的生命周期内保持绑定):

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

这是绘图代码:

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

无论发送到glColor4f()的alpha值如何,纹理现在都应用了完全不透明度。但是,如果我更改RGB值,那么它仍会反映在输出中。

1 个答案:

答案 0 :(得分:2)

您应该要求提供兼容性配置文件而不是核心配置文件。