Ogre2.1 分析笔记(八) 实现天空盒

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

         materialSkyBox

     }

 

}

}

 

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();

    }



©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页