Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
OutputUnderwater.cpp
1 #include <GLviewer/OutputAttachments/OutputUnderwater.hh>
2 
3 using namespace std;
4 using namespace BIAS;
5 
6 OutputUnderwater::OutputUnderwater(BIAS::glfRenderTarget *out)
8 {
9  InitShader_();
10 }
11 
13 }
14 
17  string vertexCode =
18  "void main()\r\n"
19  "{\r\n"
20  " gl_TexCoord[0] = gl_MultiTexCoord0;\r\n"
21  " gl_Position = ftransform();\r\n"
22  "}\r\n";
23  string fragmentCode =
24  "uniform sampler2D tex;\r\n"
25  "uniform sampler2D dep;\r\n"
26  "uniform float w;\r\n"
27  "uniform float h;\r\n"
28  "uniform float qualityR;\r\n"
29  "uniform float qualityG;\r\n"
30  "uniform float qualityB;\r\n"
31  "uniform float zNear;\r\n"
32  "uniform float zFar;\r\n"
33  "uniform float colorR;\r\n"
34  "uniform float colorG;\r\n"
35  "uniform float colorB;\r\n"
36  "uniform float alphaR;\r\n"
37  "uniform float alphaG;\r\n"
38  "uniform float alphaB;\r\n"
39  "void main()\r\n"
40  "{\r\n"
41  " vec4 color;\r\n"
42  " vec2 pos = gl_TexCoord[0].st;\r\n"
43  " color = texture2D(tex,pos);\r\n"
44  " float depthZ = texture2D(dep,pos).z;\r\n"
45  " float depth = zNear * zFar / (zFar - depthZ * (zFar - zNear));\r\n"
46  " vec4 B = vec4(colorR,colorG,colorB,1.0);\r\n"
47  " vec4 A = vec4(alphaR,alphaG,alphaB,1.0);\r\n"
48 // " vec4 B = vec4(0.2,0.25,0.35,1.0);\r\n"
49  " vec4 mu = vec4(qualityR, qualityR, qualityB, 1.0);\r\n"
50  " vec4 t = vec4(exp(-mu[0]*depth),exp(-mu[1]*depth),exp(-mu[2]*depth),1.0);\r\n"
51  " vec4 t2 = vec4(exp(-2.0*mu[0]*depth),exp(-2.0*mu[1]*depth),exp(-2.0*mu[2]*depth),1.0);\r\n"
52  " color = A*(color*t2 + B*(vec4(1.0) - t));\r\n"
53  " color[3] = 1.0;\r\n"
54  " if(depthZ >= 1.0) gl_FragColor = B;\r\n"
55  " else gl_FragColor = color;\r\n"
56  "}\r\n";
57 
58  return GLProjectionOutputAttachment::InitShader(vertexCode, fragmentCode);
59 }
60 
62 UpdateParams(double zNear,double zFar,std::vector<int> &waterParams) {
63 
64  waterQualityR_ = (float)waterParams[0]/100.0f;
65  waterQualityG_ = (float)waterParams[1]/100.0f;
66  waterQualityB_ = (float)waterParams[2]/100.0f;
67  zNear_ = (float)zNear;
68  zFar_ = (float)zFar;
69  waterColorR_ = (float)waterParams[3]/100.0f;
70  waterColorG_ = (float)waterParams[4]/100.0f;
71  waterColorB_ = (float)waterParams[5]/100.0f;
72 
73  alphaR_ = (float)waterParams[6]/100.0f;
74  alphaG_ = (float)waterParams[7]/100.0f;
75  alphaB_ = (float)waterParams[8]/100.0f;
76 
77  //cout << waterQuality_ << endl;
78 }
79 
81 Draw_() {
82  cout<<"OutputUnderwater!"<<endl;
86  shaderProg_.SetUniform("zNear", zNear_);
87  shaderProg_.SetUniform("zFar", zFar_);
91  shaderProg_.SetUniform("alphaR", alphaR_);
92  shaderProg_.SetUniform("alphaG", alphaG_);
93  shaderProg_.SetUniform("alphaB", alphaB_);
94 
96 }
void SetUniform(const std::string &varName, float value)
int InitShader_()
creates strings with shader code and calls InitShader(vertex, fragment) from superclass ...
int Draw_()
standard draw from superclass does not suffice to get zNear and zFar, so we need to implement this he...
int InitShader(std::string vertexCode, std::string fragmentCode)
can be called to create output attachment without having to derive new subclass will also create FBO ...
void UpdateParams(double zNear, double zFar, std::vector< int > &waterQuality)
int StandardDraw_()
this function suffices for simple shaders that only need the standard uniforms to be set...
Class for extra rendering pass in GLProjection (for now only used in glutGLviewer) ...
Interface for render targets.