rendered paste body// BasicGL.cpp : Defines the entry point for the application.
//
#define WIN32_LEAN_AND_MEAN // This trims down the libraries.
#define VC_LEANMEAN
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
//#include "stdafx.h"
int g_width=1280, g_height=1024;
int g_half_width=640, g_half_height=512;
bool keys[256];
/****************************************************************
* CLASS DECLARATION AND DEFINITION FOR GLCONTEXT CLASS WHICH IS *
* USED TO INITIALIZE A DEVICE CONTEXT FOR RENDERING OPENGL*
* COMMANDS IN A WINDOW (WIN32 ENVIRONMENT). *
*****************************************************************/
HDC g_hDC;
class GLContext
{
public:
GLContext()
{
reset();
}
~GLContext()
{
purge();
}
void init(HWND);
void purge(void);
private:
void reset(void);
HWND mhWnd;
HDC mhDC;
HGLRC mhRC;
};
void GLContext::init(HWND hWnd)
{
mhWnd = hWnd;
// get the device context (DC)
mhDC = GetDC( mhWnd );
// set the pixel format for the DC
PIXELFORMATDESCRIPTOR pfd;
ZeroMemory( &pfd, sizeof( pfd ) );
pfd.nSize = sizeof( pfd );
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
int format = ChoosePixelFormat( mhDC, &pfd );
SetPixelFormat( mhDC, format, &pfd );
// create the render context (RC)
mhRC = wglCreateContext( mhDC );
// make it the current render context
wglMakeCurrent( mhDC, mhRC );
g_hDC = mhDC;
}
void GLContext::purge()
{
if ( mhRC )
{
wglMakeCurrent( NULL, NULL );
wglDeleteContext( mhRC );
}
if ( mhWnd && mhDC )
{
ReleaseDC( mhWnd, mhDC );
}
reset();
}
void GLContext::reset()
{
mhWnd = NULL;
mhDC = NULL;
mhRC = NULL;
}
GLContext g_contextGL;
/********************************************************************
Initialize OpenGL
********************************************************************/
void init(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Clear background color to black
// Viewport transformation
glViewport(0,0,g_width,g_height);
// Projection transformation
glMatrixMode(GL_PROJECTION); // Specifies which matrix stack is the target for matrix operations
glLoadIdentity(); // We initialize the projection matrix as identity
gluPerspective(45.0f,(GLfloat)g_width/(GLfloat)g_height,0.1f,10000.0f); // We define the "viewing volume"
//Other initializations
glShadeModel(GL_SMOOTH); // Type of shading for the polygons
glEnable(GL_DEPTH_TEST); // Enable the depth test (also called z buffer)
}
/********************************************************************
Render OpenGL Scene
********************************************************************/
void display(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Clear background color to black
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(-2.0f, 1.0f, -10.0f);
glVertex3f(2.0f, 1.0f, -10.0f);
glVertex3f(0.0f, 3.0f, -10.0f);
glEnd();
glFlush(); // This force the execution of OpenGL commands
SwapBuffers(g_hDC);
}
/********************************************************************
Parent Window Procedure
********************************************************************/
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
switch(msg)
{
case WM_PAINT:
display();
BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
break;
case WM_SIZE:
g_height = HIWORD(lParam); // This gets the height of the window.
g_width = LOWORD(lParam); // This gets the width of the window.
if(g_height==0) // we don't want it to be possible for a
{ // height of 0. If it is 0 me make it 1.
g_height = 1;
}
g_half_width = g_width/2;
g_half_height = g_height/2;
glViewport(0, 0, g_width, g_height);// resets the viewport to new dimensions.
glMatrixMode(GL_PROJECTION); // Sets the projection matrix.
glLoadIdentity(); // Reset the modelview matrix.
// calculate the aspect ratio of the window.
gluPerspective(45.0f, (GLfloat)g_width/(GLfloat)g_height, 0.1f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); // Reset the modelview matrix.
break;
case WM_KEYDOWN:
if(wParam==VK_ESCAPE)
PostQuitMessage(0);
keys[wParam] = true;
break;
case WM_KEYUP:
keys[wParam] = false;
break;
case WM_COMMAND:
display();
PostMessage(hwnd, NULL, 0, 0);
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
/********************************************************************
Main Function
********************************************************************/
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = "clientGL";
wc.hIconSm = NULL;
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
"clientGL",
"Basic GL",
WS_OVERLAPPEDWINDOW,
0, 0, g_width, g_height,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, SW_SHOWMAXIMIZED);
g_contextGL.init(hwnd);
init();
UpdateWindow(hwnd);
while (1)
{
if(GetMessage(&Msg, NULL, 0, 0) != TRUE)
break;
while (PeekMessage(&Msg, NULL, 0, 0, PM_NOREMOVE) == FALSE)
display();
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}