void OpenGLBatch::draw(){ unsigned int i, nFormats = formats.getCount(); if (GL_ARB_vertex_buffer_object_supported && vertexBuffer != 0){ glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexBuffer); } for (i = 0; i < nFormats; i++){ Format format = formats[i]; GLenum type = getGLType(format.attFormat); GLenum array = arrayType[format.attType]; char *basePointer = (vertexBuffer != 0)? NULL : vertices; switch (format.attType){ case ATT_VERTEX: glVertexPointer(format.size, type, vertexSize, basePointer + format.offset); break; case ATT_NORMAL: glNormalPointer(type, vertexSize, basePointer + format.offset); break; case ATT_TEXCOORD: glClientActiveTextureARB(GL_TEXTURE0_ARB + format.index); glTexCoordPointer(format.size, type, vertexSize, basePointer + format.offset); break; case ATT_COLOR: glColorPointer(format.size, type, vertexSize, basePointer + format.offset); break; } glEnableClientState(array); } if (nIndices == 0){ glDrawArrays(getGLPrimitive(primitiveType), 0, nVertices); } else { if (indexBuffer != 0) glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, indexBuffer); glDrawElements(getGLPrimitive(primitiveType), nIndices, indexSize == 2? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (indexBuffer != 0)? NULL : indices); if (indexBuffer != 0) glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); } for (i = 0; i < nFormats; i++){ if (formats[i].attType == ATT_TEXCOORD){ glClientActiveTextureARB(GL_TEXTURE0_ARB + formats[i].index); } glDisableClientState(arrayType[formats[i].attType]); } if (GL_ARB_vertex_buffer_object_supported && vertexBuffer != 0){ glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); } }
boost::shared_ptr<StaticGeometry> Renderer::createStaticGeometry(const boost::shared_ptr<VertexListInterface>& spVertexList, Geometry::PrimitiveType ePrimitiveType) { LOG_VERBOSE << "Creating static geometry hardware buffers"; // Create VAO unsigned int uVAO = ~0; glGenVertexArrays(1, &uVAO); glBindVertexArray(uVAO); // Create vertex buffer (VBO) unsigned int uVBO = ~0; glGenBuffers(1, &uVBO); glBindBuffer(GL_ARRAY_BUFFER, uVBO); glBufferData(GL_ARRAY_BUFFER, spVertexList->getVertexBufferSize(), spVertexList->getVertexBufferData(), GL_STATIC_DRAW); // Set vertex attribute layouts auto spVertexLayout = spVertexList->getVertexLayout(); int iVertexSize = spVertexList->getVertexSize(); auto aAttributes = spVertexLayout->getAttributes(); boost::for_each(aAttributes, [iVertexSize](const VertexAttribute& va) { glVertexAttribPointer(va.iIndex, va.iNumElements, getGLType(va.eType), getGLBool(va.bNormalized), iVertexSize, (const GLvoid*)va.iOffset); glEnableVertexAttribArray(va.iIndex); }); // Create index buffer unsigned int uIB = ~0; glGenBuffers(1, &uIB); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, uIB); glBufferData(GL_ELEMENT_ARRAY_BUFFER, spVertexList->getIndexBufferSize(), spVertexList->getIndexBufferData(), GL_STATIC_DRAW); // Unbind VAO glBindVertexArray(0); LOG_VERBOSE << "Successfully created static geometry hardware buffers"; return boost::shared_ptr<StaticGeometry>(new StaticGeometry(uVAO, uVBO, uIB, ePrimitiveType, spVertexList)); }