#include<GL/gl.h>
#include<windows.h>
#include<iostream>
#include<GL/glu.h>
using namespace std;
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
switch(msg){
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
BOOL SetupPixelFormat(HDC hdc){
PIXELFORMATDESCRIPTOR pfd;
int pixelformat;
ZeroMemory(&pfd.bReserved,sizeof(PIXELFORMATDESCRIPTOR));
pfd.nSize=sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion=1;
pfd.iPixelType=PFD_TYPE_RGBA;
pfd.cColorBits=32;
pfd.cDepthBits=32;
pfd.iLayerType=PFD_MAIN_PLANE;
pixelformat=ChoosePixelFormat(hdc,&pfd);
if(pixelformat==0){
return FALSE;
}
if(SetPixelFormat(hdc,pixelformat,&pfd)==FALSE) return FALSE;
return TRUE;
}
void InitOpenGL(HDC hdc){
HGLRC hrc=wglCreateContext(hdc);
wglMakeCurrent(hdc,hrc);
glClearColor(0.0f,0.0f,0.0f,1.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, 800.0f / 600.0f, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){
WNDCLASS wc;
HWND hwnd;
MSG msg;
ZeroMemory(&wc,sizeof(WNDCLASS));
wc.lpfnWndProc=WndProc;
wc.hInstance=hInstance;
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.lpszClassName="OpenGLWindow";
RegisterClass(&wc);
hwnd=CreateWindow("OpenGLWindow","OpenGL 3D Window",WS_OVERLAPPEDWINDOW,100,100,800,600,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd, nCmdShow);
HDC hdc = GetDC(hwnd);
SetupPixelFormat(hdc);
InitOpenGL(hdc);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -5.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-0.5f, -0.5f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.5f, 0.0f);
glEnd();
SwapBuffers(hdc);
}
wglMakeCurrent(NULL, NULL);
wglDeleteContext(wglGetCurrentContext());
ReleaseDC(hwnd, hdc);
DestroyWindow(hwnd);
UnregisterClass("OpenGLWindow", hInstance);
return msg.wParam;
}
返回值:Process exited after 2.585 seconds with return value 3221225477