/*
* This Code Was Created By Jeff Molofee 2000
* A HUGE Thanks To Fredric Echols For Cleaning Up
* And Optimizing The Base Code, Making It More Flexible!
* If You've Found This Code Useful, Please Let Me Know.
* Visit My Site At nehe.gamedev.net
*/
#include <windows.h> // Header File For Windows
#include <stdio.h> // Header File For Standard Input / Output
#include <stdarg.h> // Header File For Variable Argument Routines
#include <string.h> // Header File For String Management
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
HDC hDC=NULL; // Private GDI Device Context
HGLRC hRC=NULL; // Permanent Rendering Context
HWND hWnd=NULL; // Holds Our 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=FALSE; // Fullscreen Flag Set To Fullscreen Mode By Default
GLfloat xrot; // X Rotation ( NEW )
GLfloat yrot; // Y Rotation ( NEW )
GLfloat zrot; // Z Rotation ( NEW )
typedef struct // Create A Structure
{
GLubyte *imageData; // Image Data (Up To 32 Bits)
GLuint bpp; // Image Color Depth In Bits Per Pixel
GLuint width; // Image Width
GLuint height; // Image Height
GLuint texID; // Texture ID Used To Select A Texture
} TextureImage; // Structure Name
TextureImage textures[1]; // Storage For One Texture
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc
bool LoadTGA(TextureImage *texture, char *filename) // Loads A TGA File Into Memory
{
GLubyte TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0}; // Uncompressed TGA Header
GLubyte TGAcompare[12]; // Used To Compare TGA Header
GLubyte header[6]; // First 6 Useful Bytes From The Header
GLuint bytesPerPixel; // Holds Number Of Bytes Per Pixel Used In The TGA File
GLuint imageSize; // Used To Store The Image Size When Setting Aside Ram
GLuint temp; // Temporary Variable
GLuint type=GL_RGBA; // Set The Default GL Mode To RBGA (32 BPP)
FILE *file = fopen(filename, "rb"); // Open The TGA File
if( file==NULL || // Does File Even Exist?
fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) || // Are There 12 Bytes To Read?
memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0 || // Does The Header Match What We Want?
fread(header,1,sizeof(header),file)!=sizeof(header)) // If So Read Next 6 Header Bytes
{
if (file == NULL) // Did The File Even Exist? *Added Jim Strong*
return false; // Return False
else
{
fclose(file); // If Anything Failed, Close The File
return false; // Return False
}
}
texture->width = header[1] * 256 + header[0]; // Determine The TGA Width (highbyte*256+lowbyte)
texture->height = header[3] * 256 + header[2]; // Determine The TGA Height (highbyte*256+lowbyte)
if( texture->width <=0 || // Is The Width Less Than Or Equal To Zero
texture->height <=0 || // Is The Height Less Than Or Equal To Zero
(header[4]!=24 && header[4]!=32)) // Is The TGA 24 or 32 Bit?
{
fclose(file); // If Anything Failed, Close The File
return false; // Return False
}
texture->bpp = header[4]; // Grab The TGA's Bits Per Pixel (24 or 32)
bytesPerPixel = texture->bpp/8; // Divide By 8 To Get The Bytes Per Pixel
imageSize = texture->width*texture->height*bytesPerPixel; // Calculate The Memory Required For The TGA Data
texture->imageData=(GLubyte *)malloc(imageSize); // Reserve Memory To Hold The TGA Data
if( texture->imageData==NULL || // Does The Storage Memory Exist?
fread(texture->imageData, 1, imageSize, file)!=imageSize) // Does The Image Size Match The Memory Reserved?
{
if(texture->imageData!=NULL) // Was Image Data Loaded
free(texture->imageData); // If So, Release The Image Data
fclose(file); // Close The File
return false; // Return False
}
for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel) // Loop Through The Image Data
{ // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
temp=texture->imageData; // Temporarily Store The Value At Image Data 'i'
texture->imageData = texture->imageData; <span class=cpp-comment>// Set The 1st Byte To The Value Of The 3rd Byte</span>
texture->imageData = temp; <span class=cpp-comment>// Set The 3rd Byte To The Value In 'temp' (1st Byte Value)</span>
}
fclose (file); <span class=cpp-comment>// Close The File</span>
<span class=cpp-comment>// Build A Texture From The Data</span>
glGenTextures(<span class=cpp-number>1</span>, &texture[<span class=cpp-number>0</span>].texID); <span class=cpp-comment>// Generate OpenGL texture IDs</span>
glBindTexture(GL_TEXTURE_2D, texture[<span class=cpp-number>0</span>].texID); <span class=cpp-comment>// Bind Our Texture</span>
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); <span class=cpp-comment>// Linear Filtered</span>
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); <span class=cpp-comment>// Linear Filtered</span>
<span class=cpp-keyword>if</span> (texture[<span class=cpp-number>0</span>].bpp==<span class=cpp-number>24</span>) <span class=cpp-comment>// Was The TGA 24 Bits</span>
{
type=GL_RGB; <span class=cpp-comment>// If So Set The 'type' To GL_RGB</span>
}
glTexImage2D(GL_TEXTURE_2D, <span class=cpp-number>0</span>, type, texture[<span class=cpp-number>0</span>].width, texture[<span class=cpp-number>0</span>].height, <span class=cpp-number>0</span>, type, GL_UNSIGNED_BYTE, texture[<span class=cpp-number>0</span>].imageData);
<span class=cpp-keyword>return</span> <span class=cpp-keyword>true</span>; <span class=cpp-comment>// Texture Building Went Ok, Return True</span>
}
GLvoid ReSizeGLScene(GLsizei width, GLsizei height) <span class=cpp-comment>// Resize And Initialize The GL Window</span>
{
<span class=cpp-keyword>if</span> (height==<span class=cpp-number>0</span>) <span class=cpp-comment>// Prevent A Divide By Zero By</span>
{
height=<span class=cpp-number>1</span>; <span class=cpp-comment>// Making Height Equal One</span>
}
glViewport(<span class=cpp-number>0</span>,<span class=cpp-number>0</span>,width,height); <span class=cpp-comment>// Reset The Current Viewport</span>
glMatrixMode(GL_PROJECTION); <span class=cpp-comment>// Select The Projection Matrix</span>
glLoadIdentity(); <span class=cpp-comment>// Reset The Projection Matrix</span>
<span class=cpp-comment>// Calculate The Aspect Ratio Of The Window</span>
gluPerspective(<span class=cpp-number>45</span>.0f,(GLfloat)width/(GLfloat)height,<span class=cpp-number>0</span>.1f,<span class=cpp-number>100</span>.0f);
glMatrixMode(GL_MODELVIEW); <span class=cpp-comment>// Select The Modelview Matrix</span>
glLoadIdentity(); <span class=cpp-comment>// Reset The Modelview Matrix</span>
}
<span class=cpp-keyword>int</span> InitGL(GLvoid) <span class=cpp-comment>// All Setup For OpenGL Goes Here</span>
{
<span class=cpp-keyword>if</span> (!LoadTGA(&textures[<span class=cpp-number>0</span>],<span class=cpp-literal>"woot.tga"</span>)) <span class=cpp-comment>// Jump To Texture Loading Routine ( NEW )</span>
{
<span class=cpp-keyword>return</span> <span class=cpp-keyword>FALSE</span>; <span class=cpp-comment>// If Texture Didn't Load Return FALSE</span>
}
glEnable(GL_TEXTURE_2D); <span class=cpp-comment>// Enable Texture Mapping ( NEW )</span>
glShadeModel(GL_SMOOTH); <span class=cpp-comment>// Enable Smooth Shading</span>
glClearColor(<span class=cpp-number>0</span>.0f, <span class=cpp-number>0</span>.0f, <span class=cpp-number>0</span>.0f, <span class=cpp-number>0</span>.5f); <span class=cpp-comment>// Black Background</span>
glClearDepth(<span class=cpp-number>1</span>.0f); <span class=cpp-comment>// Depth Buffer Setup</span>
glEnable(GL_DEPTH_TEST); <span class=cpp-comment>// Enables Depth Testing</span>
glDepthFunc(GL_LEQUAL); <span class=cpp-comment>// The Type Of Depth Testing To Do</span>
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); <span class=cpp-comment>// Really Nice Perspective Calculations</span>
<span class=cpp-keyword>return</span> <span class=cpp-keyword>TRUE</span>; <span class=cpp-comment>// Initialization Went OK</span>
}
<span class=cpp-keyword>int</span> DrawGLScene(GLvoid) <span class=cpp-comment>// Here's Where We Do All The Drawing</span>
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); <span class=cpp-comment>// Clear The Screen And The Depth Buffer</span>
glLoadIdentity(); <span class=cpp-comment>// Reset The View</span>
glTranslatef( <span class=cpp-number>0</span>.0f,<span class=cpp-number>0</span>.0f,-<span class=cpp-number>6</span>.0f);
glRotatef(xrot,<span class=cpp-number>1</span>.0f,<span class=cpp-number>0</span>.0f,<span class=cpp-number>0</span>.0f);
glRotatef(yrot,<span class=cpp-number>0</span>.0f,<span class=cpp-number>1</span>.0f,<span class=cpp-number>0</span>.0f);
glRotatef(zrot,<span class=cpp-number>0</span>.0f,<span class=cpp-number>0</span>.0f,<span class=cpp-number>1</span>.0f);
glBegin(GL_QUADS);
<span class=cpp-comment>// Front Face</span>
glTexCoord2f(<span class=cpp-number>0</span>.0f, <span class=cpp-number>0</span>.0f); glVertex3f(-<span class=cpp-number>1</span>.0f, -<span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f);
glTexCoord2f(<span class=cpp-number>1</span>.0f, <span class=cpp-number>0</span>.0f); glVertex3f( <span class=cpp-number>1</span>.0f, -<span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f);
glTexCoord2f(<span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f); glVertex3f( <span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f);
glTexCoord2f(<span class=cpp-number>0</span>.0f, <span class=cpp-number>1</span>.0f); glVertex3f(-<span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f);
<span class=cpp-comment>// Back Face</span>
glTexCoord2f(<span class=cpp-number>1</span>.0f, <span class=cpp-number>0</span>.0f); glVertex3f(-<span class=cpp-number>1</span>.0f, -<span class=cpp-number>1</span>.0f, -<span class=cpp-number>1</span>.0f);
glTexCoord2f(<span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f); glVertex3f(-<span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f, -<span class=cpp-number>1</span>.0f);
glTexCoord2f(<span class=cpp-number>0</span>.0f, <span class=cpp-number>1</span>.0f); glVertex3f( <span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f, -<span class=cpp-number>1</span>.0f);
glTexCoord2f(<span class=cpp-number>0</span>.0f, <span class=cpp-number>0</span>.0f); glVertex3f( <span class=cpp-number>1</span>.0f, -<span class=cpp-number>1</span>.0f, -<span class=cpp-number>1</span>.0f);
<span class=cpp-comment>// Top Face</span>
glTexCoord2f(<span class=cpp-number>0</span>.0f, <span class=cpp-number>1</span>.0f); glVertex3f(-<span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f, -<span class=cpp-number>1</span>.0f);
glTexCoord2f(<span class=cpp-number>0</span>.0f, <span class=cpp-number>0</span>.0f); glVertex3f(-<span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f);
glTexCoord2f(<span class=cpp-number>1</span>.0f, <span class=cpp-number>0</span>.0f); glVertex3f( <span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f);
glTexCoord2f(<span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f); glVertex3f( <span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f, -<span class=cpp-number>1</span>.0f);
<span class=cpp-comment>// Bottom Face</span>
glTexCoord2f(<span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f); glVertex3f(-<span class=cpp-number>1</span>.0f, -<span class=cpp-number>1</span>.0f, -<span class=cpp-number>1</span>.0f);
glTexCoord2f(<span class=cpp-number>0</span>.0f, <span class=cpp-number>1</span>.0f); glVertex3f( <span class=cpp-number>1</span>.0f, -<span class=cpp-number>1</span>.0f, -<span class=cpp-number>1</span>.0f);
glTexCoord2f(<span class=cpp-number>0</span>.0f, <span class=cpp-number>0</span>.0f); glVertex3f( <span class=cpp-number>1</span>.0f, -<span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f);
glTexCoord2f(<span class=cpp-number>1</span>.0f, <span class=cpp-number>0</span>.0f); glVertex3f(-<span class=cpp-number>1</span>.0f, -<span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f);
<span class=cpp-comment>// Right face</span>
glTexCoord2f(<span class=cpp-number>1</span>.0f, <span class=cpp-number>0</span>.0f); glVertex3f( <span class=cpp-number>1</span>.0f, -<span class=cpp-number>1</span>.0f, -<span class=cpp-number>1</span>.0f);
glTexCoord2f(<span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f); glVertex3f( <span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f, -<span class=cpp-number>1</span>.0f);
glTexCoord2f(<span class=cpp-number>0</span>.0f, <span class=cpp-number>1</span>.0f); glVertex3f( <span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f);
glTexCoord2f(<span class=cpp-number>0</span>.0f, <span class=cpp-number>0</span>.0f); glVertex3f( <span class=cpp-number>1</span>.0f, -<span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f);
<span class=cpp-comment>// Left Face</span>
glTexCoord2f(<span class=cpp-number>0</span>.0f, <span class=cpp-number>0</span>.0f); glVertex3f(-<span class=cpp-number>1</span>.0f, -<span class=cpp-number>1</span>.0f, -<span class=cpp-number>1</span>.0f);
glTexCoord2f(<span class=cpp-number>1</span>.0f, <span class=cpp-number>0</span>.0f); glVertex3f(-<span class=cpp-number>1</span>.0f, -<span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f);
glTexCoord2f(<span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f); glVertex3f(-<span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f);
glTexCoord2f(<span class=cpp-number>0</span>.0f, <span class=cpp-number>1</span>.0f); glVertex3f(-<span class=cpp-number>1</span>.0f, <span class=cpp-number>1</span>.0f, -<span class=cpp-number>1</span>.0f);
glEnd();
xrot+=<span class=cpp-number>0</span>.3f;
yrot+=<span class=cpp-number>0</span>.2f;
zrot+=<span class=cpp-number>0</span>.4f;
<span class=cpp-keyword>return</span> <span class=cpp-keyword>TRUE</span>; <span class=cpp-comment>// Keep Going</span>
}
GLvoid KillGLWindow(GLvoid) <span class=cpp-comment>// Properly Kill The Window</span>
{
<span class=cpp-keyword>if</span> (fullscreen) <span class=cpp-comment>// Are We In Fullscreen Mode?</span>
{
ChangeDisplaySettings(NULL,<span class=cpp-number>0</span>); <span class=cpp-comment>// If So Switch Back To The Desktop</span>
ShowCursor(<span class=cpp-keyword>TRUE</span>); <span class=cpp-comment>// Show Mouse Pointer</span>
}
<span class=cpp-keyword>if</span> (hRC) <span class=cpp-comment>// Do We Have A Rendering Context?</span>
{
<span class=cpp-keyword>if</span> (!wglMakeCurrent(NULL,NULL)) <span class=cpp-comment>// Are We Able To Release The DC And RC Contexts?</span>
{
MessageBox(NULL,<span class=cpp-literal>"Release Of DC And RC Failed."</span>,<span class=cpp-literal>"SHUTDOWN ERROR"</span>,MB_OK | MB_ICONINFORMATION);
}
<span class=cpp-keyword>if</span> (!wglDeleteContext(hRC)) <span class=cpp-comment>// Are We Able To Delete The RC?</span>
{
MessageBox(NULL,<span class=cpp-literal>"Release Rendering Context Failed."</span>,<span class=cpp-literal>"SHUTDOWN ERROR"</span>,MB_OK | MB_ICONINFORMATION);
}
hRC=NULL; <span class=cpp-comment>// Set RC To NULL</span>
}
<span class=cpp-keyword>if</span> (hDC && !ReleaseDC(hWnd,hDC)) <span class=cpp-comment>// Are We Able To Release The DC</span>
{
MessageBox(NULL,<span class=cpp-literal>"Release Device Context Failed."</span>,<span class=cpp-literal>"SHUTDOWN ERROR"</span>,MB_OK | MB_ICONINFORMATION);
hDC=NULL; <span class=cpp-comment>// Set DC To NULL</span>
}
<span class=cpp-keyword>if</span> (hWnd && !DestroyWindow(hWnd)) <span class=cpp-comment>// Are We Able To Destroy The Window?</span>
{
MessageBox(NULL,<span class=cpp-literal>"Could Not Release hWnd."</span>,<span class=cpp-literal>"SHUTDOWN ERROR"</span>,MB_OK | MB_ICONINFORMATION);
hWnd=NULL; <span class=cpp-comment>// Set hWnd To NULL</span>
}
<span class=cpp-keyword>if</span> (!UnregisterClass(<span class=cpp-literal>"OpenGL"</span>,hInstance)) <span class=cpp-comment>// Are We Able To Unregister Class</span>
{
MessageBox(NULL,<span class=cpp-literal>"Could Not Unregister Class."</span>,<span class=cpp-literal>"SHUTDOWN ERROR"</span>,MB_OK | MB_ICONINFORMATION);
hInstance=NULL; <span class=cpp-comment>// Set hInstance To NULL</span>
}
}
<span class=cpp-comment>/* This Code Creates Our OpenGL Window. Parameters Are: *
* title - Title To Appear At The Top Of The Window *
* width - Width Of The GL Window Or Fullscreen Mode *
* height - Height Of The GL Window Or Fullscreen Mode *
* bits - Number Of Bits To Use For Color (8/16/24/32) *
* fullscreenflag - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE) */</span>
<span class=cpp-keyword>BOOL</span> CreateGLWindow(<span class=cpp-keyword>char</span>* title, <span class=cpp-keyword>int</span> width, <span class=cpp-keyword>int</span> height, <span class=cpp-keyword>int</span> bits, <span class=cpp-keyword>bool</span> fullscreenflag)
{
GLuint PixelFormat; <span class=cpp-comment>// Holds The Results After Searching For A Match</span>
WNDCLASS wc; <span class=cpp-comment>// Windows Class Structure</span>
DWORD dwExStyle; <span class=cpp-comment>// Window Extended Style</span>
DWORD dwStyle; <span class=cpp-comment>// Window Style</span>
RECT WindowRect; <span class=cpp-comment>// Grabs Rectangle Upper Left / Lower Right Values</span>
WindowRect.left=(<span class=cpp-keyword>long</span>)<span class=cpp-number>0</span>; <span class=cpp-comment>// Set Left Value To 0</span>
WindowRect.right=(<span class=cpp-keyword>long</span>)width; <span class=cpp-comment>// Set Right Value To Requested Width</span>
WindowRect.top=(<span class=cpp-keyword>long</span>)<span class=cpp-number>0</span>; <span class=cpp-comment>// Set Top Value To 0</span>
WindowRect.bottom=(<span class=cpp-keyword>long</span>)height; <span class=cpp-comment>// Set Bottom Value To Requested Height</span>
fullscreen=fullscreenflag; <span class=cpp-comment>// Set The Global Fullscreen Flag</span>
hInstance = GetModuleHandle(NULL); <span class=cpp-comment>// Grab An Instance For Our Window</span>
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; <span class=cpp-comment>// Redraw On Size, And Own DC For Window.</span>
wc.lpfnWndProc = (WNDPROC) WndProc; <span class=cpp-comment>// WndProc Handles Messages</span>
wc.cbClsExtra = <span class=cpp-number>0</span>; <span class=cpp-comment>// No Extra Window Data</span>
wc.cbWndExtra = <span class=cpp-number>0</span>; <span class=cpp-comment>// No Extra Window Data</span>
wc.hInstance = hInstance; <span class=cpp-comment>// Set The Instance</span>
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); <span class=cpp-comment>// Load The Default Icon</span>
wc.hCursor = LoadCursor(NULL, IDC_ARROW); <span class=cpp-comment>// Load The Arrow Pointer</span>
wc.hbrBackground = NULL; <span class=cpp-comment>// No Background Required For GL</span>
wc.lpszMenuName = NULL; <span class=cpp-comment>// We Don't Want A Menu</span>
wc.lpszClassName = <span class=cpp-literal>"OpenGL"</span>; <span class=cpp-comment>// Set The Class Name</span>
<span class=cpp-keyword>if</span> (!RegisterClass(&wc)) <span class=cpp-comment>// Attempt To Register The Window Class</span>
{
MessageBox(NULL,<span class=cpp-literal>"Failed To Register The Window Class."</span>,<span class=cpp-literal>"ERROR"</span>,MB_OK|MB_ICONEXCLAMATION);
<span class=cpp-keyword>return</span> <span class=cpp-keyword>FALSE</span>; <span class=cpp-comment>// Return FALSE</span>
}
<span class=cpp-keyword>if</span> (fullscreen) <span class=cpp-comment>// Attempt Fullscreen Mode?</span>
{
DEVMODE dmScreenSettings; <span class=cpp-comment>// Device Mode</span>
memset(&dmScreenSettings,<span class=cpp-number>0</span>,<span class=cpp-keyword>sizeof</span>(dmScreenSettings)); <span class=cpp-comment>// Makes Sure Memory's Cleared</span>
dmScreenSettings.dmSize=<span class=cpp-keyword>sizeof</span>(dmScreenSettings); <span class=cpp-comment>// Size Of The Devmode Structure</span>
dmScreenSettings.dmPelsWidth = width; <span class=cpp-comment>// Selected Screen Width</span>
dmScreenSettings.dmPelsHeight = height; <span class=cpp-comment>// Selected Screen Height</span>
dmScreenSettings.dmBitsPerPel = bits; <span class=cpp-comment>// Selected Bits Per Pixel</span>
dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
<span class=cpp-comment>// Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.</span>
<span class=cpp-keyword>if</span> (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
{
<span class=cpp-comment>// If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode.</span>
<span class=cpp-keyword>if</span> (MessageBox(NULL,<span class=cpp-literal>"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?"</span>,<span class=cpp-literal>"NeHe GL"</span>,MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
{
fullscreen=<span class=cpp-keyword>FALSE</span>; <span class=cpp-comment>// Windowed Mode Selected. Fullscreen = FALSE</span>
}
<span class=cpp-keyword>else</span>
{
<span class=cpp-comment>// Pop Up A Message Box Letting User Know The Program Is Closing.</span>
MessageBox(NULL,<span class=cpp-literal>"Program Will Now Close."</span>,<span class=cpp-literal>"ERROR"</span>,MB_OK|MB_ICONSTOP);
<span class=cpp-keyword>return</span> <span class=cpp-keyword>FALSE</span>; <span class=cpp-comment>// Return FALSE</span>
}
}
}
<span class=cpp-keyword>if</span> (fullscreen) <span class=cpp-comment>// Are We Still In Fullscreen Mode?</span>
{
dwExStyle=WS_EX_APPWINDOW; <span class=cpp-comment>// Window Extended Style</span>
dwStyle=WS_POPUP; <span class=cpp-comment>// Windows Style</span>
ShowCursor(<span class=cpp-keyword>FALSE</span>); <span class=cpp-comment>// Hide Mouse Pointer</span>
}
<span class=cpp-keyword>else</span>
{
dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; <span class=cpp-comment>// Window Extended Style</span>
dwStyle=WS_OVERLAPPEDWINDOW; <span class=cpp-comment>// Windows Style</span>
}
AdjustWindowRectEx(&WindowRect, dwStyle, <span class=cpp-keyword>FALSE</span>, dwExStyle); <span class=cpp-comment>// Adjust Window To True Requested Size</span>
<span class=cpp-comment>// Create The Window</span>
<span class=cpp-keyword>if</span> (!(hWnd=CreateWindowEx( dwExStyle, <span class=cpp-comment>// Extended Style For The Window</span>
<span class=cpp-literal>"OpenGL"</span>, <span class=cpp-comment>// Class Name</span>
title, <span class=cpp-comment>// Window Title</span>
dwStyle | <span class=cpp-comment>// Defined Window Style</span>
WS_CLIPSIBLINGS | <span class=cpp-comment>// Required Window Style</span>
WS_CLIPCHILDREN, <span class=cpp-comment>// Required Window Style</span>
<span class=cpp-number>0</span>, <span class=cpp-number>0</span>, <span class=cpp-comment>// Window Position</span>
WindowRect.right-WindowRect.left, <span class=cpp-comment>// Calculate Window Width</span>
WindowRect.bottom-WindowRect.top, <span class=cpp-comment>// Calculate Window Height</span>
NULL, <span class=cpp-comment>// No Parent Window</span>
NULL, <span class=cpp-comment>// No Menu</span>
hInstance, <span class=cpp-comment>// Instance</span>
NULL))) <span class=cpp-comment>// Dont Pass Anything To WM_CREATE</span>
{
KillGLWindow(); <span class=cpp-comment>// Reset The Display</span>
MessageBox(NULL,<span class=cpp-literal>"Window Creation Error."</span>,<span class=cpp-literal>"ERROR"</span>,MB_OK|MB_ICONEXCLAMATION);
<span class=cpp-keyword>return</span> <span class=cpp-keyword>FALSE</span>; <span class=cpp-comment>// Return FALSE</span>
}
<span class=cpp-keyword>static</span> PIXELFORMATDESCRIPTOR pfd= <span class=cpp-comment>// pfd Tells Windows How We Want Things To Be</span>
{
<span class=cpp-keyword>sizeof</span>(PIXELFORMATDESCRIPTOR), <span class=cpp-comment>// Size Of This Pixel Format Descriptor</span>
<span class=cpp-number>1</span>, <span class=cpp-comment>// Version Number</span>
PFD_DRAW_TO_WINDOW | <span class=cpp-comment>// Format Must Support Window</span>
PFD_SUPPORT_OPENGL | <span class=cpp-comment>// Format Must Support OpenGL</span>
PFD_DOUBLEBUFFER, <span class=cpp-comment>// Must Support Double Buffering</span>
PFD_TYPE_RGBA, <span class=cpp-comment>// Request An RGBA Format</span>
bits, <span class=cpp-comment>// Select Our Color Depth</span>
<span class=cpp-number>0</span>, <span class=cpp-number>0</span>, <span class=cpp-number>0</span>, <span class=cpp-number>0</span>, <span class=cpp-number>0</span>, <span class=cpp-number>0</span>, <span class=cpp-comment>// Color Bits Ignored</span>
<span class=cpp-number>0</span>, <span class=cpp-comment>// No Alpha Buffer</span>
<span class=cpp-number>0</span>, <span class=cpp-comment>// Shift Bit Ignored</span>
<span class=cpp-number>0</span>, <span class=cpp-comment>// No Accumulation Buffer</span>
<span class=cpp-number>0</span>, <span class=cpp-number>0</span>, <span class=cpp-number>0</span>, <span class=cpp-number>0</span>, <span class=cpp-comment>// Accumulation Bits Ignored</span>
<span class=cpp-number>16</span>, <span class=cpp-comment>// 16Bit Z-Buffer (Depth Buffer) </span>
<span class=cpp-number>0</span>, <span class=cpp-comment>// No Stencil Buffer</span>
<span class=cpp-number>0</span>, <span class=cpp-comment>// No Auxiliary Buffer</span>
PFD_MAIN_PLANE, <span class=cpp-comment>// Main Drawing Layer</span>
<span class=cpp-number>0</span>, <span class=cpp-comment>// Reserved</span>
<span class=cpp-number>0</span>, <span class=cpp-number>0</span>, <span class=cpp-number>0</span> <span class=cpp-comment>// Layer Masks Ignored</span>
};
<span class=cpp-keyword>if</span> (!(hDC=GetDC(hWnd))) <span class=cpp-comment>// Did We Get A Device Context?</span>
{
KillGLWindow(); <span class=cpp-comment>// Reset The Display</span>
MessageBox(NULL,<span class=cpp-literal>"Can't Create A GL Device Context."</span>,<span class=cpp-literal>"ERROR"</span>,MB_OK|MB_ICONEXCLAMATION);
<span class=cpp-keyword>return</span> <span class=cpp-keyword>FALSE</span>; <span class=cpp-comment>// Return FALSE</span>
}
<span class=cpp-keyword>if</span> (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) <span class=cpp-comment>// Did Windows Find A Matching Pixel Format?</span>
{
KillGLWindow(); <span class=cpp-comment>// Reset The Display</span>
MessageBox(NULL,<span class=cpp-literal>"Can't Find A Suitable PixelFormat."</span>,<span class=cpp-literal>"ERROR"</span>,MB_OK|MB_ICONEXCLAMATION);
<span class=cpp-keyword>return</span> <span class=cpp-keyword>FALSE</span>; <span class=cpp-comment>// Return FALSE</span>
}
<span class=cpp-keyword>if</span>(!SetPixelFormat(hDC,PixelFormat,&pfd)) <span class=cpp-comment>// Are We Able To Set The Pixel Format?</span>
{
KillGLWindow(); <span class=cpp-comment>// Reset The Display</span>
MessageBox(NULL,<span class=cpp-literal>"Can't Set The PixelFormat."</span>,<span class=cpp-literal>"ERROR"</span>,MB_OK|MB_ICONEXCLAMATION);
<span class=cpp-keyword>return</span> <span class=cpp-keyword>FALSE</span>; <span class=cpp-comment>// Return FALSE</span>
}
<span class=cpp-keyword>if</span> (!(hRC=wglCreateContext(hDC))) <span class=cpp-comment>// Are We Able To Get A Rendering Context?</span>
{
KillGLWindow(); <span class=cpp-comment>// Reset The Display</span>
MessageBox(NULL,<span class=cpp-literal>"Can't Create A GL Rendering Context."</span>,<span class=cpp-literal>"ERROR"</span>,MB_OK|MB_ICONEXCLAMATION);
<span class=cpp-keyword>return</span> <span class=cpp-keyword>FALSE</span>; <span class=cpp-comment>// Return FALSE</span>
}
<span class=cpp-keyword>if</span>(!wglMakeCurrent(hDC,hRC)) <span class=cpp-comment>// Try To Activate The Rendering Context</span>
{
KillGLWindow(); <span class=cpp-comment>// Reset The Display</span>
MessageBox(NULL,<span class=cpp-literal>"Can't Activate The GL Rendering Context."</span>,<span class=cpp-literal>"ERROR"</span>,MB_OK|MB_ICONEXCLAMATION);
<span class=cpp-keyword>return</span> <span class=cpp-keyword>FALSE</span>; <span class=cpp-comment>// Return FALSE</span>
}
ShowWindow(hWnd,SW_SHOW); <span class=cpp-comment>// Show The Window</span>
SetForegroundWindow(hWnd); <span class=cpp-comment>// Slightly Higher Priority</span>
SetFocus(hWnd); <span class=cpp-comment>// Sets Keyboard Focus To The Window</span>
ReSizeGLScene(width, height); <span class=cpp-comment>// Set Up Our Perspective GL Screen</span>
<span class=cpp-keyword>if</span> (!InitGL()) <span class=cpp-comment>// Initialize Our Newly Created GL Window</span>
{
KillGLWindow(); <span class=cpp-comment>// Reset The Display</span>
MessageBox(NULL,<span class=cpp-literal>"Initialization Failed."</span>,<span class=cpp-literal>"ERROR"</span>,MB_OK|MB_ICONEXCLAMATION);
<span class=cpp-keyword>return</span> <span class=cpp-keyword>FALSE</span>; <span class=cpp-comment>// Return FALSE</span>
}
<span class=cpp-keyword>return</span> <span class=cpp-keyword>TRUE</span>; <span class=cpp-comment>// Success</span>
}
LRESULT CALLBACK WndProc( HWND hWnd, <span class=cpp-comment>// Handle For This Window</span>
UINT uMsg, <span class=cpp-comment>// Message For This Window</span>
WPARAM wParam, <span class=cpp-comment>// Additional Message Information</span>
LPARAM lParam) <span class=cpp-comment>// Additional Message Information</span>
{
<span class=cpp-keyword>switch</span> (uMsg) <span class=cpp-comment>// Check For Windows Messages</span>
{
<span class=cpp-keyword>case</span> WM_ACTIVATE: <span class=cpp-comment>// Watch For Window Activate Message</span>
{
<span class=cpp-keyword>if</span> (!HIWORD(wParam)) <span class=cpp-comment>// Check Minimization State</span>
{
active=<span class=cpp-keyword>TRUE</span>; <span class=cpp-comment>// Program Is Active</span>
}
<span class=cpp-keyword>else</span>
{
active=<span class=cpp-keyword>FALSE</span>; <span class=cpp-comment>// Program Is No Longer Active</span>
}
<span class=cpp-keyword>return</span> <span class=cpp-number>0</span>; <span class=cpp-comment>// Return To The Message Loop</span>
}
<span class=cpp-keyword>case</span> WM_SYSCOMMAND:
{
<span class=cpp-keyword>switch</span> (wParam)
{
<span class=cpp-keyword>case</span> SC_SCREENSAVE:
<span class=cpp-keyword>case</span> SC_MONITORPOWER:
<span class=cpp-keyword>return</span> <span class=cpp-number>0</span>;
}
<span class=cpp-keyword>break</span>;
}
<span class=cpp-keyword>case</span> WM_CLOSE: <span class=cpp-comment>// Did We Receive A Close Message?</span>
{
PostQuitMessage(<span class=cpp-number>0</span>); <span class=cpp-comment>// Send A Quit Message</span>
<span class=cpp-keyword>return</span> <span class=cpp-number>0</span>; <span class=cpp-comment>// Jump Back</span>
}
<span class=cpp-keyword>case</span> WM_KEYDOWN: <span class=cpp-comment>// Is A Key Being Held Down?</span>
{
keys[wParam] = <span class=cpp-keyword>TRUE</span>; <span class=cpp-comment>// If So, Mark It As TRUE</span>
<span class=cpp-keyword>return</span> <span class=cpp-number>0</span>; <span class=cpp-comment>// Jump Back</span>
}
<span class=cpp-keyword>case</span> WM_KEYUP: <span class=cpp-comment>// Has A Key Been Released?</span>
{
keys[wParam] = <span class=cpp-keyword>FALSE</span>; <span class=cpp-comment>// If So, Mark It As FALSE</span>
<span class=cpp-keyword>return</span> <span class=cpp-number>0</span>; <span class=cpp-comment>// Jump Back</span>
}
<span class=cpp-keyword>case</span> WM_SIZE: <span class=cpp-comment>// Resize The OpenGL Window</span>
{
ReSizeGLScene(LOWORD(lParam),HIWORD(lParam)); <span class=cpp-comment>// LoWord=Width, HiWord=Height</span>
<span class=cpp-keyword>return</span> <span class=cpp-number>0</span>; <span class=cpp-comment>// Jump Back</span>
}
}
<span class=cpp-comment>// Pass All Unhandled Messages To DefWindowProc</span>
<span class=cpp-keyword>return</span> DefWindowProc(hWnd,uMsg,wParam,lParam);
}
<span class=cpp-keyword>int</span> WINAPI WinMain( HINSTANCE hInstance, <span class=cpp-comment>// Instance</span>
HINSTANCE hPrevInstance, <span class=cpp-comment>// Previous Instance</span>
LPSTR lpCmdLine, <span class=cpp-comment>// Command Line Parameters</span>
<span class=cpp-keyword>int</span> nCmdShow) <span class=cpp-comment>// Window Show State</span>
{
MSG msg; <span class=cpp-comment>// Windows Message Structure</span>
<span class=cpp-keyword>BOOL</span> done=<span class=cpp-keyword>FALSE</span>; <span class=cpp-comment>// Bool Variable To Exit Loop</span>
<span class=cpp-comment>// Create Our OpenGL Window</span>
<span class=cpp-keyword>if</span> (!CreateGLWindow(<span class=cpp-literal>"NeHe's First Polygon Tutorial"</span>,<span class=cpp-number>640</span>,<span class=cpp-number>480</span>,<span class=cpp-number>16</span>,fullscreen))
{
<span class=cpp-keyword>return</span> <span class=cpp-number>0</span>; <span class=cpp-comment>// Quit If Window Was Not Created</span>
}
<span class=cpp-keyword>while</span>(!done) <span class=cpp-comment>// Loop That Runs While done=FALSE</span>
{
<span class=cpp-keyword>if</span> (PeekMessage(&msg,NULL,<span class=cpp-number>0</span>,<span class=cpp-number>0</span>,PM_REMOVE)) <span class=cpp-comment>// Is There A Message Waiting?</span>
{
<span class=cpp-keyword>if</span> (msg.message==WM_QUIT) <span class=cpp-comment>// Have We Received A Quit Message?</span>
{
done=<span class=cpp-keyword>TRUE</span>; <span class=cpp-comment>// If So done=TRUE</span>
}
<span class=cpp-keyword>else</span> <span class=cpp-comment>// If Not, Deal With Window Messages</span>
{
TranslateMessage(&msg); <span class=cpp-comment>// Translate The Message</span>
DispatchMessage(&msg); <span class=cpp-comment>// Dispatch The Message</span>
}
}
<span class=cpp-keyword>else</span> <span class=cpp-comment>// If There Are No Messages</span>
{
<span class=cpp-comment>// Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene()</span>
<span class=cpp-keyword>if</span> ((active && !DrawGLScene()) || keys[VK_ESCAPE]) <span class=cpp-comment>// Active? Was There A Quit Received?</span>
{
done=<span class=cpp-keyword>TRUE</span>; <span class=cpp-comment>// ESC or DrawGLScene Signalled A Quit</span>
}
<span class=cpp-keyword>else</span> <span class=cpp-comment>// Not Time To Quit, Update Screen</span>
{
SwapBuffers(hDC); <span class=cpp-comment>// Swap Buffers (Double Buffering)</span>
}
<span class=cpp-keyword>if</span> (keys[VK_F1]) <span class=cpp-comment>// Is F1 Being Pressed?</span>
{
keys[VK_F1]=<span class=cpp-keyword>FALSE</span>; <span class=cpp-comment>// If So Make Key FALSE</span>
KillGLWindow(); <span class=cpp-comment>// Kill Our Current Window</span>
fullscreen=!fullscreen; <span class=cpp-comment>// Toggle Fullscreen / Windowed Mode</span>
<span class=cpp-comment>// Recreate Our OpenGL Window</span>
<span class=cpp-keyword>if</span> (!CreateGLWindow(<span class=cpp-literal>"NeHe's First Polygon Tutorial"</span>,<span class=cpp-number>640</span>,<span class=cpp-number>480</span>,<span class=cpp-number>16</span>,fullscreen))
{
<span class=cpp-keyword>return</span> <span class=cpp-number>0</span>; <span class=cpp-comment>// Quit If Window Was Not Created</span>
}
}
}
}
<span class=cpp-comment>// Shutdown</span>
KillGLWindow(); <span class=cpp-comment>// Kill The Window</span>
<span class=cpp-keyword>return</span> (msg.wParam); <span class=cpp-comment>// Exit The Program</span>
}
</pre></div><!–ENDSCRIPT–><!–EDIT–><span class=editedby><!–/EDIT–>[Edited by - LessBread on September 16, 2005 5:38:01 PM]<!–EDIT–></span><!–/EDIT–>
Need Help Substituting TGA for Glaux
Hello, I'm very new to both C++ and OpenGL (about a week >_>) so I'm still working on grasping everything. When I got to lesson 6 I decided to try and use the code for texturing with TGA's instead of using outdated glaux... (to be honest i couldn't find the dll anyway XD) but after a very tricky process for me of figuring out what parts to use in this code and what not to, i got this! but it doesn't work ;_; I have the rotating cube still, and it processes the tga fine (at least it doesn't give any errors) it's a 32bit non compressed TGA, but still nothing shows up, so any help at all will be greatly appreciated!! Here's the code, something in here must be wrong, but after a few hours I still can't figure out what! Thanks for any help!!
Well, I didn't read all of the code, but the NeHe TGA loader is horrible. It isn't compliant with the spec at all. I suggest that (since you're new to this) you use an image loading library such as DevIL, which is nice and easy to use.[smile].
If at first you don't succeed, redefine success.
I dunno about DevIL, caused alot of memory errors when I used it.
- relpats_eht
Quote:
Original post by relpats_eht
I dunno about DevIL, caused alot of memory errors when I used it.
What sort of errors? I've never used it myself (I just wrote my own image library - or got someone on the team to write one [smile]), but just heard good things about it. Still, there's SDL_Image if you don't like it, and there are many others available.
If at first you don't succeed, redefine success.
try this class for image loading it is very very easy to use
http://www.codeproject.com/info/error500.asp?500;http://www.codeproject.com/bitmap/cximage.asp
http://www.codeproject.com/info/error500.asp?500;http://www.codeproject.com/bitmap/cximage.asp
Quote:
Original post by python_regious Quote:
Original post by relpats_eht
I dunno about DevIL, caused alot of memory errors when I used it.
What sort of errors? I've never used it myself (I just wrote my own image library - or got someone on the team to write one [smile]), but just heard good things about it. Still, there's SDL_Image if you don't like it, and there are many others available.
the kind of errors that caused every program ive used it with to crash with access violations. though, I havent used it for about a year now, so maybe it works better.
- relpats_eht
Quote:
Original post by relpats_eht Quote:
Original post by python_regious Quote:
Original post by relpats_eht
I dunno about DevIL, caused alot of memory errors when I used it.
What sort of errors? I've never used it myself (I just wrote my own image library - or got someone on the team to write one [smile]), but just heard good things about it. Still, there's SDL_Image if you don't like it, and there are many others available.
the kind of errors that caused every program ive used it with to crash with access violations. though, I havent used it for about a year now, so maybe it works better.
...That's usually a problem you yourself cause by not deleteing/freeing the data properly(not so much on XP, but on older OS's with poorer memory management, like 95, 98, and ME), or deleteing/freeing data that doesn't need to be deleted/freed.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement