Cocos2dx源码记录(5) CCRenderCommand

#1RenderCommand 在以前的时候,Cocos2dx的刷新界面的方式是直接将GL刷新frameBuffer对象的方法都写在对应的DrawCall方法里,现在为了更好地提升渲染性能,现在各自显示对象的draw方法里调用的是一个全新的API visit系列函数,在这些函数里面试根据显示对象的一系列属性创建对应的RenderCommad渲染命令,添加到相应的RenderCommandQueue中,最后使用总的渲染调用对渲染队列的各个command进行渲染。在添加时可以把使用同一个纹理对象的显示对象的RenderCommand添加到同一队列中,opengl的实现原理和状态机类似,相同批次的渲染可以在不改变状态的同时大量显示相同的图形,大大提高了渲染效率。 ##1.1 属性及方法

class CC_DLL RenderCommand
{
public:
    /**Enum the type of render command. */
    /**各种类型RenderCommand的枚举值 */
    enum class Type
    {
        /** Reserved type.*/
        UNKNOWN_COMMAND,
        /** Quad command, used for draw quad.*/
        /** 矩形*/
        QUAD_COMMAND,
        /**Custom command, used for calling callback for rendering.*/
        CUSTOM_COMMAND,
        /**Batch command, used for draw batches in texture atlas.*/
        /* 批次渲染命令(相同纹理及相同着色器对象)*/
        BATCH_COMMAND,
        /**Group command, which can group command in a tree hierarchy.*/
        /** 组渲染(包含的渲染命令 有节点层次关系)*/
        GROUP_COMMAND,
        /* 网格渲染 (主要用于模型渲染, terrian 地图渲染)*/
        /**Mesh command, used to draw 3D meshes.*/
        MESH_COMMAND,
        /** 图元渲染 划线啊 三角形 啊点啥的(opengl提供的三种基础图元)*/
        /**Primitive command, used to draw primitives such as lines, points and triangles.*/
        PRIMITIVE_COMMAND,
        /**Triangles command, used to draw triangles.*/
        TRIANGLES_COMMAND


        /* 嘿呀没有发现曲面细分相关的, 不知道opengles2.0 有没有tesselation*/
    };

    /**
     Init function, will be called by all the render commands.
     @param globalZOrder The global order of command, used for rendercommand sorting.
     @param modelViewTransform Modelview matrix when submitting the render command.
     @param flags Flag used to indicate whether the command should be draw at 3D mode or not.
     */
    void init(float globalZOrder, const Mat4& modelViewTransform, uint32_t flags);

    /** 获取全局z值 主要用于深度测试和组渲染时的显示问题*/
    /** Get global Z order. */
    inline float getGlobalOrder() const { return _globalOrder; }

    /** 获取具体是什么Command*/
    /** Returns the Command type. */
    inline Type getType() const { return _type; }
    
    /** 判断是不是透明的不是的话RGBA的A值直接设为1.0(255 or 127)*/
    /** Returns whether is transparent. */
    inline bool isTransparent() const { return _isTransparent; }
    
    /** Set transparent flag. */
    inline void setTransparent(bool isTransparent) { _isTransparent = isTransparent; }
    /**
     Get skip batching status, if a rendering is skip batching, it will be forced to be rendering separately.
     */
    /** 如果为false 则作为单独批次处理,true 则加入到当前批次中一起渲染*/
    inline bool isSkipBatching() const { return _skipBatching; }
    /**Set skip batching.*/
    inline void setSkipBatching(bool value) { _skipBatching = value; }
    /**Whether the command should be rendered at 3D mode.*/
    /** 是否作为3D对象 渲染(3D 渲染 和2D渲染还是有很大区别的)*/
    inline bool is3D() const { return _is3D; }
    /**Set the command rendered in 3D mode or not.*/
    inline void set3D(bool value) { _is3D = value; }
    /**Get the depth by current model view matrix.*/
    /**获取以当前视锥体为基础的 depth 值 做丢弃处理, 避免不必要的刷新 */
    inline float getDepth() const { return _depth; }
    
protected:
    /**Constructor.*/
    RenderCommand();
    /**Destructor.*/
    virtual ~RenderCommand();
    //used for debug but it is not implemented.
    void printID();

    /**Type used in order to avoid dynamic cast, faster. */
    Type _type;

    /** Commands are sort by global Z order. */
    float _globalOrder;
    
    /** Transparent flag. */
    bool  _isTransparent;
    
    /**
     QuadCommand and TrianglesCommand could be auto batched if there material ID is the same, however, if
     a command is skip batching, it would be forced to draw in a separate function call, and break the batch.
     */
    bool _skipBatching;
    
    /** Is the command been rendered on 3D pass. */
    bool _is3D;
    
    /** Depth from the model view matrix.*/
    float _depth;
};
复制代码

一些需要讲解的地方已在上面代码里通过注释的方式说明 PS:等到后面记录完几个重要的RenderCommand后, 会写一篇关于Cocos2dx 完整渲染流程的说明

转载于:https://juejin.im/post/5a30dd46f265da432840d93b

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值