1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
#define WIN32_LEAN_AND_MEAN // trim the excess fat from Windows
#include <windows.h> // standard Windows app include
#include <gl\GL.h>
#include <gl\GLU.h>
#include <gl\GLAux.h>
//////Global Variables
float angle = 0.0f; //current angle of the rotating triangle
HDC g_HDC; //global device context
//function to set the pixel format for the device context
void SetupPixelFormat(HDC hDC)
{
int nPixelFormat; //your pixel format index
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 32bit color mode
0, 0, 0, 0, 0, 0, //ignore color bits, not used
0, //no alpha buffer
0, //ignore shift bit
0, //no accumulation buffer
0, 0, 0, 0, //ignore accumulation bits
16, //16-bits 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 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;
switch(message)
{
case WM_CREATE: // window is being created
hDC = GetDC(hwnd); // get curent windows 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_CLOSE: // windows is closing
//deselect rendering context and delete it
wglMakeCurrent(hDC, NULL);
wglDeleteContext(hRC);
PostQuitMessage(0);
return 0;
break;
case WM_SIZE:
height = HIWORD(lParam); // retrieve width and height
width = LOWORD(lParam);
if(height == 0) //dont want a divide by zero
{
height = 1;
}
//reset the viewport to new dimensions
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION); //set Projection matrix
glLoadIdentity(); //reset Projection Matrix
//calculate aspect ration of window
gluPerspective(45.0f, (GLfloat)width/(GLfloat)height,1.0f, 1000.0f);
glMatrixMode(GL_MODELVIEW); //set modelview matrix
glLoadIdentity(); //reset modelview matrix
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
// 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
"The OpenGl Window Application!", // app name
WS_OVERLAPPEDWINDOW | // window style
WS_VISIBLE |
WS_SYSMENU | WS_CLIPCHILDREN |
WS_CLIPSIBLINGS,
100, 100, // x,y coordinate
400, 400, // width, 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;
done = false; // initialize the loop condition variable
// main message loop
while (!done)
{
PeekMessage(&msg, hwnd, NULL, NULL, PM_REMOVE);
if (msg.message == WM_QUIT) // do you receive a WM_QUIT message?
{
done = true; // if so, time to quit the application
}
else
{
//do rendering here
//clear screen and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); //reset modelview matrix
angle = angle + 0.1f; //increase your rotation angle counter
if(angle >= 360.0f) //reset angle counter
angle = 0.0f;
glTranslatef(0.0f, 0.0f, -5.0f); //move back 5 units
glRotatef(angle, 0.0f,0.0f,1.0f);//rotate along z-axis
glColor3f(1.0f,0.0f,0.0f); //set color to red
glBegin(GL_TRIANGLES); //draw the triangle
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glEnd();
SwapBuffers(g_HDC); //bring back buffer to foreground
TranslateMessage(&msg); // translate and dispatch to event queue
DispatchMessage(&msg);
}
}
return msg.wParam;
}
|