• Advertisement

Archived

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

Lesson 17: OpenGL & SDL Ortho View

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

Hi,I am using Lesson 17 for my font engine in my 2D OpenGL engine which is underconstruction. I used the similiar code the NeHe used but my printed out fonts appear backwards and upside down. Here is all my code, which should make it simpler to catch the bug. MAIN.CPP
          
#include <SDL.h>
#include <SDL_opengl.h>	
#include <gl\glu.h>

#include "glSurface.h"
#include "glGraphics.h"

int main(int argc, char** argv)
{
	glGraphics Graphics;
	Graphics.Initialize("2D OpenGL Engine");
	Graphics.CreateFont("verdana.tga");

for(int i=0;i<=200;i++)
	{
		Graphics.Render(true);
			Graphics.Print("2D OpenGL Engine",5,5,0);
		Graphics.Render(false);
	}
	Graphics.KillFont();
	Graphics.Terminate();


return 0;
}
  

glEngine.CPP
      
      
/*	glGraphics Class
	May 30, 2002
	Thomas Szafran */
#include <fstream.h>
#include <cmath>
using namespace std;
#include <stdio.h>

class glGraphics
{
private:
	//Required Font Declarations.

	glSurface *Font_Surface;
	GLuint Base;
	//Screen Declarations.

	short ScreenWidth;
	short ScreenHeight;

public:
	void Initialize(char *WindowCaption);
	void Blit(glSurface *Surface, short XPos, short YPos);
	void Render(bool Process);
	void Terminate(void);
	void CreateFont(char *Filename);
	void Print(char *Message, short X, short Y, short Set);
	void KillFont(void);
};

//---------------------------------------------------------------------

void glGraphics::Initialize(char *WindowCaption)
{
//Configuration file loading.

	 ifstream Config("config.txt");

	//Load Configuration file parameters.

	 short FullScreen, Width, Height, BPP; 
	  Config>>FullScreen;
	  Config>>Width;  ScreenWidth= Width;
	  Config>>Height; ScreenHeight=Height;
	  Config>>BPP;
	  Config.close();

	//Initialize SDL Video.

     SDL_Init(SDL_INIT_VIDEO);

	//Setup OpenGL require attributes.

	 SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
	 SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
	 SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
	 SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
	
	//Setupd VideoMode flags based on FullScreen Parameter.

	 int Flags;
	 if(FullScreen)
		{Flags=SDL_OPENGL|SDL_FULLSCREEN;}
	 else
		{Flags=SDL_OPENGL;}

	//Set VideoMode and check for errors.

	 SDL_SetVideoMode(Width, Height, BPP, Flags);

	//Set Window Caption

	 SDL_WM_SetCaption(WindowCaption,NULL);

	//Set GLClear color to BLACK.

	 glClearColor(0, 0, 0, 0);
	//Set ViewPort Width & Height

	 glViewport(0, 0, Width, Height);
	//Set OpenGL shaing.

	 glShadeModel(GL_SMOOTH);
	//Disable OpenGL Depth Testing 

	 glDisable(GL_DEPTH_TEST);
	//Enable 2D Texturing

	 glEnable(GL_TEXTURE_2D);
    //Set Matrix Mode

	 glMatrixMode(GL_PROJECTION);
	//Clear 

	 glLoadIdentity();
    //Set Ortho Mode

	 glOrtho(0.0f,Width,Height,0,-1.0f,1.0f);
	//Init DevIL library.

	 ilInit();
}
//---------------------------------------------------------------------

void glGraphics::Blit(glSurface *Surface, short XPos, short YPos)
{
//Enable alpha-blending(opacity) if needed.

	if(Surface->Opacity!=100)
		{	glColor4f(1.0f,1.0f,1.0f,float((Surface->Opacity*.01)));
			glBlendFunc(GL_SRC_ALPHA,GL_ONE);
			glEnable(GL_BLEND);
		}
//Enable transparency if needed.

	if(Surface->Transparent)
		{
		glEnable(GL_ALPHA_TEST);
		glAlphaFunc(GL_GREATER, 0.25);
		}

	//Bind texture & create quad based on surface width & height.

	glBindTexture(GL_TEXTURE_2D, Surface->Pixels);
	
	glBegin(GL_QUADS);
		glTexCoord2f(0, 1.0f);    glVertex2s(XPos,YPos); //Top Left

		glTexCoord2f(0, 0);       glVertex2s(XPos,(YPos+Surface->Height)); //Bottom Left

		glTexCoord2f(1.0f, 0);    glVertex2s((XPos+Surface->Width), (YPos+Surface->Height)); //Bottom Right

		glTexCoord2f(1.0f, 1.0f); glVertex2s((XPos+Surface->Width), YPos); //Top Right

	glEnd();

//Disable blending(opacity) if needed.

	if(Surface->Opacity!=100){glDisable(GL_BLEND);}
//Disable transparency if needed.

	if(Surface->Transparent){glDisable(GL_ALPHA_TEST);}
}
//---------------------------------------------------------------------

