Archived

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

Lesson 17: Out of ideas

This topic is 4944 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

I''ve been trying to get lesson 17''s texture fonts to work in a program of mine. In fact, I''ve been trying to get the texture fonts to work for about four hours, without the slightest alteration in the output. Rather than displaying text on the screen, it displays a blank, colored rectangle where I told it to put the text. Yes, I''ve disabled GL_TEXTURE_2D. Yes, the font is in the right directory. Yes, the images size is a power of 2. I''ve even gone as far as to copy code word for word from programs where it works flawlessly, only to have the same mocking white rectangle staring me in the face. Other textures load fine. I simply can''t think of anything else to try. here''s all the relevant source I could think of. If you need anything else, say so. Please.
// Includes

#ifdef WIN32													// If We''re Under MSVC

#include <windows.h>											// Include The Windows Header

#include <winuser.h>
#else															// Otherwise

#include <stdio.h>												// Include The Standar IO Header

#include <stdlib.h>												// And The Standard Lib (for exit())

#include <stdarg.h>
#endif															// Then...


#include <math.h>												// We Require The Math Lib For Sin and Cos

#include <GL/gl.h>												// And Obviously The OpenGL Header

#include <GL/glu.h>												// And The GLu Heander

#include <GL/glaux.h>
#include <cctype>

#include <SDL.h>												// Finally: The SDL Header!

#include <assert.h>

#include "main.h"												// We''re Including theHeader Where Defs And Prototypes Are


#ifdef WIN32													// If We''re Under MSVC

#pragma comment( lib, "OpenGL32.lib" )							// We Can Tell The Linker To Look For OpenGl32.lib ...

#pragma comment( lib, "GLu32.lib" )								// ...GLu32.lib ...

#pragma comment( lib, "SDLmain.lib" )							// ...SDLmain.lib ...

#pragma comment( lib, "SDL.lib" )								// And SDL.lib At Link Time

#endif			


extern S_AppStatus AppStatus;									// We''re Using This Struct As A Repository For The Application State (Visible, Focus, ecc)


// Graphical Variables

GLUquadricObj *quadratic;

GLfloat lightposition[]={-1.0f, 3.0f, 0.0f, 1.0f};
GLfloat LightDiffuse[]=	{ 1.0f, 1.0f, 1.0f, 1.0f};

int list[256];				// Display list for shot particles

GLuint	texture[4];			// Storage for textures

GLuint	base;				// Base Display List For The Font



//Game-related variables

Controller controller = Controller();
Player player = Player();
Starfield starfield = Starfield();

// Code

GLvoid BuildLists() {
	list[0]=glGenLists(3);
	glNewList(list[0], GL_COMPILE);
		glBindTexture(GL_TEXTURE_2D, texture[1]);
		glBegin(GL_TRIANGLE_STRIP);
			glTexCoord2f(0,1);
			glVertex3f(-0.05f, 0.05f, 0.0f);
			glTexCoord2f(1,1);
			glVertex3f( 0.05f, 0.05f, 0.0f);
			glTexCoord2f(0,0);
			glVertex3f(-0.05f,-0.05f, 0.0f);
			glTexCoord2f(1,0);
			glVertex3f( 0.05f,-0.05f, 0.0f);
		glEnd();
	glEndList();
	list[1] = list[0] + 1;
	glNewList(list[1], GL_COMPILE);
		glBindTexture(GL_TEXTURE_2D, texture[2]);
		glBegin(GL_TRIANGLE_STRIP);
			glTexCoord2f(0,1);
			glVertex3f(-0.06f, 0.06f, 0.0f);
			glTexCoord2f(1,1);
			glVertex3f( 0.06f, 0.06f, 0.0f);
			glTexCoord2f(0,0);
			glVertex3f(-0.06f,-0.06f, 0.0f);
			glTexCoord2f(1,0);
			glVertex3f( 0.06f,-0.06f, 0.0f);
		glEnd();
	glEndList();
	list[2] = list[1] + 1;
	glNewList(list[2], GL_COMPILE);
		glBindTexture(GL_TEXTURE_2D, texture[3]);
		glBegin(GL_TRIANGLE_STRIP);
			glTexCoord2f(0,1);
			glVertex3f(-0.06f, 0.06f, 0.0f);
			glTexCoord2f(1,1);
			glVertex3f( 0.06f, 0.06f, 0.0f);
			glTexCoord2f(0,0);
			glVertex3f(-0.06f,-0.06f, 0.0f);
			glTexCoord2f(1,0);
			glVertex3f( 0.06f,-0.06f, 0.0f);
		glEnd();
	glEndList();
}



