Commit dd123f5e authored by Ken Martin's avatar Ken Martin

lots of rough changes

parent e017b8e9
......@@ -167,10 +167,13 @@ vtkScalarBarActor::vtkScalarBarActor()
tc->Delete();
vtkCellArray* polys2 = vtkCellArray::New();
polys2->InsertNextCell(4);
polys2->InsertNextCell(3);
polys2->InsertCellPoint(0);
polys2->InsertCellPoint(1);
polys2->InsertCellPoint(2);
polys2->InsertNextCell(3);
polys2->InsertCellPoint(0);
polys2->InsertCellPoint(2);
polys2->InsertCellPoint(3);
this->TexturePolyData->SetPolys(polys2);
polys2->Delete();
......
......@@ -65,6 +65,7 @@ int TestVBOPLYMapper(int argc, char *argv[])
actor->GetProperty()->SetAmbient(0.5);
actor->GetProperty()->SetSpecularPower(20.0);
actor->GetProperty()->SetOpacity(1.0);
actor->GetProperty()->SetRepresentationToWireframe();
vtkNew<vtkRenderWindowInteractor> interactor;
interactor->SetRenderWindow(renderWindow.Get());
......
......@@ -50,14 +50,12 @@ class vtkOpenGL2PolyDataMapper2D::Private
public:
vtkgl::BufferObject vbo;
vtkgl::VBOLayout layout;
vtkgl::BufferObject lineIBO;
vtkgl::BufferObject polyIBO;
vtkgl::BufferObject pointIBO;
vtkgl::BufferObject triStripIBO;
size_t numberOfPoints;
size_t numberOfIndices;
std::vector<GLintptr> offsetArray;
std::vector<unsigned int> elementsArray;
// Structures for the various cell types we render.
vtkgl::CellBO points;
vtkgl::CellBO lines;
vtkgl::CellBO tris;
vtkgl::CellBO triStrips;
const char *vertexShaderFile;
const char *fragmentShaderFile;
......@@ -246,7 +244,7 @@ void vtkOpenGL2PolyDataMapper2D::SetCameraShaderParameters(vtkViewport* viewport
//-------------------------------------------------------------------------
void vtkOpenGL2PolyDataMapper2D::UpdateVBO(vtkActor2D *vtkNotUsed(act))
void vtkOpenGL2PolyDataMapper2D::UpdateVBO(vtkActor2D *act)
{
vtkPolyData *poly = this->GetInput();
if (poly == NULL)
......@@ -257,60 +255,42 @@ void vtkOpenGL2PolyDataMapper2D::UpdateVBO(vtkActor2D *vtkNotUsed(act))
// Mark our properties as updated.
this->Internal->propertiesTime.Modified();
bool colorAttributes = false;
this->Internal->colorComponents = 0;
this->Internal->colorAttributes = false;
if (this->ScalarVisibility)
{
// We must figure out how the scalars should be mapped to the polydata.
//this->MapScalars(NULL, 1.0, false, poly);
if (this->Internal->colorComponents == 3 ||
this->Internal->colorComponents == 4)
this->MapScalars(act->GetProperty()->GetOpacity());
if (this->Colors && (this->Colors->GetNumberOfComponents() == 3 ||
this->Colors->GetNumberOfComponents() == 4))
{
this->Internal->colorAttributes = colorAttributes = true;
cout << "Scalar colors: "
<< this->Internal->colors.size() / this->Internal->colorComponents
<< " with " << int(this->Internal->colorComponents) << " components." << endl;
this->Internal->colorAttributes = true;
}
}
// Iterate through all of the different types in the polydata, building VBOs
// and IBOs as appropriate for each type.
vtkPoints* p = poly->GetPoints();
switch(p->GetDataType())
{
vtkTemplateMacro(
this->Internal->layout =
CreateTriangleVBO(static_cast<VTK_TT*>(p->GetVoidPointer(0)),
static_cast<VTK_TT*>(NULL),
p->GetNumberOfPoints(),
this->Internal->colorComponents ? &this->Internal->colors[0] : NULL,
this->Internal->colorComponents,
this->Internal->vbo));
}
vtkIdType *pts = 0;
vtkIdType npts = 0;
int cellNum = 0;
vtkCellArray* lines = poly->GetLines();
std::vector<unsigned int> indexArray;
unsigned int count = 0;
indexArray.reserve(lines->GetNumberOfCells() * 3);
for (lines->InitTraversal(); lines->GetNextCell(npts,pts); cellNum++)
{
this->Internal->offsetArray.push_back(count*sizeof(unsigned int));
this->Internal->elementsArray.push_back(npts);
for (int j = 0; j < npts; ++j)
{
indexArray.push_back(static_cast<unsigned int>(pts[j]));
count++;
}
}
this->Internal->lineIBO.upload(indexArray, vtkgl::BufferObject::ElementArrayBuffer);
this->Internal->numberOfIndices = indexArray.size();
this->Internal->numberOfPoints = CreateIndexBuffer(poly->GetVerts(),
this->Internal->pointIBO, 1);
this->Internal->layout =
CreateVBO(poly->GetPoints(),
NULL,
this->Internal->colorAttributes ? this->Colors->GetPointer(0) : NULL,
this->Colors ? this->Colors->GetNumberOfComponents() : 0,
this->Internal->vbo);
this->Internal->triStrips.indexCount = CreateMultiIndexBuffer(poly->GetStrips(),
this->Internal->triStrips.ibo,
this->Internal->triStrips.offsetArray,
this->Internal->triStrips.elementsArray);
this->Internal->lines.indexCount = CreateMultiIndexBuffer(poly->GetLines(),
this->Internal->lines.ibo,
this->Internal->lines.offsetArray,
this->Internal->lines.elementsArray);
this->Internal->points.indexCount = CreatePointIndexBuffer(poly->GetVerts(),
this->Internal->points.ibo);
this->Internal->tris.indexCount = CreateTriangleIndexBuffer(poly->GetPolys(),
this->Internal->tris.ibo,
poly->GetPoints());
}
......@@ -467,36 +447,61 @@ void vtkOpenGL2PolyDataMapper2D::RenderOverlay(vtkViewport* viewport,
this->Internal->program.useAttributeArray("diffuseColor", sizeof(float) * 6,
stride,
VTK_UNSIGNED_CHAR,
this->Internal->colorComponents,
this->Colors->GetNumberOfComponents(),
vtkgl::ShaderProgram::Normalize);
}
if (this->Internal->offsetArray.size() > 0)
{
this->Internal->lineIBO.bind();
glMultiDrawElements(GL_LINE_STRIP,
(GLsizei *)(&this->Internal->elementsArray[0]),
GL_UNSIGNED_INT,
reinterpret_cast<const GLvoid **>(&(this->Internal->offsetArray[0])),
this->Internal->offsetArray.size());
this->Internal->lineIBO.release();
}
if (this->Internal->numberOfPoints)
if (this->Internal->points.indexCount)
{
// Set the PointSize
glPointSize(actor->GetProperty()->GetPointSize());
//vtkOpenGLGL2PSHelper::SetPointSize(actor->GetProperty()->GetPointSize());
this->Internal->pointIBO.bind();
this->Internal->points.ibo.bind();
glDrawRangeElements(GL_POINTS, 0,
static_cast<GLuint>(this->Internal->layout.VertexCount - 1),
static_cast<GLsizei>(this->Internal->numberOfPoints),
static_cast<GLsizei>(this->Internal->points.indexCount),
GL_UNSIGNED_INT,
reinterpret_cast<const GLvoid *>(NULL));
this->Internal->points.ibo.release();
}
if (this->Internal->tris.indexCount)
{
this->Internal->tris.ibo.bind();
glDrawRangeElements(GL_TRIANGLES, 0,
static_cast<GLuint>(this->Internal->layout.VertexCount - 1),
static_cast<GLsizei>(this->Internal->tris.indexCount),
GL_UNSIGNED_INT,
reinterpret_cast<const GLvoid *>(NULL));
this->Internal->pointIBO.release();
this->Internal->tris.ibo.release();
}
if (this->Internal->lines.offsetArray.size() > 0)
{
this->Internal->lines.ibo.bind();
for (int eCount = 0; eCount < this->Internal->lines.offsetArray.size(); ++eCount)
{
glDrawElements(GL_LINE_STRIP,
this->Internal->lines.elementsArray[eCount],
GL_UNSIGNED_INT,
(GLvoid *)(this->Internal->lines.offsetArray[eCount]));
}
this->Internal->lines.ibo.release();
}
if (this->Internal->triStrips.offsetArray.size() > 0)
{
this->Internal->triStrips.ibo.bind();
for (int eCount = 0; eCount < this->Internal->triStrips.offsetArray.size(); ++eCount)
{
glDrawElements(GL_LINE_STRIP,
this->Internal->triStrips.elementsArray[eCount],
GL_UNSIGNED_INT,
(GLvoid *)(this->Internal->triStrips.offsetArray[eCount]));
}
this->Internal->triStrips.ibo.release();
}
this->Internal->vbo.release();
this->Internal->program.disableAttributeArray("vertexWC");
......
This diff is collapsed.
......@@ -12,11 +12,136 @@
=========================================================================*/
#include "vtkCellArray.h"
#include "vtkPoints.h"
#include "vtkPolygon.h"
#include "vtkglVBOHelper.h"
namespace vtkgl {
// internal function called by CreateVBO
template<typename T, typename T2>
VBOLayout TemplatedCreateVBO2(T* points, T2* normals, vtkIdType numPts,
unsigned char *colors, int colorComponents,
BufferObject &vertexBuffer)
{
VBOLayout layout;
// Figure out how big each block will be, currently 6 or 7 floats.
int blockSize = 3;
layout.VertexOffset = 0;
if (normals)
{
blockSize += 3;
layout.NormalOffset = sizeof(float) * 3;
}
else
{
layout.NormalOffset = 0;
}
if (colors)
{
++blockSize;
layout.ColorComponents = colorComponents;
layout.ColorOffset = sizeof(float) * 6;
}
else
{
layout.ColorComponents = 0;
layout.ColorOffset = 0;
}
layout.Stride = sizeof(float) * blockSize;
// Create a buffer, and copy the data over.
std::vector<float> packedVBO;
packedVBO.resize(blockSize * numPts);
std::vector<float>::iterator it = packedVBO.begin();
for (vtkIdType i = 0; i < numPts; ++i)
{
// Vertices
*(it++) = *(points++);
*(it++) = *(points++);
*(it++) = *(points++);
if (normals)
{
*(it++) = *(normals++);
*(it++) = *(normals++);
*(it++) = *(normals++);
}
if (colors)
{
if (colorComponents == 4)
{
*(it++) = *reinterpret_cast<float *>(colors);
colors += 4;
}
else
{
unsigned char c[4] = { *(colors++), *(colors++), *(colors++), 255 };
*(it++) = *reinterpret_cast<float *>(c);
}
}
}
vertexBuffer.upload(packedVBO, vtkgl::BufferObject::ArrayBuffer);
layout.VertexCount = numPts;
return layout;
}
//----------------------------------------------------------------------------
template<typename T>
VBOLayout TemplatedCreateVBO(T* points, vtkDataArray *normals, vtkIdType numPts,
unsigned char *colors, int colorComponents,
BufferObject &vertexBuffer)
{
if (normals)
{
switch(normals->GetDataType())
{
vtkTemplateMacro(
return
TemplatedCreateVBO2(points,
static_cast<VTK_TT*>(normals->GetVoidPointer(0)),
numPts,
colors,
colorComponents,
vertexBuffer));
}
}
else
{
return
TemplatedCreateVBO2(points,
(float *)NULL,
numPts,
colors,
colorComponents,
vertexBuffer);
}
}
// Take the points, and pack them into the VBO object supplied. This currently
// takes whatever the input type might be and packs them into a VBO using
// floats for the vertices and normals, and unsigned char for the colors (if
// the array is non-null).
VBOLayout CreateVBO(vtkPoints *points, vtkDataArray *normals,
unsigned char *colors, int colorComponents,
BufferObject &vertexBuffer)
{
switch(points->GetDataType())
{
vtkTemplateMacro(
return
TemplatedCreateVBO(static_cast<VTK_TT*>(points->GetVoidPointer(0)),
normals,
points->GetNumberOfPoints(),
colors,
colorComponents,
vertexBuffer));
}
}
// Process the string, and return a version with replacements.
std::string replace(std::string source, const std::string &search,
......@@ -41,23 +166,102 @@ std::string replace(std::string source, const std::string &search,
}
size_t CreateIndexBuffer(vtkCellArray *cells, BufferObject &indexBuffer,
int num)
// used to create an IBO for triangle primatives
size_t CreateTriangleIndexBuffer(vtkCellArray *cells, BufferObject &indexBuffer,
vtkPoints *points)
{
std::vector<unsigned int> indexArray;
vtkIdType* indices(NULL);
vtkIdType points(0);
indexArray.reserve(cells->GetNumberOfCells() * num);
for (cells->InitTraversal(); cells->GetNextCell(points, indices); )
vtkIdType npts(0);
indexArray.reserve(cells->GetNumberOfCells() * 3);
vtkPolygon *polygon = NULL;
vtkIdList *tris = NULL;
for (cells->InitTraversal(); cells->GetNextCell(npts, indices); )
{
if (points != num)
if (npts < 3)
{
exit(-1); // assert(points == num);
exit(-1); // assert(points >= 3);
}
// triangulate needed
if (npts > 3)
{
// special case for quads which VTK uses a lot
if (npts == 4)
{
indexArray.push_back(static_cast<unsigned int>(indices[0]));
indexArray.push_back(static_cast<unsigned int>(indices[1]));
indexArray.push_back(static_cast<unsigned int>(indices[2]));
indexArray.push_back(static_cast<unsigned int>(indices[0]));
indexArray.push_back(static_cast<unsigned int>(indices[2]));
indexArray.push_back(static_cast<unsigned int>(indices[3]));
}
else
{
if (!polygon)
{
polygon = vtkPolygon::New();
tris = vtkIdList::New();
}
polygon->Initialize(npts, indices, points);
polygon->Triangulate(tris);
for (int j = 0; j < tris->GetNumberOfIds(); ++j)
{
indexArray.push_back(static_cast<unsigned int>(indices[tris->GetId(j)]));
}
}
}
for (int j = 0; j < num; ++j)
else
{
indexArray.push_back(static_cast<unsigned int>(*(indices++)));
indexArray.push_back(static_cast<unsigned int>(*(indices++)));
indexArray.push_back(static_cast<unsigned int>(*(indices++)));
}
}
if (polygon)
{
polygon->Delete();
tris->Delete();
}
indexBuffer.upload(indexArray, vtkgl::BufferObject::ElementArrayBuffer);
return indexArray.size();
}
// used to create an IBO for point primatives
size_t CreatePointIndexBuffer(vtkCellArray *cells, BufferObject &indexBuffer)
{
std::vector<unsigned int> indexArray;
vtkIdType* indices(NULL);
vtkIdType npts(0);
indexArray.reserve(cells->GetNumberOfCells());
for (cells->InitTraversal(); cells->GetNextCell(npts, indices); )
{
indexArray.push_back(static_cast<unsigned int>(*(indices++)));
}
indexBuffer.upload(indexArray, vtkgl::BufferObject::ElementArrayBuffer);
return indexArray.size();
}
// used to create an IBO for stripped primatives such as lines and triangle strips
size_t CreateMultiIndexBuffer(vtkCellArray *cells, BufferObject &indexBuffer,
std::vector<GLintptr> &memoryOffsetArray,
std::vector<unsigned int> &elementCountArray)
{
vtkIdType *pts = 0;
vtkIdType npts = 0;
std::vector<unsigned int> indexArray;
unsigned int count = 0;
indexArray.reserve(cells->GetData()->GetSize());
for (cells->InitTraversal(); cells->GetNextCell(npts,pts); )
{
memoryOffsetArray.push_back(count*sizeof(unsigned int));
elementCountArray.push_back(npts);
for (int j = 0; j < npts; ++j)
{
indexArray.push_back(static_cast<unsigned int>(pts[j]));
count++;
}
}
indexBuffer.upload(indexArray, vtkgl::BufferObject::ElementArrayBuffer);
......
......@@ -15,6 +15,8 @@
#ifndef __vtkGLVBOHelpher_h
#define __vtkGLVBOHelpher_h
#include <GL/glew.h>
#include "vtkglBufferObject.h"
#include "vtkglShader.h"
#include "vtkglShaderProgram.h"
......@@ -27,8 +29,18 @@ namespace vtkgl
std::string replace(std::string source, const std::string &search,
const std::string replace, bool all = true);
size_t CreateIndexBuffer(vtkCellArray *cells, BufferObject &indexBuffer,
int num);
// used to create an IBO for triangle primatives
size_t CreateTriangleIndexBuffer(vtkCellArray *cells, BufferObject &indexBuffer,
vtkPoints *points);
// used to create an IBO for point primatives
size_t CreatePointIndexBuffer(vtkCellArray *cells, BufferObject &indexBuffer);
// used to create an IBO for line strips and triangle strips
size_t CreateMultiIndexBuffer(vtkCellArray *cells, BufferObject &indexBuffer,
std::vector<GLintptr> &memoryOffsetArray,
std::vector<unsigned int> &elementCountArray);
// Store the shaders, program, and ibo in a common struct.
struct CellBO
{
......@@ -60,73 +72,10 @@ struct VBOLayout
// takes whatever the input type might be and packs them into a VBO using
// floats for the vertices and normals, and unsigned char for the colors (if
// the array is non-null).
template<typename T>
VBOLayout CreateTriangleVBO(T* points, T* normals, vtkIdType numPts,
unsigned char *colors, int colorComponents,
BufferObject &vertexBuffer)
{
VBOLayout layout;
// Figure out how big each block will be, currently 6 or 7 floats.
int blockSize = 3;
layout.VertexOffset = 0;
if (normals)
{
blockSize += 3;
layout.NormalOffset = sizeof(float) * 3;
}
else
{
layout.NormalOffset = 0;
}
if (colors)
{
++blockSize;
layout.ColorComponents = colorComponents;
layout.ColorOffset = sizeof(float) * 6;
}
else
{
layout.ColorComponents = 0;
layout.ColorOffset = 0;
}
layout.Stride = sizeof(float) * blockSize;
// Create a buffer, and copy the data over.
std::vector<float> packedVBO;
packedVBO.resize(blockSize * numPts);
std::vector<float>::iterator it = packedVBO.begin();
for (vtkIdType i = 0; i < numPts; ++i)
{
// Vertices
*(it++) = *(points++);
*(it++) = *(points++);
*(it++) = *(points++);
if (normals)
{
*(it++) = *(normals++);
*(it++) = *(normals++);
*(it++) = *(normals++);
}
if (colors)
{
if (colorComponents == 4)
{
*(it++) = *reinterpret_cast<float *>(colors);
colors += 4;
}
else
{
unsigned char c[4] = { *(colors++), *(colors++), *(colors++), 255 };
*(it++) = *reinterpret_cast<float *>(c);
}
}
}
vertexBuffer.upload(packedVBO, vtkgl::BufferObject::ArrayBuffer);
layout.VertexCount = numPts;
return layout;
}
VBOLayout CreateVBO(vtkPoints *points, vtkDataArray *normals,
unsigned char *colors, int colorComponents,
BufferObject &vertexBuffer);
} // End namespace
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment