Blame view

ManageShaders.cpp 3.43 KB
ee96a02c   David Mayerich   first commit from...
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
123
124
  /*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();
  
  }