[gameprogrammer] Re: OpenGL Colours and Alpha

Here is an old function for rendering a 2D text overlay on the screen.. its pretty slow (there are better ways of doing text) but it shows the state changes you might need for alpha.

-Josh

/*
renders text to a 2D overlay.
*/
void renderText(int x, int y, const char* text, Pixel3 color, GLubyte lifetime)
{

  int c; //character from string

  /* color value with alpha */
  glColor4ub(color[0], color[1], color[2], lifetime);

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

/* CREDIT: this method of rendering text to an orthogonal 2D "overlay" was outlined by
     Mark Kilgard from SGI in a post to comp.graphics.api.opengl
       I modified it to include an alpha blending fade :)
  */
 glPopMatrix();
   glPushAttrib(GL_ENABLE_BIT);
     glDisable(GL_DEPTH_TEST);
     glDisable(GL_LIGHTING);

     glEnable(GL_BLEND);
     glEnable(GL_ALPHA_TEST);
     glAlphaFunc(GL_GREATER,.005);

     glMatrixMode(GL_PROJECTION);
     glPushMatrix();
       glLoadIdentity();
gluOrtho2D(0,glutGet(GLUT_WINDOW_WIDTH), 0,glutGet(GLUT_WINDOW_HEIGHT));
       glMatrixMode(GL_MODELVIEW);
       glPushMatrix();
         glLoadIdentity();
glRasterPos2i(x, y);
           c = *text;
           while (c != NULL) {
               glutBitmapCharacter(GLUT_BITMAP_9_BY_15, c);
               text++;
               c = *text;
           }

      glPopMatrix();
      glMatrixMode(GL_PROJECTION);

      glDisable(GL_ALPHA_TEST);
       glDisable(GL_BLEND);

    glPopMatrix();
 glPopAttrib();
 glMatrixMode(GL_MODELVIEW);

 /* turn these back on */
 glEnable(GL_LIGHTING);
 glEnable(GL_DEPTH_TEST);

}


Stephen Smith wrote:
Thanks a lot, that certainly changed things. However, I've just realised that all I needed to do what use Color3f, and set the colour to dark grey before drawing the texture for all the "normal" objects, but set it to white for all "bright" objects. I think I got a bit side-tracked with the whole alpha thing!

Cheers,

Steve



Alan Wolfe wrote:
Heya Steve,
Yep you got it, you have to enable blending glEnable(GL_BLEND); is the way to do it i think (dont have any gl code right here at the moment!) you also can change the type of blending you want with the glBlendFunc command, so you can do alpha blending, additive blending, multiplicative blending, difference blending etc. (:

On 10/31/06, *Stephen Smith* <gp@xxxxxxxxxxxxxxxxxxxxxxxx <mailto:gp@xxxxxxxxxxxxxxxxxxxxxxxx>> wrote:

    Hi all,

I'm trying to implement the alpha value in my colours when drawing my
    OpenGL scene.  I've changed all my colour commands to "glColor4f",
    and
have the fourth value as 0.1, except for lasers and explosions where I
    have to set to 1.  My intention is that these should be bright
    compared
    to everything else.  However, there doesn't seem to be any
    difference.
Can anyone tell me why? Do i need "enable" alpha values or something?
    (BTW, I'm using Java and JOGL, but since it's a direct mapping to
    OpenGL
    it should be a non-Java specific answer).

    Thanks in advance,

    Steve Smith



    ---------------------
    To unsubscribe go to http://gameprogrammer.com/mailinglist.html






---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html




---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html


Other related posts: