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


OpenGL basics - background / text

Recommended Posts

KevArnold    122
I am trying to learn OpenGL and am going through some basic functions in order to learn, I have the following function to draw a background:
void DrawBackground()

	// Reset the current matrix to our identify matrix

	//Pass in our 2D ortho screen so (left, right, bottom, top).  The last
	// 2 parameters are the near and far planes.
	glOrtho( 0, 500, 500, 0, 0, 1 );	


	// Bind the mask texture to our new 2D quad
	glBindTexture(GL_TEXTURE_2D,  g_Texture[0]);

	// Display a 2D quad with the scope/cross hair mask
		// Notice that when we are in ortho mode, we use glVertex2f() to pass
		// in screen coordinates, not vertices.  This makes it incredibly easy
		// to put up 2D interface art.  It's just like doing 2D graphics.
		// The texture coordinate stay the same regardless.

		// Display the top left point of the 2D image
		glTexCoord2f(0.0f, 1.0f);	glVertex2f(0, 0);

		// Display the bottom left point of the 2D image
		glTexCoord2f(0.0f, 0.0f);	glVertex2f(0, 500);

		// Display the bottom right point of the 2D image
		glTexCoord2f(1.0f, 0.0f);	glVertex2f(500, 500);

		// Display the top right point of the 2D image
		glTexCoord2f(1.0f, 1.0f);	glVertex2f(500, 0);

	// Stop drawing 

The window is 500x500 but the image is 320x320. I was under the impression that the image size did nto matter, but this only shows part of the background image, not all of it. I also am using a function I found on this site to draw text..
void DrawText()



	glPrint("Total: %d - Modifier: %f", total, modifier);


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(base - 32);								// Sets The Base Character to 32
	glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);	// Draws The Display List Text
	glPopAttrib();										// Pops The Display List Bits
If you notice in DrawText I use the coordinates glRasterPos2f(-0.18f,-0.18f); to set the position to the bottom left of the window. Why isn't 0.1 or 0.9 the correct location? thanks kev [edited by - KevArnold on April 30, 2003 12:47:53 PM]

Share this post

Link to post
Share on other sites
Yann L    1802

The window is 500x500 but the image is 320x320.

320*320 is an invalid size for a (normal) texture, as it is not a power of two. How do you create your texture image ? Are you resizing the 320 onto something valid (eg. 256*256, or 512*512), or perhaps cutting of the excess ?

Share this post

Link to post
Share on other sites
KevArnold    122
I made the image 128x128 and am still having the same problem, it only shows the middle part of it. It is a BMP I created using paint. The image is loaded with the following function:

void CreateTexture(UINT textureArray[], LPSTR strFileName, int textureID)
AUX_RGBImageRec *pBitmap = NULL;

if(!strFileName) // Return from the function if no file name was passed in

// We need to load the texture data, so we use a cool API that the glaux.lib offers.

pBitmap = auxDIBImageLoad(strFileName); // Load the bitmap and store the data

if(pBitmap == NULL) // If we can''t load the file, quit!

// Generate a texture with the associative texture ID stored in the array
glGenTextures(1, &textureArray[textureID]);

// Bind the texture to the texture arrays index and init the texture
glBindTexture(GL_TEXTURE_2D, textureArray[textureID]);

// Build Mipmaps (builds different versions of the picture for distances - looks better)
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pBitmap->sizeX, pBitmap->sizeY, GL_RGB, GL_UNSIGNED_BYTE, pBitmap->data);

// Lastly, we need to tell OpenGL the quality of our texture map. GL_LINEAR_MIPMAP_LINEAR
// is the smoothest. GL_LINEAR_MIPMAP_NEAREST is faster than GL_LINEAR_MIPMAP_LINEAR,
// but looks blochy and pixilated. Good for slower computers though.


// Now we need to free the bitmap data that we loaded since openGL stored it as a texture

if (pBitmap) // If we loaded the bitmap
if (pBitmap->data) // If there is texture data
free(pBitmap->data); // Free the texture data, we don''t need it anymore

free(pBitmap); // Free the bitmap structure


Share this post

Link to post
Share on other sites