【OpenGL从入门到精通(五)】三角形的绘制

it2023-05-19  72

#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 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; int pixelFormat = ChoosePixelFormat(dc, &pfd); SetPixelFormat(dc, pixelFormat, &pfd); HGLRC rc = wglCreateContext(dc); wglMakeCurrent(dc, rc);//setup opengl context complete //opengl init glMatrixMode(GL_PROJECTION);//tell the gpu processer that i would select the projection matrix gluPerspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f);//set some values to projection matrix glMatrixMode(GL_MODELVIEW);//tell .... model view matrix glLoadIdentity(); glClearColor(0.1f, 0.4f, 0.6f, 1.0f);//set "clear color" for background //show window ShowWindow(hwnd, SW_SHOW); UpdateWindow(hwnd); glEnable(GL_CULL_FACE);//逆时针方向为正面 //front face : ccw -> counter clock wind //glFrontFace(GL_CW);设置顺时针为正面 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); glBegin(GL_TRIANGLE_FAN);//start to draw something n-2 /* * Bengin和End间 * GL_TRIANGLE,每三个点组成一个三角形 * GL_TRIANGLE_STRIP ,如果遇到第一个点是奇数点连接会用n+1,n+2,n+3 * 组成三角形,偶数点时候连接顺序N+1->N->N+2(二者选点统一) * GL_TRIANGLE_FAN,每个三角形都以第一个点为原点. */ glColor4ub(255, 0, 0, 255); glVertex3f(0.0f, 0.0f, -10.0f); glColor4ub(0, 0, 255, 255); glVertex3f(-5.0f, -2.0f, -10.0f); glColor4ub(0, 255, 0, 255); glVertex3f(-3.0f, -2.0f, -10.0f); glColor4ub(0, 0, 255, 255); glVertex3f(-1.0f, -2.0f, -10.0f); glColor4ub(0, 255, 255, 255); glVertex3f(2.0f, -2.0f, -10.0f); glEnd();//end //present scene SwapBuffers(dc); } return 0; }

 

最新回复(0)