# OpenGL GLUT to WinAPI - alpha bitplane problem?

## Recommended Posts

First off, I consider myself pretty adept with both OpenGL as well as WinAPI, so I've hopefully managed to exclude any dead obvious explanations (then again, you'll never know...). I'm trying to convert a GLUT-based sample application to a straightforward WinAPI-based application. I've never really taken an interest in GLUT and it's always seemed as something overly straightforward to me. However, in this case something (probably really simple) is happening that is genuinely puzzling me. I've narrowed the problem down to what is most likely the window creation code. The sample application uses the stencil buffer to segment a projective texture, so the bulk of the attention should rightfully be on the stencil buffer. However, GLUT requires an additional alpha bit (GLUT_ALPHA) to be specified for the window to work in RGBA mode. Specs say: "Bit mask to request a window with an alpha component to the color buffer(s)." This makes little sense to me since the window is (in Windows) initialized through CreateWindow() and the pixel format descriptor should already include the RGBA mode, which should ensure the presence of an alpha channel. Just to be sure I wasn't misunderstanding, I tried setting the alpha buffer in the pixel format descriptor to 1, which only seemed to make matters worse (no projection drawn at all). The effect of the demo (sadly I am not in a position to post screenshots) is like this: I The original demo using GLUT draws the projected texture properly with appropriate bits cut out using a simple stencil test. The initialization code looks like this:
   glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH|GLUT_RGB|GLUT_ALPHA|GLUT_STENCIL);

II My adoption of that code draws the projected texture properly, but does not render the occluded areas properly (no occluding occurs through the stencil buffer) at all. That is, the projection blasts through everything as if no stencil tests were ever performed. The pixel format initialization code looks like this:
   PIXELFORMATDESCRIPTOR pfd= {
sizeof(PIXELFORMATDESCRIPTOR), 1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,    //4-component mode
24,		//color depth
0, 0, 0, 0, 0, 0,
0,                //no alpha buffer
0, 0, 0, 0, 0, 0,
24,               //depth buffer
1,                //stencil buffer
0,PFD_MAIN_PLANE, 0, 0, 0, 0 };

I'm fairly certain the problem is not in the stencil buffer, however, because I reverted to trying the code out without any alterations at all other than switching the GLUT windowing code to my own. I could probably explain the problem more closely, although without the ability to post screenshots, I'm feeling a little helpless without any suggestive questions. As I mentioned, the problem is most likely in the GLUT_ALPHA flag - can someone at least explain what that does, because even Google doesn't seem to know what the heck it really is. Thank you for your time [Edited by - irreversible on November 12, 2005 7:16:05 AM]

##### Share on other sites
All I can suggest is try an 8-bit stencil buffer instead.

##### Share on other sites
Calling glGetIntegerv() for GL_STENCIL_BITS returns 8 regardless of the number of bits defined in the pixel format descriptor, so I suppose the buffer is always initialized to 8 bits (at least on my GF2).

What's more iteresting, is that calling glGetIntegerv() for GL_ALPHA_BITS in the GLUT implementation returns 8. However, setting the alpha buffer 8 bits (similarly to the stencil buffer) in the WinAPI version causes no projected texture to be drawn at all. There are also some other discrepancies if the alpha buffer is enabled, but those fall beyond the scope of my question in this case.

The behaviors simply do not match, so I guess there's something more that I'm missing...

##### Share on other sites
all available hardware i know supports only 8 bit stencil (perhaps one of the professional cards has support for greater)
why do u need more than 8bit?

##### Share on other sites
Is it using the alpha test to control writing to the stencil buffer?

##### Share on other sites
LilBudyWizer - yes. I did not alter the render code, though. At least not consciously (that is, unless GLUT does something behind the scenes, everything should be the same).

zedzeek - I don't need more than 8 bits. I just wanted to express that setting either the alpha or stencil bits to 1 (or any other number) in the PFD automatically uses 8 bits for the respective buffer, that's all.

##### Share on other sites
I guess the second question is do you have an alpha buffer? Overall I think the starting point is verifying the buffers are there and working as intended. If they are there and working as intended then there really isn't much room for you to have done something wrong in the setup.

##### Share on other sites
As I mentioned, if I don't initialize the alpha buffer, the projection is drawn, but not clipped. If I do initialize it, no projection at all is drawn... That's what's confusing me.

##### Share on other sites
Incidentally - I was browsing around in docs on PIXELFORMATDESCRIPTOR and - I don't know how I missed this before, but:

cAlphaBits
Specifies the number of alpha bitplanes in each RGBA color buffer. Alpha bitplanes are not supported.
cAlphaShift
Specifies the shift count for alpha bitplanes in each RGBA color buffer. Alpha bitplanes are not supported.

This is really really confusing because GLUT_ALPHA says:

GLUT_ALPHA
Bit mask to request a window with an alpha component to the color buffer(s).

