Page 1 of 1
Opengl window transparent
Posted: Mon Dec 27, 2010 8:37 am
by like80ninjas
Hey, I'm just getting started using opengl rather than straight SDL. All I want at the moment is to display a blank window, simple. I want it to be a just a black box, but when I use my code, is turns out to be a window border with a transparent inside!
Here is my code, can someone help me just have a black background.
Code: Select all
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
screen = SDL_SetVideoMode( 640, 480, 32, SDL_OPENGL );
glEnable( GL_TEXTURE_2D );
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glViewport( 0, 0, 640, 480 );
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho(0.0f, 640, 480, 0.0f, -1.0f, 1.0f);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
Re: Opengl window transparent
Posted: Mon Dec 27, 2010 9:43 am
by N64vSNES
Is that your
entire initialization code?
1- You're not initializing SDL at any point
2- I may be wrong but shouldn't you be calling
Code: Select all
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
After you initialize a buffer?
3- You should have the matrix mode set to GL_PROJECTION whilst setting up the the context I.E. glOrtho, And then switch it to GL_MODELVIEW
Re: Opengl window transparent
Posted: Mon Dec 27, 2010 10:28 am
by like80ninjas
Sorry I do initialize SDL, shortly above the posted code. But I got the code from gpwiki's article on sdl with opengl. The call the SDL_GL_SetAttribute line after the buffer, but I'm no expert, i'm just doing what they say. Also, for your third suggestion, I do that. In the code posted above.
Re: Opengl window transparent
Posted: Mon Dec 27, 2010 12:10 pm
by Ginto8
SDL_GL_SetAttribute is in the right place, it's usually best to have it before SetVideoMode (I dunno if it works otherwise). However, you don't need screen. Keeping a reference to the screen surface is really only necessary when blitting, which you shouldn't be doing if you're using opengl. As for the clearing, it looks like it should work, but maybe you should try making it glClearColor(0,0,0,1) instead (the last number is the alpha, and 0 is transparent). I don't know if it will work for sure, but it may solve your problem.
Re: Opengl window transparent
Posted: Mon Dec 27, 2010 12:31 pm
by N64vSNES
Ginto8 wrote:SDL_GL_SetAttribute is in the right place, it's usually best to have it before SetVideoMode (I dunno if it works otherwise). However, you don't need screen. Keeping a reference to the screen surface is really only necessary when blitting, which you shouldn't be doing if you're using opengl. As for the clearing, it looks like it should work, but maybe you should try making it glClearColor(0,0,0,1) instead (the last number is the alpha, and 0 is transparent). I don't know if it will work for sure, but it may solve your problem.
Well I doubt glClearColor() will make a diffrence, I know what he means as I've encounted this problem before. If you're asking for a OpenGL context without initializing it correctly then the buffer fucks with the desktop.
try
Code: Select all
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL,1);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
buffer = SDL_SetVideoMode(640,480,32,SDL_OPENGL);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glClearColor(0,0,0,0);
glOrtho(0,640,480,0-1,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
Also in your main loop make sure you're calling "SDL_GL_SwapBuffers()"
NOT SDL_Flip() or glFlush() Also you still have to poll for events. It could be somthing in your main loop aswell I'm not sure.
Re: Opengl window transparent
Posted: Mon Dec 27, 2010 1:24 pm
by like80ninjas
I've tried the suggestions and it doesn't seem to help, I even tried copy pasting the above code. It's still a transparent window, no errors or crashes.
Re: Opengl window transparent
Posted: Mon Dec 27, 2010 2:07 pm
by pritam
It sounds like your viewport isn't updated at all, ever, as if you're simply not touching it in your main loop.
Re: Opengl window transparent
Posted: Tue Dec 28, 2010 1:02 pm
by N64vSNES
I agree with Pritam, show us your main loop.
It
should look somthing like:
Code: Select all
for(;;) {
glClear(GL_COLOR_BUFFER_BIT);
SDL_PollEvent(&event);
SDL_GL_SwapBuffers();
}