AUX_RGBImageRec *LoadBMP(char *Filename)				// Loads A Bitmap Image

{
	FILE *File=NULL;									// File Handle


	if (!Filename)										// Make Sure A Filename Was Given

	{
		return NULL;									// If Not Return NULL

	}

	File=fopen(Filename,"r");							// Check To See If The File Exists


	if (File)											// Does The File Exist?

	{
		fclose(File);									// Close The Handle

		return auxDIBImageLoad(Filename);				// Load The Bitmap And Return A Pointer

	}

	return NULL;										// If Load Failed Return NULL

}

int LoadGLTextures()									// Load Bitmaps And Convert To Textures

{
	int Status=FALSE;									// Status Indicator


	AUX_RGBImageRec *TextureImage[4];					// Create Storage Space For The Textures


	memset(TextureImage,0,sizeof(void *)*4);           	// Set The Pointer To NULL


	// Load The Bitmap, Check For Errors, If Bitmap''s Not Found Quit

	if ((TextureImage[0]=LoadBMP("data/Font.bmp")) && 
		(TextureImage[1]=LoadBMP("data/shot0.bmp")) && 
		(TextureImage[2]=LoadBMP("data/shot1.bmp")) &&
		(TextureImage[3]=LoadBMP("data/shot2.bmp")))			// Load Bullet Texture

	{
		Status=TRUE;                                    // Set The Status To TRUE

		glGenTextures(4, &texture[0]);                  // Create One Textures


		for (int loop1=0; loop1<4; loop1++)			// Loop Through 5 Textures

		{
			glBindTexture(GL_TEXTURE_2D, texture[loop1]);
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
			glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[loop1]->sizeX, TextureImage[loop1]->sizeY, 0,
				GL_RGB, GL_UNSIGNED_BYTE, TextureImage[loop1]->data);
		}
		for (loop1=0; loop1<4; loop1++)				// Loop Through 5 Textures

		{
			if (TextureImage[loop1])			// If Texture Exists

			{
				if (TextureImage[loop1]->data)		// If Texture Image Exists

				{
					free(TextureImage[loop1]->data);// Free The Texture Image Memory

				}
				free(TextureImage[loop1]);		// Free The Image Structure

			}
		}
	}
	return Status;										// Return The Status

}

GLvoid BuildFont(GLvoid)								// Build Our Font Display List

{
	float	cx;											// Holds Our X Character Coord

	float	cy;											// Holds Our Y Character Coord


	base=glGenLists(256);								// Creating 256 Display Lists

	glBindTexture(GL_TEXTURE_2D, texture[0]);			// Select Our Font Texture

	for (int loop=0; loop<256; loop++)						// Loop Through All 256 Lists

	{
		cx=float(loop%16)/16.0f;						// X Position Of Current Character

		cy=float(loop/16)/16.0f;						// Y Position Of Current Character


		glNewList(base+loop,GL_COMPILE);				// Start Building A List

			glBegin(GL_QUADS);							// Use A Quad For Each Character

				glTexCoord2f(cx,1-cy-0.0625f);			// Texture Coord (Bottom Left)

				glVertex2i(0,0);						// Vertex Coord (Bottom Left)

				glTexCoord2f(cx+0.08f,1-cy-0.0625f);	// Texture Coord (Bottom Right)

				glVertex2i(16,0);						// Vertex Coord (Bottom Right)

				glTexCoord2f(cx+0.08f,1-cy);			// Texture Coord (Top Right)

				glVertex2i(16,16);						// Vertex Coord (Top Right)

				glTexCoord2f(cx,1-cy);					// Texture Coord (Top Left)

				glVertex2i(0,16);						// Vertex Coord (Top Left)

			glEnd();									// Done Building Our Quad (Character)

			glTranslated(10,0,0);						// Move To The Right Of The Character

		glEndList();									// Done Building The Display List

	}													// Loop Until All 256 Are Built

}

