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
|
/*=========================================================================
Program: Visualization Toolkit
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 "vtkShader.h"
#include "vtkObjectFactory.h"
#include "vtk_glew.h"
vtkStandardNewMacro(vtkShader)
vtkShader::vtkShader()
{
this->Dirty = true;
this->Handle = 0;
this->ShaderType = vtkShader::Unknown;
}
vtkShader::~vtkShader()
{
}
void vtkShader::SetType(Type type)
{
this->ShaderType = type;
this->Dirty = true;
}
void vtkShader::SetSource(const std::string &source)
{
this->Source = source;
this->Dirty = true;
}
bool vtkShader::Compile()
{
if (this->Source.empty() || this->ShaderType == Unknown || !this->Dirty)
{
return false;
}
// Ensure we delete the previous shader if necessary.
if (this->Handle != 0)
{
glDeleteShader(static_cast<GLuint>(this->Handle));
this->Handle = 0;
}
GLenum type = GL_VERTEX_SHADER;
switch (this->ShaderType)
{
#ifdef GL_GEOMETRY_SHADER
case vtkShader::Geometry:
type = GL_GEOMETRY_SHADER;
break;
#endif
case vtkShader::Fragment:
type = GL_FRAGMENT_SHADER;
break;
case vtkShader::Vertex:
default:
type = GL_VERTEX_SHADER;
break;
}
GLuint handle = glCreateShader(type);
const GLchar *source = static_cast<const GLchar *>(this->Source.c_str());
glShaderSource(handle, 1, &source, NULL);
glCompileShader(handle);
GLint isCompiled;
glGetShaderiv(handle, GL_COMPILE_STATUS, &isCompiled);
// Handle shader compilation failures.
if (!isCompiled)
{
GLint length(0);
glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &length);
if (length > 1)
{
char *logMessage = new char[length];
glGetShaderInfoLog(handle, length, NULL, logMessage);
this->Error = logMessage;
delete[] logMessage;
}
glDeleteShader(handle);
return false;
}
// The shader compiled, store its handle and return success.
this->Handle = static_cast<int>(handle);
this->Dirty = false;
return true;
}
void vtkShader::Cleanup()
{
if (this->ShaderType == Unknown || this->Handle == 0)
{
return;
}
glDeleteShader(static_cast<GLuint>(this->Handle));
this->Handle = 0;
this->Dirty = true;
}
// ----------------------------------------------------------------------------
void vtkShader::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
|