Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: How can I change the color of my text?

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2003
    Location
    Oslo, Norway
    Posts
    2

    How can I change the color of my text?

    Hi everyone
    I am brand new to OpenGL and C-programming and I am using some predefined code to generate 3D text. I would like to change the color of this text and I was hoping that someone can help me

    -Hope to be hearing from some of you soon!
    -Margrete-
    ____________________________________________
    Here's the source code that I'm utilizing, font3D.c:
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #if defined(_WIN32)
    #include <windows.h>
    #else
    #include <strings.h>
    #endif
    #include <GL/gl.h>
    #include <GL/glut.h>
    #include <GL/glaux.h>

    GLuint base;
    GLYPHMETRICSFLOAT agmf[256];
    // Private GDI device context
    HDC hDC=NULL;
    // Permanent rendering context
    HGLRC hRC=NULL;
    // Holds our window handle
    HWND hWnd=NULL;
    // Holds the instance of the application
    HINSTANCE hInstance;

    GLvoid init3DFont()
    {

    LOGFONT lf;
    HFONT hFont, hOldFont;
    base = glGenLists(256);

    hDC = wglGetCurrentDC();
    hRC = wglGetCurrentContext();

    //An hDC and an hRC have already been created
    wglMakeCurrent(hDC,hRC);

    // Create a true type font to display
    memset(&lf,0,sizeof(LOGFONT));

    lf.lfHeight = -20;
    lf.lfWeight = FW_NORMAL;
    lf.lfCharSet = ANSI_CHARSET;
    lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
    lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
    lf.lfQuality = DEFAULT_QUALITY;
    lf.lfPitchAndFamily = FF_DONTCARE|DEFAULT_PITCH;

    lstrcpy(lf.lfFaceName,"Arial");

    hFont = CreateFontIndirect(&lf);
    hOldFont = SelectObject(hDC,hFont);

    // Create a set of display lists based on the TrueType font selected
    if(!(wglUseFontOutlines(hDC,0,255,base,0.0f,0.20f, WGL_FONT_POLYGONS,agmf)))
    MessageBox(hWnd,"wglUseFontOutlines failed!","GLFont",MB_OK);
    }

    // Create 3D font. correct = 1 will adjust placement to initial starting point
    GLvoid create3DFont(const char *fmt, int correctX)
    {
    float length = 0;
    unsigned int loop;
    // Holds the text string
    char text[256];
    // Pointer to list of arguments
    va_list ap;

    // If there's no text do nothing
    if (fmt == NULL)
    return;
    // Parse the string for variables
    va_start(ap, fmt);
    // Convert symbols to actual numbers
    vsprintf(text, fmt, ap);
    // Results are stored in text
    va_end(ap);

    if (correctX == 1)
    {
    // loop to find text length
    for (loop=0;loop<(strlen(text));loop++)
    {
    // Increase length by each characters width
    length+=agmf[text[loop]].gmfCellIncX;
    }
    glTranslatef(-length/2,0.0f,0.0f);
    }

    glPushAttrib(GL_LIST_BIT);
    // Indicates the start of display lists for the glyphs
    glListBase(base);
    // Draw the characters in a string
    glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);
    glPopAttrib();

    if (correctX == 1)
    glTranslatef(-length/2,0.0,0.0);
    }

    ____________________________________________

    I am utilizing the create3DFont("TEXT TO DISPLAY", int) -function.

  2. #2
    Junior Member Regular Contributor
    Join Date
    May 2003
    Location
    Germany
    Posts
    232

    Re: How can I change the color of my text?

    Just use glColor3f (or one of the other params like ub) before you draw your text.This should work fine.

  3. #3
    Junior Member Newbie
    Join Date
    Sep 2003
    Location
    Oslo, Norway
    Posts
    2

    Re: How can I change the color of my text?

    Hi
    Thanks for your answer!!
    I guess I forgot to say that I've enabled GL_COLOR_MATERIAL and GL_COLR_LOGIC_OP, and my code already includes:
    glColor3f(1.0, 0, 0) to display red text. Adding this line, changes the color of for instance a glutSolidCube that I'm drawing, but NOT the text...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •