MIKE'S COOL PAGE

Right I have been inspired, the following is a small list of my more useful openGL code fragments.

I have a very dodgy C/C++ compiler, not to be sued it shall not be named, but it wouldn't let me use any of the nice openGL libraries, like GLUT to name one. So I had to create or modify existing functions that didn't use calls to these. Anyway a list follows:

Placing Text On Screen
Loading Bitmaps
Playing Sound Files And Video Files

Placing Text On Screen

The following is code that will build any font that you have installed on your system, the text is written to the screen using the glPrint() command, positioned on the screen using the glTranslatef() command, and resized by using glScalef(). The BuildFont() command should be called in your OpenGL initialization function, and KillFont() when your clearing up.

I suggest that you copy the following code into a header file.

#include <stdlib.h>
#include <stdio.h>
#include <gl/glu.h>

GLuint nFontList;

/******************
* Delete fonts
*
******************/
GLvoid KillFont(GLvoid) // Delete The Font List
{
glDeleteLists(nFontList, 128); // Delete All 96 Characters
}

/******************
* Print Text
*
******************/

GLvoid glPrint(const char *fmt, ...) // Custom GL "Print" Routine
{
char text[256]; // Holds Our String
va_list ap; // Pointer To List Of Arguments

if (fmt == NULL) // If There's No Text
return; // Do Nothing

va_start(ap, fmt); // Parses The String For Variables
vsprintf(text, fmt, ap); // And Converts Symbols To Actual Numbers
va_end(ap); // Results Are Stored In Text

glPushAttrib(GL_LIST_BIT); // Pushes The Display List Bits
glListBase(nFontList); // Sets The Base Character

glCallLists(strlen(text), GL_UNSIGNED_BYTE, text); // Draws The Display List Text
glPopAttrib(); // Pops The Display List Bits
}

/******************
* Build Font
*
******************/

void buildfont(HDC hDC, const char *font)
{

LOGFONT logfont;
HFONT hFont;
GLYPHMETRICSFLOAT gmf[129];

logfont.lfHeight = -12; // setup font characteristics
logfont.lfWidth = 0;
logfont.lfEscapement = 0;
logfont.lfOrientation = 0;
logfont.lfWeight = FW_NORMAL;
logfont.lfItalic = FALSE;
logfont.lfUnderline = FALSE;
logfont.lfStrikeOut = FALSE;
logfont.lfCharSet = ANSI_CHARSET;
logfont.lfOutPrecision = OUT_DEFAULT_PRECIS;
logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
logfont.lfQuality = PROOF_QUALITY;
logfont.lfPitchAndFamily = DEFAULT_PITCH || FF_ROMAN;
strcpy(logfont.lfFaceName,font);


hFont = CreateFontIndirect(&logfont);
SelectObject (hDC, hFont);
nFontList = glGenLists(128);

wglUseFontOutlines(hDC, 0, 128, nFontList, 0.0f, 0.2f, WGL_FONT_POLYGONS, gmf);
DeleteObject(hFont);
}



Loading Bitmaps


The following is a piece of code that loads a bitmap into OpenGL, to load a file, simply call LoadBitmap(). The parameters translate to the location of the bitmap file, the ID that you wish to refer to it as, the width of the bitmap and the height of the bitmap.

int LoadBitmap(LPTSTR szFileName, GLuint &texid, int width, int height)
// Creates Texture From A Bitmap File
{
unsigned char tex[width*height*3 + 18*3]; //array to hold data input
unsigned char tex2[width*height*3]; //array to hold texture data
unsigned char temp; //swap variable
int num; //loop variable
FILE * f; //open the bitmap for reading

if ((f=fopen(szFileName,"r"))== NULL)
{return 1;}



fread(tex,width*height*3 + 18*3,1,f); //read data from bitmap

fclose(f); //close the file

for (num = 18*3-1; num < width*height*3 +18*3; num = num + 1)
{
//remove the first 18 pixels, RGB code = 3 bytes
tex2[num - 18*3] = tex[num];
}



for (num = 0; num < width*height*3; num = num + 3)
{
//bitmap backwards so change BGR code to RGB code
//by swaping the every third bit with evert first.
temp = tex2[num];
tex2[num] = tex2[num+2];
tex2[num +2] = temp;
}


glBindTexture(GL_TEXTURE_2D, texid); //select ID to store the texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0,3, width ,height, 0,GL_RGB, GL_UNSIGNED_BYTE, tex2);
//produce the texture
return 0;
}


Playing Sound Files And Video Files


This Code I love so much, I found it in my VB programming days and converted it to c. It allows any file that windows media player can play to be played in your program.

I won't give you all of the MCI functions that there are, as there are alot of them, but i will give enought to whet your appetite. Basically you open the file, then you can play it however you like and close it.


For More info please consult the MSDN Library's page on the topic Multimedia Command Strings, Each can be sent to the mcisendString command.

int closefile()
{
long errcode;

errcode = mciSendString("Close mediafile", NULL, 0, 0);
return 0;
}


int loadfile()
{
long errcode;

errcode = mciSendString("open C:\sample.mid alias mediafile", NULL, 0, 0);

return int(errcode);
}

int playfile()
{
long errcode;

errcode = mciSendString("Play mediafile", "", 0, 0);

if (!(errcode == 0))
return 1;

return 0;
}



Thanks for visiting mccann.tk