Index: trunk/src/lib/graphics/shader.cc
===================================================================
--- trunk/src/lib/graphics/shader.cc	(revision 9406)
+++ trunk/src/lib/graphics/shader.cc	(revision 9869)
@@ -18,5 +18,4 @@
 #include "shader.h"
 
-#include "stdlibincl.h"
 #include "compiler.h"
 //#include <stdio.h>
@@ -25,5 +24,4 @@
 #include "debug.h"
 
-#include "util/loading/resource_manager.h"
 
 
@@ -33,5 +31,50 @@
 
 
+ObjectListDefinition(Shader);
 
+void Shader::Uniform::setV(unsigned int count, float* vv) const
+{
+  switch (count)
+  {
+    case 1:
+    glUniform1fv(this->uniform, 1, vv);
+    break;
+    case 2:
+    glUniform2fv(this->uniform, 2, vv);
+    break;
+    case 3:
+    glUniform3fv(this->uniform, 3, vv);
+    break;
+    case 4:
+    glUniform4fv(this->uniform, 4, vv);
+    break;
+  }
+}
+void Shader::Uniform::setV(unsigned int count, int* vv) const
+{
+  switch (count)
+  {
+    case 1:
+    glUniform1iv(this->uniform, 1, vv);
+    break;
+    case 2:
+    glUniform2iv(this->uniform, 2, vv);
+    break;
+    case 3:
+    glUniform3iv(this->uniform, 3, vv);
+    break;
+    case 4:
+    glUniform4iv(this->uniform, 4, vv);
+    break;
+  }
+}
+
+
+
+Shader::Shader()
+  : data(new ShaderData)
+{
+
+}
 
 /**
@@ -39,240 +82,27 @@
 */
 Shader::Shader (const std::string& vertexShaderFile, const std::string& fragmentShaderFile)
+  : data(new ShaderData)
 {
-   this->setClassID(CL_SHADER, "Shader");
+  if (!Shader::checkShaderAbility())
+    PRINTF(2)("Your system does not support shaders\n");
+  this->load(vertexShaderFile, fragmentShaderFile);
 
-   this->shaderProgram = 0;
-   this->vertexShader = 0;
-   this->fragmentShader = 0;
-
-   if (GLEW_ARB_shader_objects && GLEW_ARB_shading_language_100)
-     {
-       this->shaderProgram = glCreateProgramObjectARB();
-
-       if (!vertexShaderFile.empty())
-         this->loadShaderProgramm(Shader::Vertex, vertexShaderFile);
-       if (!fragmentShaderFile.empty())
-         this->loadShaderProgramm(Shader::Fragment, fragmentShaderFile);
-
-       this->linkShaderProgram();
-
-    }
-   else
-     {
-       PRINTF(2)("Shaders are not supported on your hardware\n");
-     }
 }
 
+const Shader* Shader::storedShader = NULL;
 
-/**
- * standard deconstructor
- */
-Shader::~Shader ()
+void Shader::activateShader() const
 {
-  if (this->shaderProgram == glGetHandleARB(GL_PROGRAM_OBJECT_ARB))
-    Shader::deactivateShader();
-
-  // delete what has to be deleted here
-  this->deleteProgram(Shader::Vertex);
-  this->deleteProgram(Shader::Fragment);
-
-  if (this->fragmentShader != 0)
+  if (likely (this->getProgram() != 0))
   {
-    glDetachObjectARB(this->shaderProgram, this->fragmentShader);
-    glDeleteObjectARB(this->fragmentShader);
-  }
-  if (this->vertexShader != 0)
-  {
-    glDetachObjectARB(this->shaderProgram, this->vertexShader);
-    glDeleteObjectARB(this->vertexShader);
-  }
-  if (this->shaderProgram != 0)
-  {
-    GLint status = 0;
-    //glLinkProgramARB(this->shaderProgram);
-    glDeleteObjectARB(this->shaderProgram);
-       // link error checking
-    glGetObjectParameterivARB(this->shaderProgram, GL_OBJECT_DELETE_STATUS_ARB, &status);
-    if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
-      this->printError(this->shaderProgram);
-  }
-}
-
-Shader* Shader::getShader(const std::string& vertexShaderFile, const std::string& fragmentShaderFile)
-{
-  return (Shader*)ResourceManager::getInstance()->load(vertexShaderFile, SHADER,  RP_LEVEL, fragmentShaderFile);
-}
-
-bool Shader::unload(Shader* shader)
-{
-  return ResourceManager::getInstance()->unload(shader);
-}
-
-Shader* Shader::storedShader = NULL;
-
-
-bool Shader::loadShaderProgramm(Shader::Type type, const std::string& fileName)
-{
-  GLhandleARB shader = 0;
-
-  if (type != Shader::Vertex && type != Shader::Fragment)
-    return false;
-  this->deleteProgram(type);
-
-
-  std::string program;
-  if (!readShader(fileName, program))
-    return false;
-
-  if (type == Shader::Vertex && GLEW_ARB_vertex_shader)
-  {
-    this->vertexShaderFile = fileName;
-
-    shader = this->vertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
-  }
-
-  if (type == Shader::Fragment && GLEW_ARB_fragment_shader)
-  {
-    this->fragmentShaderFile = fileName;
-
-    shader = this->fragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
-  }
-
-  if (shader != 0)
-  {
-    GLint status = 0;
-    const char* prog = program.c_str();
-
-    glShaderSourceARB(shader, 1, &prog, NULL);
-    glCompileShaderARB(shader);
-    // checking on error.
-    glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status);
-    if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
-      this->printError(shader);
-    else
-      glAttachObjectARB(this->shaderProgram, shader);
-  }
-  return true;
-}
-
-
-void Shader::linkShaderProgram()
-{
-  GLint status = 0;
-
-  glLinkProgramARB(this->shaderProgram);
-       // link error checking
-  glGetObjectParameterivARB(this->shaderProgram, GL_OBJECT_LINK_STATUS_ARB, &status);
-  if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
-    this->printError(this->shaderProgram);
-}
-
-
-bool Shader::readShader(const std::string& fileName, std::string& output)
-{
-  char lineBuffer[PARSELINELENGTH];
-
-  std::ifstream shader;
-  shader.open(fileName.c_str());
-  if (!shader.is_open())
-    return false;
-
-
-  while (!shader.eof())
-  {
-    shader.getline(lineBuffer, PARSELINELENGTH);
-    output += lineBuffer;
-    output += "\n";
-  }
-
-
-  shader.close();
-  return true;
-}
-
-
-
-void Shader::activateShader()
-{
-  if (likely (this->shaderProgram != 0))
-  {
-    glUseProgramObjectARB(this->shaderProgram);
+    glUseProgramObjectARB(this->getProgram());
     Shader::storedShader = this;
   }
 }
