Archived

This topic is now archived and is closed to further replies.

2D Text in a 3D application

This topic is 5149 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Exactly how can I print 2d text in my 3d application? I just want to print on the upper right corner of my window , the rotation degrees of my object. Thanks

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
There are some lessons on nehe.gamedev.net and lighthouse3d.com

Share this post


Link to post
Share on other sites
i feel like giving away my source now... so here you go:



#ifndef CON_H

#define CON_H

#include "textures.h"
#include "vectors.h"

class CConsole
{
public:

CConsole();

int LoadFont(char* fname);
void Destroy();

void TextOut(int x, int y, int size, const char* strString, ...);
void SelectFont(int i);
void SetColor(float r, float g, float b, float a);

private:

void DrawChar(unsigned char c, float x, float y, int size);

CTexture fonts[4];
CVector4 fontColor;
int curFont;
int numfonts;
};

extern CConsole Console;

#endif




#include "stdafx.h"

#include "opengl.h"
#include "console.h"
#include "vectors.h"
#include "config.h"
#include "log.h"

CConsole Console;

CConsole::CConsole()
{
numfonts = 0;
}

void CConsole::SetColor(float r, float g, float b, float a)
{
fontColor.x = r;
fontColor.y = g;
fontColor.z = b;
fontColor.w = a;
}

int CConsole::LoadFont(char *fname)
{
lprintf(" * Loading Console * \n");
if (fonts[numfonts++].LoadFromFile(fname)) return SYS_ERR;
return SYS_OK;
}

void CConsole::SelectFont(int i)
{
curFont = i;
}

void CConsole::DrawChar(unsigned char c, float x, float y, int size)
{
float cPosX, cPosY;

float texSz = 256.0f;

float origX = 0;
float origY = 0;
float sizeX = texSz / 16;
float sizeY = texSz / 16;

y = y + (texSz / 16);

cPosX = (c % 16) * (texSz / 16);
cPosY = (c / 16) * (texSz / 16);

CVector2 Vertices[4];
CVector2 TexCoords[4];

TexCoords[3].x = (cPosX + origX) / texSz; TexCoords[3].y = (texSz - cPosY - origY) / texSz;
TexCoords[2].x = (cPosX + origX + sizeX) / texSz; TexCoords[2].y = (texSz - cPosY - origY) / texSz;
TexCoords[1].x = (cPosX + origX + sizeX) / texSz; TexCoords[1].y = (texSz - cPosY - origY - sizeY) / texSz;
TexCoords[0].x = (cPosX + origX) / texSz; TexCoords[0].y = (texSz - cPosY - origY - sizeY) / texSz;

Vertices[0].x = x; Vertices[0].y = y;
Vertices[1].x = x + size; Vertices[1].y = y;
Vertices[2].x = x + size; Vertices[2].y = y - size;
Vertices[3].x = x; Vertices[3].y = y - size;

GL.SetVertexPointer(2, GL_FLOAT, 0, Vertices);
GL.SetTexCoordPointer(2, GL_FLOAT, 0, TexCoords);

GL.DrawArrays(GL_QUADS, 0, 4);
}


void CConsole::TextOut(int x, int y, int size, const char *strString, ...)
{
char strText[256];
va_list argumentPtr;
float curColor[4];

if (strString == NULL)
return;

va_start(argumentPtr, strString);

vsprintf(strText, strString, argumentPtr);

va_end(argumentPtr);

GL.BindBuffer(GL_ARRAY_BUFFER_ARB, 0);

GL.MatrixMode( GL_MODELVIEW );
GL.PushMatrix();
GL.LoadIdentity();

GL.MatrixMode(GL_PROJECTION);
GL.PushMatrix();
GL.Ortho2DProjection(0, curConfig.scrWidth, curConfig.scrHeight, 0);

GL.Disable(GL_DEPTH_TEST);
GL.Disable(GL_CULL_FACE);
GL.Disable(GL_LIGHTING);

GL.Enable(GL_BLEND);
GL.BlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

GL.Enable(GL_ALPHA_TEST);
GL.AlphaFunc(GL_GREATER, 0.0f);

GL.Enable(GL_TEXTURE_2D);
GL.BindTexture(GL_TEXTURE_2D, fonts[curFont].GetName());

GL.Get(GL_CURRENT_COLOR, curColor);
GL.Color(1.0f, 1.0f, 1.0f);
// GL.Color(fontColor.x, fontColor.y, fontColor.z, fontColor.w);

GL.EnableClientState(GL_VERTEX_ARRAY);
GL.EnableClientState(GL_TEXTURE_COORD_ARRAY);

int i = 0;
while (strText[i++] != 0)
{
DrawChar(strText[i - 1], x + i * size, y + size / 2, size);
}

GL.DisableClientState(GL_VERTEX_ARRAY);
GL.DisableClientState(GL_TEXTURE_COORD_ARRAY);

GL.Color(curColor[0], curColor[1], curColor[2], curColor[3]);

GL.Disable(GL_BLEND);
GL.Disable(GL_ALPHA_TEST);

GL.Enable(GL_DEPTH_TEST);
GL.Enable(GL_CULL_FACE);

GL.MatrixMode(GL_PROJECTION);
GL.PopMatrix();
GL.MatrixMode( GL_MODELVIEW );
GL.PopMatrix();
}


void CConsole::Destroy()
{
for (int i = 0; i < numfonts; i++)
fonts[i].Destroy();
}


have fun!

[ My Site ]
All your source are belong to us
/*ilici*/

Share this post


Link to post
Share on other sites