PDA

View Full Version : Need help with Bitmap Fonts on a glOrtho Viewport



Erfdain
08-12-2005, 09:33 AM
Okay, with the font header that I have I can draw my text on a gluPerspective Veiwing area. However, when I move to a glOrtho moving area I cannot display fonts. I can not tell where I am messing up. Here is my code excluding the other headers. If you can help me it would be great

#define WIN32_LEAN_AND_MEAN // trim the excess fat from Windows

///// includes
#include <windows.h> // standard Windows app include
#include <gl/gl.h> // standard OpenGL include
#include <gl/glu.h> // OpenGL utilities
#include <gl/glaux.h> // OpenGL auxiliary functions
#include <stdio.h>
#include <stdlib.h>
#include "SetupPixelFormat.h"
#include "SetupLights.h"
#include "FontSetup.h"

////// Global Variables
bool keyPressed[256]; // holds true for keys that are pressed

/////// the camera variables
float cameraX, cameraY, cameraZ; // the camera's location
float lookX, lookY, lookZ; // the camera's target
float upX, upY, upZ;

// this is for the font base
unsigned int listBase;

//HDC g_hDC;

//********************************************
// displayText()
// This function Displays Text to the screen
//********************************************
void displayText(char *str)
{
glPushMatrix();
glLoadIdentity();
//glRasterPos2f(-0.35, 0.0);
glTranslated(10.0, 10.0, 0.0);
glColor3f(1.0 , 1.0, 1.0);
PrintString(listBase, str);
glPopMatrix();

}

//********************************************
// initCamera()
// This function sets the initial setting for the camera
//********************************************
void initCamera()
{
cameraX = 0.0; // cameraX,Y,Z -> variables for the location of the
cameraY = 0.0; // of the camera
cameraZ = 12.0;
lookX = 0.0; // lookX,Y,Z -> variables for where the camera
lookY = 0.0; // is looking at
lookZ = -5.0;
upX = 0.0; // upX,Y,Z -> variables to specify what direction
upY = 1.0; // is up
upZ = 0.0;
}

//**********************************************
// Initialize()
// Function that sets and calls all the initial
// functions and values to start the program
//**********************************************
void Initialize()
{


// clear color to black
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// use smooth shading
glShadeModel(GL_SMOOTH);
// setup the camera
initCamera();
listBase = CreateBitmapFont("Comic Sans MS", 10);
}

// tells the program in what order to draw stuff
void Draw(void)
{

// reset modelview matrix
//glLoadIdentity();


// move the camera to where it needs to be
//glTranslatef(0.0, 0.0, -1.0);
// Setup the camera and place it in the scene
//gluLookAt(cameraX, cameraY, cameraZ, lookX, lookY, lookZ, upX, upY, upZ);

displayText("Displaying Text!");

}
// Setup the ViewPorts
void SetupViewPorts(HWND hwnd)
{
RECT rect; // Holds Coordinates Of A Rectangle

GetClientRect(hwnd, &rect); // Get Window Dimensions
int width=rect.right-rect.left; // Calculate The Width (Right Side-Left Side)
int height=rect.bottom-rect.top; // Calculate The Height (Bottom-Top)
// take the first half of the screen
// but for now fill up the window
/*
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION); // set projection matrix
glLoadIdentity(); // reset projection matrix

// calculate aspect ratio of window
gluPerspective(45.0f, (GLfloat)(width)/(GLfloat)height,1.0f,1000.0f);

glMatrixMode(GL_MODELVIEW); // set modelview matrix
glLoadIdentity(); // reset modelview matrix
Draw();
/*
// Set The Viewport To The Top Right. It Will Take Up Half The Screen Width And Height
glViewport (width/2, height/2, width/2, height/2);
glMatrixMode (GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity (); // Reset The Projection Matrix
// Set Up Perspective Mode To Fit 1/4 The Screen (Size Of A Viewport)
gluPerspective( 45.0, (GLfloat)(width)/(GLfloat)(height), 0.1f, 500.0 );

glMatrixMode (GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity ();
Draw();
*/
// for right now though, just set the ortho to the entire window
// Set The Viewport To The Bottom Right. It Will Take Up Half The Screen Width And Height
//glViewport (0, 0, width, height);
glMatrixMode (GL_PROJECTION); // Select The Projection Matrix
glPushMatrix();
glLoadIdentity (); // Reset The Projection Matrix
// Set Up Ortho Mode To Fit 1/4 The Screen (Size Of A Viewport)
glOrtho(0, width, 0, height, -1.0, 1.0);
glMatrixMode (GL_MODELVIEW); // Select The Modelview Matrix
//SetupLights();
// draw a 2d polygon
glDisable(GL_TEXTURE_2D);
/*
glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 0.0);
glVertex2i(0, 0);
glColor3f(1.0, 0.0, 0.0);
glVertex2i(width, 0);
glColor3f(1.0, 0.0, 0.0);
glVertex2i(width, height);
glColor3f(1.0, 0.0, 0.0);
glVertex2i(0, height);
glEnd();
*/
Draw();
glEnable(GL_TEXTURE_2D);
glPopMatrix();

}

