Opengl window transparent

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
like80ninjas
Chaos Rift Regular
Chaos Rift Regular
Posts: 101
Joined: Thu Dec 09, 2010 2:13 am

Opengl window transparent

Post 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();
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: Opengl window transparent

Post by N64vSNES »

Is that your entire initialization code?

1- You're not initializing SDL at any point

Code: Select all

SDL_Init(SDL_INIT_VIDEO);
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
like80ninjas
Chaos Rift Regular
Chaos Rift Regular
Posts: 101
Joined: Thu Dec 09, 2010 2:13 am

Re: Opengl window transparent

Post 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.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Opengl window transparent

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: Opengl window transparent

Post 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.
like80ninjas
Chaos Rift Regular
Chaos Rift Regular
Posts: 101
Joined: Thu Dec 09, 2010 2:13 am

Re: Opengl window transparent

Post 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.
pritam
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 991
Joined: Thu Nov 13, 2008 3:16 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Amiga, PSOne, NDS
Programming Language of Choice: C++
Location: Sweden

Re: Opengl window transparent

Post 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.
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: Opengl window transparent

Post 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();
}
Post Reply