GLvoid KillFont(GLvoid)									// Delete The Font From Memory

{
	glDeleteLists(base,256);							// Delete All 256 Display Lists

}

GLvoid glPrint(GLint x, GLint y, int set, char *string, ...)	// Where The Printing Happens

{
	char		text[256];				// Holds Our String

	if (set>1)
	{
		set=1;
	}
	va_list	ap;										// Pointer To List Of Arguments

	va_start(ap, string);					// Parses The String For Variables

	    vsprintf(text, string, ap);				// And Converts Symbols To Actual Numbers

	va_end(ap);						// Results Are Stored In Text


	glBindTexture(GL_TEXTURE_2D, texture[0]);			// Select Our Font Texture

	glDisable(GL_DEPTH_TEST);							// Disables Depth Testing

	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix

	glPushMatrix();										// Store The Projection Matrix

	glLoadIdentity();									// Reset The Projection Matrix

	glOrtho(0,640,0,480,-1,1);							// Set Up An Ortho Screen

	glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix

	glPushMatrix();										// Store The Modelview Matrix

	glLoadIdentity();									// Reset The Modelview Matrix

	glTranslated(x,y,0);								// Position The Text (0,0 - Bottom Left)

	glListBase(base-32+(128*set));						// Choose The Font Set (0 or 1)

	glCallLists(strlen(string),GL_BYTE,text);			// Write The Text To The Screen

	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix

	glPopMatrix();										// Restore The Old Projection Matrix

	glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix

	glPopMatrix();										// Restore The Old Projection Matrix

	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing

}

bool InitGL(SDL_Surface *S)										// All Setup For OpenGL Goes Here

{
	srand(GetTickCount());
	if (!LoadGLTextures())								// Jump To Texture Loading Routine

	{
		return FALSE;									// If Texture Didn''t Load Return FALSE

	}
	BuildFont();										// Build The Font

	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading

	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);				// Black Background

	glClearDepth(1.0f);									// Depth Buffer Setup

	quadratic=gluNewQuadric();							// Create A Pointer To The Quadric Object

	gluQuadricNormals(quadratic, GLU_SMOOTH);			// Create Smooth Normals

	gluQuadricTexture(quadratic, GL_TRUE);				// Create Texture Coords

	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations

	glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
	glLightfv(GL_LIGHT1, GL_POSITION, lightposition);
	glDepthFunc (GL_LEQUAL);							// The Type Of Depth Testing (Less)

	glBlendFunc(GL_SRC_ALPHA,GL_ONE);					// Select The Type Of Blending

	glEnable(GL_COLOR_MATERIAL);
	glEnable(GL_LIGHT1);
	glEnable(GL_LIGHTING);
	LoadGLTextures();
	glEnable(GL_TEXTURE_2D);							// Enable 2D Texture Mapping

//	Create_Village();

	BuildLists();
	return(true);										// Initialization Went OK

}

/* --some other code-- */
//and then:



void Draw(SDL_Surface *Screen)									// Our Drawing Code

{
	assert(Screen);												// We won''t go on if the Screen pointer is invalid


	Draw3D(Screen);												// We split our drawing code into two pieces:

	Draw2D(Screen);												// First draw the 3d stuff, then blit 2d surfaces onto it


	return;
}

void Draw3D(SDL_Surface *S)										// OpenGL drawing code here

{
	glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// Clear Screen And Depth Buffer

	glLoadIdentity();											// Reset The Modelview Matrix

	player.render();
	controller.getLevel(controller.getThisLevel())->render();
	starfield.render();
	glFlush();													// Flush The GL Rendering Pipelines

	return;
}

void Draw2D(SDL_Surface *S)										// SDL drawing code here

