Commit 5452f583 authored by Aashish Chaudhary's avatar Aashish Chaudhary

Adding support for 3D textures

Change-Id: I536ca9863e729b4dc4b7fe61041b2ba7e6fa1dde
parent ed2e827d
......@@ -400,6 +400,12 @@ void vtkTextureObject::CreateTexture()
glTexParameteri(this->Target, GL_TEXTURE_WRAP_T,
this->GetWrapTMode(this->WrapT));
if (this->Target == GL_TEXTURE_3D)
{
glTexParameteri(this->Target, GL_TEXTURE_WRAP_R,
this->GetWrapRMode(this->WrapR));
}
#ifdef GL_TEXTURE_BASE_LEVEL
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
#endif
......@@ -408,12 +414,6 @@ void vtkTextureObject::CreateTexture()
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
#endif
if (this->Depth)
{
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE,
this->GetDepthTextureModeFormat(this->DepthTextureMode));
}
glBindTexture(this->Target, 0);
}
}
......@@ -531,7 +531,12 @@ void vtkTextureObject::SendParameters()
assert("pre: is_bound" && this->IsBound());
glTexParameteri(this->Target,GL_TEXTURE_WRAP_S, OpenGLWrap[this->WrapS]);
glTexParameteri(this->Target,GL_TEXTURE_WRAP_T,OpenGLWrap[this->WrapT]);
glTexParameteri(this->Target,GL_TEXTURE_WRAP_T, OpenGLWrap[this->WrapT]);
if (this->Target == GL_TEXTURE_3D)
{
glTexParameteri(this->Target,GL_TEXTURE_WRAP_R, OpenGLWrap[this->WrapR]);
}
#ifdef GL_TEXTURE_WRAP_R
glTexParameteri(
......@@ -554,6 +559,12 @@ void vtkTextureObject::SendParameters()
#if GL_ES_VERSION_3_0 != 1
glTexParameterfv(this->Target,GL_TEXTURE_BORDER_COLOR,this->BorderColor);
if (this->Depth)
{
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE,
this->GetDepthTextureModeFormat(this->DepthTextureMode));
}
if(DepthTextureCompare)
{
glTexParameteri(
......@@ -1132,6 +1143,12 @@ unsigned int vtkTextureObject::GetWrapTMode(int vtktype)
return this->GetWrapSMode(vtktype);
}
//----------------------------------------------------------------------------
unsigned int vtkTextureObject::GetWrapRMode(int vtktype)
{
return this->GetWrapSMode(vtktype);
}
#if GL_ES_VERSION_2_0 != 1
//----------------------------------------------------------------------------
......@@ -1523,6 +1540,61 @@ bool vtkTextureObject::Create2DFromRaw(unsigned int width, unsigned int height,
return true;
}
//----------------------------------------------------------------------------
bool vtkTextureObject::Create3DFromRaw(unsigned int width, unsigned int height,
unsigned int depth, int numComps,
int dataType, void *data)
{
assert(this->Context);
// Now, detemine 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 detemine texture parameters.");
return false;
}
this->Target = GL_TEXTURE_3D;
this->Format = format;
this->Type = type;
this->Components = numComps;
this->Width = width;
this->Height = height;
this->Depth = depth;
this->NumberOfDimensions = 3;
this->CreateTexture();
this->Bind();
// Source texture data from the PBO.
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage3D(
this->Target,
0,
internalFormat,
static_cast<GLsizei>(this->Width),
static_cast<GLsizei>(this->Height),
static_cast<GLsizei>(this->Depth),
0,
this->Format,
this->Type,
static_cast<const GLvoid *>(data));
vtkOpenGLCheckErrorMacro("failed at glTexImage2D");
this->UnBind();
return true;
}
// ----------------------------------------------------------------------------
// Description:
// Create a 1D alpha texture using a raw pointer.
......
......@@ -202,6 +202,13 @@ public:
bool Create2DFromRaw(unsigned int width, unsigned int height,
int numComps, int dataType, void *data);
// Description:
// Create a 3D texture from client memory
// numComps must be in [1-4].
bool Create3DFromRaw(unsigned int width, unsigned int height,
unsigned int depth, int numComps,
int dataType, void *data);
// Description:
// Create a 2D depth texture using a raw pointer.
// This is a blocking call. If you can, use PBO instead.
......@@ -312,7 +319,8 @@ public:
unsigned int GetMinificationFilterMode(int vtktype);
unsigned int GetMagnificationFilterMode(int vtktype);
unsigned int GetWrapSMode(int vtktype);
unsigned int GetWrapTMode(int vtkTypeFloat32);
unsigned int GetWrapTMode(int vtktype);
unsigned int GetWrapRMode(int vtktype);
// Description:
// Optional, require support for floating point depth buffer
......
......@@ -92,7 +92,8 @@ public:
this->CubeVAOId = 0;
#endif
this->CubeIndicesId = 0;
this->VolumeTextureId = 0;
// this->VolumeTextureId = 0;
this->VolumeTextureObject = 0;
this->NoiseTextureObject = 0;
this->DepthTextureObject = 0;
// this->NoiseTextureId = 0;
......@@ -190,7 +191,8 @@ public:
void Initialize(vtkRenderer* ren, vtkVolume* vol);
bool LoadVolume(vtkImageData* imageData, vtkDataArray* scalars);
bool LoadVolume(vtkRenderer* ren, vtkImageData* imageData,
vtkDataArray* scalars);
bool LoadMask(vtkImageData* input,
vtkImageData* maskInput,
......@@ -271,9 +273,10 @@ public:
#endif
GLuint CubeIndicesId;
GLuint VolumeTextureId;
vtkTextureObject * NoiseTextureObject;
vtkTextureObject * DepthTextureObject;
// GLuint VolumeTextureId;
vtkTextureObject* VolumeTextureObject;
vtkTextureObject* NoiseTextureObject;
vtkTextureObject* DepthTextureObject;
// GLuint NoiseTextureId;
// GLuint DepthTextureId;
......@@ -440,22 +443,22 @@ void vtkOpenGLGPUVolumeRayCastMapper::vtkInternal::Initialize(
}
//----------------------------------------------------------------------------
bool vtkOpenGLGPUVolumeRayCastMapper::vtkInternal::LoadVolume(
bool vtkOpenGLGPUVolumeRayCastMapper::vtkInternal::LoadVolume(vtkRenderer* ren,
vtkImageData* imageData, vtkDataArray* scalars)
{
// Generate OpenGL texture
glActiveTexture(GL_TEXTURE8);
glGenTextures(1, &this->VolumeTextureId);
glBindTexture(GL_TEXTURE_3D, this->VolumeTextureId);
// Set the texture parameters
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
GLfloat borderColor[4]={0.0,0.0,0.0,0.0};
glTexParameterfv(GL_TEXTURE_3D,GL_TEXTURE_BORDER_COLOR, borderColor);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// // Generate OpenGL texture
// glActiveTexture(GL_TEXTURE8);
// glGenTextures(1, &this->VolumeTextureId);
// glBindTexture(GL_TEXTURE_3D, this->VolumeTextureId);
// // Set the texture parameters
// glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
// glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
// GLfloat borderColor[4]={0.0,0.0,0.0,0.0};
// glTexParameterfv(GL_TEXTURE_3D,GL_TEXTURE_BORDER_COLOR, borderColor);
// glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// Allocate data with internal format and foramt as (GL_RED)
GLint internalFormat = 0;
......@@ -596,17 +599,41 @@ bool vtkOpenGLGPUVolumeRayCastMapper::vtkInternal::LoadVolume(
++i;
}
if (!this->VolumeTextureObject)
{
this->VolumeTextureObject = vtkTextureObject::New();
this->VolumeTextureObject->SetContext(vtkOpenGLRenderWindow::SafeDownCast(
ren->GetRenderWindow()));
}
if (!handleLargeDataTypes)
{
void* dataPtr = scalars->GetVoidPointer(0);
glPixelTransferf(GL_RED_SCALE,static_cast<GLfloat>(this->Scale));
glPixelTransferf(GL_RED_BIAS,static_cast<GLfloat>(this->Bias));
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage3D(GL_TEXTURE_3D, 0, internalFormat,
this->TextureSize[0],this->TextureSize[1],
this->TextureSize[2], 0,
format, type, dataPtr);
this->VolumeTextureObject->Create3DFromRaw(
this->TextureSize[0],
this->TextureSize[1],
this->TextureSize[2],
scalars->GetNumberOfComponents(),
scalarType,
dataPtr);
this->VolumeTextureObject->Activate();
this->VolumeTextureObject->SetWrapS(vtkTextureObject::ClampToEdge);
this->VolumeTextureObject->SetWrapT(vtkTextureObject::ClampToEdge);
this->VolumeTextureObject->SetWrapR(vtkTextureObject::ClampToEdge);
this->VolumeTextureObject->SetMagnificationFilter(vtkTextureObject::Linear);
this->VolumeTextureObject->SetMinificationFilter(vtkTextureObject::Linear);
this->VolumeTextureObject->SetBorderColor(0.0f, 0.0f, 0.0f, 0.0f);
// glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// glTexImage3D(GL_TEXTURE_3D, 0, internalFormat,
// this->TextureSize[0],this->TextureSize[1],
// this->TextureSize[2], 0,
// format, type, dataPtr);
// Set scale and bias to their defaults
glPixelTransferf(GL_RED_SCALE,1.0);
......@@ -1724,14 +1751,14 @@ void vtkOpenGLGPUVolumeRayCastMapper::ReleaseGraphicsResources(
{
this->Impl->DeleteBufferObjects();
if(this->Impl->VolumeTextureId)
{
window->MakeCurrent();
GLuint volumeTextureObject = static_cast<GLuint>(
this->Impl->VolumeTextureId);
glDeleteTextures(1, &volumeTextureObject);
this->Impl->VolumeTextureId = 0;
}
// if(this->Impl->VolumeTextureId)
// {
// window->MakeCurrent();
// GLuint volumeTextureObject = static_cast<GLuint>(
// this->Impl->VolumeTextureId);
// glDeleteTextures(1, &volumeTextureObject);
// this->Impl->VolumeTextureId = 0;
// }
// if(this->Impl->NoiseTextureId)
// {
......@@ -1742,6 +1769,13 @@ void vtkOpenGLGPUVolumeRayCastMapper::ReleaseGraphicsResources(
// this->Impl->NoiseTextureId = 0;
// }
if (this->Impl->VolumeTextureObject)
{
this->Impl->VolumeTextureObject->ReleaseGraphicsResources(window);
this->Impl->VolumeTextureObject->Delete();
this->Impl->VolumeTextureObject = 0;
}
if (this->Impl->NoiseTextureObject)
{
this->Impl->NoiseTextureObject->ReleaseGraphicsResources(window);
......@@ -2094,7 +2128,7 @@ void vtkOpenGLGPUVolumeRayCastMapper::GPURender(vtkRenderer* ren,
// Update bounds, data, and geometry
this->Impl->ComputeBounds(input);
this->Impl->LoadVolume(input, scalars);
this->Impl->LoadVolume(ren, input, scalars);
this->Impl->LoadMask(input, this->MaskInput,
this->Impl->Extents, vol);
}
......@@ -2228,7 +2262,8 @@ void vtkOpenGLGPUVolumeRayCastMapper::GPURender(vtkRenderer* ren,
vtkInternal::ToFloat(this->Impl->ScalarsRange, fvalue2);
this->Impl->ShaderProgram->SetUniform2fv("in_scalarsRange", 1, &fvalue2);
this->Impl->ShaderProgram->SetUniformi("in_volume", 8);
this->Impl->ShaderProgram->SetUniformi("in_volume",
this->Impl->VolumeTextureObject->GetTextureUnit());
this->Impl->ShaderProgram->SetUniformi("in_opacityTransferFunc",
this->Impl->OpacityTables->GetTable(0)->GetTextureUnit());
this->Impl->ShaderProgram->SetUniformi("in_noiseSampler",
......@@ -2277,9 +2312,7 @@ void vtkOpenGLGPUVolumeRayCastMapper::GPURender(vtkRenderer* ren,
this->Impl->ShaderProgram->SetUniformf("in_shininess", fvalue3[0]);
// Bind textures
// Volume texture is at unit 0
glActiveTexture(GL_TEXTURE8);
glBindTexture(GL_TEXTURE_3D, this->Impl->VolumeTextureId);
this->Impl->VolumeTextureObject->Activate();
// Color texture is at unit 1
if (numberOfScalarComponents == 1)
......
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