Eigene Dateien/FlowVis/src/VShader.cpp

Go to the documentation of this file.
00001 #include "VShader.h"
00002 #include "glew.h"
00003 
00004 void VShader::initialize()
00005 {
00006         if (!GLEW_ARB_shader_objects)
00007                 std::cout << "GL_ARB_shader_objects is not supported" << std::endl;
00008 
00009         if (!GLEW_ARB_fragment_shader)
00010                 std::cout << "GL_ARB_fragment_shader is not supported" << std::endl;
00011 
00012         if (!GLEW_ARB_vertex_shader)
00013                 std::cout << "GL_ARB_vertex_shader is not supported" << std::endl;
00014 
00015         if (!m_uObject)
00016         {
00017                 m_uObject = glCreateShaderObjectARB(m_uType);
00018                 const GLenum glError = glGetError();
00019 
00020                 if (glError != GL_NO_ERROR)
00021                         std::cout << "Error generating shader " << gluErrorString(glError) << std::endl;
00022 
00023                 s_mapAttachment[m_uObject] = 0;
00024         }
00025 }
00026 
00027 void VShader::uninitialize()
00028 {
00029         if (m_uObject)
00030         {
00031                 if((int)s_mapAttachment.size() > 0)
00032                 if (s_mapAttachment[m_uObject] == 0)
00033                 {
00034                         glDeleteObjectARB(m_uObject);
00035                         const GLenum glError = glGetError();
00036 
00037                         if (glError != GL_NO_ERROR)
00038                                 std::cout << "Error deleting shader " << gluErrorString(glError) << std::endl;
00039                 }
00040         }
00041 }
00042 
00043 void VShader::load(const std::string &strFilename)
00044 {
00045         if (m_uObject)
00046         {
00047                 std::ifstream fin(strFilename.c_str());
00048 
00049                 if(!fin.is_open()) 
00050                 {
00051                         std::cout << "Unable to load " << strFilename.c_str() << std::endl;
00052                 }
00053 
00054                 std::string strLine;
00055                 std::string strCode;
00056 
00057                 // Probably not efficient, but who cares...
00058                 while(std::getline(fin, strLine))
00059                         strCode += strLine + "\n";
00060 
00061                 const char *vpStrings[] = { strCode.c_str() };
00062 
00063                 glShaderSourceARB(m_uObject,1,vpStrings,NULL);
00064                 GLenum glError = glGetError();
00065 
00066                 if (glError != GL_NO_ERROR)
00067                         std::cout << "Error loading shader " << strFilename.c_str() << " " << gluErrorString(glError) << std::endl;
00068 
00069                 glCompileShaderARB(m_uObject);
00070                 glError = glGetError();
00071 
00072                 if (glError != GL_NO_ERROR)
00073                         std::cout << "Error compiling shader " << strFilename.c_str() << " " << gluErrorString(glError) << std::endl;
00074 
00075                 int iReturn = 1;
00076                 glGetObjectParameterivARB(m_uObject,GL_OBJECT_COMPILE_STATUS_ARB,&iReturn);
00077 
00078                 if (!iReturn)
00079                 {
00080                         int iLength = 0;
00081 
00082                         glGetObjectParameterivARB(m_uObject, GL_OBJECT_INFO_LOG_LENGTH_ARB,&iLength);
00083 
00084                         char *pInfo = new char[iLength];
00085                         int iWritten = 0;
00086 
00087                         glGetInfoLogARB(m_uObject, iLength, &iWritten, pInfo);
00088                         std::string strInfo(pInfo);
00089                         delete[] pInfo;
00090 
00091                         std::cout << "Error compiling shader " << strFilename.c_str() << "\n" << strInfo.c_str() << std::endl;
00092                 }
00093         }
00094 }
00095 
00096 VFragmentShader::VFragmentShader(const std::string &strFilename) : VShader(GL_FRAGMENT_SHADER_ARB)
00097 {
00098         load(strFilename);
00099 }
00100 
00101 VVertexShader::VVertexShader(const std::string & strFilename) : VShader(GL_VERTEX_SHADER_ARB)
00102 {
00103         load(strFilename);
00104 }
00105 
00106 std::map<unsigned int, unsigned int> VShader::s_mapAttachment;

Generated on Mon Jan 21 01:15:16 2008 for FlowVis by  doxygen 1.5.4