PDA

View Full Version : How can I change the color of my text?



Margrete
09-14-2003, 11:29 PM
Hi everyone http://www.opengl.org/discussion_boards/ubb/smile.gif
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 http://www.opengl.org/discussion_boards/ubb/smile.gif

-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.

PanzerSchreck
09-14-2003, 11:41 PM
Just use glColor3f (or one of the other params like ub) before you draw your text.This should work fine.

Margrete
09-14-2003, 11:51 PM
Hi http://www.opengl.org/discussion_boards/ubb/smile.gif
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...