tags:

views:

138

answers:

2

Hi, I'm somewhat new to OpenGL. I've used it for extremely simple stuff like rendering Textures(2D) and basic MODELVIEW transformations, but now I want to modify the alpha values in fashion similar to the MODELVIEW stack.

I basically have a glTex class, which renders a texture at a certain point after applying certain transformations to it. (Translation, Rotation, Alpha.) Now, this works fine for simple things like simple-images, but if I want to draw something a little more complex say a simple grid. I would translate to the required position of the grid, and then subsequently draw every part of it. This works fine cause I usually call glPushMatrix() and glPopMatrix() before and after rendering a texture.

The problem arises when I want to control the alhpa of the grid. Suppose I want to render it at 0.50 alpha. I could go through each of it's components and change their alpha values, but then this means that if some parts of the grid are made of smaller parts, they would have to do the same or if the smaller parts them selves had alpha values I would have to calculate a brand new alpha, which turns into a really lengthy process.

Is there some way I could get an alpha stack like the MODELVIEW one ? Or is there any better way to go about it?

Example - Grid is rendered at 0.5 alpha. If it has a component which is normally rendered at 0.25 alpha, it will now be rendered at .125 alpha

Btw, I have the glBlendFunc set to GL _SRC _ALPHA and GL _ONE _MINUS _SRC _ALPHA.

A: 

Try glPushAttrib and glPopAttrib

http://www.opengl.org/documentation/specs/man%5Fpages/hardcopy/GL/html/gl/pushattrib.html

rikh
This lets you save the alpha value, but it doesn't let you multiply alpha values.
Jay Conrod
+3  A: 

There is no way to multiply some additional alpha into the current alpha like you can modify the transformation matrixes in OpenGL, as OpenGL has no "alpha" state. alpha values are passed alongside color information. So what you want to do is to write a small helper class that keeps track of your alpha states:

enter code here

#define ALPHA_STACK_SIZE 10

class CAlphaState {
private:
   float m_alphaStack [ALPHA_STACK_SIZE];
   int m_tos;
public:
   CAlphaState () { m_tos = -1; }
   inline float Push (float alpha) {
      if (m_tos >= 0)
         alpha *= m_alphaStack [m_tos];
      if (m_tos < ALPHA_STACK_SIZE - 1) // TODO: Add dynamic stack size handling
         m_alphaStack [++m_tos] = alpha;
      return alpha;
      }
   inline void Pop (void) { if (m_tos >= 0) m_tos--; }
   inline void Get (void) { return (m_tos < 0) ? 1.0f : m_alphaStack [m_tos]; }
   inline void Set (float alpha) { glColor4f (1.0f, 1.0f, 1.0f, Push (alpha)); }
   };

The restriction of this class is that it always sets your draw color to white. You can add color book keeping to the class or use the Get function to set the color using the current alpha after pushing it.

karx11erx
Not exactly what I was looking for, but as close as it can get I suppose. Thanks :)
vhanda
I know, but there is no other way to do this than to code it oneself.
karx11erx