void glGraphics::Render(bool Process)
{
	//Start render.

	if(Process)
		{glClear(GL_COLOR_BUFFER_BIT);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();}
	//End render.

	else if(!Process)
		{SDL_GL_SwapBuffers();}
}
//---------------------------------------------------------------------

void glGraphics::Terminate(void)
{
	ilShutDown();
	SDL_Quit();
}
//---------------------------------------------------------------------

void glGraphics::CreateFont(char *Filename)
{
	Font_Surface=new glSurface;
	glLoadBitmap(Font_Surface,Filename);
    
	register float Cx;
	register float Cy;
	
	Base=glGenLists(256);						
	glBindTexture(GL_TEXTURE_2D, Font_Surface->Pixels);

	register short  i;

	for (i=0; i<256; i++)
	{
		Cx=float(i%16)/16.0f;
		Cy=float(i/16)/16.0f;

		glNewList(Base+i,GL_COMPILE);

		glBegin(GL_QUADS);
			glTexCoord2f(Cx,1-Cy-0.0625f);		
			glVertex2i(0,0);

			glTexCoord2f(Cx+0.0625f,1-Cy-0.0625f);	
			glVertex2i(16,0);

			glTexCoord2f(Cx+0.0625f,1-Cy);			
			glVertex2i(16,16);	
				
			glTexCoord2f(Cx,1-Cy);			
			glVertex2i(0,16);		
		glEnd();

		glTranslated(10,0,0);
		glEndList();
	}
}
//---------------------------------------------------------------------

void glGraphics::Print(char *Message, short X, short Y, short Set)
{
glTranslated(X,Y,0);

	glBindTexture(GL_TEXTURE_2D, Font_Surface->Pixels);
	glListBase(Base-32+(128*Set));
	glCallLists(strlen(Message),GL_BYTE,Message);
}
//---------------------------------------------------------------------

void glGraphics::KillFont(void)
{
	glDeleteLists(Base,256);
	glFreeSurface(Font_Surface);
}
//---------------------------------------------------------------------


        
[edited by - tszafran on June 28, 2002 1:02:46 PM]

Share this post


Link to post
Share on other sites
Advertisement
Try changing this:
glOrtho(0.0f,ScreenWidth,ScreenHeight,0,-1.0f,1.0f);

Into this:
glOrtho(0,ScreenWidth,0,ScreenHeight,-1,1);

Share this post


Link to post
Share on other sites
Thanks now it works, but I dont understand why. Also why do I have to change my matrix mode and everything if I already have it set to something similiar.

Share this post


Link to post
Share on other sites
Who says you have to? I have not experimented with it but try eliminating the extra Ortho call. It is in there because it was meant to work with a 3d environment. Ortho projection makes it easier to place text. If you already have one setup, try omitting the Print glOrtho call (thereby eliminating a Push/Pull Matrix, and a glMatrixMode call).

EDIT: Oh yeah, and I am not sure why it works one way and not the other way. It probably has something to do with the way you build the font. I just figured it out because I tried switching those two parameters and my text got screwed up.

[edited by - Erunama on June 28, 2002 11:32:17 AM]

Share this post


Link to post
Share on other sites
quote:

Try changing this:
glOrtho(0.0f,ScreenWidth,ScreenHeight,0,-1.0f,1.0f);

Into this:
glOrtho(0,ScreenWidth,0,ScreenHeight,-1,1);



It prints fine but my screen coordinates are reversed. I am used to having 0,0 to be the top left of my screen, not the bottom left of my screen. How would I change my code to represent that and still print my text properly.

PS.
Also after I write some text my blitting function no longer works properly, its blitting position is set to the last text print out position, so if I print out text at 50,50 then my next blit call (Blit(My_Surface, 0, 0); doesnt go to 0,0 but rather 50,50.

Share this post


Link to post
Share on other sites
If you don''t understand why some code change fixed your problem you should read the docs about the function that you changed the arguments to.

There''s a description for all of the OpenGL functions in MSDN.


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share this post


Link to post
Share on other sites

  • Advertisement