欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

Cocos2dx源码记录(11) CCPrimtiveCommand,CCPrimetive

发布时间:2023/12/20 编程问答 35 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Cocos2dx源码记录(11) CCPrimtiveCommand,CCPrimetive 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

#1 CCPrimetive 因为CCPrimtiveCommand里有用到这个就先分析下这个吧 按照字面的意思应该是由一系列顶点属性和带有顺序的顶点索引的图元面片类

/**Primitive can support sending points, lines and triangles to glpipeline, which is an abstractionof primitive data.*/ class CC_DLL Primitive : public Ref { public:/**Create an instance of primitive.@param verts VertexData used in the primitive.@param indices Optional index data.@param type The type (Points, Lines, Triangles) used.*/static Primitive* create(VertexData* verts, IndexBuffer* indices, int type);/**Get the vertexData.*/const VertexData* getVertexData() const;/**Get the optional index data, will return null if index data is not used.*/const IndexBuffer* getIndexData() const;/**Get the primitive type.*/int getType() const { return _type; }/**called by rendering framework, will send the data to GLPipeline.*/void draw();/**Get the start index of primitive.*/int getStart() const { return _start; }/**Get the number of vertices or indices used for drawing.*/int getCount() const { return _count; }/**Setter for the start index.*/void setStart(int start);/**Setter for the count. */void setCount(int count);protected:Primitive();virtual ~Primitive();bool init(VertexData* verts, IndexBuffer* indices, int type);protected:VertexData* _verts;IndexBuffer* _indices;int _start;int _count;int _type; }; 复制代码

##1.1

void Primitive::draw() {/*void VertexData::use() {uint32_t flags(0);for(auto& element : _vertexStreams){flags = flags | (1 << element.second._stream._semantic);}GL::enableVertexAttribs(flags);int lastVBO = -1;for(auto& element : _vertexStreams){//glEnableVertexAttribArray((GLint)element.second._stream._semantic);auto vertexStreamAttrib = element.second._stream;auto vertexBuffer = element.second._buffer;// don't call glBindBuffer() if not needed. Expensive operation.int vbo = vertexBuffer->getVBO();if (vbo != lastVBO) {glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer->getVBO());lastVBO = vbo;}glVertexAttribPointer(GLint(vertexStreamAttrib._semantic),vertexStreamAttrib._size,vertexStreamAttrib._type,vertexStreamAttrib._normalize,vertexBuffer->getSizePerVertex(),(GLvoid*)((long)vertexStreamAttrib._offset));} }*/if(_verts){_verts->use();if(_indices!= nullptr){GLenum type = (_indices->getType() == IndexBuffer::IndexType::INDEX_TYPE_SHORT_16) ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indices->getVBO());size_t offset = _start * _indices->getSizePerIndex();glDrawElements((GLenum)_type, _count, type, (GLvoid*)offset);}else{glDrawArrays((GLenum)_type, _start, _count);}glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);glBindBuffer(GL_ARRAY_BUFFER, 0);} } 复制代码

#2 CCPrimtiveCommand

class CC_DLL PrimitiveCommand : public RenderCommand { public:/**@{Constructor and Destructor.*/PrimitiveCommand();~PrimitiveCommand();/**@}*//** Initializes the command.@param globalOrder GlobalZOrder of the command.@param textureID The openGL handle of the used texture.@param glProgramState The specified glProgram and its uniform.@param blendType Blend function for the command.@param primitive Rendered primitive for the command.@param mv ModelView matrix for the command.@param flags to indicate that the command is using 3D rendering or not.*/void init(float globalOrder, GLuint textureID, GLProgramState* glProgramState, BlendFunc blendType, Primitive* primitive, const Mat4& mv, uint32_t flags);CC_DEPRECATED_ATTRIBUTE void init(float globalOrder, GLuint textureID, GLProgramState* glProgramState, BlendFunc blendType, Primitive* primitive,const Mat4& mv);/**Get the generated material ID.*/inline uint32_t getMaterialID() const { return _materialID; }/**Get the texture ID used for drawing.*/inline GLuint getTextureID() const { return _textureID; }/**Get the glprogramstate used for drawing.*/inline GLProgramState* getGLProgramState() const { return _glProgramState; }/**Get the blend function for drawing.*/inline BlendFunc getBlendType() const { return _blendType; }/**Get the modelview matrix when draw the primitive.*/inline const Mat4& getModelView() const { return _mv; }/**Execute and draw the command, called by renderer.*/void execute() const; protected:uint32_t _materialID;GLuint _textureID;GLProgramState* _glProgramState;BlendFunc _blendType;Primitive* _primitive;Mat4 _mv; }; 复制代码

##2.1 execute函数 每个非Triangle跟quad 类型的command应该都会执行这个execute这个函数 哟哟哟 用到了 Primtive 的 draw() 函数

void PrimitiveCommand::execute() const {//Set textureGL::bindTexture2D(_textureID);//set blend modeGL::blendFunc(_blendType.src, _blendType.dst);_glProgramState->apply(_mv);_primitive->draw();CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,_primitive->getCount()); } 复制代码

实际上相当于还是用的Primitive的draw函数

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

总结

以上是生活随笔为你收集整理的Cocos2dx源码记录(11) CCPrimtiveCommand,CCPrimetive的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。