[email protected]">

当前位置:网站首页>Ogre入门尝鲜

Ogre入门尝鲜

2022-07-07 11:05:00 MirrorYuChen

1.Ogre库编译

1.1 下载安装DIrectX SDK

从下面链接下载和安装DirectX SDK:
下载地址:DirectX SDK

1.2 从git拉取最新代码

>> git clone --recursive [email protected]:OGRECave/ogre.git
>> git checkout v13.4.1

1.3 使用CMake编译Ogre

这里推荐使用cmake-gui,按照我这边的经验,一次就可以编译通过,最后在编译路径下会生成一个sdk文件夹,里面包含所有编译文件。

2.Ogre配置

2.1 将sdk子目录下的bin目录添加到path路径

2.2 新建一个vs工程,配置sdk路径下的include和lib

include路径添加如下:
添加ogre的include
lib路径添加如下:
lib路径
添加附加依赖项:
附加依赖项

3.添加测试代码

#include <iostream>

#include "Ogre.h"
#include "OgreApplicationContext.h"
#include "OgreInput.h"
#include "OgreRTShaderSystem.h"

using namespace Ogre;
using namespace OgreBites;

class Basic : 
	public ApplicationContext,
	public InputListener {
public:
	Basic();
	virtual ~Basic() {}

	void setup();
	bool keyPressed(const KeyboardEvent& evt);
};

Basic::Basic() : ApplicationContext("Ogre") {}

void Basic::setup() {
	// 1.调用父类
	ApplicationContext::setup();
	addInputListener(this);

	// 2.获取一个指针指向已经创建的root
	Root* root = getRoot();
	SceneManager* scnMgr = root->createSceneManager();

	// 3.注册我们的scene
	RTShader::ShaderGenerator* shadergen = RTShader::ShaderGenerator::getSingletonPtr();
	shadergen->addSceneManager(scnMgr);

	// 4.调节灯光
	scnMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));

	// 5.新灯光
	Light* light = scnMgr->createLight("MainLight");
	SceneNode* lightNode = scnMgr->getRootSceneNode()->createChildSceneNode();
	lightNode->attachObject(light);

	// 6.灯光位置
	lightNode->setPosition(20, 80, 50);

	// 7.相机
	SceneNode* camNode = scnMgr->getRootSceneNode()->createChildSceneNode();
	Camera* cam = scnMgr->createCamera("myCam");
	cam->setNearClipDistance(5);
	cam->setAutoAspectRatio(true);
	camNode->attachObject(cam);
	camNode->setPosition(0, 0, 140);

	// 8.告诉他渲染到主窗口
	getRenderWindow()->addViewport(cam);

	// 9.Entity
	Entity* ogreEntity = scnMgr->createEntity("ogrehead.mesh");
	SceneNode* ogreNode = scnMgr->getRootSceneNode()->createChildSceneNode();
	ogreNode->attachObject(ogreEntity);
	// 10.移动相机
	camNode->setPosition(0, 47, 222);

	// 11.Entity2
	Entity* ogreEntity2 = scnMgr->createEntity("ogrehead.mesh");
	SceneNode* ogreNode2 = scnMgr->getRootSceneNode()->createChildSceneNode(Vector3(84, 48, 0));
	ogreNode2->attachObject(ogreEntity2);

	// 12.Entity3
	Entity* ogreEntity3 = scnMgr->createEntity("ogrehead.mesh");
	SceneNode* ogreNode3 = scnMgr->getRootSceneNode()->createChildSceneNode();
	ogreNode3->setPosition(0, 104, 0);
	ogreNode3->setScale(2, 1.2, 1);
	ogreNode3->attachObject(ogreEntity3);

	// 13.Entity4
	Entity* ogreEntity4 = scnMgr->createEntity("ogrehead.mesh");
	SceneNode* ogreNode4 = scnMgr->getRootSceneNode()->createChildSceneNode();
	ogreNode4->setPosition(-84, 48, 0);
	ogreNode4->roll(Degree(-90));
	ogreNode4->attachObject(ogreEntity4);
}

bool Basic::keyPressed(const KeyboardEvent& evt) {
	if (evt.keysym.sym == SDLK_ESCAPE) {
		getRoot()->queueEndRendering();
	}
	return true;
}

int main(int argc, char** argv) {
	try {
		Basic app;
		app.initApp();
		app.getRoot()->startRendering();
		app.closeApp();
	}
	catch (const std::exception& e) {
		std::cerr << "Error occurred during execution: " << e.what() << '\n';
		return 1;
	}

	return 0;
}

4.处理资源加载问题

4.1 将sdk的bin文件夹下面所有.dll文件和.cfg文件拷贝到工程生成的.exe文件夹下

>> E:\vs_project\OgreTest\x64\Release

4.2 修改resources.cfg文件,设置资源加载路径

# Ogre Core Resources
[OgreInternal]
FileSystem=../../Media/Main
FileSystem=../../Media/RTShaderLib/GLSL
FileSystem=../../Media/Terrain/

# Resources required by the sample browser and most samples.
[Essential]
Zip=../../Media/packs/SdkTrays.zip
Zip=../../Media/packs/profiler.zip
FileSystem=../../Media/thumbnails

# Common sample resources needed by many of the samples.
# Rarely used resources should be separately loaded by the
# samples which require them.
[General]
# PBR media must come before the scripts that reference it
FileSystem=../../Media/PBR
FileSystem=../../Media/PBR/filament
FileSystem=../../Media/materials/textures/PBR

FileSystem=../../Media/materials/programs/GLSL
FileSystem=../../Media/materials/programs/GLSL120
FileSystem=../../Media/materials/programs/GLSL150
FileSystem=../../Media/materials/programs/GLSL400
FileSystem=../../Media/materials/programs/GLSLES
FileSystem=../../Media/materials/programs/SPIRV
FileSystem=../../Media/materials/programs/Cg
FileSystem=../../Media/materials/programs/HLSL
FileSystem=../../Media/materials/programs/HLSL_Cg
FileSystem=../../Media/materials/scripts
FileSystem=../../Media/materials/textures
FileSystem=../../Media/materials/textures/terrain
FileSystem=../../Media/models
FileSystem=../../Media/particle
FileSystem=../../Media/DeferredShadingMedia
FileSystem=../../Media/DeferredShadingMedia/DeferredShading/post
FileSystem=../../Media/PCZAppMedia
FileSystem=../../Media/materials/scripts/SSAO
FileSystem=../../Media/materials/textures/SSAO
FileSystem=../../Media/volumeTerrain
FileSystem=../../Media/CSMShadows
Zip=../../Media/packs/cubemap.zip
Zip=../../Media/packs/cubemapsJS.zip
Zip=../../Media/packs/dragon.zip
Zip=../../Media/packs/fresneldemo.zip
Zip=../../Media/packs/ogredance.zip
Zip=../../Media/packs/Sinbad.zip
Zip=../../Media/packs/skybox.zip
Zip=../../Media/volumeTerrain/volumeTerrainBig.zip

Zip=../../Media/packs/DamagedHelmet.zip
Zip=../../Media/packs/filament_shaders.zip

[BSPWorld]
Zip=../../Media/packs/oa_rpg3dm2.pk3
Zip=../../Media/packs/ogretestmap.zip

# Materials for visual tests
[Tests]
FileSystem=../Tests/Media

4.3 将sdk下资源文件Media夹拷贝到设置的路径

资源文件夹

4.4 再次运行测试例子

测试用例效果

参考资料

原网站

版权声明
本文为[MirrorYuChen]所创,转载请带上原文链接,感谢
https://blog.csdn.net/sinat_31425585/article/details/125578763