CustusX  22.04-rc5
An IGT application
shadercallback.cpp
Go to the documentation of this file.
1 #include "shadercallback.h"
2 
3 #include <vtkOpenGLHelper.h>
4 #include <vtkShaderProgram.h>
5 #include <vtkOpenGLBufferObject.h>
6 #include <vtkNew.h>
7 #include <vtkOpenGLVertexArrayObject.h>
8 #include <vtkOpenGLIndexBufferObject.h>
9 #include <vtkShader.h>
10 #include <vtkOpenGLRenderWindow.h>
11 #include <vtkTextureObject.h>
12 //#include <vtkCubeSource.h>
13 
14 #include "vtkfixture.h"
15 
16 #include "cxGLHelpers.h"
17 
18 
20 { return new ShaderCallback; }
21 
22 /*
23 void ShaderCallback::test2(unsigned long event, void *cbo)
24 {
25  std::cout << "START TEST 2" << std::endl;
26 
27  if(event == vtkCommand::UpdateShaderEvent)
28  {
29  std::cout << "--- START UpdateShaderEvent" << std::endl;
30  vtkOpenGLHelper *cellBO = reinterpret_cast<vtkOpenGLHelper*>(cbo);
31  report_gl_error();
32  std::cout << "--- 1" << std::endl;
33 
34  if(cellBO && cellBO->VAO)
35  {
36  std::cout << "IBO count " << cellBO->IBO->IndexCount << std::endl;
37 
38  cellBO->VAO->Bind();
39 
40  GLint color_frag_out_index = glGetFragDataLocation(cellBO->Program->GetHandle(), "color");
41  std::cout << "color_frag_out_index " << color_frag_out_index << std::endl;
42  glBindFragDataLocation(cellBO->Program->GetHandle(), color_frag_out_index, "color");
43 
44  std::cout << "--- 2" << std::endl;
45 
46 
47  // An array of 3 vectors which represents 3 vertices
48  // One color for each vertex. They were generated randomly.
49  std::cout << "--- 3" << std::endl;
50 
51  //GLuint VertexArrayID;
52  //glGenVertexArrays(1, &VertexArrayID);
53  //glBindVertexArray(cellBO->IBO->GetHandle());
54 
55  cellBO->IBO->Bind();
56 
57  // This will identify our color buffer
58  GLuint colorbuffer;
59 
60  // Generate 1 buffer, put the resulting identifier in colorbuffer
61  glGenBuffers(1, &colorbuffer);
62  std::cout << "--- 4" << std::endl;
63 
64  // The following commands will talk about our 'colorbuffer' buffer
65  glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
66  std::cout << "--- 5" << std::endl;
67 
68  // Give our color to OpenGL.
69  glBufferData(GL_ARRAY_BUFFER, sizeof(color_data), color_data, GL_STATIC_DRAW);
70  report_gl_error();
71  std::cout << "--- 6" << std::endl;
72 
73  GLint index_color = glGetAttribLocation(cellBO->Program->GetHandle(), "COLOR_VSIN");
74  std::cout << "index_color " << index_color << std::endl;
75  report_gl_error();
76 
77 
78  // 2nd attribute buffer : color
79  glEnableVertexAttribArray(index_color);
80  glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
81  glVertexAttribPointer(
82  index_color, // attribute 0. No particular reason for 0, but must match the layout in the shader.
83  3, // size
84  GL_FLOAT, // type
85  GL_FALSE, // normalized?
86  0, // stride
87  (void*)0 // array buffer offset
88  );
89 
90  // Cleanup VBO and shader
91  //glDeleteBuffers(1, &colorbuffer);
92  //glDeleteProgram(programID);
93  //glDeleteVertexArrays(1, &VertexArrayID);
94  //cellBO->IBO->Release();
95 
96 
97  glDisableVertexAttribArray(index_color);
98  report_gl_error();
99 
100 
101  }
102  std::cout << "--- END UpdateShaderEvent" << std::endl;
103  }
104  std::cout << " END TEST 2" << std::endl;
105 }
106 */
107 
108 void ShaderCallback::test( unsigned long event, void *cbo)
109 {
110  std::cout << " START TEST" << std::endl;
111  if(event == vtkCommand::UpdateShaderEvent)
112  {
113  std::cout << "--- START UpdateShaderEvent" << std::endl;
114  vtkOpenGLHelper *cellBO = reinterpret_cast<vtkOpenGLHelper*>(cbo);
115  report_gl_error();
116 
117  if(cellBO && cellBO->VAO)
118  {
119  report_gl_error();
120 
121  vtkShaderProgram *program = cellBO->Program;
122  vtkOpenGLVertexArrayObject *vao = cellBO->VAO;
123 
124  std::cout << "Program is compiled? " << program->GetCompiled() << std::endl;
125  std::cout << "Program is bound? " << program->isBound() << std::endl;
126  std::cout << "IBO index count " << cellBO->IBO->IndexCount << std::endl;
127  report_gl_error();
128 
129 
130  GLint color_frag_out_index = glGetFragDataLocation(cellBO->Program->GetHandle(), "color");
131  std::cout << "color index " << color_frag_out_index << std::endl;
132  glBindFragDataLocation(cellBO->Program->GetHandle(), color_frag_out_index, "color"); //setting output of fragment shader
133 
134 
135  std::cout << "ADDING ATTRIBUTE ARRAY" << std::endl;
136  /*
137  vao->Bind();
138  if (!vao->AddAttributeArray(program, vbo.Get(), "vertexMC", 0, sizeof(float)*3, VTK_FLOAT, 3, false))
139  {
140  vtkGenericWarningMacro(<< "Error setting 'vertexMC' in shader VAO.");
141  }
142  */
143  vao->Bind();
144  //Input color
145  int vec_size = 3;
146  if (!vao->AddAttributeArray(
147  program, //vtkShaderProgram
148  mColorBufferObject.Get(), //vtkOpenGLBufferObject
149  "COLOR_VSIN", //std::string
150  0, //int (offset) where to start reading g_color_buffer_data, offset != 0 == discard some of the first values
151  sizeof(float)*3, //size_t (stride) If stride is 0, the generic vertex attributes are understood to be tightly packed in the array.
152  VTK_FLOAT, //int (elementType) Specifies the data type of each component in the array
153  vec_size, //int (elementTupleSize) Specifies the number of components per generic vertex attribute. Must be 1, 2, 3, 4.
154  false //bool (normalize)
155  ))
156  {
157  vtkGenericWarningMacro(<< "Error setting 'COLOR' in shader VAO.");
158  }
159 
160  //Input texture coordinates
161  vec_size = 3;
162  if (!vao->AddAttributeArray(
163  program, //vtkShaderProgram
164  mTextureBufferObject.Get(), //vtkOpenGLBufferObject
165  "TEXTURE_COORDINATE_VSIN", //std::string
166  0, //int (offset) where to start reading g_color_buffer_data, offset != 0 == discard some of the first values
167  sizeof(float)*3, //size_t (stride) If stride is 0, the generic vertex attributes are understood to be tightly packed in the array.
168  VTK_FLOAT, //int (elementType) Specifies the data type of each component in the array
169  vec_size, //int (elementTupleSize) Specifies the number of components per generic vertex attribute. Must be 1, 2, 3, 4.
170  false //bool (normalize)
171  ))
172  {
173  vtkGenericWarningMacro(<< "Error setting 'COLOR' in shader VAO.");
174  }
175 
176  //Uniform texture sampler id
177  if(!program->SetUniformi("my_texture_1", mTextureObject1->GetTextureUnit())) //not the handle
178  std::cout << "my_texture_1 -------------------------------------> ERROR!!!" << std::endl;
179 
180  //Uniform texture sampler id
181  if(!program->SetUniformi("my_texture_2", mTextureObject2->GetTextureUnit())) //not the handle
182  std::cout << "my_texture_2 -------------------------------------> ERROR!!!" << std::endl;
183 
184  report_gl_error();
185  std::cout << "--- END UpdateShaderEvent" << std::endl;
186 
187  }
188  report_gl_error();
189  }
190  std::cout << "END TEST" << std::endl;
191 }
192 
193 void ShaderCallback::Execute(vtkObject *, unsigned long event, void *cbo)
194 {
195  if(!mRenderWindow)
196  std::cout << "ERROR NO CONTEXT!!!" << std::endl;
197  mRenderWindow->MakeCurrent();
198 
199  //test2(event, cbo)
200  test(event, cbo); //WORKS!
201 
202 }
203 
205 {
206 }
207 
vtkSmartPointer< class vtkOpenGLRenderWindow > mRenderWindow
virtual void Execute(vtkObject *, unsigned long event, void *cbo)
vtkSmartPointer< class vtkOpenGLBufferObject > mTextureBufferObject
static ShaderCallback * New()
void test(unsigned long event, void *cbo)
vtkSmartPointer< class vtkTextureObject > mTextureObject1
vtkSmartPointer< class vtkTextureObject > mTextureObject2
vtkSmartPointer< class vtkOpenGLBufferObject > mColorBufferObject
#define report_gl_error()
Definition: cxGLHelpers.h:28