It is stated that GLUT will not initialize the alpha channel properly unless GLUT_ALPHA is specified, even though GLUT_RGBA may be specified (looking up GLUT_RGBA in glut.h, we'll find that it's defined as:

#define GLUT_RGBA GLUT_RGB)

This makes no sense, simply because PIXELFORMATDESCRIPTOR doesn't seem to provide a means to initialize the color buffer without an alpha component anyway (the only eligible format choice for OpenGL is PFD_TYPE_RGBA, which should already include the four channels). Does GLUT somehow implement an additional alpha channel internally if GLUT_ALPHA is specified? If so then how exactly.

Furthermore - calling:

glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH|GLUT_RGBA|GLUT_ALPHA|GLUT_STENCIL);
glGetIntegerv(GL_STENCIL_BITS, &stencil);
glGetIntegerv(GL_ALPHA_BITS, & alpha);

returns stecil and alpha both as 8 bits.

Failing to specify the alpha bitplane in the PIXELFORMATDESCRIPTOR in a Windows-specific example will return alpha as 0 (however, it will be 8 if the alpha bitplane is specified). Again, this makes little sense if you think about it for a moment.

Has no one really had a problem similar to this?

##### Share on other sites
GLUT_ALPHA is used for creating a window with destination alpha ie u wanna store the alpha info in the window
eg it lets u do blending like GL_DST_ALPHA GL_ONE_MINUS_DST_ALPHA

without it u can still use textures etc with alpha channels + blending like SRC_ALPHA (source)

wiht 98% of things u dont need destination alpha

##### Share on other sites
Resolved. It was a semicolon. I'd like to kick myself right now, but I won't because experience has shown me that life's like that most of the time anyway. I'd like to thank everyone who replied in this thread. The both of you, that is :).

PS - OpenGL specs on PIXELFORMATDESCRIPTOR apply to the generic GDI implementation, not the generic hardware-accelerated case. That is, alpha bitplanes aren't supported in GDI only - something that the docs fail to mention.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627746
• Total Posts
2978905
• ### Similar Content

• Hello! As an exercise for delving into modern OpenGL, I'm creating a simple .obj renderer. I want to support things like varying degrees of specularity, geometry opacity, things like that, on a per-material basis. Different materials can also have different textures. Basic .obj necessities. I've done this in old school OpenGL, but modern OpenGL has its own thing going on, and I'd like to conform as closely to the standards as possible so as to keep the program running correctly, and I'm hoping to avoid picking up bad habits this early on.
Reading around on the OpenGL Wiki, one tip in particular really stands out to me on this page:
For something like a renderer for .obj files, this sort of thing seems almost ideal, but according to the wiki, it's a bad idea. Interesting to note!
So, here's what the plan is so far as far as loading goes:
Set up a type for materials so that materials can be created and destroyed. They will contain things like diffuse color, diffuse texture, geometry opacity, and so on, for each material in the .mtl file. Since .obj files are conveniently split up by material, I can load different groups of vertices/normals/UVs and triangles into different blocks of data for different models. When it comes to the rendering, I get a bit lost. I can either:
Between drawing triangle groups, call glUseProgram to use a different shader for that particular geometry (so a unique shader just for the material that is shared by this triangle group). or
Between drawing triangle groups, call glUniform a few times to adjust different parameters within the "master shader", such as specularity, diffuse color, and geometry opacity. In both cases, I still have to call glBindTexture between drawing triangle groups in order to bind the diffuse texture used by the material, so there doesn't seem to be a way around having the CPU do *something* during the rendering process instead of letting the GPU do everything all at once.
The second option here seems less cluttered, however. There are less shaders to keep up with while one "master shader" handles it all. I don't have to duplicate any code or compile multiple shaders. Arguably, I could always have the shader program for each material be embedded in the material itself, and be auto-generated upon loading the material from the .mtl file. But this still leads to constantly calling glUseProgram, much more than is probably necessary in order to properly render the .obj. There seem to be a number of differing opinions on if it's okay to use hundreds of shaders or if it's best to just use tens of shaders.
So, ultimately, what is the "right" way to do this? Does using a "master shader" (or a few variants of one) bog down the system compared to using hundreds of shader programs each dedicated to their own corresponding materials? Keeping in mind that the "master shaders" would have to track these additional uniforms and potentially have numerous branches of ifs, it may be possible that the ifs will lead to additional and unnecessary processing. But would that more expensive than constantly calling glUseProgram to switch shaders, or storing the shaders to begin with?
With all these angles to consider, it's difficult to come to a conclusion. Both possible methods work, and both seem rather convenient for their own reasons, but which is the most performant? Please help this beginner/dummy understand. Thank you!

• I want to make professional java 3d game with server program and database,packet handling for multiplayer and client-server communicating,maps rendering,models,and stuffs Which aspect of java can I learn and where can I learn java Lwjgl OpenGL rendering Like minecraft and world of tanks

• A friend of mine and I are making a 2D game engine as a learning experience and to hopefully build upon the experience in the long run.

-What I'm using:
C++;. Since im learning this language while in college and its one of the popular language to make games with why not.     Visual Studios; Im using a windows so yea.     SDL or GLFW; was thinking about SDL since i do some research on it where it is catching my interest but i hear SDL is a huge package compared to GLFW, so i may do GLFW to start with as learning since i may get overwhelmed with SDL.
-Questions
Knowing what we want in the engine what should our main focus be in terms of learning. File managements, with headers, functions ect. How can i properly manage files with out confusing myself and my friend when sharing code. Alternative to Visual studios: My friend has a mac and cant properly use Vis studios, is there another alternative to it?

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.

• 10
• 10
• 21
• 14
• 14