{
	static SDL_Rect src1={0,0,0,0},								// We''re blitting 3 rectangles,

					src2={0,0,0,0},								// so we have to prepare them

					src3={0,0,0,0};

	SDL_FillRect(S, &src1, SDL_MapRGBA(S->format,0,0,0,0));
																// That''s an issue many people is having!

	glDisable(GL_LIGHTING);
	glEnable(GL_BLEND);
	glDisable(GL_TEXTURE_2D);							// Enable 2D Texture Mapping

	glLoadIdentity();
	glColor3f(0.0f, 1.0f, 1.0f);
																// We set up our Alpha Channel first!

	glPrint(0,464,1,"Shield:%2.0f",GLfloat(player.getShield()));
	glEnable(GL_LIGHTING);
	glDisable(GL_BLEND);
																

	return;														// We''re Always Making Functions Return

}
Can anyone figure out what I''m doing wrong? Anyone?

Share this post


Link to post
Share on other sites
2 things..

quote:
, I''ve disabled GL_TEXTURE_2D.


Maybe I am just too tired to understand this. So, why do you disable GL_TEXTURE_2D when you try to display a texture?

quote:
glCallLists(strlen(string),GL_BYTE,text);


You are displaying the "text" with the length of (string). should read:

glCallLists(strlen(text),GL_BYTE,text);


Share this post


Link to post
Share on other sites
quote:
Original post by schue
2 things..

quote:
, I''ve disabled GL_TEXTURE_2D.


Maybe I am just too tired to understand this. So, why do you disable GL_TEXTURE_2D when you try to display a texture?

quote:
glCallLists(strlen(string),GL_BYTE,text);


You are displaying the "text" with the length of (string). should read:

glCallLists(strlen(text),GL_BYTE,text);





I disabled GL_TEXTURE_2D because scanning the forums for people with similar problems seemed to suggest that it was the right thing to do. I''ve tried it with and without that line.

As for the glCallLists, you raise a valid point, but it still doesn''t render the text when I fix it. THanks, though.

Share this post


Link to post
Share on other sites
quote:
Original post by zebulon
quote:
Original post by schue
2 things..

quote:
, I''ve disabled GL_TEXTURE_2D.


Maybe I am just too tired to understand this. So, why do you disable GL_TEXTURE_2D when you try to display a texture?

quote:
glCallLists(strlen(string),GL_BYTE,text);


You are displaying the "text" with the length of (string). should read:

glCallLists(strlen(text),GL_BYTE,text);





I disabled GL_TEXTURE_2D because scanning the forums for people with similar problems seemed to suggest that it was the right thing to do. I''ve tried it with and without that line.

As for the glCallLists, you raise a valid point, but it still doesn''t render the text when I fix it. THanks, though.


Oh.. my God. Sorry thats all I can say after reading this =(

Share this post


Link to post
Share on other sites
quote:
Original post by wAVaRiaN
quote:
Original post by zebulon
quote:
Original post by schue
2 things..

quote:
, I''ve disabled GL_TEXTURE_2D.


Maybe I am just too tired to understand this. So, why do you disable GL_TEXTURE_2D when you try to display a texture?

quote:
glCallLists(strlen(string),GL_BYTE,text);


You are displaying the "text" with the length of (string). should read:

glCallLists(strlen(text),GL_BYTE,text);





I disabled GL_TEXTURE_2D because scanning the forums for people with similar problems seemed to suggest that it was the right thing to do. I''ve tried it with and without that line.

As for the glCallLists, you raise a valid point, but it still doesn''t render the text when I fix it. THanks, though.


Oh.. my God. Sorry thats all I can say after reading this =(


It would have been nice to have appended some kind of explanation to that. That way, I could feel like the n00b I am AND have a solution.
Unless I''m misunderstanding you, that is.

Share this post


Link to post
Share on other sites
Ah HA! I had tried it with and without *that* line of code, but there was a glDisable(GL_TEXTURE_2D) snuck into one of my other functions. Problem solved.

On a side note, does anyone know where I can get some more bitmaps like the font provided with the lesson code?

Share this post


Link to post
Share on other sites
Yes, I think that in NeHe''s download section there is a place where you can get an app that will let you create them

--------------------------------------

It took me long enough, but now I am finally 16. Yay! Sorta funny, but I am probably the only person on earth who wrote a 3D driving game prior to actually driving .

Share this post


Link to post
Share on other sites