Skip to content

Instantly share code, notes, and snippets.

@agirault
Last active June 27, 2016 18:09
Show Gist options
  • Save agirault/d514bd9ab9e3b7c2f1d374cc4281604e to your computer and use it in GitHub Desktop.
Save agirault/d514bd9ab9e3b7c2f1d374cc4281604e to your computer and use it in GitHub Desktop.
Create VBO to support multitextures. Write byte after byte
//-----------------------------------------------------------------------------
void vtkOpenGLVertexBufferObject::AddDataArray(vtkDataArray *data)
{
if (!this->VertexCount)
{
this->VertexCount = data->GetNumberOfTuples();
}
else if (VertexCount != data->GetNumberOfTuples())
{
vtkErrorMacro("AddDataArray() can not add array with a different number of "
<< "tuples than previously added arrays:\n"
<< "Should be "<< this->VertexCount << " tuples, got "
<< data->GetNumberOfTuples() << " instead.");
return;
}
this->DataArrays.push_back(data);
this->Offset[data->GetName()] = this->Stride;
this->Components[data->GetName()] = data->GetNumberOfComponents();
this->DataType[data->GetName()] = data->GetDataType();
this->Stride += data->GetDataTypeSize() * data->GetNumberOfComponents();
}
//-----------------------------------------------------------------------------
void vtkOpenGLVertexBufferObject::CreateVBO()
{
// Resize VBO and set up iterator
this->_PackedVBO.resize(this->Stride * this->VertexCount);
std::vector<unsigned char>::iterator it = this->_PackedVBO.begin();
// Fill VBO
for (vtkIdType i = 0; i < this->VertexCount; ++i) // For each vertex
for (int j = 0; j < this->DataArrays.size(); ++j) // For each data array (position, normals, texture, ...)
{
vtkDataArray* data = this->DataArrays.at(j);
unsigned char* dataPtr = reinterpret_cast<unsigned char*>
(data->GetVoidPointer(i * data->GetNumberOfComponents()));
for (int k = 0; k < data->GetNumberOfComponents(); ++k) // For each component (X,Y,Z; U,V; R,G,B,A...)
for (int l = 0; l < data->GetDataTypeSize(); ++l) // For each byte (float:4, char:1, ...)
{
*(it++) = *(dataPtr++);
}
}
// Upload & Reset
this->Upload(this->_PackedVBO, vtkOpenGLBufferObject::ArrayBuffer);
this->_PackedVBO.resize(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment