C++ opengl 绘制三角形带

程序运行截图如下:
C++ opengl 绘制三角形带_第1张图片

程序源码如下:
ggl.h

#pragma once
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

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/C++,OpenGL)