Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
OutputWowDisplay.cpp
1 #include <GLviewer/OutputAttachments/OutputWowDisplay.hh>
2 
3 using namespace std;
4 using namespace BIAS;
5 
6 OutputWowDisplay::OutputWowDisplay(BIAS::glfRenderTarget *out, int factor, int offset) : GLProjectionOutputAttachment(out) {
7  wowFactor_ = factor;
8  wowOffset_ = offset;
9  InitShader_();
10 }
11 
13 
14 }
15 
18 
19  string vertexCode =
20  "void main()\r\n"
21  "{\r\n"
22  " gl_TexCoord[0] = gl_MultiTexCoord0;\r\n"
23  " gl_Position = ftransform();\r\n"
24  "}\r\n";
25  string fragmentCode =
26  "uniform sampler2D tex;\r\n"
27  "uniform sampler2D dep;\r\n"
28  "uniform float w;\r\n"
29  "uniform float h;\r\n"
30  "uniform int header[256];\r\n"
31  "uniform float wow_disparity_M;\r\n"
32  "uniform float wow_disparity_Zd;\r\n"
33  "uniform float wow_disparity_vz;\r\n"
34  "uniform float wow_disparity_C;\r\n"
35 
36  "float disparity(float Z)\r\n"
37  "{\r\n"
38  " return (wow_disparity_M*(1.0-(wow_disparity_vz/(Z-wow_disparity_Zd+wow_disparity_vz)))\r\n"
39  " + wow_disparity_C) / 255.0;\r\n"
40  "}\r\n"
41  "\r\n"
42  "void main()\r\n"
43  "{\r\n"
44  " vec4 color;\r\n"
45  " vec2 pos = gl_TexCoord[0].st;\r\n"
46  " if (mod(pos[1]*h,2.0) < 0.5)\r\n"
47  " {\r\n"
48  " color = vec4(0.0,0.0,0.0,0.0);\r\n"
49  " }\r\n"
50  " else\r\n"
51  " {\r\n"
52 // " pos[1] = pos[1] * 2.0;\r\n"
53  " if (pos[0] < 0.5)\r\n"
54  " {\r\n"
55  // write header
56  " \r\n"
57 // " if (pos[1]*h>=h-1.0 && (pos[0]*w<256) && (mod(pos[0]*w,2.0)<1.0))\r\n"
58  " if (pos[1]*h>=h-1.0 && (pos[0]*w<256))\r\n"
59  " {\r\n"
60 // " int xpos = int(pos[0]*w)/2;\r\n"
61  " int xpos = int(pos[0]*w);\r\n"
62  " color = vec4(0.0,0.0, header[ xpos ]/255.0,1.0);\r\n"
63  " }\r\n"
64  " else\r\n"
65  // else take original color
66  " {\r\n"
67  " pos[0] = pos[0] * 2.0;\r\n"
68  " color = texture2D(tex,pos);\r\n"
69  " }\r\n"
70  " \r\n"
71  " }\r\n"
72  " else\r\n"
73  " {\r\n"
74  " pos[0] = (pos[0]-0.5) * 2.0;\r\n"
75 // " color = vec4(1.0,1.0,1.0,1.0) - texture2D(dep,pos);\r\n"
76  " float depth = texture2D(dep,pos).z;\r\n"
77  " if (depth == 1.0)\r\n"
78  " color = vec4(0.0);\r\n"
79  " else\r\n"
80  " color = vec4(disparity(depth)/2.0);\r\n"
81  " }\r\n"
82  " }\r\n"
83  " gl_FragColor = color;\r\n"
84  "}\r\n";
85 
86  return GLProjectionOutputAttachment::InitShader(vertexCode, fragmentCode);
87 }
88 
90 Draw_() {
91 
92  unsigned char wow_content=0x02;
93  unsigned char wow_use_factor = 0x80; // 0x00 for default
94  unsigned char wow_use_offset = 0x40; // 0x00 for default
95  unsigned char wow_factor_UChar = wowFactor_;
96  unsigned char wow_Offset_UChar = wowOffset_;
97  unsigned char wow_use_factor_plut_offset = wow_use_factor+wow_use_offset;
98 
99  unsigned char header[]= {0xF1,wow_content,wow_factor_UChar,wow_Offset_UChar,wow_use_factor_plut_offset,0x00,0x00,0x00,0x00,0x00};
100  // Calc the CRC32
101  {
102  unsigned long _register = 0;
103  for(int i = 0; i < 10; ++i) {
104  unsigned char mask = 0x80;
105  unsigned char byte = header[i];
106  for (int j = 0; j < 8; ++j)
107  {
108  bool topBit = (_register & 0x80000000) != 0;
109  _register <<= 1;
110  _register ^= ((byte & mask) != 0? 0x1: 0x0);
111  if (topBit)
112  {
113  _register ^= 0x04c11db7;
114  }
115  mask >>= 1;
116  }
117  }
118  unsigned char *p = (unsigned char*) &_register;
119  for(size_t i = 0; i < 4; ++i)
120  {
121  header[i+6] = p[3-i];
122  }
123  }
124 
125  int fullheader[256];
126  {
127  for (int x=0; x<256; ++x){
128  fullheader[x] = 0;
129  }
130  for (int x=0; x<=9; ++x){
131  for (int y=7; y>=0; --y){
132  int i = 2*(7-y)+16*x;
133  fullheader[i] = (((1<<(y))&(header[x])) << (7-(y)));
134  }
135  }
136  }
137  shaderProg_.SetUniform("header", 256, fullheader);
138 
139  // Philips Wow 42`` Display constants (from whitepaper)
140  shaderProg_.SetUniform("wow_disparity_Zd", 0.467481f);
141  shaderProg_.SetUniform("wow_disparity_vz", 7.655192f);
142  shaderProg_.SetUniform("wow_disparity_M", -1960.37f);
143  shaderProg_.SetUniform("wow_disparity_C", 127.5f);
144 
146 }
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 set the header information for wow display...
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 ...
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.