[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(, 1.0f, 0.1f, );
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(, , ), heart, redips::float3(, , ));
return true;
}
void RenderScene(){
m_mesh->uniformFloat3("lightColor", redips::float3(, , ));
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(, , , ),
this, //this is the parent
);
05-27 21:02