简体   繁体   English

透明度Opengl + SDL不起作用C ++

[英]Transparency Opengl+SDL not working C++

I've been trying to get transparency working with SDL+ OpenGL. 我一直在尝试使用SDL + OpenGL来提高透明度。

Here are my functions that initialize OpenGL and SDL, draw an image, and create a texture 这是我用于初始化OpenGL和SDL,绘制图像并创建纹理的函数

void initGL()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho( 0.0, Screen_Width,Screen_Height, 0.0, 1.0, -1.0 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, Screen_Width, Screen_Height);
}

void initSDL()
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
Bob=SDL_SetVideoMode(0, 0, 32, SDL_OPENGL);
Screen_Width= Bob->w;
Screen_Height= Bob->h;
SDL_WM_SetCaption("Project", NULL);;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
}

void Texture::draw(int x1, int y1,int x2, int y2,std::string filename)
{
glClear( GL_COLOR_BUFFER_BIT );
CreateTexture(filename.c_str());
glEnable( GL_TEXTURE_2D );
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, Tex);
glBegin(GL_QUADS);
    glTexCoord2f(0.f, 0.f);
    glVertex2f( 0.f, 0.f );
    glTexCoord2f(1.f, 0.f);
    glVertex2f(  1920.f, 0.f );
    glTexCoord2f(1.f, 1.f);
    glVertex2f(  1920.f,  1080.f );
    glTexCoord2f(0.f, 1.f);
    glVertex2f( 0.f,  1080.f );
glEnd();
glDisable(GL_TEXTURE_2D);
SDL_GL_SwapBuffers();
}

void Texture::CreateTexture(std::string filename)
{
SDL_Surface* image =NULL;
image=IMG_Load( filename.c_str() );
glClearColor( 0, 0, 0, 0 );
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glGenTextures(1, &Tex);
glBindTexture(GL_TEXTURE_2D, Tex);
int mode = GL_RGB;
if(image->format->BytesPerPixel == 4)
    mode = GL_RGBA;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 4, image->w, image->h, 0, mode, GL_UNSIGNED_BYTE, image-  >pixels);
SDL_FreeSurface(image);
}

You're drawing exactly one quad, with one texture. 您正好绘制一个具有一个纹理的四边形。 What exactly do you expect to be visible in the translucent parts? 您希望在半透明部分中看到什么? The background color? 背景颜色?

Or your desktop, or the windows beneath? 还是您的桌面,还是下面的窗户? That takes quite some additional work. 这需要很多额外的工作。

First you need a compositor running (on Windows this requires Windows Vista with Aero or later). 首先,您需要运行一个合成器(在Windows上,这需要具有Aero或更高版本的Windows Vista)。

And the window must be configured in a way, that its alpha channel is actually considered when compositing; 并且必须以某种方式配置窗口,以便在合成时实际考虑其Alpha通道。 AFAIK SDL doesn't take those preparations. AFAIK SDL不进行这些准备。

You can check the texture format is should be RGBA and you can test a white texture(RGB(255,255,255)) that has alpha channel of 128, so the whole texture is just white with alpha half white. 您可以检查纹理格式是否应为RGBA,并且可以测试具有128个alpha通道的白色纹理(RGB(255,255,255)),因此整个纹理仅为白色,alpha值为白色。 Drawing this on a black background should result a half white texture being drawn. 在黑色背景上绘制此图形将导致绘制一半白色的纹理。

Also the vertex positions might not be right, you could try something like this (with identity matrix for projection and world transform) to see if everything is in order with opengl setup 同样顶点位置可能不正确,您可以尝试类似的方法(使用用于投影和世界变换的单位矩阵)来查看是否一切均与opengl设置一致

glTexCoord2f(0.f, 0.f);
glVertex2f( 0.25f, 0.25f );
glTexCoord2f(1.f, 0.f);
glVertex2f(  0.25f,0.5f );
glTexCoord2f(1.f, 1.f);
glVertex2f(  0.5f,0.5f );
glTexCoord2f(0.f, 1.f);
glVertex2f(  0.5f,0.25f );

Hope this helps. 希望这可以帮助。 Razvan. 拉兹万

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM