Commit ee190de1 authored by Sankhesh Jhaveri's avatar Sankhesh Jhaveri

Ability to modify externally created lights in the external renderer

parent d9d8f108
......@@ -16,8 +16,10 @@
#include "vtkCamera.h"
#include "vtkCommand.h"
#include "vtkExternalLight.h"
#include "vtkExternalOpenGLCamera.h"
#include "vtkLightCollection.h"
#include "vtkLightCollection.h"
#include "vtkLight.h"
#include "vtkMath.h"
#include "vtkMatrix4x4.h"
......@@ -38,11 +40,14 @@ vtkExternalOpenGLRenderer::vtkExternalOpenGLRenderer()
this->PreserveColorBuffer = 1;
this->PreserveDepthBuffer = 1;
this->SetAutomaticLightCreation(0);
this->ExternalLights = vtkLightCollection::New();
}
//----------------------------------------------------------------------------
vtkExternalOpenGLRenderer::~vtkExternalOpenGLRenderer()
{
this->ExternalLights->Delete();
this->ExternalLights = NULL;
}
//----------------------------------------------------------------------------
......@@ -99,63 +104,181 @@ void vtkExternalOpenGLRenderer::Render(void)
curLight++)
{
GLboolean status;
GLfloat info[4];
glGetBooleanv(curLight, &status);
// Find out if there is an external light object associated with this
// light index.
vtkCollectionSimpleIterator sit;
vtkExternalLight* eLight;
vtkExternalLight* curExtLight = NULL;
for (this->ExternalLights->InitTraversal(sit);
(eLight = vtkExternalLight::SafeDownCast(
this->ExternalLights->GetNextLight(sit))); )
{
if (eLight &&
(static_cast<GLenum>(eLight->GetLightIndex()) == curLight))
{
curExtLight = eLight;
break;
}
}
if (status)
{
// For each enabled OpenGL light, add a new VTK headlight.
// Headlight because VTK will apply transform matrices.
vtkLight* light = vtkLight::New();
light->SetLightTypeToHeadlight();
// Set color parameters
glGetLightfv(curLight, GL_AMBIENT, info);
light->SetAmbientColor(info[0], info[1], info[2]);
glGetLightfv(curLight, GL_DIFFUSE, info);
light->SetDiffuseColor(info[0], info[1], info[2]);
glGetLightfv(curLight, GL_SPECULAR, info);
light->SetSpecularColor(info[0], info[1], info[2]);
// Position, focal point and positional
glGetLightfv(curLight, GL_POSITION, info);
light->SetPositional(info[3] > 0.0 ? 1 : 0);
if (!light->GetPositional())
if (curExtLight &&
(curExtLight->GetReplaceMode() == vtkExternalLight::ALL_PARAMS))
{
light->SetFocalPoint(0, 0, 0);
light->SetPosition(-info[0], -info[1], -info[2]);
this->AddLight(curExtLight);
}
else
{
light->SetFocalPoint(0, 0, 0);
light->SetPosition(info[0], info[1], info[2]);
// For each enabled OpenGL light, add a new VTK headlight.
// Headlight because VTK will apply transform matrices.
vtkLight* light = vtkLight::New();
light->SetLightTypeToHeadlight();
GLfloat info[4];
// Set color parameters
if (curExtLight && curExtLight->GetIntensitySet())
{
light->SetIntensity(curExtLight->GetIntensity());
}
// Attenuation
glGetLightfv(curLight, GL_CONSTANT_ATTENUATION, &info[0]);
glGetLightfv(curLight, GL_LINEAR_ATTENUATION, &info[1]);
glGetLightfv(curLight, GL_QUADRATIC_ATTENUATION, &info[2]);
light->SetAttenuationValues(info[0], info[1], info[2]);
if (curExtLight && curExtLight->GetAmbientColorSet())
{
light->SetAmbientColor(curExtLight->GetAmbientColor());
}
else
{
glGetLightfv(curLight, GL_AMBIENT, info);
light->SetAmbientColor(info[0], info[1], info[2]);
}
if (curExtLight && curExtLight->GetDiffuseColorSet())
{
light->SetDiffuseColor(curExtLight->GetDiffuseColor());
}
else
{
glGetLightfv(curLight, GL_DIFFUSE, info);
light->SetDiffuseColor(info[0], info[1], info[2]);
}
if (curExtLight && curExtLight->GetSpecularColorSet())
{
light->SetSpecularColor(curExtLight->GetSpecularColor());
}
else
{
glGetLightfv(curLight, GL_SPECULAR, info);
light->SetSpecularColor(info[0], info[1], info[2]);
}
// Cutoff
glGetLightfv(curLight, GL_SPOT_CUTOFF, &info[0]);
light->SetConeAngle(info[0]);
// Position, focal point and positional
glGetLightfv(curLight, GL_POSITION, info);
if (light->GetConeAngle() < 180.0)
if (curExtLight && curExtLight->GetPositionalSet())
{
// Exponent
glGetLightfv(curLight, GL_SPOT_EXPONENT, &info[0]);
light->SetExponent(info[0]);
light->SetPositional(curExtLight->GetPositional());
}
else
{
light->SetPositional(info[3] > 0.0 ? 1 : 0);
}
// Direction
glGetLightfv(curLight, GL_SPOT_DIRECTION, info);
for (unsigned int i = 0; i < 3; ++i)
if (!light->GetPositional())
{
if (curExtLight && curExtLight->GetFocalPointSet())
{
info[i] += light->GetPosition()[i];
light->SetFocalPoint(curExtLight->GetFocalPoint());
if (curExtLight->GetPositionSet())
{
light->SetPosition(curExtLight->GetPosition());
}
else
{
light->SetPosition(info[0], info[1], info[2]);
}
}
else
{
light->SetFocalPoint(0, 0, 0);
if (curExtLight->GetPositionSet())
{
light->SetPosition(curExtLight->GetPosition());
}
else
{
light->SetPosition(-info[0], -info[1], -info[2]);
}
}
light->SetFocalPoint(info[0], info[1], info[2]);
}
else
{
if (curExtLight && curExtLight->GetPositionSet())
{
light->SetPosition(curExtLight->GetPosition());
}
else
{
light->SetPosition(info[0], info[1], info[2]);
}
// Attenuation
if (curExtLight && curExtLight->GetAttenuationValuesSet())
{
light->SetAttenuationValues(curExtLight->GetAttenuationValues());
}
else
{
glGetLightfv(curLight, GL_CONSTANT_ATTENUATION, &info[0]);
glGetLightfv(curLight, GL_LINEAR_ATTENUATION, &info[1]);
glGetLightfv(curLight, GL_QUADRATIC_ATTENUATION, &info[2]);
light->SetAttenuationValues(info[0], info[1], info[2]);
}
// Cutoff
if (curExtLight && curExtLight->GetConeAngleSet())
{
light->SetConeAngle(curExtLight->GetConeAngle());
}
else
{
glGetLightfv(curLight, GL_SPOT_CUTOFF, &info[0]);
light->SetConeAngle(info[0]);
}
if (light->GetConeAngle() < 180.0)
{
// Exponent
if (curExtLight && curExtLight->GetExponentSet())
{
light->SetExponent(curExtLight->GetExponent());
}
else
{
glGetLightfv(curLight, GL_SPOT_EXPONENT, &info[0]);
light->SetExponent(info[0]);
}
// Direction
if (curExtLight && curExtLight->GetFocalPointSet())
{
light->SetFocalPoint(curExtLight->GetFocalPoint());
}
else
{
glGetLightfv(curLight, GL_SPOT_DIRECTION, info);
for (unsigned int i = 0; i < 3; ++i)
{
info[i] += light->GetPosition()[i];
}
light->SetFocalPoint(info[0], info[1], info[2]);
}
}
}
this->AddLight(light);
light->Delete();
}
this->AddLight(light);
light->Delete();
}
}
......@@ -171,9 +294,50 @@ vtkCamera* vtkExternalOpenGLRenderer::MakeCamera()
return cam;
}
//----------------------------------------------------------------------------
void vtkExternalOpenGLRenderer::AddExternalLight(vtkExternalLight *light)
{
if (!light)
{
return;
}
vtkExternalLight* aLight;
vtkCollectionSimpleIterator sit;
for (this->ExternalLights->InitTraversal(sit);
(aLight = vtkExternalLight::SafeDownCast(
this->ExternalLights->GetNextLight(sit))); )
{
if (aLight && (aLight->GetLightIndex() == light->GetLightIndex()))
{
vtkErrorMacro( << "Attempting to add light with index " <<
light->GetLightIndex() <<
". But light with same index already exists.");
return;
}
}
this->ExternalLights->AddItem(light);
}
//----------------------------------------------------------------------------
void vtkExternalOpenGLRenderer::RemoveExternalLight(vtkExternalLight *light)
{
this->ExternalLights->RemoveItem(light);
}
//----------------------------------------------------------------------------
void vtkExternalOpenGLRenderer::RemoveAllExternalLights()
{
this->ExternalLights->RemoveAllItems();
}
//----------------------------------------------------------------------------
void vtkExternalOpenGLRenderer::PrintSelf(ostream &os, vtkIndent indent)
{
os << indent << "PreserveColorBuffer: " << this->PreserveColorBuffer << "\n";
this->Superclass::PrintSelf(os, indent);
os << indent << "External Lights:\n";
this->ExternalLights->PrintSelf(os, indent.GetNextIndent());
}
......@@ -33,6 +33,10 @@
#include "vtkRenderingExternalModule.h" // For export macro
#include "vtkOpenGLRenderer.h"
// Forward declarations
class vtkLightCollection;
class vtkExternalLight;
class VTKRENDERINGEXTERNAL_EXPORT vtkExternalOpenGLRenderer :
public vtkOpenGLRenderer
{
......@@ -50,10 +54,26 @@ public:
// This function creates the vtkExternalOpenGLCamera.
vtkCamera* MakeCamera();
// Description:
// Add an external light to the list of external lights.
virtual void AddExternalLight(vtkExternalLight *);
// Description:
// Remove an external light from the list of external lights.
virtual void RemoveExternalLight(vtkExternalLight *);
// Description:
// Remove all external lights
virtual void RemoveAllExternalLights();
// Description:
protected:
vtkExternalOpenGLRenderer();
~vtkExternalOpenGLRenderer();
vtkLightCollection *ExternalLights;
private:
vtkExternalOpenGLRenderer(const vtkExternalOpenGLRenderer&); // Not implemented.
void operator=(const vtkExternalOpenGLRenderer&); // Not implemented.
......
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