ManageShaders.cpp 3.43 KB
/*This file manages all of the shader program stuff, including:
	o) Loading shaders
	o) Combining the ray-casting code with the volume shader code
*/

#include "trueeyes.h"
#include "VolumeShaderStruct.h"
#include "rtsFilename.h"

rts_glShaderProgram gVertexShader;

vector<VolumeShader> ShaderList;
int SelectedShader = -1;

void SetRenderSource()
{
	ShaderList[SelectedShader].glProgram.AttachGlobalUniform("camera", SourceList[RenderSource].pos);
}

void AttachShaderVariables()
{
	if(SelectedShader < 0)
		return;

	//ShaderList[id].glProgram.AttachGlobalUniform("camera", SourceList[SelectedSource].pos);
	ShaderList[SelectedShader].glProgram.AttachGlobalUniform("d", gpVolSize);
	ShaderList[SelectedShader].glProgram.AttachGlobalUniform("stepsize", &StepSize);
	ShaderList[SelectedShader].glProgram.AttachGlobalUniform("ray_min", gpCropMin);
	ShaderList[SelectedShader].glProgram.AttachGlobalUniform("ray_max", gpCropMax);

	//attach all available texture maps
	for(int v = 0; v<VolumeList.size(); v++)
		ShaderList[SelectedShader].glProgram.AttachTextureMap(VolumeList[v].Name.c_str(), VolumeList[v].Texture);
	for(int t = 0; t<TextureList.size(); t++)
		ShaderList[SelectedShader].glProgram.AttachTextureMap(TextureList[t].Name.c_str(), TextureList[t].Texture);

	//attach all sources	
	for(int s=1; s<SourceList.size(); s++)
	{
		ShaderList[SelectedShader].glProgram.AttachGlobalUniform(SourceList[s].Name.c_str(), SourceList[s].pos);
	}

	//attach the selected render source to the "camera" variable
	SetRenderSource();

}



void ReloadShaders()
{
	int nShaders = ShaderList.size();
	for(int s = 0; s<nShaders; s++)
	{
		//load the source code for the ray caster
		rtsSourceCode sourceRayCaster("fragRayCast.glsl");

		//add the sampler for each loaded volume
		for(int v=0; v<VolumeList.size(); v++)
		{
			string samplerString = "uniform sampler3D ";
			samplerString += VolumeList[v].Name;
			samplerString += ";";
			sourceRayCaster += samplerString;
		}
		//add the sampler for each texture map
		for(int t=0; t<TextureList.size(); t++)
		{
			string samplerString = "uniform sampler2D ";
			samplerString += TextureList[t].Name;
			samplerString += ";";
			sourceRayCaster += samplerString;
		}

		//add a variable for each source (SourceList[0] is the camera)
		for(int src=1; src<SourceList.size(); src++)
		{
			string sourceString = "uniform vec3 ";
			sourceString += SourceList[src].Name;
			sourceString += ";";
			sourceRayCaster += sourceString;
		}

		//load the user-designed portion of the shader
		rtsSourceCode sourceShader(ShaderList[s].Filename.c_str());

		//assemble the source code
		sourceRayCaster += sourceShader;

		//compile and show logs
		cout<<endl;
		cout<<"Compile and Link Log for \""<<ShaderList[s].Name<<"\""<<endl;
		cout<<"---------------------------------------------------------"<<endl;
		ShaderList[s].glProgram.Clean();
		ShaderList[s].glProgram.AttachShader(GL_FRAGMENT_SHADER, sourceRayCaster);
		ShaderList[s].glProgram.Compile();
		ShaderList[s].glProgram.Link();
		ShaderList[s].glProgram.PrintLog();
	}

	//attach variables to the selected shader
	AttachShaderVariables();
}

void CreateShader(string filename)
{	
	VolumeShader newShader;

	//create a name for the shader
	rtsFilename f(filename);
	newShader.Name = f.getPrefix();

	//save the shader filename
	newShader.Filename = filename;

	//push the shader into the shader list
	ShaderList.push_back(newShader);

	SelectedShader = ShaderList.size() - 1;

	//reload the shaders
	ReloadShaders();

}