Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
GLProjectionOutputAttachment.cpp
1 #include <GLviewer/OutputAttachments/GLProjectionOutputAttachment.hh>
2 
3 using namespace std;
4 using namespace BIAS;
5 
6 GLProjectionOutputAttachment::GLProjectionOutputAttachment(BIAS::glfRenderTarget *out) {
7  target_ = out;
8  ShaderInited_ = false;
9  ClearColor_ = Vector4<float>(0.0f,0.0f,0.0f,0.0f);
10  InitShader_();
11 }
12 
13 GLProjectionOutputAttachment::~GLProjectionOutputAttachment() {
14 
15 }
16 
17 int GLProjectionOutputAttachment::Draw() {
18  if (!ShaderInited_) {
19  InitShader_();
20  }
21 
22  return Draw_();
23 }
24 
25 int GLProjectionOutputAttachment::InitShader(std::string vertexCode, std::string fragmentCode) {
26  Vector4<int> vp;
27  glGetIntegerv(GL_VIEWPORT, vp.GetData());
28 
29  int stencilbits;
30  glGetIntegerv(GL_STENCIL_BITS, &stencilbits);
31  int alphaBits;
32  glGetIntegerv(GL_ALPHA_BITS, &alphaBits);
33 
34  unsigned int imgWidth = vp[2] - vp[0];
35  unsigned int imgHeight = vp[3] - vp[1];
36 
37  specialDisplayTex_.Create();
38  specialDisplayTex_.Set(GL_LINEAR, GL_LINEAR, GL_CLAMP, GL_CLAMP, GL_TEXTURE0);
39  specialDisplayTex_.SetTextureNr(GL_TEXTURE0);
40  if (alphaBits > 0)
41  specialDisplayTex_.Allocate(imgWidth, imgHeight, GL_RGBA8);
42  else
43  specialDisplayTex_.Allocate(imgWidth, imgHeight, GL_RGB8);
44  specialDisplayDepth_.Create();
45  specialDisplayDepth_.Set(GL_NEAREST, GL_NEAREST, GL_CLAMP, GL_CLAMP, GL_TEXTURE1);
46  specialDisplayDepth_.SetTextureNr(GL_TEXTURE1);
47  if (stencilbits == 0)
48  specialDisplayDepth_.Allocate(imgWidth, imgHeight, GL_DEPTH_COMPONENT);
49  else
50  specialDisplayDepth_.Allocate(imgWidth, imgHeight, GL_DEPTH24_STENCIL8_EXT);
51 
52  specialDisplayRenderingFBO_.Create();
53  specialDisplayRenderingFBO_.AttachTexture(specialDisplayTex_, GL_COLOR_ATTACHMENT0_EXT);
54  specialDisplayRenderingFBO_.AttachTexture(specialDisplayDepth_, GL_DEPTH_ATTACHMENT_EXT);
55  if (stencilbits != 0)
56  specialDisplayRenderingFBO_.AttachTexture(specialDisplayDepth_, GL_STENCIL_ATTACHMENT_EXT);
57  specialDisplayRenderingFBO_.CheckComplete();
58 
59  shaderF_.Create(GL_FRAGMENT_SHADER, fragmentCode.c_str());
60  shaderV_.Create(GL_VERTEX_SHADER, vertexCode.c_str());
61 
62  shaderProg_.Create();
63  shaderProg_.AttachShader(shaderF_);
64  shaderProg_.AttachShader(shaderV_);
65  shaderProg_.Link();
66  ShaderInited_ = true;
67  return 0;
68 }
69 
70 void GLProjectionOutputAttachment::SetClearColor(const BIAS::Vector4<float>& cc)
71 {
72  ClearColor_ = cc;
73 }
74 
75 int GLProjectionOutputAttachment::StandardDraw_() {
76  Vector4<int> vp;
77  glGetIntegerv(GL_VIEWPORT, vp.GetData());
78 
79  unsigned int imgWidth = vp[2] - vp[0];
80  unsigned int imgHeight = vp[3] - vp[1];
81  // cout << imgWidth << " " << imgHeight << endl;
82  // bind render target
83  target_->ClearColorBuffer(ClearColor_[0],ClearColor_[1],ClearColor_[2],ClearColor_[3]);
84  target_->ClearDepthBuffer();
85  target_->Bind();
86  shaderProg_.Bind();
87  shaderProg_.SetUniform("tex", specialDisplayTex_.GetTextureNrInUniformFormat());
88  shaderProg_.SetUniform("dep", specialDisplayDepth_.GetTextureNrInUniformFormat());
89  shaderProg_.SetUniform("w", (float)imgWidth);
90  shaderProg_.SetUniform("h", (float)imgHeight);
91 
92  glMatrixMode(GL_PROJECTION);
93  glPushMatrix();
94  glLoadIdentity();
95  gluOrtho2D(0, imgWidth, 0, imgHeight);
96  glMatrixMode(GL_MODELVIEW);
97  glPushMatrix();
98  glLoadIdentity();
99 
100  // quad to render texture on
101  glPushAttrib(GL_ENABLE_BIT);
102  glEnable(GL_TEXTURE_2D);
103  specialDisplayTex_.BindTU();
104  specialDisplayDepth_.BindTU();
105  glBegin(GL_QUADS);
106  glColor3f(1.0, 0.0, 0.0);
107  glTexCoord2f(0.0, 0.0);
108  glVertex2f(0.0, 0.0);
109 
110  glColor3f(0.0, 1.0, 0.0);
111  glTexCoord2f(0.0, 1.0);
112  glVertex2f(0.0, imgHeight);
113 
114  glColor3f(0.0, 0.0, 1.0);
115  glTexCoord2f(1.0, 1.0);
116  glVertex2f(imgWidth, imgHeight);
117 
118  glColor3f(1.0, 1.0, 1.0);
119  glTexCoord2f(1.0, 0.0);
120  glVertex2f(imgWidth, 0.0);
121  glEnd();
122  glFlush();
123  glPopAttrib();
124  glMatrixMode(GL_MODELVIEW);
125  glPopMatrix();
126  glMatrixMode(GL_PROJECTION);
127  glPopMatrix();
128  glUseProgram(0);
129 
130  GLF_THROW_ON_OPENGL_ERROR
131 
132  return 0;
133 }
134 
void Set(const T &scalar)
set all elements to a scalat value
Definition: Vector4.hh:421
const T * GetData() const
get the data pointer the member function itself is const (before {..}) because it doesn&#39;t change the...
Definition: Vector4.hh:177
Interface for render targets.