Ogre2.1 分析笔记(八) 实现天空盒
2017-05-08 21:44
281 查看
1. 新建一个空工程添加如下代码,完成Ogre的初始化。
int WinMain(HINSTANCE
hInst, HINSTANCE
hPrevInstance, LPSTR
strCmdLine, INT
nCmdShow) {
Ogre::Root *root =
new Ogre::Root;
string renderSystemName =
"OpenGL 3+ Rendering Subsystem";
//在Root的构造函数中会直接启动OGL3+和DX11两种渲染系统,一下代码只是去获取渲染系统的指针
Ogre::RenderSystem *renderSystem =root->getRenderSystemByName(renderSystemName);
renderSystem->setConfigOption("Full Screen",
"No");
renderSystem->setConfigOption("Video Mode",
"800x600 @32-bit colour");
root->setRenderSystem(renderSystem);
Ogre::RenderWindow *renderWindow = root->initialise(true);
while (true) {
root->renderOneFrame();
}
}
2. 创建相机
Ogre::SceneManager *sceneManager =root->createSceneManager(Ogre::ST_GENERIC, 1, Ogre::INSTANCING_CULLING_SINGLETHREAD);
Ogre::Camera *camera =sceneManager->createCamera("MainCamera",
true, true);
camera->setPosition(0,5, 15);
camera->setNearClipDistance(0.2f);
camera->setFarClipDistance(1000.0f);
camera->setAutoAspectRatio(true);
3. 定义合成器。在SkyBox.compositor中添加以下定义。
compositor_node SkyBoxNode
{
in 0 renderWindow
target renderWindow
{
pass clear
{
colour_value0.5 1 1 1
}
passrender_quad
{
quad_normals camera_direction
m
4000
aterialSkyBox
}
}
}
workspace SkyBox
{
connect_outputSkyBoxNode 0
}
4. 添加材质文件。在SkyBox.material中添加以下代码
vertex_program SkyBox_vs glsl
{
sourceSkyBox_vs.glsl
default_params
{
param_named_autoworldViewProj worldviewproj_matrix
}
}
fragment_program SkyBox_ps glsl
{
sourceSkyBox_ps.glsl
default_params
{
param_namedskyCubemap int 0
}
}
material SkyBox
{
technique
{
pass
{
vertex_program_refSkyBox_vs
{
}
fragment_program_refSkyBox_ps
{
}
texture_unit
{
textureSaintPetersBasilica.dds cubic gamma
filtering trilinear
tex_address_mode clamp
}
}
}
}
5. 添加对应的顶点shader和像素shader。在SkyBox_vs.glsl中添加以下代码。
#version 330
in vec4 vertex;
in vec3 normal;
uniform mat4 worldViewProj;
out gl_PerVertex
{
vec4 gl_Position;
};
out block
{
vec3 cameraDir;
} outVs;
void main()
{
gl_Position = (worldViewProj * vertex).xyww;
outVs.cameraDir.xyz = normal.xyz;
}
在在SkyBox_ps.glsl中添加以下代码。
#version 330
uniform samplerCube skyCubemap;
in block
{
vec3 cameraDir;
} inPs;
out vec3 fragColour;
void main()
{
//Cubemapsare left-handed
fragColour= texture( skyCubemap, vec3( inPs.cameraDir.xy, -inPs.cameraDir.z ) ).xyz;
}
6. 在程序中添加资源
Ogre::ResourceGroupManager::getSingleton().addResourceLocation("C:/Users/aa/Documents/Visual Studio2015/Projects/ORGE/OgreSkyBox",
"FileSystem");
Ogre::ResourceGroupManager::getSingleton().addResourceLocation("C:/Users/aa/Documents/ogre/Samples/Media/materials/textures/Cubemaps",
"FileSystem");
Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
7. 在程序中添加工作空间
Ogre::CompositorManager2 *compositorManager = root->getCompositorManager2();
compositorManager->addWorkspace(sceneManager,renderWindow, camera,
"SkyBox", true);
8. 渲染循环
while (true) {
root->renderOneFrame();
}
int WinMain(HINSTANCE
hInst, HINSTANCE
hPrevInstance, LPSTR
strCmdLine, INT
nCmdShow) {
Ogre::Root *root =
new Ogre::Root;
string renderSystemName =
"OpenGL 3+ Rendering Subsystem";
//在Root的构造函数中会直接启动OGL3+和DX11两种渲染系统,一下代码只是去获取渲染系统的指针
Ogre::RenderSystem *renderSystem =root->getRenderSystemByName(renderSystemName);
renderSystem->setConfigOption("Full Screen",
"No");
renderSystem->setConfigOption("Video Mode",
"800x600 @32-bit colour");
root->setRenderSystem(renderSystem);
Ogre::RenderWindow *renderWindow = root->initialise(true);
while (true) {
root->renderOneFrame();
}
}
2. 创建相机
Ogre::SceneManager *sceneManager =root->createSceneManager(Ogre::ST_GENERIC, 1, Ogre::INSTANCING_CULLING_SINGLETHREAD);
Ogre::Camera *camera =sceneManager->createCamera("MainCamera",
true, true);
camera->setPosition(0,5, 15);
camera->setNearClipDistance(0.2f);
camera->setFarClipDistance(1000.0f);
camera->setAutoAspectRatio(true);
3. 定义合成器。在SkyBox.compositor中添加以下定义。
compositor_node SkyBoxNode
{
in 0 renderWindow
target renderWindow
{
pass clear
{
colour_value0.5 1 1 1
}
passrender_quad
{
quad_normals camera_direction
m
4000
aterialSkyBox
}
}
}
workspace SkyBox
{
connect_outputSkyBoxNode 0
}
4. 添加材质文件。在SkyBox.material中添加以下代码
vertex_program SkyBox_vs glsl
{
sourceSkyBox_vs.glsl
default_params
{
param_named_autoworldViewProj worldviewproj_matrix
}
}
fragment_program SkyBox_ps glsl
{
sourceSkyBox_ps.glsl
default_params
{
param_namedskyCubemap int 0
}
}
material SkyBox
{
technique
{
pass
{
vertex_program_refSkyBox_vs
{
}
fragment_program_refSkyBox_ps
{
}
texture_unit
{
textureSaintPetersBasilica.dds cubic gamma
filtering trilinear
tex_address_mode clamp
}
}
}
}
5. 添加对应的顶点shader和像素shader。在SkyBox_vs.glsl中添加以下代码。
#version 330
in vec4 vertex;
in vec3 normal;
uniform mat4 worldViewProj;
out gl_PerVertex
{
vec4 gl_Position;
};
out block
{
vec3 cameraDir;
} outVs;
void main()
{
gl_Position = (worldViewProj * vertex).xyww;
outVs.cameraDir.xyz = normal.xyz;
}
在在SkyBox_ps.glsl中添加以下代码。
#version 330
uniform samplerCube skyCubemap;
in block
{
vec3 cameraDir;
} inPs;
out vec3 fragColour;
void main()
{
//Cubemapsare left-handed
fragColour= texture( skyCubemap, vec3( inPs.cameraDir.xy, -inPs.cameraDir.z ) ).xyz;
}
6. 在程序中添加资源
Ogre::ResourceGroupManager::getSingleton().addResourceLocation("C:/Users/aa/Documents/Visual Studio2015/Projects/ORGE/OgreSkyBox",
"FileSystem");
Ogre::ResourceGroupManager::getSingleton().addResourceLocation("C:/Users/aa/Documents/ogre/Samples/Media/materials/textures/Cubemaps",
"FileSystem");
Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
7. 在程序中添加工作空间
Ogre::CompositorManager2 *compositorManager = root->getCompositorManager2();
compositorManager->addWorkspace(sceneManager,renderWindow, camera,
"SkyBox", true);
8. 渲染循环
while (true) {
root->renderOneFrame();
}
相关文章推荐
- Ogre2.1 分析笔记(四) 渲染流程(一)
- Ogre2.1 分析笔记(七) 材质脚本分析
- Ogre2.1 分析笔记(五) 渲染流程(二)
- Ogre2.1 分析笔记(二)-程序启动流程
- Ogre2.1 分析笔记(六) 合成器脚本分析
- Ogre2.1 分析笔记(三)-场景图结构
- Ogre2.1 分析笔记(一)
- 数据结构学习笔记之链表分析与实现(一)
- [转]简要分析Ogre的渲染队列实现原理(二)
- 数据结构学习笔记之链表分析与实现(二)
- 简要分析Ogre渲染队列的实现原理
- Linux Barrier I/O实现分析笔记
- web前端学习笔记-瀑布流的算法分析与代码实现
- Ogre源码分析与学习笔记-2 纹理
- Ogre源码分析与学习笔记-1
- Hadoop源码分析笔记(十):数据节点--流式接口的实现
- 项目分析笔记(1)---聊天室项目分析与实现(2008.6.11)
- 简要分析Ogre的渲染队列实现原理(二)
- 笔记:一个操作系统的实现第一章boot.asm文件分析
- 简要分析Ogre渲染队列的实现原理