• MFC+OpenGL可编程管线


    [github链接]

    网上的代码大都是固定管线渲染的,今天下午整理了下,把setPixelFormat、初始化glew、创建GL 4,2 context等操作封装到一个MFC类OpenGLWidget里。使用步骤

    1. 把OpenGLWidget.h和OpenGLWidget.cpp包含在项目里面。

    2. 继承类OpenGLWidget,实现两个虚函数:Initialize()[负责加载数据]、RenderScene()[负责渲染]两个函数。比如下面的LeftWindow类:

    #pragma once
    #include "OpenGLWidget.h"
    
    #include <Cameras/phc.h>
    #include <OpenglWrappers/DemoMeshes/BlinnPhongMesh.h>
    
    class LeftWindow : public OpenGLWidget{
        redips::PhC *m_phc = new redips::PhC(45, 1.0f, 0.1f, 10000);
        redips::BlinnPhongMesh* m_mesh = nullptr;
    public:
        LeftWindow(){}
        ~LeftWindow(){
        delete m_mesh; delete m_phc;
    }
    bool Initialize(){ m_mesh = new redips::BlinnPhongMesh(new redips::Triangles("E:/Documents/models/Effier/new_iffier.obj"));
         auto heart
    = m_mesh->model_ptr()->aabb_R().heart(); m_phc->lookAt(heart + redips::float3(0, 0, 500), heart, redips::float3(0, 1, 0)); return true; } void RenderScene(){ m_mesh->uniformFloat3("lightColor", redips::float3(1, 1, 1)); m_mesh->uniformFloat3("lightPos", m_phc->pos()); m_mesh->uniformFloat3("cameraPos", m_phc->pos()); m_mesh->uniformMat44f("model", redips::Mat44f::eye().ptr()); m_mesh->uniformMat44f("projection", m_phc->glProjection().ptr()); m_mesh->uniformMat44f("view", m_phc->glView().ptr()); m_mesh->draw(); } };

    [redips是我封装的渲染相关的类,详细介绍]

    3. 将下面代码添加到OnInitDialog()函数里,即在当前对话框里创建了一个OpenGLWidget

    m_leftWindow = new LeftWindow;
    m_leftWindow ->Create(NULL, NULL, WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE,
                                CRect(0, 0, 800, 800),
                                this,   //this is the parent
                                0);
  • 相关阅读:
    最小的k个数
    复杂链表的复制
    二叉树中和为某一值的路径
    二叉搜索树的后序遍历序列
    STL之Deque容器
    STL之迭代器
    STL之Vector容器
    STL之string
    STL(标准模板库)基本概念
    文件I/O
  • 原文地址:https://www.cnblogs.com/redips-l/p/8999184.html
Copyright © 2020-2023  润新知