Board index » cppbuilder » opengl newbie.

opengl newbie.

Hi, I am trying to create a font using glDrawPixels rather than glBitmap. I
cannot move the x position more than one time through the glRasterPos2i...
Should I be moving the rasterposition during the drawing...

I guess the code will lokk familiar too many examples, perhaps someone may
show me any redundant calls made also...

Thanks in advance, Cam

void __fastcall TTelGLFont2d::Create()
{
try
    {
        int cnt,i,wd,ht,pos;
        long pixel;
        int dst;
        glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
        ListBase = glGenLists(lastglyph);
        cnt = rectlist->Count;
        for (i = 0; i < cnt; i++)
        {
            TTelRect* rect = rectlist->Items[i];
            wd = rect->Width;
            ht = rect->Height;
            GLubyte* data = new GLubyte[(wd*ht*4)];

            for (int h = 0; h < ht; h++)
            {
                for (int w = 0; w < wd; w++)
                {
                    pixel=rect->Character->Canvas->Pixels[w][(ht-h)-1];
                    pos=(h*wd*4)+(w*4);

                    data[pos+0] = GetRValue(pixel);
                    data[pos+1] = GetGValue(pixel);
                    data[pos+2] = GetBValue(pixel);
                    data[pos+3] = 255;
                }
            }
            glNewList(i+ListBase, GL_COMPILE);
                glBegin();
                GLint* rpos = new GLint[4];
                glGetIntegerv(GL_CURRENT_RASTER_POSITION,rpos);
                glRasterPos2i(rpos[0]+wd,0);
                delete rpos;
                glDrawPixels(wd,ht,GL_RGBA,GL_UNSIGNED_BYTE,data);
            glEndList();
            delete data;
        }
    }

void __fastcall TTelGLFont2d::Draw(AnsiString Text)
{
    unsigned char *c=(unsigned char *)Text.c_str();
    int Index;
    int Length=strlen((char *)c);

 glDisable(GL_DEPTH_TEST);     // Disables Depth Testing
 glMatrixMode(GL_PROJECTION);    // Select The Projection Matrix
 glPushMatrix();        // Store The Projection Matrix
 glLoadIdentity();       // Reset The Projection Matrix
 glOrtho(0,640,0,480,-1,1);     // Set Up An Ortho Screen
 glMatrixMode(GL_MODELVIEW);     // Select The Modelview Matrix
 glPushMatrix();        // Store The Modelview Matrix
 glLoadIdentity();       // Reset The Modelview Matrix
// glTranslated(10.0,10.0,0);     // Position The Text (0,0 - Bottom Left)
 glListBase(ListBase-32);     // Choose The Font Set (0 or 1)
 glCallLists(Length,GL_BYTE,c);    // Write The Text To The Screen
 glMatrixMode(GL_PROJECTION);    // Select The Projection Matrix
 glPopMatrix();        // Restore The Old Projection Matrix
 glMatrixMode(GL_MODELVIEW);     // Select The Modelview Matrix
 glPopMatrix();        // Restore The Old Projection Matrix
 glEnable(GL_DEPTH_TEST);     // Enables Depth Testing

 

Re:opengl newbie.


Hi again... I managed to solve the problem by:
            glNewList(i+ListBase, GL_COMPILE);
                glDrawPixels(wd,ht,GL_RGBA,GL_UNSIGNED_BYTE,data);

                glBitmap(0,0,0,0,wd,0,NULL); ***

            glEndList();
This is done within the creation of the font lists and seems to work fast
enough.

The new problem I am facing is the blending of the image to the background
to simulate a transparent text.

When the glDrawPixel data gets created, I supply 4 components of 1 byte
each. (GLubyte).

I cannot find a consistent way of blending the pixels even though the alpha
channel on the transparent color has been set to 0. The other color(s) at
255.

Appologies if these questions are rather basic, but I could really use some
help.
Thanks in advance, Cam.

Re:opengl newbie.


Quote
Cam Ellis wrote:
> I cannot find a consistent way of blending the pixels even though the alpha
> channel on the transparent color has been set to 0. The other color(s) at
> 255.

MSDN says:
| Transparency is best implemented using
| glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) with primitives
| sorted from farthest to nearest. Note that this transparency
| calculation does not require the presence of alpha bitplanes in the
| framebuffer.

HTH
Clemens

Re:opengl newbie.


Quote
> | Transparency is best implemented using
> | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) with primitives
> | sorted from farthest to nearest. Note that this transparency
> | calculation does not require the presence of alpha bitplanes in the
> | framebuffer.

Clemens, thank you for your responce. The Image (4 component, 1
GLubyte/component) that I am trying to display has a white background and
red foreground. I have found that I cannot make the (white) background,
transparent unless I set the alpha value to zero.

Perhaps this code may highlight my errors,
thanks again.. Cam.

Called during font list creation (component initialisation.. start of
application runtime)

        glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
        ListBase = glGenLists(lastglyph);
        cnt = rectlist->Count;
        for (int h = 0; h < ht; h++)
        {
                for (int w = 0; w < wd; w++)
                {
                    pixel=rect->Character->Canvas->Pixels[w][(ht-h)-1];
                    pos=(h*wd*4)+(w*4);

                    rval = GetRValue(pixel);
                    bval = GetGValue(pixel);
                    gval = GetBValue(pixel);

                    if (rval == 255 && gval == 255 && bval == 255) aval = 0;
//                    else aval = (rval+gval+bval)/3;
                    else aval = 255;

                    data[pos+0] = rval;
                    data[pos+1] = gval;
                    data[pos+2] = bval;
                    data[pos+3] = aval;
                }
            }
            glNewList(i+ListBase, GL_COMPILE);
                glDrawPixels(wd,ht,GL_RGBA,GL_UNSIGNED_BYTE,data);
                glBitmap(0,0,0,0,wd,0,NULL);
            glEndList();
            delete data;

Called when drawing the font (runtime)

void __fastcall TTelGLFont2d::Draw(AnsiString Text)
{
    unsigned char *c=(unsigned char *)Text.c_str();
    int Index;
    int Length=strlen((char *)c);
    bool mode = glIsEnabled(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_BLEND);
    glListBase(ListBase-32);
    glCallLists(Length,GL_BYTE,c);    // Write The Text To The Screen
    if (!mode) glDisable(GL_BLEND);

Quote
}

Re:opengl newbie.


Quote
Cam Ellis wrote:
> I have found that I cannot make the (white) background,
> transparent unless I set the alpha value to zero.

This is correct.
The transparency is controlled by the alpha value _only_
(this is why every pixel has a seperate alpha value).

HTH
Clemens

Other Threads