static bool loadBuffer(GLBuffer &buffer, std::vector<T> data){ if(!buffer.create()) { std::cout<<"Failed to create buffe"<<std::endl; return false; } if(!buffer.bind()){ std::cout<<"Failed to bind buffer"<<std::endl; buffer.release(); return false; } glGetError(); //clear any uncaught errors. Report this? glBufferData(buffer.getType(), sizeof(T) * data.size(), &data[0], GL_STATIC_DRAW); GLenum error = glGetError(); if(error != GL_NO_ERROR){ std::cout<<"Error binding data "<<GLHelper::errorEnumToString(error)<<std::endl; buffer.release(); return false; } return true; }
void Lights::load(GLBuffer& buffer, int bufferIdx, const glm::mat4& viewMatrix) { buffer.bind(GL_UNIFORM_BUFFER, bufferIdx); buffer.setSubData(&m_lightCount, LIGHT_COUNT_OFFSET, 1, bufferIdx); for ( size_t i = 0; i < m_lights.size(); ++i ) { LightInfo& info = m_lights[i]; // compute light position in view coordinates glm::vec3 position = (viewMatrix * glm::vec4(info.position, 1.0)).xyz(); // set buffer data buffer.setSubData(&position, LIGHT_ARRAY_OFFSET + i * LIGHT_ARRAY_STEP + LIGHT_ARRAY_POSITION_OFFSET, 1, bufferIdx); buffer.setSubData(&info.diffuse, LIGHT_ARRAY_OFFSET + i * LIGHT_ARRAY_STEP + LIGHT_ARRAY_DIFFUSE_OFFSET, 1, bufferIdx); buffer.setSubData(&info.specular, LIGHT_ARRAY_OFFSET + i * LIGHT_ARRAY_STEP + LIGHT_ARRAY_SPECULAR_OFFSET, 1, bufferIdx); buffer.setSubData(&info.ambient, LIGHT_ARRAY_OFFSET + i * LIGHT_ARRAY_STEP + LIGHT_ARRAY_AMBIENT_OFFSET, 1, bufferIdx); } GLBuffer::unbindBuffers(GL_UNIFORM_BUFFER); }
bool Lights::loadLight(GLBuffer& buffer, int bufferIdx, const glm::mat4& viewMatrix, size_t idx) { if ( idx < m_idxMap.size() && m_idxMap[idx] != INVALID_INDEX ) { LightInfo& info = m_lights[m_idxMap[idx]]; // compute light position in view coordinates glm::vec3 position = (viewMatrix * glm::vec4(info.position, 1.0)).xyz(); // set buffer data buffer.bind(GL_UNIFORM_BUFFER, bufferIdx); buffer.setSubData(&position, LIGHT_ARRAY_OFFSET + m_idxMap[idx] * LIGHT_ARRAY_STEP + LIGHT_ARRAY_POSITION_OFFSET, 1, bufferIdx); buffer.setSubData(&info.diffuse, LIGHT_ARRAY_OFFSET + m_idxMap[idx] * LIGHT_ARRAY_STEP + LIGHT_ARRAY_DIFFUSE_OFFSET, 1, bufferIdx); buffer.setSubData(&info.specular, LIGHT_ARRAY_OFFSET + m_idxMap[idx] * LIGHT_ARRAY_STEP + LIGHT_ARRAY_SPECULAR_OFFSET, 1, bufferIdx); buffer.setSubData(&info.ambient, LIGHT_ARRAY_OFFSET + m_idxMap[idx] * LIGHT_ARRAY_STEP + LIGHT_ARRAY_AMBIENT_OFFSET, 1, bufferIdx); GLBuffer::unbindBuffers(GL_UNIFORM_BUFFER); return true; } return false; }
void Lights::loadCount(GLBuffer& buffer, int bufferIdx) { buffer.bind(GL_UNIFORM_BUFFER, bufferIdx); buffer.setSubData(&m_lightCount, LIGHT_COUNT_OFFSET, 1, bufferIdx); GLBuffer::unbindBuffers(GL_UNIFORM_BUFFER); }