void OGL4GeometryData::AttachIndexBuffer(GPUBufferPtr pBuffer, G_FORMAT format) { if(m_pIndexBuffer) { m_pIndexBuffer->Release(); } m_pIndexBuffer = pBuffer; switch(format) { case G_FORMAT_R32_UINT: m_indexType = GL_UNSIGNED_INT; m_indexElementBytes = 4; break; case G_FORMAT_R16_UINT: m_indexType = GL_UNSIGNED_SHORT; m_indexElementBytes = 2; break; default: assert(0); m_indexType = GL_UNSIGNED_SHORT; m_indexElementBytes = 2; break; } OGL4BufferPtr pOGLBuffer = std::dynamic_pointer_cast<OGL4Buffer>(m_pIndexBuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pOGLBuffer->GetBufferObject()); }
void OGL4ShaderProgram::CollectUniformBlocks() { GLint count = 0; glGetProgramiv(m_program, GL_ACTIVE_UNIFORM_BLOCKS, &count); for(int i = 0; i < count; ++i) { char szName[1024]; glGetActiveUniformBlockName(m_program, i, 1024, 0, szName); GLint bytes = 0; glGetActiveUniformBlockiv(m_program, i, GL_UNIFORM_BLOCK_DATA_SIZE, &bytes); OGL4BufferPtr pBuffer = std::make_shared<OGL4Buffer>(); if(pBuffer->Create(BT_CONSTANT_BUFFER, bytes, nullptr, true) == false) { pBuffer->Release(); pBuffer.reset(); continue; } UniformBlock block; block.index = i; block.name = szName; block.pBuffer = pBuffer; m_uniformBlocks.push_back(block); } }
bool OGL4GeometryData::AllocIndexBuffer(uint32 bytes, void* initData, bool dynamic, G_FORMAT format) { OGL4BufferPtr pBuffer = std::make_shared<OGL4Buffer>(); if(pBuffer->Create(BT_INDEX_BUFFER, bytes, initData, dynamic) == false) { return false; } AttachIndexBuffer(pBuffer, format); return true; }
bool OGL4GeometryData::AllocVertexBuffer(uint32 bytes, void* initData, bool dynamic, const VertexLayout& layout) { OGL4BufferPtr pBuffer = std::make_shared<OGL4Buffer>(); if(pBuffer->Create(BT_VERTEX_BUFFER, bytes, initData, dynamic) == false) { return false; } AttachVertexBuffer(pBuffer, layout); return true; }
void OGL4GeometryData::CommitModify() { glBindVertexArray(m_vao); OGL4BufferPtr pOGLBuffer = std::dynamic_pointer_cast<OGL4Buffer>(m_pVertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, pOGLBuffer->GetBufferObject()); pOGLBuffer = std::dynamic_pointer_cast<OGL4Buffer>(m_pIndexBuffer); if(pOGLBuffer) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pOGLBuffer->GetBufferObject()); } glBindVertexArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); }
void OGL4GeometryData::SetVertexLayout(const VertexLayout& layout) { m_layout = layout; unsigned int stride = m_layout.VertexStride(); OGL4BufferPtr pOGLBuffer = std::dynamic_pointer_cast<OGL4Buffer>(m_pVertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, pOGLBuffer->GetBufferObject()); unsigned int offset = 0; for(unsigned int i = 0; i < m_layout.AttrCount(); ++i) { glEnableVertexAttribArray(i); G_FORMAT type = m_layout.AttrType(i); unsigned int value_count = 0; GLenum gltype = GL_INVALID_ENUM; if(OGL4Convert::FormatToGLVertexAttr(type, value_count, gltype) == false) { assert(0); } switch(gltype) { case GL_INT: case GL_UNSIGNED_INT: glVertexAttribIPointer(i, value_count, gltype, stride, (GLvoid*)(m_vbOffset + offset)); break; default: glVertexAttribPointer(i, value_count, gltype, GL_FALSE, stride, (GLvoid*)(m_vbOffset + offset)); break; } offset += m_layout.TypeBytes(type); } }