#include
#include
#include
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glu32.lib")
void DraeScene() {
glClear(GL_COLOR_BUFFER_BIT); //清除背景颜色
glPointSize(30.0f); //设置点大小
glEnable(GL_POINT_SMOOTH); //圆点
//画点
glBegin(GL_POINTS); //开始绘制
glColor4ub(200, 0, 0, 255);
glVertex3f(0, 0, -100.0f);
glEnd(); //结束绘制
//画线
glLineWidth(4.0f); //线的粗细
glBegin(GL_LINES);
glColor4ub(200, 0, 0, 255);
glVertex3f(0, 0, -100.0f);
glColor4ub(0, 200, 0, 255);
glVertex3f(50.0f, 0, -100.0f);
glVertex3f(0, 50.0f, -100.0f);
glVertex3f(50.0f, 0, -100.0f);
glVertex3f(0, 50.0f, -100.0f);
glVertex3f(0, 0, -100.0f);
glEnd();
//
glBegin(GL_TRIANGLES);
glVertex3f(0, 0, -100.0f);
glVertex3f(0, -30.0f, -100.0f);
glVertex3f(-30.0f, 0, -100.0f);
glEnd();
}
LRESULT CALLBACK RenderWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CLOSE:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd) {
WNDCLASSEX wndClass;
wndClass.cbClsExtra = 0;
wndClass.cbSize = sizeof(WNDCLASSEX);
wndClass.cbWndExtra = 0;
wndClass.hbrBackground = NULL;
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hIcon = NULL;
wndClass.hIconSm = NULL;
wndClass.hInstance = hInstance;
wndClass.lpfnWndProc = (WNDPROC)RenderWindowProc;
wndClass.lpszClassName = L"OpenGLWindow";
wndClass.lpszMenuName = NULL;
wndClass.style = CS_HREDRAW | CS_VREDRAW;
ATOM atom = RegisterClassEx(&wndClass);
//创建窗口
HWND hwnd = CreateWindowEx(NULL, L"OpenGLWindow", L"OpenGL Render Window", WS_OVERLAPPEDWINDOW, 100, 100, 800, 600, NULL, NULL, hInstance, NULL);
//创建OpenGl渲染窗口
HDC dc = GetDC(hwnd);
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
pfd.nVersion = 1;
pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_TYPE_RGBA;
pfd.iLayerType = PFD_MAIN_PLANE;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 24;
pfd.cStencilBits = 8;
int pixelFormateID = ChoosePixelFormat(dc, &pfd);
SetPixelFormat(dc, pixelFormateID, &pfd);
HGLRC rc = wglCreateContext(dc);
wglMakeCurrent(dc, rc);
glClearColor(41.0f / 255.0f, 71.0f / 255.0f, 121.0f / 255.0f, 1.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
MSG msg;
while (true) {
if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) {
if (msg.message == WM_QUIT) {
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
DraeScene();
SwapBuffers(dc); //交换前景与背景缓冲
}
return 0;
}
其中图形绘制部分:
glClear(GL_COLOR_BUFFER_BIT); //清除背景颜色
glPointSize(30.0f); //设置点大小
glEnable(GL_POINT_SMOOTH); //圆点
//画点
glBegin(GL_POINTS); //开始绘制
glColor4ub(200, 0, 0, 255);
glVertex3f(0, 0, -100.0f);
glEnd(); //结束绘制
//画线
glLineWidth(4.0f); //线的粗细
glBegin(GL_LINES);
glColor4ub(200, 0, 0, 255);
glVertex3f(0, 0, -100.0f);
glColor4ub(0, 200, 0, 255);
glVertex3f(50.0f, 0, -100.0f);
glVertex3f(0, 50.0f, -100.0f);
glVertex3f(50.0f, 0, -100.0f);
glVertex3f(0, 50.0f, -100.0f);
glVertex3f(0, 0, -100.0f);
glEnd();
//画三角形
glBegin(GL_TRIANGLES);
glVertex3f(0, 0, -100.0f);
glVertex3f(0, -30.0f, -100.0f);
glVertex3f(-30.0f, 0, -100.0f);
glEnd();