-
-void Shader::bindShader(const char* name, const float* value, size_t size)
-{
-	if (likely (this->shaderProgram != 0)) {
-		glUseProgramObjectARB(this->shaderProgram);
-
-		unsigned int location = glGetUniformLocationARB(this->shaderProgram, name);
-		/* This is EXPENSIVE and should be avoided. */
-
-		if      (size == 1)  glUniform1fvARB(location, 1, value);
-		else if (size == 2)  glUniform2fvARB(location, 1, value);
-		else if (size == 3)  glUniform3fvARB(location, 1, value);
-		else if (size == 4)  glUniform4fvARB(location, 1, value);
-		else if (size == 9)  glUniformMatrix3fvARB(location, 1, false, value);
-		else if (size == 16) glUniformMatrix4fvARB(location, 1, false, value);
-
-	}
-}
-
 void Shader::deactivateShader()
 {
- if (storedShader != NULL)
-   glUseProgramObjectARB(0);
- Shader::storedShader = NULL;
-}
-
-
-void Shader::deleteProgram(Shader::Type type)
-{
-  GLint status = 0;
-  if (type == Shader::Vertex && this->vertexShader != 0)
-  {
-    this->vertexShaderFile = "";
-    glDetachObjectARB(this->shaderProgram, this->vertexShader);
-    glDeleteObjectARB(this->vertexShader);
-    glGetObjectParameterivARB(this->vertexShader, GL_OBJECT_DELETE_STATUS_ARB, &status);
-    if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
-      Shader::printError(this->vertexShader);
-    this->vertexShader = 0;
-  }
-  else if (type == Shader::Fragment && this->fragmentShader != 0)
-  {
-    this->fragmentShaderFile = "";
-    glDetachObjectARB(this->shaderProgram, this->fragmentShader);
-    glDeleteObjectARB(this->fragmentShader);
-    glGetObjectParameterivARB(this->fragmentShader, GL_OBJECT_DELETE_STATUS_ARB, &status);
-    if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
-      Shader::printError(this->fragmentShader);
-    this->fragmentShader = 0;
-  }
-  else
-    return;
-}
-
-
-void Shader::printError(GLhandleARB program)
-{
-  if (program == 0)
-    return;
-
-  int infologLength = 0;
-  int charsWritten  = 0;
-  char *infoLog;
-
-  glGetObjectParameterivARB(program, GL_OBJECT_INFO_LOG_LENGTH_ARB,
-                            &infologLength);
-
-  if (infologLength > 0)
-  {
-    infoLog = new char[infologLength+1];
-    glGetInfoLogARB(program, infologLength, &charsWritten, infoLog);
-    printf("%s\n", infoLog);
-    delete[] infoLog;
-  }
+  if (storedShader != NULL)
+    glUseProgramObjectARB(0);
+  Shader::storedShader = NULL;
 }
 
@@ -288,21 +118,3 @@
 }
 
-void Shader::debug() const
-{
-  PRINT(3)("Shader info: (SHADER: %d)\n", this->shaderProgram);
-  if (this->vertexShader != 0)
-  {
-/*    PRINT(3)("VertexShaderProgramm: number=%d, file='%s'\n", this->vertexShader, this->vertexShaderFile);
-    if (this->vertexShaderSource != NULL)
-      for (unsigned int i = 0; i < this->vertexShaderSource->getCount(); i++)
-        PRINT(3)("%d: %s\n", i, this->vertexShaderSource->getEntry(i));
-  }
-  if (this->fragmentShader != 0)
-  {
-    PRINT(3)("FragmentShaderProgramm: number=%d, file='%s'\n", this->fragmentShader, this->fragmentShaderFile);
-    if (this->fragmentShaderSource != NULL)
-      for (unsigned int i = 0; i < this->fragmentShaderSource->getCount(); i++)
-        PRINT(3)("%d: %s\n", i, this->fragmentShaderSource->getEntry(i));*/
-  }
-}
 
