vs2015-OpenGL绘制三角形
生活随笔
收集整理的這篇文章主要介紹了
vs2015-OpenGL绘制三角形
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
程序運行截圖如下:
?
代碼如下:
ggl.h
#pragma once #include <windows.h> #include <gl/GL.h> #include <gl/GLU.h> #include <gl/glext.h> #include <stdio.h> #include <math.h> #include <string.h> #include <string> #include <sstream> #include <vector> #include <functional>scene.h
#pragma once#include "ggl.h"void Init(); //初始化3D場景 void Draw(); //3D繪畫main.cpp
#include "ggl.h" #include "scene.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);return 0;}return DefWindowProc(hwnd, msg, wParam, lParam); }INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevlnstance, LPSTR lpCmdLine, 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 = GLWindowProc;wndclass.lpszClassName = L"GLWindow";wndclass.lpszMenuName = NULL;wndclass.style = CS_VREDRAW | CS_HREDRAW;ATOM atam = RegisterClassEx(&wndclass);if (!atam) {MessageBox(NULL, L"Notice", L"Error", MB_OK);return 0;}RECT rect;rect.left = 0;rect.right = 800;rect.top = 0;rect.bottom = 600;AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, NULL);int windowWidth = rect.right - rect.left;int windowHeight = rect.bottom - rect.top;HWND hwnd = CreateWindowEx(NULL, L"GLWindow", L"OpenGL Window", WS_OVERLAPPEDWINDOW, 100, 100, windowWidth, windowHeight, NULL, NULL, hInstance, NULL);//OpenGL相關//選定像素格式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.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); //使渲染環境生效Init();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();SwapBuffers(dc);}return 0; }?
scene.cpp
#include "scene.h"void Init() {//設置當前矩陣glMatrixMode(GL_PROJECTION); //設置為投影矩陣(對矩陣造成影響的代碼,都會影響當前矩陣)//第一個參數是垂直方面的視角,第二個是寬和高的比,第三個是最近可以看到的距離,第四個是最遠距離gluPerspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f);glMatrixMode(GL_MODELVIEW);//把當前矩陣切換為模型視圖矩陣glLoadIdentity(); //加載一個單位矩陣 }void Draw() {glClearColor(0.0f, 0.0f, 0.0f, 1.0f); //擦除背景使用的顏色(分別表示顏色分量的值)glClear(GL_COLOR_BUFFER_BIT); //擦除當前背景顏色//開始繪制圖形(三角形)glBegin(GL_TRIANGLES);glColor4ub(255, 255, 255, 255); //設置當前顏色glVertex3f(-0.2f, -0.2f, -1.5f); //設置頂點,使用當前顏色進行著色glColor4ub(255, 0, 0, 255);glVertex3f(0.2f, -0.2f, -1.5f);glColor4ub(0, 255, 0, 255);glVertex3f(0.0f, 0.2f, -1.5f);glEnd(); }?
總結
以上是生活随笔為你收集整理的vs2015-OpenGL绘制三角形的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ STL list排序
- 下一篇: Qt工作笔记-QGraphics重设场景