• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Ozwald

OpenGL HUD

8 posts in this topic

I'm extremely new to OpenGL, but not game design (i used to use SDL). Is there any way to draw directly to the screen to do a HUD without having to use glTranslate or glRotate or anything else. Either opengl doesn't do this or I'm just looking through the wrong functions. Any insight would be awsome Ozwald~
0

Share this post


Link to post
Share on other sites
Search for the millions of threads about 2d using OpenGL. The same method can be used to draw a hud over your 3d scene.
0

Share this post


Link to post
Share on other sites
it's simple, use glOrtho and the turn off depthwriting and depthtesting with
glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
and you have a good hud drawing mode.

also you can use these functions to help switching back and forth between the different modes.


void ViewOrtho(int x, int y) // Set Up An Ortho View
{
glMatrixMode(GL_PROJECTION); // Select Projection
glPushMatrix(); // Push The Matrix
glLoadIdentity(); // Reset The Matrix
glOrtho( 0, x , y , 0, -1, 1 ); // Select Ortho Mode
glMatrixMode(GL_MODELVIEW); // Select Modelview Matrix
glPushMatrix(); // Push The Matrix
glLoadIdentity(); // Reset The Matrix
}

void ViewPerspective(void) // Set Up A Perspective View
{
glMatrixMode( GL_PROJECTION ); // Select Projection
glPopMatrix(); // Pop The Matrix
glMatrixMode( GL_MODELVIEW ); // Select Modelview
glPopMatrix(); // Pop The Matrix
}





you might still have to use glTranslate (you won't get away from that in openGL) but it's definitly a lot easier to draw the hud in this mode.
1

Share this post


Link to post
Share on other sites
does anyone know how to output variables to the HUD like the score
Thanks
AC
0

Share this post


Link to post
Share on other sites
just print what you want to the screen...here's a routine from nehe's tutorial for printing...works just like printf in c

GLvoid glPrint(GLuint glfontlist, GLfloat x, GLfloat y, const char *fmt, ...)
{
if( glIsList( glfontlist ) == GL_FALSE ) {
// Error Report?
return;
}

glRasterPos2f( x, y );

char text[256];
va_list ap;

if(fmt==NULL) return;

va_start(ap, fmt);
vsprintf(text, fmt, ap);
va_end(ap);

glPushAttrib(GL_LIST_BIT);
glListBase(glfontlist - 32);
glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);
glPopAttrib();
}


it uses lists for the font...if you look through nehe's tutorials, and find some of the font/printing text tutorials, you find how to load such.

hope that helps.

Oz~

PS:

int score = 50;
glPrint( yourfont, 1.0f, 500.0f, "Your score is: %d", score );
0

Share this post


Link to post
Share on other sites
I'm having a problem.

I'm drawing a polygon in perspective view then switching over to ortho view to render text. I can display text but only by using openGL coordinates in an ortho view by using RasterPos2i(x,y)

I want to display my text using Windows coordinates like in the Lesson 32 program on nehe.gavedev.net.

The code to set up perspective is here
______________________________________

void SetupViewPorts(int width, int height)
{
// now setup the viewports
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
}
-------------------------------------------------------------------------
and my rendering code which renders in the viewport created in previous function
a polygon, then switches over to ortho and renders the text
-------------------------------------------------------------------------
// fuction for rendering
void Render()
{

char output[50] = " ";
char output2[50] = " ";
char ch_zoom[50];

// clear screen and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); // reset modelview matrix



// move one unit into the screen
glTranslatef(0.0f, 0.0f, zoom);
glPushMatrix();
glRotatef(angle, 1.0f, 0.0f, 0.0f);
glRotatef(angle, 0.0f, 1.0f, 0.0f);
glRotatef(angle, 0.0f, 0.0f, 1.0f);
glBegin(GL_QUADS);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f, -1.0f, 0.0);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(1.0f, -1.0f, 0.0);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(1.0f, 1.0f, 0.0);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f, 1.0f, 0.0);
glEnd();
glPopMatrix();




// setup ortho view to draw text
glPushMatrix();

glMatrixMode(GL_PROJECTION);

glOrtho(0, winWidth, 0, winTop, -1, 1);
glMatrixMode(GL_MODELVIEW);
glColor4f(1.0f, 1.0f, 1.0f, 0.75f);