// function to render the current scene
void Render(HWND hwnd)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SetupViewPorts(hwnd);
// do rendering here
// clear screen and depth buffer



SwapBuffers(g_HDC); // bring back buffer to foreground
glFlush();
}

// the windows Procedure event handler
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HGLRC hRC; // rendering context
static HDC hDC; // device context

switch(message)
{
case WM_CREATE: // window is being created

hDC = GetDC(hwnd); // get current window's device context
g_HDC = hDC;
SetupPixelFormat(hDC); // call your pixel format setup function

// create rendering context and make it current
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);

return 0;
break;

case WM_DESTROY: // window is closing

// deselect rendering context and delete it
wglMakeCurrent(hDC, NULL);
wglDeleteContext(hRC);

// send WM_DESTROY to message queue
PostQuitMessage(0);

return 0;
break;

case WM_SIZE:
SetupViewPorts(hwnd); // reset modelview matrix
return 0;
break;

case WM_KEYDOWN:
SetCapture(hwnd);
// is a key pressed?
keyPressed[wParam] = true;
return 0;
break;

case WM_KEYUP:
keyPressed[wParam] = false;
ReleaseCapture();
return 0;
break;

default:
break;
}

return (DefWindowProc(hwnd, message, wParam, lParam));
}

// the application entry point
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
int nShowCmd)
{
WNDCLASSEX windowClass; // window class
HWND hwnd; // window handler
MSG msg; // message

// fill out the window class structure
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = WndProc;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = hInstance;
windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
windowClass.lpszMenuName = NULL;
windowClass.lpszClassName = "MyClass";
windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);

// register the window class
if (!RegisterClassEx(&windowClass))
return 0;

// class registered, so now create your window
hwnd = CreateWindowEx(NULL, // extended style
"MyClass", // class name
"Text Testing", // app name
WS_OVERLAPPEDWINDOW | WS_VISIBLE |
WS_SYSMENU | WS_CLIPCHILDREN |
WS_CLIPSIBLINGS,
100, 100, // x,y coordinate
400, 400, // width and height
NULL, // handle to parent
NULL, // handle to menu
hInstance, // application instance
NULL); // no extra params

// check if window creation failed (hwnd would equal NULL)
if (!hwnd)
return 0;

ShowWindow(hwnd, SW_SHOW); // display the window
UpdateWindow(hwnd); // update the window

Initialize();


// main message loop

while (true)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
// Process the message
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
if (keyPressed[VK_ESCAPE])
PostQuitMessage(0);

if (keyPressed['W']) // if the a key is pressed, turn to the left
{
cameraY += 0.01;
}
if (keyPressed['A']) // if the a key is pressed, turn to the left
{
}
if (keyPressed['S']) // if the a key is pressed, turn to the left
{
cameraY -= 0.01;
}
if (keyPressed['Z']) // if the a key is pressed, turn to the left
{
cameraZ += 0.01;
}
if (keyPressed['Q']) // if the a key is pressed, turn to the left
{
cameraZ -= 0.01;
}
if (keyPressed['D']) // if the a key is pressed, turn to the left
{

}
if (keyPressed['X']) // if the a key is pressed, turn to the left
{
// Reset Camera
initCamera();
}


// render scene
Render(hwnd);
}
}

return msg.wParam;
}

Hlz
08-12-2005, 07:28 PM
:confused:

OK, I give up. What is this code supposed to do?

For future reference, try to keep your code simple, neat, complete, compilable, and to the point. GLUT is perfect for this sort of thing.

Erfdain
08-13-2005, 10:31 PM
Okay, that code was basic code that generates a window and has all the opengl code in it besides the pixel format and my lights and stuff. It was what they taugh us to use in school. I am developing my own engine now using other books that I have collected. Sorry about that

