1 /*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkColorMaterialHelper.cxx
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14 =========================================================================*/
15 #include "vtkColorMaterialHelper.h"
16
17 #include "vtkShaderProgram2.h"
18 #include "vtkObjectFactory.h"
19 #include "vtkShader2.h"
20 #include "vtkShader2Collection.h"
21 #include "vtkUniformVariables.h"
22 #include "vtkgl.h"
23
24 extern const char * vtkColorMaterialHelper_vs;
25
26 //----------------------------------------------------------------------------
27 vtkStandardNewMacro(vtkColorMaterialHelper);
28
29 //----------------------------------------------------------------------------
30 vtkCxxSetObjectMacro(vtkColorMaterialHelper, Shader, vtkShaderProgram2);
31
32 //----------------------------------------------------------------------------
vtkColorMaterialHelper()33 vtkColorMaterialHelper::vtkColorMaterialHelper()
34 {
35 this->Shader = 0;
36 }
37
38 //----------------------------------------------------------------------------
~vtkColorMaterialHelper()39 vtkColorMaterialHelper::~vtkColorMaterialHelper()
40 {
41 this->SetShader(0);
42 }
43
44 //----------------------------------------------------------------------------
Initialize(vtkShaderProgram2 * pgm)45 void vtkColorMaterialHelper::Initialize(vtkShaderProgram2* pgm)
46 {
47 if (this->Shader != pgm)
48 {
49 this->SetShader(pgm);
50 if (pgm)
51 {
52 vtkShader2 *s=vtkShader2::New();
53 s->SetSourceCode(vtkColorMaterialHelper_vs);
54 s->SetType(VTK_SHADER_TYPE_VERTEX);
55 s->SetContext(pgm->GetContext());
56 pgm->GetShaders()->AddItem(s);
57 s->Delete();
58 }
59 }
60 }
61
62 //----------------------------------------------------------------------------
PrepareForRendering()63 void vtkColorMaterialHelper::PrepareForRendering()
64 {
65 #ifndef NDEBUG
66 if (!this->Shader)
67 {
68 vtkErrorMacro("Please Initialize() before calling PrepareForRendering().");
69 return ;
70 }
71 #endif
72
73 this->Mode = vtkColorMaterialHelper::DISABLED;
74 if (glIsEnabled(GL_COLOR_MATERIAL))
75 {
76 GLint colorMaterialParameter;
77 glGetIntegerv(GL_COLOR_MATERIAL_PARAMETER, &colorMaterialParameter);
78 switch (colorMaterialParameter)
79 {
80 case GL_AMBIENT:
81 this->Mode = vtkColorMaterialHelper::AMBIENT;
82 break;
83
84 case GL_DIFFUSE:
85 this->Mode = vtkColorMaterialHelper::DIFFUSE;
86 break;
87
88 case GL_SPECULAR:
89 this->Mode = vtkColorMaterialHelper::SPECULAR;
90 break;
91
92 case GL_AMBIENT_AND_DIFFUSE:
93 this->Mode = vtkColorMaterialHelper::AMBIENT_AND_DIFFUSE;
94 break;
95
96 case GL_EMISSION:
97 this->Mode = vtkColorMaterialHelper::EMISSION;
98 break;
99 }
100 }
101 }
102
103 //----------------------------------------------------------------------------
Render()104 void vtkColorMaterialHelper::Render()
105 {
106 #ifndef NDEBUG
107 if (!this->Shader)
108 {
109 vtkErrorMacro("Please Initialize() before calling Render().");
110 return;
111 }
112 #endif
113
114 int value=this->Mode;
115 this->Shader->GetUniformVariables()->SetUniformi("vtkColorMaterialHelper_Mode",1,&value);
116 }
117
118 //----------------------------------------------------------------------------
SetUniformVariables()119 void vtkColorMaterialHelper::SetUniformVariables()
120 {
121 this->PrepareForRendering(); // iniitialize this with gl state
122 this->Render(); // send as uniforms
123 }
124
125 //----------------------------------------------------------------------------
PrintSelf(ostream & os,vtkIndent indent)126 void vtkColorMaterialHelper::PrintSelf(ostream& os, vtkIndent indent)
127 {
128 this->Superclass::PrintSelf(os, indent);
129 os << indent << "Shader: " << this->Shader << endl;
130 }
131