glRasterPos2i(-4, -4);
// build the string
gcvt(zoom, 4, ch_zoom);
strcpy(output, "Zoom is currently: ");
strcpy(output2, ch_zoom);
strcat(output, output2);

PrintString(listbase, output);

glFlush();
SwapBuffers(g_HDC); // bring back buffer to foreground
angle += 0.01f;
if(angle >= 360.0f)
angle = 0.0f;

}
___________________________________________________________________

can someone help me fix this code and clean it up so I can render my text using windows coordinate system like the nehe.gamedev.net Lesson 32 program?
___________________________________________________________________
0

Share this post


Link to post
Share on other sites
The coordinates using ortho mode should be the same as the window coordinates (as in if the res is 800x600, x goes 0-800, y goes 0-600)
If you want true windows coords, use ScreenToClient( hWnd, POINT coords );
or ClientToScreen( <same parameters> );

Oz~

PS:

glOrtho(0, winWidth, winTop, 0, -1, 1);

that should fix it :p
0

Share this post


Link to post
Share on other sites
here is my complete code. Maybe you can figure it out.

____________________________________________________________________________
#define WIN32_LEAN_AND_MEAN

#include
#include
#include
#include
#include
#include

// global variables
bool keyPressed[256]; // holds true for keys that are pressed
float angle = 0.0f; // current angle of the rotating triangle
HDC g_HDC; // global device context
HWND g_HWND;

bool b_fullScreen = TRUE; // flag value to test for full screen mode

unsigned int listbase;

int winHeight, winWidth, winTop, winBottom; // rect structure to store window values
float zoom = -10.0f;

// Prototype Functions
void SetupFont();
void Render();
void SetupFullScreenMode();
void SetupViewPorts(int width, int height);
unsigned int CreateBitmapFont(char *fontName, int fontSize);
void PrintString(unsigned int base, char *str);
void ClearFont(unsigned int base);
void Initialize();

// End Prototypes
void Initialize()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // clear to black

glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);

listbase = CreateBitmapFont("Comic Sans MS", 20); // create the bitmap font
}


unsigned int CreateBitmapFont(char *fontName, int fontSize)
{
HFONT hFont; // windows font
unsigned int base;

base = glGenLists(96); // create storage for 96 characters

if(stricmp(fontName, "symbol") == 0)
{
hFont = CreateFont(fontSize, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, SYMBOL_CHARSET,
OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
FF_DONTCARE | DEFAULT_PITCH, fontName);
}
else
{
hFont = CreateFont(fontSize, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, ANSI_CHARSET,
OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
FF_DONTCARE | DEFAULT_PITCH, fontName);
}
if(!hFont)
{
MessageBox(NULL, "Failed to Create Font!", "Font Error", MB_OK);
return 0;
}

SelectObject(g_HDC, hFont);
wglUseFontBitmaps(g_HDC, 32, 96, base);

return base;
}

void PrintString(unsigned int base, char *str)
{
glDisable(GL_DEPTH_TEST);
if((base == 0) || (str == NULL))
return;

glPushAttrib(GL_LIST_BIT);
glListBase(base -32);
glCallLists(strlen(str), GL_UNSIGNED_BYTE, str);
glPopAttrib();
glEnable(GL_DEPTH_TEST);
}

void ClearFont(unsigned int base)
{
if(base != 0)
glDeleteLists(base, 96);
}



void SetupViewPorts(int width, int height)
{
// now setup the viewports
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
}


//function that sets up fullscreen mode
void SetupFullScreenMode()
{
// do nothing
MessageBox(NULL, "Full screen mode not available yet!", "Fullscreen?", MB_OK);
b_fullScreen = FALSE;
}


// fuction for rendering
void Render()
{

char output[50] = " ";
char output2[50] = " ";
char ch_zoom[50];

// clear screen and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); // reset modelview matrix



// move one unit into the screen
glTranslatef(0.0f, 0.0f, zoom);
glPushMatrix();
glRotatef(angle, 1.0f, 0.0f, 0.0f);
glRotatef(angle, 0.0f, 1.0f, 0.0f);
glRotatef(angle, 0.0f, 0.0f, 1.0f);
glBegin(GL_QUADS);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f, -1.0f, 0.0);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(1.0f, -1.0f, 0.0);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(1.0f, 1.0f, 0.0);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f, 1.0f, 0.0);
glEnd();
glPopMatrix();




