1. 程式人生 > >Ogre2.1 分析筆記(八) 實現天空盒

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

    }