Ogre源码分析与学习笔记-1

既然是3D引擎, 最感兴趣的当然是他的渲染机制了, 所以, 就从他的RenderSystem入手!

 用VC2005 打开Ogre.dsw, 然后导出为05工程, 就可以在VC中看到所有工程了.
RenderSystem头文件位于Ogre -> OgreMain -> Header Files中

RenderSystem是一个纯虚的基类, 抽象出了与3D SDKs 无关的接口操作, 包括两大部分: 渲染环境/渲染窗口的创建与管理, 3D模型的渲染.
我重点关心的是渲染机制, 也就是模型的渲染.

 

由于这个类只有基类, 而且每个函数都有详细注释, 所以就不说函数的用法了

 

几点心得:
1. 可以注册一个FrameListener 到 RenderSystem, 可以在绘制每一帧时得到通知, 这是一个典型的观察者模式, FrameListener 有两个回调:

        virtual bool frameStarted(const FrameEvent& evt) { return true; }
        virtual bool frameEnded(const FrameEvent& evt) { return true; }

 

2. Textrue的生成要通过RenderSystem来, 这又是个典型的类工厂模式, 至于Texture的详情, 下次再说.

		/** Creates and registers a render texture object.
			@param name 
				The name for the new render texture. Note that names must be unique.
			@param width
				The requested width for the render texture. See Remarks for more info.
			@param height
				The requested width for the render texture. See Remarks for more info.
			@returns
				On succes, a pointer to a new platform-dependernt, RenderTexture-derived
				class is returned. On failiure, NULL is returned.
			@remarks
				Because a render texture is basically a wrapper around a texture object,
				the width and height parameters of this method just hint the preferred
				size for the texture. Depending on the hardware driver or the underlying
				API, these values might change when the texture is created.
		*/
        virtual RenderTexture * createRenderTexture( const String & name, int width, int height ) = 0;


3. 渲染要通过RenderOperation, 这又是一个渲染平台独立的类, 主要定义了模型的数据, 包括vertices, normals, texture coordinates, color, blend info 等, 不支持VBO...

    class RenderOperation {
    public:
        /** Pointer to list of vertices (float {x, y z} * numVertices).
            @remarks
                If useIndexes is false each group of 3 vertices describes a face (anticlockwise winding) in
                trianglelist mode.
        */
        Real* pVertices;

        /// Optional vertex normals for vertices (float {x, y, z} * numVertices).
        Real* pNormals;

        /** Optional texture coordinates for vertices (float {u, [v], [w]} * numVertices).
            @remarks
                There can be up to 8 sets of texture coordinates, and the number of components per
                vertex depends on the number of texture dimensions (2 is most common).
        */
        Real* pTexCoords[OGRE_MAX_TEXTURE_COORD_SETS];

        /// Optional pointer to a list of diffuse vertex colours (32-bit RGBA * numVertices).
        RGBA* pDiffuseColour;
        /// Optional pointer to a list of specular vertex colours (32-bit RGBA * numVertices)
        RGBA* pSpecularColour;
        /** Optional pointer to a list of vertex blending details, organised in vertex order. 
            The number of weights per vertex is recorded in numBlendWeightsPerVertex - there must
            be this many for every vertex: set the weight to 0 for those vertices that don't 
            use all the entries (if some vertices have more than others)
        */
        VertexBlendData* pBlendingWeights;

        /** Pointer to a list of vertex indexes describing faces (only used if useIndexes is true).
            @note
                Each group of 3 describes a face (anticlockwise winding order).
        */
        unsigned short* pIndexes;

        /// Flags indicating vertex types
        int vertexOptions;
        /// The type of rendering operation.
        OpType operationType;

    };

 

4. 渲染的结果会输出到RenderTarget, RenderTarget可能是窗口, 也可能是一个纹理, 一个图片等等

    /** A 'canvas' which can receive the results of a rendering
        operation.
        @remarks
            This abstract class defines a common root to all targets of rendering operations. A
            render target could be a window on a screen, or another
            offscreen surface like a texture or bump map etc.
        @author
            Steven Streeting
        @version
            1.0
     */
    class _OgreExport RenderTarget


5. 其它一些状态管理函数

这一版的Ogre已经提供了四个RenderSystem: RenderSystem_Direct3D7, RenderSystem_Direct3D8, RenderSystem_Direct3D9, 和RenderSystem_GL, 如果有需要, 可以自己加新的, 继承RenderSystem, 实现接口就可以了.

 

题外话:
这是一篇关于状态管理的文章, 写的挺好http://dev.gameres.com/Program/Visual/3D/renderstate.htm
Ogre的函数分两类: 普通函数和以下划线开始的函数, 名字以_开始的函数称为Lower level methods, 一般用于Ogre函数的内部调用, 或者用于一些高级用法

    /**     
            one which will be used often by the caller of the Ogre library,
            and one which is at a lower level and will be used by the
            other classes provided by Ogre. These lower level
            methods are prefixed with '_' to differentiate them.
     */



 

你可能感兴趣的:(Ogre源码分析与学习笔记-1)