// setup ortho view to draw text
glPushMatrix();

glMatrixMode(GL_PROJECTION);

glOrtho(0, winWidth, winTop, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glColor4f(1.0f, 1.0f, 1.0f, 0.75f);

glRasterPos2i(-4,-4);
// build the string
gcvt(zoom, 4, ch_zoom);
strcpy(output, "Zoom is currently: ");
strcpy(output2, ch_zoom);
strcat(output, output2);

PrintString(listbase, output);

glFlush();
SwapBuffers(g_HDC); // bring back buffer to foreground
angle += 0.01f;
if(angle >= 360.0f)
angle = 0.0f;

}

// function to set the pixel format for the device context
void SetupPixelFormat(HDC hDC)
{
int nPixelFormat; // your pixel format

static PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // size of the structure
1, // version, always set to 1
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGL
PFD_DOUBLEBUFFER, // support double buffering
PFD_TYPE_RGBA, // RGBA color mode
32, // go for 32 bit color mode
0, 0, 0, 0, 0, 0, // ignore color bits, not used
0, // no alpha buffer
0, // ignore shift bit
0, // no accumulation bits
0, 0, 0, 0, // ignore accumulation bits
16, // 16 bit z-buffer size
0, // no stencil buffer
0, // no auxiliary buffer
PFD_MAIN_PLANE, // main drawing plane
0, // reserved
0, 0, 0 }; // layer masks ignored

//choose best matching pixel format, return index
nPixelFormat = ChoosePixelFormat(hDC, &pfd);

// set pixel format to device context
SetPixelFormat(hDC, nPixelFormat, &pfd);
}

// 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
char string[] = "Hello world!"; // text to be displayed
int width, height; // window width and height
RECT myWinDimensions;



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

listbase = CreateBitmapFont("Comic Sans MS", 48);
if(MessageBox(NULL, "Do you want to run in fullscreen mode?", "Fullscreen?", MB_YESNO) == IDYES)
{
SetupFullScreenMode();
}
else
{
MessageBox(NULL, "Running in windowed mode", "Fullscreen?", MB_OK);
}
hDC = GetDC(hwnd); // get device context for window
g_HDC = hDC;
SetupPixelFormat(hDC); // call your pixel format setup function

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

case WM_DESTROY: // window is closing
wglMakeCurrent(hDC, NULL); // deselect Rendering context
wglDeleteContext(hRC); // delete rendering context
ClearFont(listbase);
PostQuitMessage(0);
return 0;
break;

case WM_SIZE:
height = HIWORD(lParam); // retrieve width and height
width = LOWORD(lParam);

if (height == 0) // dont want a divied by zero
{
height = 1;
}
g_HWND = hwnd;
SetupViewPorts(width, height);



return 0;
break;

case WM_KEYDOWN:
SetCapture(hwnd);
// is a key pressed?
keyPressed[wParam] = true;
return 0;
break;
case WM_KEYUP:
ReleaseCapture();
keyPressed[wParam] = false;
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 handle
MSG msg; // message
bool done; // flag saying when your app is complete

DWORD my_exstyle, my_style;

// 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;

if (b_fullScreen)
{
my_exstyle = WS_EX_APPWINDOW; // hide top level windows
my_style = WS_POPUP; // no border on your window
ShowCursor(TRUE); // hide the cursor
}
else
{
my_exstyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // same as regular window
my_style = WS_OVERLAPPEDWINDOW;
}
// class registered, so now create your window
hwnd = CreateWindowEx(NULL,
"MyClass",
"Bitmap Fonts Application in OpenGL",
WS_OVERLAPPEDWINDOW |
WS_VISIBLE |
WS_SYSMENU,
100, 100,
400, 400,
NULL,
NULL,
hInstance,
NULL);

// 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['A'])
{
zoom += 0.01f;
}
if (keyPressed['Z'])
{
zoom -= 0.01f;
}
// Render Here
Render(); // Call rendering Function
}

}



return msg.wParam;
}
_______________________________________________________________________________
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0