4 #include <vtkCubeSource.h> 5 #include <vtkOpenGLPolyDataMapper.h> 7 #include <vtkRenderer.h> 8 #include <vtkProperty.h> 10 #include <vtkOpenGLRenderWindow.h> 11 #include <vtkRenderWindowInteractor.h> 13 #include <vtkTextureObject.h> 14 #include <vtkOpenGLVertexArrayObject.h> 15 #include <vtkOpenGLProperty.h> 16 #include <vtkOpenGLBufferObject.h> 28 void vtkfixture::printActiveVertexAndFragmentShader(vtkSmartPointer<vtkOpenGLPolyDataMapper> mapper)
30 char *vertexShader = mapper->GetVertexShaderCode();
33 std::cout <<
"VERTEX SHADER:" << std::endl;
34 std::cout << vertexShader << std::endl;
37 char *fragmentShader = mapper->GetFragmentShaderCode();
40 std::cout <<
"FRAGMENT SHADER:" << std::endl;
41 std::cout << fragmentShader << std::endl;
45 void vtkfixture::printOpenGLVersion()
47 const GLubyte* version = glGetString(GL_VERSION);
48 std::cout <<
"OpenGL version " << version << std::endl;
52 void vtkfixture::printVtkOpenGLRenderWindowInfo(vtkSmartPointer<vtkOpenGLRenderWindow> opengl_renderwindow)
54 std::cout <<
"context support for open gl core 3.2: " << vtkOpenGLRenderWindow::GetContextSupportsOpenGL32() << std::endl;
55 std::cout <<
"context was created at: " << opengl_renderwindow->GetContextCreationTime() << std::endl;
59 vtkSmartPointer<vtkTextureObject>
vtkfixture::createTextureObject(
unsigned int width,
unsigned int height,
unsigned int depth,
int dataType,
int numComps,
void *data, vtkSmartPointer<vtkOpenGLRenderWindow> opengl_renderwindow)
61 vtkNew<vtkTextureObject> texture_object;
62 texture_object->SetContext(opengl_renderwindow);
64 if(!texture_object->Create3DFromRaw(width, height, depth, numComps, dataType, data))
65 std::cout <<
"---------------------------------------- > Error creating 3D texture" << std::endl;
70 std::cout << texture_object->GetFormat(dataType, numComps,
true) << std::endl;
72 texture_object->Activate();
74 texture_object->SetWrapS(vtkTextureObject::ClampToEdge);
75 texture_object->SetWrapT(vtkTextureObject::ClampToEdge);
76 texture_object->SetWrapR(vtkTextureObject::ClampToEdge);
77 texture_object->SetMagnificationFilter(vtkTextureObject::Linear);
78 texture_object->SetMinificationFilter(vtkTextureObject::Linear);
79 texture_object->SendParameters();
81 std::cout <<
"Texture unit: " << texture_object->GetTextureUnit() << std::endl;
82 texture_object->PrintSelf(std::cout, vtkIndent(4));
86 return texture_object.Get();
91 vtkNew<vtkOpenGLBufferObject> buffer_object;
92 std::cout <<
"ALLOCATING BUFFER" << std::endl;
93 buffer_object->GenerateBuffer(vtkOpenGLBufferObject::ArrayBuffer);
94 if(!buffer_object->Bind())
95 std::cout <<
"buffer object not bind" << std::endl;
98 std::cout <<
"UPLOADING" << std::endl;
99 if(!buffer_object->Upload(
101 numberOfLines*numberOfComponentsLine,
102 vtkOpenGLBufferObject::ArrayBuffer
105 vtkGenericWarningMacro(<<
"Error uploading buffer object data.");
109 return buffer_object.Get();
119 vtkSmartPointer<vtkCubeSource> cube = vtkSmartPointer<vtkCubeSource>::New();
128 vtkSmartPointer<vtkOpenGLPolyDataMapper> mapper = vtkSmartPointer<vtkOpenGLPolyDataMapper>::New();
129 mapper->SetInputConnection(cube->GetOutputPort());
136 mapper->AddShaderReplacement(
138 "//VTK::PositionVC::Dec",
140 "//VTK::PositionVC::Dec\n" 141 "attribute vec3 COLOR_VSIN;\n" 142 "attribute vec3 TEXTURE_COORDINATE_VSIN;\n" 143 "varying vec3 COLOR_VSOUT;\n" 144 "varying vec3 TEXTURE_COORDINATE_VSOUT;\n",
151 mapper->AddShaderReplacement(
153 "//VTK::PositionVC::Impl",
155 "//VTK::PositionVC::Impl\n" 156 "COLOR_VSOUT = COLOR_VSIN;\n" 157 "TEXTURE_COORDINATE_VSOUT = TEXTURE_COORDINATE_VSIN;\n",
166 std::string fragment_shader =
167 "//VTK::System::Dec\n" 168 "//VTK::Output::Dec\n" 169 "in vec3 COLOR_VSOUT;\n" 170 "in vec3 TEXTURE_COORDINATE_VSOUT;\n" 171 "uniform sampler3D my_texture_1;\n" 172 "uniform sampler3D my_texture_2;\n" 175 " vec4 color_1 = texture(my_texture_1, TEXTURE_COORDINATE_VSOUT);\n" 176 " vec4 color_2 = texture(my_texture_2, TEXTURE_COORDINATE_VSOUT);\n" 177 " color = vec4(color_1.xy, color_2.zw);\n" 179 mapper->SetFragmentShaderCode(fragment_shader.c_str());
190 vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
191 actor->SetMapper(mapper);
193 actor->RotateX(30.0);
194 actor->RotateY(-45.0);
204 vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
205 renderer->AddActor(actor);
206 renderer->SetBackground(0.1, 0.2, 0.4);
208 renderer->ResetCamera();
209 renderer->GetActiveCamera()->Zoom(1.5);
218 vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
219 renderWindow->SetSize(200, 200);
220 renderWindow->AddRenderer(renderer);
230 renderWindow->Render();
237 GLenum err = glewInit();
241 fprintf(stderr,
"Error: %s\n", glewGetErrorString(err));
243 fprintf(stdout,
"Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
246 printOpenGLVersion();
254 int my_numberOfColors = numberOfColors;
255 int my_numberOfComponentsPerColor = numberOfComponentsPerColor;
256 const GLfloat *my_color_data = color_data;
257 vtkSmartPointer<vtkOpenGLBufferObject> color_buffer_object =
allocateAndUploadArrayBuffer(my_numberOfColors, my_numberOfComponentsPerColor, my_color_data);
265 int my_numberOfTextureCoordinates = numberOfTextureCoordinates;
266 int my_numberOfComponentsPerTexture = numberOfComponentsPerTexture;
267 const GLfloat *my_texture_data = texture_data;
268 vtkSmartPointer<vtkOpenGLBufferObject> texture_buffer_object =
allocateAndUploadArrayBuffer(my_numberOfTextureCoordinates, my_numberOfComponentsPerTexture, my_texture_data);
275 vtkSmartPointer<vtkOpenGLRenderWindow> opengl_renderwindow = vtkOpenGLRenderWindow::SafeDownCast(renderWindow.Get());
276 if(!opengl_renderwindow)
277 std::cout <<
"not opengl_renderwindow" << std::endl;
280 printVtkOpenGLRenderWindowInfo(opengl_renderwindow);
289 std::cout <<
"ALLOCATING AND UPLOADING TEXTURE OBJECT" << std::endl;
290 unsigned int width = 4;
291 unsigned int height = 4;
292 unsigned int depth = 4;
295 int dataType = VTK_FLOAT;
296 void *data1 = (
void*)color_data;
297 vtkSmartPointer<vtkTextureObject> texture_object_1 =
createTextureObject(width, height, depth, dataType, numComps, data1, opengl_renderwindow);
300 dataType = VTK_UNSIGNED_CHAR;
301 void *data2 = generateTexture<unsigned char>(width, height, depth, 200, 140, 0, 200);
303 vtkSmartPointer<vtkTextureObject> texture_object_2 =
createTextureObject(width, height, depth, dataType, numComps, data2, opengl_renderwindow);
310 vtkSmartPointer<ShaderCallback> callback = vtkSmartPointer<ShaderCallback>::New();
311 callback->mRenderWindow = opengl_renderwindow;
313 callback->mColorBufferObject = color_buffer_object;
314 callback->mTextureBufferObject = texture_buffer_object;
315 callback->mTextureObject1 = texture_object_1;
316 callback->mTextureObject2 = texture_object_2;
318 mapper->AddObserver(vtkCommand::UpdateShaderEvent,callback);
328 vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
329 renderWindowInteractor->SetRenderWindow(renderWindow);
337 renderWindowInteractor->Start();
344 renderer->ReleaseGraphicsResources(renderWindow);
vtkSmartPointer< class vtkTextureObject > createTextureObject(unsigned int width, unsigned int height, unsigned int depth, int dataType, int numComps, void *data, vtkSmartPointer< class vtkOpenGLRenderWindow > opengl_renderwindow)
void createVTKWindowWithCylinderSourceWith3DTexture()
vtkSmartPointer< class vtkOpenGLBufferObject > allocateAndUploadArrayBuffer(int my_numberOfTextureCoordinates, int numberOfComponentsPerTexture, const GLfloat *texture_data)
#define report_gl_error()