1.想要绘制一个点,首先要在OpenGL初始化中先设置矩阵
2.然后在绘制场景中进行点的绘制。其中包括 当前颜色设置;点的位置,点的大小等等
#include <windows.h>
#include<gl/GL.h>
#include<gl/GLU.h>
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glu32.lib")
LRESULT CALLBACK GLWindowProc(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)
{
//register window
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 = GLWindowProc;
wndclass.lpszClassName = L"GLWindow";
wndclass.lpszMenuName = NULL;
wndclass.style = CS_VREDRAW | CS_HREDRAW;
ATOM atom = RegisterClassEx(&wndclass);
if (!atom)
{
return 0;
}
//create window
HWND hwnd = CreateWindowEx(NULL, L"GLWindow", L"OpenGL Window", WS_OVERLAPPEDWINDOW,
100, 100, 800, 600, NULL, NULL, hInstance, NULL);
//create opengl render context HDC是Windows的设备描述表句柄。
HDC dc = GetDC(hwnd);
//颜色描述符,渲染的像素格式
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
pfd.nVersion = 1;
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.cColorBits = 32;//颜色缓冲区
pfd.cDepthBits = 24;//深度缓冲区
pfd.cStencilBits = 8;//
pfd.iPixelType = PFD_TYPE_RGBA;//像素格式
pfd.iLayerType = PFD_MAIN_PLANE;//分层式
pfd.dwFlags = PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER;//渲染到窗口上,支持OPENGL像素格式,双缓冲
//选择像素格式
int pixelFormat = ChoosePixelFormat(dc,&pfd);
SetPixelFormat(dc, pixelFormat, &pfd);
//创建OPenGL的渲染设备,
HGLRC rc = wglCreateContext(dc);
//把rc和dc设置成当前的渲染设备
wglMakeCurrent(dc, rc);
//初始化OpenGL的
/*画点需要先设置矩阵*/
glMatrixMode(GL_PROJECTION);//告诉显卡要操作投影矩阵
gluPerspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f);//设置投影矩阵
glMatrixMode(GL_MODELVIEW);//设置模型矩阵
glLoadIdentity();//给它一个单位矩阵
glClearColor(0.1, 0.4, 0.6, 1.0);//设置清除缓冲区背景颜色
//show window
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);
}
//draw scene绘制场景
glClear(GL_COLOR_BUFFER_BIT);
glColor4ub(255, 255, 255, 255);//设置当前颜色
glPointSize(20.0f);
glBegin(GL_POINTS);//从当前点取颜色
glVertex3f(0.0f, 0.0f, -0.5f);//画点的数据
glEnd();//end
//present scene 前面缓冲区
//后缓冲区交换到前缓冲区
SwapBuffers(dc);
}
return 0;
}