#include <windows.h> // Header File for Windows
#include <stdio.h> // Header File for standard Input/Output library
#include <gl\gl.h> // Header File for the OpenGL32 library
#include <gl\glu.h> // Header File for the GLu32 library
#include <gl\glaux.h> // Header File for the GLaux library
HGLRC hRC=NULL; // Permanent rendering ocntext
HDC hDC=NULL; // Private GDI device context
HWND hWnd=NULL; // Holds out window handle
HINSTANCE hInstance; // Holds the instance of the application
bool keys[256]; // Array used for the keyboard routine
bool active=TRUE; // Window active flag set to true by default
bool fullscreen=TRUE; // fullscreen flag is set to fullscreen mode by default
GLfloat xrot; // X Rotation
GLfloat yrot; // Y Rotation
GLfloat zrot; // Z Rotation
GLuint texture[1]; // Storage for one texture
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration for WinProc
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,"x"); // 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[1]; // Create storage space for the texture
memset (TextureImage,0,sizeof(void *)*1); // Set the pointer to NULL
// Load the bitmap, check for errors, if bitmap's not found quit
if (TextureImage[0]=LoadBMP("Data/NeHe.bmp"))
{
Status=TRUE; // Set the status to TRUE
glGenTextures(1, &texture[0]); // Create the texture
// Typical texture generation using data from the bitmmap
glBindTexture(GL_TEXTURE_2D, texture[0]);
// Generate the texture
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear filtering
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear filtering
}
if (TextureImage[0]) // If texture exists
{
if(TextureImage[0]->data) // If texture image exists
{
free(TextureImage[0]->data); // Free the texture image memory
}
free(TextureImage[0]); // Free the image structure
}
return Status; // Return the status
}
GLvoid ReSizeGLScene (GLsizei width, GLsizei height) // resize and initialize the gl window
{
if (height==0) // prevent a divide by zero by
{
height=1; // making height equal one
}
glViewport(0, 0, width, height); // reset the current viewport
glMatrixMode(GL_PROJECTION); // select the projection matrix
glLoadIdentity(); // Reset the projection matrix
// calculate the aspect ratio of the window
gluPerspective(45.0f, (GLfloat)width/(GLfloat)height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW); // select the modelview matrix
glLoadIdentity(); // reset the modelview matrix
}
int InitGL(GLvoid) // all setup for OpenGL goes here
{
if (!LoadGLTextures()) // Jump to texture loading routing
{
return FALSE; // if texture didn't load return FALSE
}
glEnable(GL_TEXTURE_2D); // Enables texture mapping
glShadeModel(GL_SMOOTH); // enables smooth shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // black background
glClearDepth(1.0f); // depth buffer setup
glEnable(GL_DEPTH_TEST); // enables depth testing
glDepthFunc(GL_LEQUAL); // the type of depth test to do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // really nice perspective calculations
return TRUE; // initializatoin went ok
}
int DrawGLScene(GLvoid) // Here's where we do all the drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear screen and depth buffer
glLoadIdentity(); // Reset the current matrix
glTranslatef(0.0f, 0.0f, -5.0f); // Move into the screen 5 units
glRotatef(xrot,1.0f,0.0f,0.0f); // Rotate on the X axis
glRotatef(yrot,0.0f,1.0f,0.0f); // Rotate on the Y axis
glRotatef(zrot,0.0f,0.0f,1.0f); // Rotate on the Z axis
glBindTexture(GL_TEXTURE_2D, texture[0]); // Select our texture
glBegin(GL_QUADS);
// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom left of the texture and quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom right of the texture and quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top right of the texture and quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top left of the texture and quad
// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom right of the texture and quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f,-1.0f); // Top right of the texture and quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f,-1.0f); // Top left of the texture and quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom left of the texture and quad
// Top Face
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f,-1.0f); // Top left of the texture and quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom left of the texture and quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom right of the texture and quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,-1.0f,-1.0f); // Top right of the texture and quad
// Bottom face
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f,-1.0f); // Top right of the texture and quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,-1.0f,-1.0f); // Top left of the texture and quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom left of the texture and quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom right of the texture and quad
// Right face
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom right of the texture and quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f,-1.0f); // Top right of the texture and quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top left of the texture and quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom left of the texture and quad
// Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom left of the texture and quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom right of the texture and quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top right of the texture and quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f,-1.0f); // Top left of the texture and quad
glEnd();
xrot+=0.3f; // X axis rotation
yrot+=0.2f; // Y axis rotation
zrot+=0.4f; // Z axis rotation
return true; // Keep going
}
GLvoid KillGLWindow(GLvoid) // properly kill the window
{
if (fullscreen) // are we in fullscreen mode?
{
ChangeDisplaySettings(NULL,0); // if so switch back to the desktop
ShowCursor(TRUE); // show mouse pointer
}
if (hRC) // do we have a rendering context?
{
if (!wglMakeCurrent(NULL,NULL)) // are we able to release the DC and RC contexts?
{
MessageBox(NULL,"Release of DC and RC failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}
if (!wglDeleteContext (hRC)) // are we able to delete the RC?
{
MessageBox(NULL,"Release rendering context failed.","SHUTDOWN ERROR",MB_OK |
MB_ICONINFORMATION) ;
}
hRC=NULL; // set RC to NULL
}
if (hDC && !ReleaseDC(hWnd,hDC)) // are we able to release the DC
{
MessageBox(NULL,"Release device context failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hDC=NULL; // set the DC to NULL
}
if (hWnd && !DestroyWindow(hWnd)) // are we able to destrou the window?
{
MessageBox(NULL,"Could not release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hWnd=NULL; // Set hWnd to NULL
}
if (!UnregisterClass("OpenGL",hInstance)) // are we able to unregister class
{
MessageBox(NULL,"Could not unregister class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hInstance=NULL; // set hInstance to NULL
}
}
BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
{
GLuint PixelFormat; // holds the results after searching for a match
WNDCLASS wc; // window class structure
DWORD dwExStyle; // window extended style
DWORD dwStyle; // window style
RECT WindowRect; // grabs rectangle upper left / lower right values
WindowRect.left=(long)0; // set left value to 0
WindowRect.right=(long)width; // set right value to requested width
WindowRect.top=(long)0; // set top value to 0
WindowRect.bottom=(long)height; // set bottom value to requested height
fullscreen=fullscreenflag; // set the global fullscreen flag
hInstance = GetModuleHandle(NULL); // grab an instance for our window
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // redraw or move, and own DC for window
wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc handles messages
wc.cbClsExtra = 0; // no extra window data
wc.cbWndExtra = 0; // no extra window data
wc.hInstance = hInstance; // set the instance
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // load the default icon
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // load the arrow pointer
wc.hbrBackground = NULL; // no background required for GL
wc.lpszMenuName = NULL; // we don't want a menu
wc.lpszClassName = "OpenGL"; // set the class name
if (!RegisterClass(&wc)) // attempt to register the window class
{
MessageBox(NULL,"Failed to register the window class.","ERROR",MB_OK | MB_ICONEXCLAMATION);
return FALSE; //exit and return FALSE
}
if (fullscreen) // attempt fullscreen mode?
{
DEVMODE dmScreenSettings; // device mode
memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // make sure memory's cleared
dmScreenSettings.dmSize = sizeof(dmScreenSettings); // size of the devmode structure
dmScreenSettings.dmPelsWidth = width; // selected screen width
dmScreenSettings.dmPelsHeight = height; // selected screen height
dmScreenSettings.dmBitsPerPel = bits; // selected bits per pixel
dmScreenSettings.dmFields = DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
// Try to set delected mode and get results. NOTE: CDS_FULLSCREEN gets rid of start bar
if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
{
// If the mode fials, offer two options. Quit or run in a window
if (MessageBox(NULL,"The requested fullscreen mode is not supported by\nYour video card. Use Windowed mode instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
{
fullscreen=FALSE; // select windowed mode (Fullscreen=FALSE)
}
else
{
// Pop up a message box letting user know the program is closing
MessageBox(NULL,"Program will now close.","ERROR",MB_OK|MB_ICONSTOP);
return FALSE; // exit and return FALSE
}
}
}
if (fullscreen) // are we still in fullscreen mode?
{
dwExStyle=WS_EX_APPWINDOW; // window extended class
dwStyle=WS_POPUP; // windows style
ShowCursor(FALSE); // hide mouse pointer
}
else
{
dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // window extended style
dwStyle=WS_OVERLAPPEDWINDOW; //windows style
}
AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // adjust window to requested size
if (!(hWnd=CreateWindowEx( dwExStyle, // extended window style
"OpenGL", // class name
title, // window title
WS_CLIPSIBLINGS | // required window style
WS_CLIPCHILDREN | // required window style
dwStyle, // selected window style
0, 0, // window position
WindowRect.right-WindowRect.left, // Calculate adjusted window width
WindowRect.bottom-WindowRect.top, // Calculate adjusted window height
NULL, // no parent window
NULL, // no menu
hInstance, // instance
NULL))) // don't pass anything to WM_CREATE
{
KillGLWindow(); // Reset the display
MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // return FALSE
}
static PIXELFORMATDESCRIPTOR pfd = // pfd tells widnws how we want things to be
{
sizeof(PIXELFORMATDESCRIPTOR), // size of this pixel format descriptor
1, // version number
PFD_DRAW_TO_WINDOW | // format must support window
PFD_SUPPORT_OPENGL | // format must support OpenGL
PFD_DOUBLEBUFFER, // must support double buffering
PFD_TYPE_RGBA, // Request an RGBA format
bits, // select our color depth
0, 0, 0, 0, 0, 0, // color bits ignored
0, // no alpha buffer
0, // shift bit ignored
0, // no accumulation buffer
0, 0, 0, 0, // accumulation bits ignored
16, // 16bit stencil buffer
0, // no auxiliary buffer
PFD_MAIN_PLANE, // main drawing layer
0, // reserved
0, 0, 0, // layer masks ignored
};
if (!(hDC=GetDC(hWnd))) // did we get a device context?
{
KillGLWindow(); // Reset the display
MessageBox(NULL,"Can't create a GL device context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // return FALSE
}
if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // did windows find a matching pixel format?
{
KillGLWindow(); // Reset the display
MessageBox(NULL,"Can't find a suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // return FALSE
}
if (!SetPixelFormat(hDC,PixelFormat,&pfd)) // are we able to set the pixel format?
{
KillGLWindow(); // Reset the display
MessageBox(NULL,"Can't set the PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // return FALSE
}
if (!(hRC=wglCreateContext(hDC))) // are we able to get a rendering context?
{
KillGLWindow(); // Reset the display
MessageBox(NULL,"Can't create a GL rendering context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // return FALSE
}
if (!wglMakeCurrent(hDC,hRC)) // try to activate the rendering context
{
KillGLWindow(); // Reset the display
MessageBox(NULL,"Can't activate the GL rendering context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // return FALSE
}
ShowWindow(hWnd,SW_SHOW); // show the window
SetForegroundWindow(hWnd); // Slightly higher priority
SetFocus(hWnd); // sets keyboartd focus to the window
ReSizeGLScene(width, height); // set up our perspective GL screen
if (!InitGL()) // initialize our newly created GL window
{
KillGLWindow(); // Reset the display
MessageBox(NULL,"Initialization failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // return FALSE
}
return TRUE; // success
}
LRESULT CALLBACK WndProc( HWND hWnd, // handle for this window
UINT uMsg, // message for this window
WPARAM wParam, // additional message information
LPARAM lParam) // additional message information
{
switch (uMsg)
{
case WM_ACTIVATE: // watch for window activate message
{
if (!HIWORD(wParam)) // check minimization state
{
active=TRUE; // program is active
}
else
{
active=FALSE; // program is no longer active
}
return 0; // return to message loop
}
case WM_SYSCOMMAND: // intercept system commands
{
switch(wParam) // check system calls
{
case SC_SCREENSAVE: // scrensaver trying to start?
case SC_MONITORPOWER: // monitor trying to enter powersave?
return 0; // prevent this from happening
}
break; // exit
}
case WM_CLOSE: // did we recieve a close message?
{
PostQuitMessage(0); // send a quit message
return 0; // jump back
}
case WM_KEYDOWN: // is a key being held down?
{
keys[wParam] = TRUE; // if so, mark it as TRUE
return 0; // jump back
}
case WM_KEYUP: // has a key been released?
{
keys[wParam] = FALSE; // if so, mark it as FALSE
return 0; // jump back
}
case WM_SIZE: //resize the OpenGL window
{
ReSizeGLScene(LOWORD(lParam),HIWORD(lParam)); // LoWord=Width, HiWord=Height
return 0; // jump back
}
}
// Pass ALL unhandled messages to DefWindowProc
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
int WINAPI WinMain( HINSTANCE hInstance, // instance
HINSTANCE hPrevInstance, // previous instance
LPSTR lpCmdLine, // command line parameters
int nCmdShow) // window show state
{
MSG msg; // windows message
BOOL done=FALSE; // bool variable to exit loop
// Ask the user which screen mode they prefer
if (MessageBox(NULL,"Would you like to run in fullscreen mode?", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO)
{
fullscreen=FALSE; // windowed mode
}
// create our OpenGL window
if (!CreateGLWindow("NeHe's OpenGL Framework",640,480,16,fullscreen))
{
return 0; // quit if window was not created
}
while(!done) // loop that runs until done=TRUE
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // is there a message waiting?
{
if (msg.message==WM_QUIT) // have we recieved a quit message?
{
done=TRUE; // if so done=TRUE
}
else // if not, deal with window messages
{
TranslateMessage(&msg); // translate the message
DispatchMessage(&msg); // dispatch the message
}
}
else // if there are no messages
{
// Draw the scene, watch for ESC key and quit messages from DrawGLScene()
if (active) // program active?
{
if (keys[VK_ESCAPE]) // was ESC pressed?
{
done=TRUE; // ESC signalled a quit
}
else // not time to quit, update screen
{
DrawGLScene(); // draw the scene
SwapBuffers(hDC); // swap buffers (Double buffering)
}
}
if (keys[VK_F1]) // is F1 being pressed?
{
keys[VK_F1]=FALSE; // if so, make key FALSE
KillGLWindow(); // kill our current window
fullscreen=!fullscreen; // toggle fullscren / windowed mode
// recreate our OpenGL window
if (!CreateGLWindow("NeHe's OpenGL Tutorial",640,480,16,fullscreen))
{
return 0; // quit if window was not created
}
}
}
}
// shutdown
KillGLWindow(); // kill the window
return (msg.wParam); // exit the program
}
I get no compiler errors when I build it but whenever I try to run it I get the "Initialization failed" box that is supposed to run when InitGl() doesn't go the right way:
if (!InitGL()) // initialize our newly created GL window
{
KillGLWindow(); // Reset the display
MessageBox(NULL,"Initialization failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // return FALSE
}
The code for the window is from lesson 1.
There's no town drunk here, we all take turns. Velocity Gaming Force [edited by - Ekim_Gram on July 21, 2003 8:02:12 PM] [edited by - Ekim_Gram on July 21, 2003 8:02:59 PM]