我使用如下函数加载两个纹理:
GLuint LoadImage(std::string ImageTitle, GLenum Target)
{
Image Img(ImageTitle.c_str());
std::vector<std::uint8_t> Buffer, Out(((Img.Width() * Img.GetBitsPerPixel() + 31) / 32) * 4 * Img.Height());
ImageToBuffer(Buffer, Img);
FlipImageBytes(Buffer.data(), Out.data(), Img.Width(), Img.Height(), Img.GetBitsPerPixel());
GLuint ID;
glGenTextures(1, &ID);
glBindTexture(Target, ID);
glTexParameteri(Target, GL_TEXTURE_WRAP_S, Target == GL_TEXTURE_2D ? GL_REPEAT : GL_CLAMP_TO_EDGE);
glTexParameteri(Target, GL_TEXTURE_WRAP_T, Target == GL_TEXTURE_2D ? GL_REPEAT : GL_CLAMP_TO_EDGE);
glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(Target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(Target, 0, Img.GetBitsPerPixel() == 32 ? GL_RGBA : GL_RGB, Img.Width(), Img.Height(), 0, Img.GetBitsPerPixel() == 32 ? GL_BGRA : GL_BGR, GL_UNSIGNED_BYTE, Out.data());
return ID;
}
第一个纹理是地图,我通常在屏幕上绘制它。第二个纹理是黑色的,上面有红色的文字,这就是我想在地图纹理上绘制的那个。但是,我想忽略黑色像素,以便它只绘制红色文本。
我使用以下内容进行绘制:
void DrawTexture(GLenum Target, GLuint ID, float X1, float Y1, float X2, float Y2, int Width = 1, int Height = 1)
{
//glEnable(Target);
//glBindTexture(Target, ID);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(X1, Y1);
glTexCoord2f(0, Height);
glVertex2f(X1, Y2);
glTexCoord2f(Width, Height);
glVertex2f(X2, Y2);
glTexCoord2f(Width, 0);
glVertex2f(X2, Y1);
glEnd();
glDisable(Target);
}
int X1 = 0, Y1 = 0, X2 = 512, Y2 = 512;
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
//Draw MAP.
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, ID);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
DrawTexture(GL_TEXTURE_2D, MAPID, 0, 0, 512, 512);
glFlush();
//Draw TEXT..
glBindTexture(GL_TEXTURE_2D, ID2);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glBlendFunc(GL_ONE, GL_DST_ALPHA);
DrawTexture(GL_TEXTURE_2D, TEXTID, 0, 0, 512, 512);
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
glPopMatrix();
SwapBuffers(DC);
Sleep(1);
我正在尝试做的事情:
std::vector<std::uint8_t> Buffer;
//Fill Buffer with Pixels.
glDrawPixels(Width, Height, .........);
但是,它不会使第二张图像上的黑色像素透明。我怎样才能让它只在第二个纹理中绘制除黑色之外的每个像素?所以正常绘制第一个纹理并绘制第二个忽略黑色。
我尝试使用 glDrawPixels (如果可能的话,我更喜欢用它来绘制黑色文本像素)
有任何想法吗?