1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkOpenGLScalarsToColorsPainter.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkOpenGLScalarsToColorsPainter.h"
#include "vtkActor.h"
#include "vtkCellData.h"
#include "vtkDataArray.h"
#include "vtkImageData.h"
#include "vtkMapper.h" //for VTK_MATERIALMODE_*
#include "vtkObjectFactory.h"
#include "vtkOpenGLRenderer.h"
#include "vtkOpenGLRenderWindow.h"
#include "vtkOpenGLTexture.h"
#include "vtkPointData.h"
#include "vtkPolyData.h"
#include "vtkProperty.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkOpenGL.h"
#include "vtkOpenGLError.h"
#include "vtkOpenGLExtensionManager.h"
#include "vtkgl.h" // vtkgl namespace
vtkStandardNewMacro(vtkOpenGLScalarsToColorsPainter);
//-----------------------------------------------------------------------------
vtkOpenGLScalarsToColorsPainter::vtkOpenGLScalarsToColorsPainter()
{
this->InternalColorTexture = 0;
this->AlphaBitPlanes = -1;
this->AcquiredGraphicsResources = false;
this->SupportsSeparateSpecularColor = false;
}
//-----------------------------------------------------------------------------
vtkOpenGLScalarsToColorsPainter::~vtkOpenGLScalarsToColorsPainter()
{
if (this->InternalColorTexture)
{
this->InternalColorTexture->Delete();
this->InternalColorTexture = 0;
}
}
//-----------------------------------------------------------------------------
void vtkOpenGLScalarsToColorsPainter::ReleaseGraphicsResources(vtkWindow* win)
{
if (this->InternalColorTexture)
{
this->InternalColorTexture->ReleaseGraphicsResources(win);
}
this->AcquiredGraphicsResources = false;
this->Superclass::ReleaseGraphicsResources(win);
}
//-----------------------------------------------------------------------------
int vtkOpenGLScalarsToColorsPainter::GetPremultiplyColorsWithAlpha(
vtkActor* actor)
{
// Use the AlphaBitPlanes member, which should already be initialized. If
// not, initialize it.
GLint alphaBits;
if (this->AlphaBitPlanes < 0)
{
glGetIntegerv(GL_ALPHA_BITS, &alphaBits);
this->AlphaBitPlanes = (int)alphaBits;
}
alphaBits = (GLint)this->AlphaBitPlanes;
// Dealing with having a correct alpha (none square) in the framebuffer
// is only required if there is an alpha component in the framebuffer
// (doh...) and if we cannot deal directly with BlendFuncSeparate.
return vtkgl::BlendFuncSeparate==0 && alphaBits>0 &&
this->Superclass::GetPremultiplyColorsWithAlpha(actor);
}
//-----------------------------------------------------------------------------
vtkIdType vtkOpenGLScalarsToColorsPainter::GetTextureSizeLimit()
{
GLint textureSize = 0;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &textureSize);
return static_cast<vtkIdType>(textureSize);
}
//-----------------------------------------------------------------------------
void vtkOpenGLScalarsToColorsPainter::RenderInternal(vtkRenderer *renderer,
vtkActor *actor,
unsigned long typeflags,
bool forceCompileOnly)
{
vtkOpenGLClearErrorMacro();
// If we have not yet set the alpha bit planes, do it based on the
// render window so we're not querying GL in the middle of render.
if (this->AlphaBitPlanes < 0)
{
vtkOpenGLRenderer* oglRenderer = vtkOpenGLRenderer::SafeDownCast(renderer);
if (oglRenderer)
{
vtkOpenGLRenderWindow* context = vtkOpenGLRenderWindow::SafeDownCast(
oglRenderer->GetRenderWindow());
if (context)
{
this->AlphaBitPlanes = context->GetAlphaBitPlanes();
}
}
}
// check for separate specular color support
if (!this->AcquiredGraphicsResources)
{
vtkOpenGLRenderer *oglRenderer = vtkOpenGLRenderer::SafeDownCast(renderer);
if (oglRenderer)
{
vtkOpenGLRenderWindow *oglRenderWindow =
vtkOpenGLRenderWindow::SafeDownCast(oglRenderer->GetRenderWindow());
if (oglRenderWindow)
{
vtkOpenGLExtensionManager *oglExtensionManager =
oglRenderWindow->GetExtensionManager();
if (oglExtensionManager)
{
this->SupportsSeparateSpecularColor =
(oglExtensionManager->ExtensionSupported("GL_EXT_separate_specular_color") != 0);
}
}
}
this->AcquiredGraphicsResources = true;
}
// If we are coloring by texture, then load the texture map.
if (this->ColorTextureMap)
{
if (this->InternalColorTexture == 0)
{
this->InternalColorTexture = vtkOpenGLTexture::New();
this->InternalColorTexture->RepeatOff();
this->InternalColorTexture->EdgeClampOn();
}
this->InternalColorTexture->SetInputData(this->ColorTextureMap);
this->LastWindow = renderer->GetRenderWindow();
}
else if (this->LastWindow)
{
// release the texture.
this->ReleaseGraphicsResources(this->LastWindow);
this->LastWindow = 0;
}
vtkProperty* prop = actor->GetProperty();
// if we are doing vertex colors then set lmcolor to adjust
// the current materials ambient and diffuse values using
// vertex color commands otherwise tell it not to.
glDisable(GL_COLOR_MATERIAL);
if (this->UsingScalarColoring)
{
GLenum lmcolorMode;
if (this->ScalarMaterialMode == VTK_MATERIALMODE_DEFAULT)
{
lmcolorMode = (prop->GetAmbient() > prop->GetDiffuse()) ?
GL_AMBIENT : GL_DIFFUSE;
}
else if (this->ScalarMaterialMode == VTK_MATERIALMODE_AMBIENT_AND_DIFFUSE)
{
lmcolorMode = GL_AMBIENT_AND_DIFFUSE;
}
else if (this->ScalarMaterialMode == VTK_MATERIALMODE_AMBIENT)
{
lmcolorMode = GL_AMBIENT;
}
else // if (this->ScalarMaterialMode == VTK_MATERIALMODE_DIFFUSE)
{
lmcolorMode = GL_DIFFUSE;
}
glColorMaterial(GL_FRONT_AND_BACK, lmcolorMode);
glEnable(GL_COLOR_MATERIAL);
if (this->ColorTextureMap)
{
this->InternalColorTexture->Load(renderer);
// Keep the surface color from interacting with color loaded from texture.
// We don't simple use (GL_TEXTURE_ENV_MODE, GL_REPLACE) since that
// implies all the lighting colors are lost too i.e. no diffuse
// highlights.
glColor3f(1.0, 1.0, 1.0);
}
}
int pre_multiplied_by_alpha = this->GetPremultiplyColorsWithAlpha(actor);
if (pre_multiplied_by_alpha || this->InterpolateScalarsBeforeMapping)
{
// save the blend function.
glPushAttrib(GL_COLOR_BUFFER_BIT | GL_LIGHTING_BIT);
}
// We colors were premultiplied by alpha then we change the blending
// function to one that will compute correct blended destination alpha
// value, otherwise we stick with the default.
if (pre_multiplied_by_alpha)
{
// the following function is not correct with textures because there are
// not premultiplied by alpha.
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
}
if (this->InterpolateScalarsBeforeMapping && this->SupportsSeparateSpecularColor)
{
// Turn on color sum and separate specular color so specular works
// with texturing.
glEnable(vtkgl::COLOR_SUM);
glLightModeli(vtkgl::LIGHT_MODEL_COLOR_CONTROL, vtkgl::SEPARATE_SPECULAR_COLOR);
}
this->Superclass::RenderInternal(renderer, actor, typeflags, forceCompileOnly);
if (this->InterpolateScalarsBeforeMapping && this->SupportsSeparateSpecularColor)
{
glLightModeli(vtkgl::LIGHT_MODEL_COLOR_CONTROL, vtkgl::SINGLE_COLOR);
glDisable(vtkgl::COLOR_SUM);
}
if (pre_multiplied_by_alpha || this->InterpolateScalarsBeforeMapping)
{
// restore the blend function & lights
glPopAttrib();
}
vtkOpenGLCheckErrorMacro("failed after RenderInternal");
}
//-----------------------------------------------------------------------------
void vtkOpenGLScalarsToColorsPainter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "AlphaBitPlanes: " << this->AlphaBitPlanes << endl;
}
|