Commit 36e26149 authored by Sankhesh Jhaveri's avatar Sankhesh Jhaveri 💬

Convert RGB Table to use vtkTextureObject

Change-Id: I2e282900d9d925595aa2b448bb36b7f9d7ca133f
parent a589d097
......@@ -1326,6 +1326,53 @@ vtkPixelBufferObject* vtkTextureObject::Download()
#endif
//----------------------------------------------------------------------------
bool vtkTextureObject::Create1DFromRaw(unsigned int width, int numComps,
int dataType, void *data)
{
assert(this->Context);
// Now determine the texture parameters using the arguments.
GLenum type = ::vtkGetType(dataType);
GLenum internalFormat = this->GetInternalFormat(dataType, numComps, false);
GLenum format = this->GetFormat(dataType, numComps, false);
if (!internalFormat || !format || !type)
{
vtkErrorMacro("Failed to determine texture parameters.");
return false;
}
GLenum target = GL_TEXTURE_1D;
this->Target = target;
this->CreateTexture();
this->Bind();
glTexImage1D(target,
0,
internalFormat,
static_cast<GLsizei> (width),
0,
format,
type,
static_cast<const GLvoid *> (data));
vtkOpenGLCheckErrorMacro("failed at glTexImage1D");
this->UnBind();
this->Target = target;
this->Format = format;
this->Type = type;
this->Components = numComps;
this->Width = width;
this->Height = 1;
this->Depth = 1;
this->NumberOfDimensions = 1;
return true;
}
//----------------------------------------------------------------------------
bool vtkTextureObject::Create2DFromRaw(unsigned int width, unsigned int height,
int numComps, int dataType, void *data)
......
......@@ -106,7 +106,10 @@ public:
// Description:
// Get/Set the context. This does not increase the reference count of the
// context to avoid reference loops.
// SetContext() may raise an error is the OpenGL context does not support the
//
// {
// this->TextureObject = vtkTextureObject::New();
// }SetContext() may raise an error is the OpenGL context does not support the
// required OpenGL extensions.
void SetContext(vtkOpenGLRenderWindow*);
vtkOpenGLRenderWindow* GetContext();
......@@ -168,6 +171,11 @@ public:
vtkSetMacro(AutoParameters, int);
vtkGetMacro(AutoParameters, int);
// Description:
// Create 1D texture from client memory
bool Create1DFromRaw(unsigned int width, int numComps,
int dataType, void *data);
// Description:
// Create a 2D texture from client memory
// numComps must be in [1-4].
......
......@@ -57,6 +57,7 @@
#include <vtkShaderProgram.h>
#include <vtkSmartPointer.h>
#include <vtkTessellatedBoxSource.h>
#include <vtkTextureObject.h>
#include <vtkTimerLog.h>
#include <vtkTransform.h>
#include <vtkUnsignedCharArray.h>
......@@ -198,7 +199,9 @@ public:
// Update transfer color function based on the incoming inputs and number of
// scalar components.
// TODO Deal with numberOfScalarComponents > 1
int UpdateColorTransferFunction(vtkVolume* vol, int numberOfScalarComponents);
int UpdateColorTransferFunction(vtkRenderer* ren,
vtkVolume* vol,
int numberOfScalarComponents);
// Update opacity transfer function (not gradient opacity)
int UpdateOpacityTransferFunction(vtkVolume* vol,
......@@ -794,7 +797,7 @@ void vtkOpenGLGPUVolumeRayCastMapper::vtkInternal::ComputeBounds(
//----------------------------------------------------------------------------
int vtkOpenGLGPUVolumeRayCastMapper::vtkInternal::UpdateColorTransferFunction(
vtkVolume* vol, int numberOfScalarComponents)
vtkRenderer* ren, vtkVolume* vol, int numberOfScalarComponents)
{
// Build the colormap in a 1D texture.
// 1D RGB-texture=mapping from scalar values to color values
......@@ -812,9 +815,13 @@ int vtkOpenGLGPUVolumeRayCastMapper::vtkInternal::UpdateColorTransferFunction(
colorTransferFunction->AddRGBPoint(this->ScalarsRange[1], 1.0, 1.0, 1.0);
}
int filterVal =
volumeProperty->GetInterpolationType() == VTK_LINEAR_INTERPOLATION ?
vtkTextureObject::Linear : vtkTextureObject::Nearest;
this->RGBTable->Update(
colorTransferFunction, this->ScalarsRange,
volumeProperty->GetInterpolationType() == VTK_LINEAR_INTERPOLATION);
filterVal,
vtkOpenGLRenderWindow::SafeDownCast(ren->GetRenderWindow()));
}
if (this->Parent->MaskInput != 0 &&
......@@ -825,11 +832,15 @@ int vtkOpenGLGPUVolumeRayCastMapper::vtkInternal::UpdateColorTransferFunction(
vtkColorTransferFunction* colorTransferFunc =
volumeProperty->GetRGBTransferFunction(1);
this->Mask1RGBTable->Update(colorTransferFunc, this->ScalarsRange,
false, 7);
vtkTextureObject::Nearest,
vtkOpenGLRenderWindow::SafeDownCast(
ren->GetRenderWindow()));
colorTransferFunc = volumeProperty->GetRGBTransferFunction(2);
this->Mask2RGBTable->Update(colorTransferFunc, this->ScalarsRange,
false, 8);
vtkTextureObject::Nearest,
vtkOpenGLRenderWindow::SafeDownCast(
ren->GetRenderWindow()));
}
return 0;
......@@ -2073,7 +2084,7 @@ void vtkOpenGLGPUVolumeRayCastMapper::GPURender(vtkRenderer* ren,
scalars->GetNumberOfComponents(), 0);
// Update transfer color functions
this->Impl->UpdateColorTransferFunction(vol,
this->Impl->UpdateColorTransferFunction(ren, vol,
scalars->GetNumberOfComponents());
// Update noise sampler texture
......@@ -2156,12 +2167,15 @@ void vtkOpenGLGPUVolumeRayCastMapper::GPURender(vtkRenderer* ren,
if(numberOfScalarComponents == 1 &&
this->BlendMode!=vtkGPUVolumeRayCastMapper::ADDITIVE_BLEND)
{
this->Impl->ShaderProgram->SetUniformi("in_colorTransferFunc", 1);
this->Impl->ShaderProgram->SetUniformi("in_colorTransferFunc",
this->Impl->RGBTable->GetTextureUnit());
if (this->MaskInput != 0 && this->MaskType == LabelMapMaskType)
{
this->Impl->ShaderProgram->SetUniformi("in_mask1", 7);
this->Impl->ShaderProgram->SetUniformi("in_mask2", 8);
this->Impl->ShaderProgram->SetUniformi("in_mask1",
this->Impl->Mask1RGBTable->GetTextureUnit());
this->Impl->ShaderProgram->SetUniformi("in_mask2",
this->Impl->Mask2RGBTable->GetTextureUnit());
this->Impl->ShaderProgram->SetUniformf("in_maskBlendFactor",
this->MaskBlendFactor);
}
......@@ -2191,8 +2205,8 @@ void vtkOpenGLGPUVolumeRayCastMapper::GPURender(vtkRenderer* ren,
if (this->MaskInput != 0 && this->MaskType == LabelMapMaskType)
{
this->Impl->Mask1RGBTable->Bind(7);
this->Impl->Mask2RGBTable->Bind(8);
this->Impl->Mask1RGBTable->Bind();
this->Impl->Mask2RGBTable->Bind();
}
}
......
......@@ -17,6 +17,7 @@
#define __vtkOpenGLRGBTable_h_
#include <vtkColorTransferFunction.h>
#include <vtkTextureObject.h>
#include <vtk_glew.h>
//----------------------------------------------------------------------------
......@@ -26,11 +27,12 @@ public:
//--------------------------------------------------------------------------
vtkOpenGLRGBTable()
{
this->Loaded = false;
this->LastLinearInterpolation = false;
this->TexutureWidth = 1024;
// this->Loaded = false;
// this->LastLinearInterpolation = false;
this->TextureWidth = 1024;
this->NumberOfColorComponents = 3;
this->TextureId = 0;
this->TextureObject = 0;
// this->TextureId = 0;
this->LastRange[0] = this->LastRange[1] = 0;
this->Table = 0;
}
......@@ -38,11 +40,16 @@ public:
//--------------------------------------------------------------------------
~vtkOpenGLRGBTable()
{
if(this->TextureId!=0)
if (this->TextureObject != 0)
{
glDeleteTextures(1,&this->TextureId);
this->TextureId=0;
this->TextureObject->UnRegister(0);
this->TextureObject = 0;
}
// if(this->TextureId!=0)
// {
// glDeleteTextures(1,&this->TextureId);
// this->TextureId=0;
// }
if(this->Table!=0)
{
delete[] this->Table;
......@@ -50,102 +57,141 @@ public:
}
}
// Check if color transfer function texture is loaded.
//--------------------------------------------------------------------------
bool IsLoaded()
{
return this->Loaded;
}
// // Check if color transfer function texture is loaded.
// //--------------------------------------------------------------------------
// bool IsLoaded()
// {
// return this->Loaded;
// }
// Bind texture.
//--------------------------------------------------------------------------
void Bind(int textureUnit = 1)
// void Bind(int textureUnit = 1)
void Bind(void)
{
// Activate texture 1
glActiveTexture(GL_TEXTURE0 + textureUnit);
glBindTexture(GL_TEXTURE_1D, this->TextureId);
if (!this->TextureObject)
{
return;
}
this->TextureObject->Bind();
// // Activate texture 1
// glActiveTexture(GL_TEXTURE0 + textureUnit);
// glBindTexture(GL_TEXTURE_1D, this->TextureId);
}
// Update color transfer function texture.
//--------------------------------------------------------------------------
void Update(vtkColorTransferFunction* scalarRGB,
double range[2],
bool linearInterpolation, int textureUnit = 1)
int filterValue,
vtkOpenGLRenderWindow* renWin)
{
// Activate texture 1
glActiveTexture(GL_TEXTURE0 + textureUnit);
// // Activate texture 1
// glActiveTexture(GL_TEXTURE0 + textureUnit);
bool needUpdate = false;
if(this->TextureId == 0)
if (!this->TextureObject)
{
glGenTextures(1, &this->TextureId);
needUpdate = true;
this->TextureObject = vtkTextureObject::New();
}
// if(this->TextureId == 0)
// {
// glGenTextures(1, &this->TextureId);
// needUpdate = true;
// }
if (range[0] != this->LastRange[0] || range[1] != this->LastRange[1])
{
needUpdate=true;
}
glBindTexture(GL_TEXTURE_1D, this->TextureId);
if(needUpdate)
{
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S,
GL_CLAMP_TO_EDGE);
}
if(scalarRGB->GetMTime() > this->BuildTime || needUpdate || !this->Loaded)
this->TextureObject->SetContext(renWin);
// glBindTexture(GL_TEXTURE_1D, this->TextureId);
// if(needUpdate)
// {
// glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S,
// GL_CLAMP_TO_EDGE);
// }
if (scalarRGB->GetMTime() > this->BuildTime ||
this->TextureObject->GetMTime() > this->BuildTime ||
needUpdate)
{
this->Loaded = false;
// if(scalarRGB->GetMTime() > this->BuildTime || needUpdate || !this->Loaded)
// {
// this->Loaded = false;
// Create table if not created already
if(this->Table==0)
{
this->Table = new float[this->TexutureWidth *
this->Table = new float[this->TextureWidth *
this->NumberOfColorComponents];
}
scalarRGB->GetTable(range[0],range[1], this->TexutureWidth, this->Table);
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB16, this->TexutureWidth, 0,
GL_RGB, GL_FLOAT, this->Table);
scalarRGB->GetTable(range[0],range[1], this->TextureWidth, this->Table);
this->TextureObject->Create1DFromRaw(this->TextureWidth,
this->NumberOfColorComponents,
VTK_FLOAT,
this->Table);
this->TextureObject->Activate();
this->TextureObject->SetWrapS(vtkTextureObject::ClampToEdge);
this->TextureObject->SetMagnificationFilter(filterValue);
this->TextureObject->SetMinificationFilter(filterValue);
// glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB16, this->TextureWidth, 0,
// GL_RGB, GL_FLOAT, this->Table);
this->Loaded = true;
// this->Loaded = true;
this->BuildTime.Modified();
this->LastRange[0] = range[0];
this->LastRange[1] = range[1];
}
needUpdate = needUpdate ||
this->LastLinearInterpolation!=linearInterpolation;
if (needUpdate)
// needUpdate = needUpdate ||
// this->LastLinearInterpolation!=linearInterpolation;
// if (needUpdate)
// {
// this->LastLinearInterpolation = linearInterpolation;
// GLint value;
// if (linearInterpolation)
// {
// value = GL_LINEAR;
// }
// else
// {
// value = GL_NEAREST;
// }
// glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, value);
// glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, value);
// }
//
// glActiveTexture(GL_TEXTURE0);
}
// Get the texture unit
//--------------------------------------------------------------------------
int GetTextureUnit(void)
{
if (!this->TextureObject)
{
this->LastLinearInterpolation = linearInterpolation;
GLint value;
if (linearInterpolation)
{
value = GL_LINEAR;
}
else
{
value = GL_NEAREST;
}
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, value);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, value);
return -1;
}
glActiveTexture(GL_TEXTURE0);
return this->TextureObject->GetTextureUnit();
}
protected:
bool Loaded;
bool LastLinearInterpolation;
// bool Loaded;
// bool LastLinearInterpolation;
int TexutureWidth;
int TextureWidth;
int NumberOfColorComponents;
GLuint TextureId;
// GLuint TextureId;
vtkTextureObject* TextureObject;
double LastRange[2];
float* Table;
......
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