Ogre 13.x+Qt5-例子整理
最新的Ogre13.x使用OgreBites::ApplicationContextQt 整合Qt5,不过他的example很少,例子都在他的实现代码中而且现在的Ogre版本都是推QWindow而不是QWidget,因为Qt5以后实现比Qt4差别很大,其实他的例子也是得到QWindow的windows 句柄,由于Ogre是在DX和OpenGL之上做了一层,有很多自己设计的脚本和材质,文档很少比较折腾,包装层次多需要摸索,一些图形行业业务功能性能要求高点的要对代码摸索的比较多才能深度改,这都增加了不少难度但是不错的3D学习资料,国内比起OSG生态来说相对较弱。
下面的例子作为笔记备份,Qt是Qt5.14.2,Ogre13.6.5 官方的MSVCSDK ,VC2019下面vc2017也就是v141 toolset,需要先系统环境变量配置 OGRE_CONFIG_DIR,即sdk解压后bin那个完整路径,bin里面有plugins.cfg放的有一些插件,一般开始运行会弹出一个配置对话框,去选Dx还是OpenGL,全屏还是非全屏,初始窗口尺寸等,然后确定,就会生成一个Ogre.cfg现在的Ogre13.x不知道怎么回事生成的ogre.cfg在c盘的用户哪个犄角嗄啦目录下,我是看代码发现它写在那的,再次他会去读这个Ogre.cfg其实也能代码去设置。
下面的demo是创建了个QWindow然后作为Ogre的ApplicationContextQt的渲染窗口,调setup中的创建sceneManager,light,camera,然后设置为QMainWindow的centerWidget的,然后主要是创建了一个Ortho的camera,在resize时候调整,用ManualObject绘制点线或者三角形。
QtWidgetTest.h
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QtWidgetTest.h"
class QtWidgetTest : public QMainWindow
{
Q_OBJECT
public:
QtWidgetTest(QWidget *parent = Q_NULLPTR);
private:
Ui::QtWidgetTestClass ui;
};
QtWidgetTest.cpp
#include "QtWidgetTest.h"
#include <QKeyEvent>
#include <QtGui/QWindow>
#include "OgreApplicationContextQt.h"
#include "Ogre.h"
#include "OgreInput.h"
#include "OgreRTShaderSystem.h"
#include "OgreCamera.h"
#include <iostream>
using namespace Ogre;
using namespace OgreBites;
class MyAppQt : public OgreBites::ApplicationContextQt, public OgreBites::InputListener
{
public:
Camera* cam;
MyAppQt()
{}
virtual void windowResized(Ogre::RenderWindow* win)
{
OgreBites::ApplicationContextQt::windowResized(win);
Ogre::Real hw = win->getWidth() * 0.5;
Ogre::Real hh = win->getHeight() * 0.5;
cam->setAspectRatio(hw / hh);
//cam->setFrustumExtents(-hw, hw, hh, -hh);
auto pty = cam->getProjectionType();
cam->setOrthoWindow(hw * 2, hh * 2);
// cam->getViewport()->setDimensions(0, 0, 1, 1);
auto _up = cam->getRealUp();
auto _dr = cam->getRealDirection();
auto _rt = cam->getRealRight();
auto _pos = cam->getRealPosition();
}
void setup()
{
OgreBites::ApplicationContextQt::setup();
Root* root = getRoot();
SceneManager* scnMgr = root->createSceneManager();
RTShader::ShaderGenerator* shadergen = RTShader::ShaderGenerator::getSingletonPtr();
shadergen->addSceneManager(scnMgr);
scnMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));
Light* light = scnMgr->createLight("MainLight");
SceneNode* lightNode = scnMgr->getRootSceneNode()->createChildSceneNode();
lightNode->attachObject(light);
lightNode->setPosition(20, 80, 50);
SceneNode* camNode = scnMgr->getRootSceneNode()->createChildSceneNode();
cam = scnMgr->createCamera("myCam");
cam->setNearClipDistance(5);
camNode->attachObject(cam);
getRenderWindow()->addViewport(cam);
{
ManualObject* cube = scnMgr->createManualObject("cube");
cube->begin("BaseWhiteNoLighting", Ogre::RenderOperation::OT_LINE_STRIP);
cube->position(0, 0, 0);
cube->colour(1.0, 1.0, 1.0);
cube->position(100, 0, 0);
cube->colour(1.0, 1.0, 1.0);
cube->position(100, 100, 0);
cube->colour(1.0, 1.0, 1.0);
//cube->position(-120, -120, 100);
//cube->colour(0.0, 0.0, 1.0);
//cube->position(-100, 100, -100);
//cube->colour(0.0, 1.0, 0.0);
//cube->position(100, 100, -100);
//cube->colour(1.0, 1.0, 0.0);
//cube->position(120, 120, 100);
//cube->colour(1.0, 1.0, 1.0);
//cube->position(-120, 120, 100);
//cube->colour(0.0, 1.0, 1.0);
cube->index(0);
cube->index(1);
cube->index(2);
//cube->triangle(0, 2, 3);
//cube->triangle(3, 4, 0);
//cube->triangle(3, 7, 4);
//cube->triangle(4, 7, 6);
//cube->triangle(4, 6, 5);
//cube->triangle(5, 2, 1);
//cube->triangle(5, 6, 2);
//cube->triangle(0, 4, 1);
//cube->triangle(5, 1, 4);
//cube->triangle(3, 6, 7);
//cube->triangle(3, 2, 6);
cube->end();
SceneNode* sn = scnMgr->getRootSceneNode()->createChildSceneNode();
sn->attachObject(cube);
sn->_updateBounds();
auto aabb = sn->_getWorldAABB();
}
//Entity* ogreEntity = scnMgr->createEntity("knot.mesh");
//SceneNode* ogreNode = scnMgr->getRootSceneNode()->createChildSceneNode();
//ogreNode->attachObject(ogreEntity);
camNode->setPosition(0, 0, 200);
//camNode->lookAt(Ogre::Vector3(0, 0, 0), Node::TransformSpace::TS_WORLD);
//Ogre::Quaternion camQ;
//camQ.FromAxes(Ogre::Vector3(1, 0, 0), Ogre::Vector3(0, -1, 0), Ogre::Vector3(0, 0, -1));
//camNode->setOrientation(camQ);
cam->setProjectionType(PT_ORTHOGRAPHIC);
//cam->setWindow(0, 0, 1, 1);
//cam->setOrthoWindow(600, 600);
auto _up = cam->getRealUp();
auto _dr = cam->getRealDirection();
auto _rt = cam->getRealRight();
auto _pos = cam->getRealPosition();
cam->setNearClipDistance(100);
cam->setFarClipDistance(600);
}
};
MyAppQt mapp;
QtWidgetTest::QtWidgetTest(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
QWindow* mwin = new QWindow();
mwin->setGeometry(100, 100, 800, 800);
mapp.injectMainWindow(mwin);
mapp.initApp();
mapp.startTimer(30);
QWidget* wgt = QWidget::createWindowContainer(mwin);
setCentralWidget(wgt);
}