日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++ opengl 绘制三角形带

發布時間:2025/3/15 c/c++ 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++ 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

agma 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_TRIANGLE_STRIP);glColor4ub(255, 0, 0, 255); glVertex3f(-0.5f, -0.25f, -2.0f);glColor4ub(0, 0, 255, 255); glVertex3f(0.5f, -0.25f, -2.0f);glColor4ub(0, 255, 0, 255); glVertex3f(-0.5f, 0.25f, -2.0f);glColor4ub(0, 255, 0, 255); glVertex3f(0.5f, 0.25f, -2.0f);glColor4ub(255, 0, 0, 255); glVertex3f(-0.5f, 0.75f, -2.0f);glColor4ub(0, 0, 255, 255); glVertex3f(0.5f, 0.75f, -2.0f);glEnd(); }

總結

以上是生活随笔為你收集整理的C++ opengl 绘制三角形带的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。