/** Copyright 2013 BlackBerry Inc. Copyright (c) 2015-2016 Chukong Technologies Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Original file from GamePlay3D: http://gameplay3d.org This file was modified to fit the cocos2d-x project */ #include "renderer/CCVertexAttribBinding.h" #include "renderer/CCGLProgramState.h" #include "renderer/ccGLStateCache.h" #include "platform/CCGL.h" #include "base/CCConfiguration.h" #include "renderer/CCGLProgram.h" NS_CC_BEGIN std::string s_attributeNames[] = { GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::ATTRIBUTE_NAME_TEX_COORD1, GLProgram::ATTRIBUTE_NAME_TEX_COORD2, GLProgram::ATTRIBUTE_NAME_TEX_COORD3, GLProgram::ATTRIBUTE_NAME_NORMAL, GLProgram::ATTRIBUTE_NAME_BLEND_WEIGHT, GLProgram::ATTRIBUTE_NAME_BLEND_INDEX }; //static GLuint __maxVertexAttribs = 0; static std::vector __vertexAttribBindingCache; VertexAttribBinding::VertexAttribBinding() { } VertexAttribBinding::~VertexAttribBinding() { // Delete from the vertex attribute binding cache. std::vector::iterator itr = std::find(__vertexAttribBindingCache.begin(), __vertexAttribBindingCache.end(), this); if (itr != __vertexAttribBindingCache.end()) { __vertexAttribBindingCache.erase(itr); } CC_SAFE_RELEASE(_glProgramState); _attributes.clear(); if (_handle) { glDeleteVertexArrays(1, &_handle); _handle = 0; } } void VertexAttribBinding::bind() { } void VertexAttribBinding::unbind() { if (_handle) { // Hardware GL::bindVAO(0); } else { // Software glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } } uint32_t VertexAttribBinding::getVertexAttribsFlags() const { return _vertexAttribsFlags; } void VertexAttribBinding::parseAttributes() { CCASSERT(_glProgramState, "invalid glprogram"); _attributes.clear(); _vertexAttribsFlags = 0; auto glprogram = _glProgramState->getGLProgram(); for(auto &attrib: glprogram->_vertexAttribs) { VertexAttribValue value(&attrib.second); _attributes[attrib.first] = value; } } VertexAttribValue* VertexAttribBinding::getVertexAttribValue(const std::string& name) { const auto itr = _attributes.find(name); if( itr != _attributes.end()) return &itr->second; return nullptr; } void VertexAttribBinding::setVertexAttribPointer(const std::string &name, GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid* pointer) { auto v = getVertexAttribValue(name); if(v) { v->setPointer(size, type, normalized, stride, pointer); _vertexAttribsFlags |= 1 << v->_vertexAttrib->index; } else { CCLOG("cocos2d: warning: Attribute not found: %s", name.c_str()); } } NS_CC_END