Okay. What I am trying to do here is create a window with 3 viewports. Sorta like this

__________
|****|***| 1 is perspective
|**1*|*2*| 2 is perspective
|****_____
|****|*3*| 3 is orthographic
|****|***|
__________

I can draw in all three viewports no problem.
but when I try to draw text in more than one viewport I have problems where my text doesn't show up at all or just in one window. My text code is fine, I is just where I put the code to put it in.

Where is my viewport setup code

void GameViewPorts(HWND hWindow)
{
RECT rect;
// Holds Coordinates Of A Rectangle
GetClientRect(hWindow, &rect);
// Get Window Dimensions
int width=rect.right-rect.left;
//Calculate The Width (Right Side-Left Side)
int height=rect.bottom-rect.top;
glClear(GL_COLOR_BUFFER_BIT);

// Calculate The Height (Bottom-Top)
// take the first half of the screen
glViewport(0, 0, width/2, height);
glMatrixMode(GL_PROJECTION);
// set projection matrix
glLoadIdentity();
// reset projection matrix
// calculate aspect ratio of window
gluPerspective(45.0f, (GLfloat)(width/2)/(GLfloat)height,1.0f,1000.0f);
glMatrixMode(GL_MODELVIEW);
// set modelview matrix
glLoadIdentity();
// reset modelview matrix
glClear (GL_DEPTH_BUFFER_BIT);
PrintText(-0.35, 0.0, "Text!");
GamePaint();


// Set The Viewport To The Top Right. It Will Take Up Half The Screen Width And Height
glViewport (width/2, height/2, width/2, height/2);
glMatrixMode (GL_PROJECTION);
// Select The Projection Matrix
glLoadIdentity ();
// Reset The Projection Matrix
// Set Up Perspective Mode To Fit 1/4 The Screen (Size Of A Viewport)

gluPerspective( 45.0, (GLfloat)(width)/(GLfloat)(height), 0.1f, 500.0 );

glMatrixMode (GL_MODELVIEW);
//Select The Modelview Matrix
glLoadIdentity ();
glClear (GL_DEPTH_BUFFER_BIT);
GamePaint();

// Set The Viewport To The Bottom Right. It Will
// Take Up Half The Screen Width And Height
glViewport (width/2, 0, width, height/2);
glMatrixMode (GL_PROJECTION);
//Select The Projection Matrix
glLoadIdentity ();
//Reset The Projection Matrix
gluOrtho2D(width/2, width, 0, height/2);

glMatrixMode (GL_MODELVIEW);
//Select The Modelview Matrix
glLoadIdentity();
glClear (GL_DEPTH_BUFFER_BIT);
//SetupLights();
// draw a 2d polygon
glDisable(GL_TEXTURE_2D);
glPushMatrix();

glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 0.0);
glVertex2i(0, 0);
glColor3f(1.0, 0.0, 0.0);
glVertex2i(width, 0);
glColor3f(1.0, 0.0, 0.0);
glVertex2i(width, height);
glColor3f(1.0, 0.0, 0.0);
glVertex2i(0, height);
glEnd();

glPopMatrix();
glEnable(GL_TEXTURE_2D);

PrintText(500.0, 100.0, "Display Text!");

}

could you please tell me what I am doing wrong.
the total window width is 600
height is 400

IsMakeFire
08-13-2005, 10:52 PM
Well one idea would be the generalize your veiwports into some veiw class, so you're not coping and pasting code everything. To me, this sounds like a z-buffer issue, what you might want to try temporarly disabling depth testing in your text drawing function, much in the same way you disabled and enabled textures for those polygons you draw in window #3.
Your perspectives have a z range from (0.1, something) and the ortho has(-1, 1) so if you try to draw a 2D object(like text) they'll get cliped since they lay at the Z-value 0.
Hope this helps

Hlz
08-14-2005, 10:03 AM
You need to use glScissor with glEnable/glDisable(GL_SCISSOR_TEST) to restrict clearing to a subregion.

In this particular case, you don't need to clear the depth buffer repeatedly, since the viewports don't overlap in a mix of 2D and 3D.

Give glOrtho the dimensions of the viewport rect, ignoring the origin, e.g., glOrtho(0,vw,0,vh,-1,1) for each ortho viewport.

To draw text on top of a 3D view, switch to a ortho projection after the scene is drawn, disable the depth test (or set the func to GL_ALWAYS), then draw your text.

You have calls to missing functions that appear to be responsible for the actual drawing of the text...