SparrowRenderer/src/shadersource.cpp
2016-04-25 17:36:26 +02:00

64 lines
1.6 KiB
C++

#include "shadersource.h"
#include <string>
#include <vector>
#include <sstream>
#include "shader.h"
#include "mesh.h"
#include "light.h"
ShaderSource::ShaderSource()
{
for(int i=0; i<NB_TYPES; ++i)
sources[i] = NULL;
}
ShaderSource::~ShaderSource()
{
for(int i=0; i<NB_TYPES; ++i)
if(sources[i] != NULL)
delete(sources[i]);
}
void ShaderSource::setSource(const char *source, SourceType type)
{
if(sources[type] != NULL)
delete(sources[type]);
if(source == NULL)
sources[type] = NULL;
else
sources[type] = new std::string(source);
}
Shader* ShaderSource::compile(unsigned int geomFlags, unsigned int lightFlags)
{
std::string header = "#version 330 core";
for(int i=0; i<Mesh::NB_FLAGS; ++i)
{
if((geomFlags >> i) & 0x00000001)
header += "\n#define "+std::string(Mesh::flagStr[i]);
}
for(int i=0; i<Light::NB_FLAGS; ++i)
{
if((lightFlags >> i) & 0x00000001)
header += "\n#define "+std::string(Light::flagStr[i]);
}
header += "\n#line 1\n";
if(sources[VERTEX] == NULL || sources[FRAGMENT] == NULL)
return NULL;
std::string compiledSources[NB_TYPES];
for(int i=0; i<NB_TYPES; ++i)
{
if(sources[i] != NULL)
compiledSources[i] = header + *(sources[i]);
}
if(sources[GEOMETRY] != NULL)
return new Shader(compiledSources[VERTEX], compiledSources[GEOMETRY], compiledSources[FRAGMENT]);
else
return new Shader(compiledSources[VERTEX], compiledSources[FRAGMENT]);
}