File: TestTextureSize.cxx

package info (click to toggle)
vtk6 6.3.0%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 118,880 kB
  • sloc: cpp: 1,442,792; ansic: 113,395; python: 72,383; tcl: 46,998; xml: 8,119; yacc: 4,525; java: 4,239; perl: 3,108; lex: 1,694; sh: 1,093; asm: 154; makefile: 103; objc: 17
file content (144 lines) | stat: -rw-r--r-- 4,472 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    TestOpenGLPolyDataMapper.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/

// .NAME Test different sizes with vtkTexture
// .SECTION Description
// This program tests 1D and 2D texture sizes.

#include <vtkCellArray.h>
#include <vtkFloatArray.h>
#include <vtkImageData.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper2D.h>
#include <vtkPNGWriter.h>
#include <vtkSmartPointer.h>
#include <vtkTexture.h>
#include <vtkTexturedActor2D.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>

vtkImageData* createTexture2D(int width, int height, int comp)
{
  void* data = malloc(width*height*comp*sizeof(unsigned char));
  if (!data)
    {
    return 0;
    }
  free(data);
  vtkImageData* image = vtkImageData::New();
  image->SetExtent(0, width - 1,
                   0, height - 1,
                   0, 0);
  image->AllocateScalars(VTK_UNSIGNED_CHAR, comp);

  unsigned char* ptr =
    reinterpret_cast<unsigned char*>(image->GetScalarPointer(0,0,0));
  double value = 0.;
  double valueIncr = 255. / (width*height > 1 ? width*height-1 : 1);
  for (int y = 0; y < height; ++y)
    {
    for (int x = 0; x < width; ++x)
      {
      for (int c = 0; c < comp; ++c)
        {
        *ptr++ = static_cast<unsigned char>(value);
        }
      value += valueIncr;
      }
    }
  return image;
}

int TestTextureSize(int vtkNotUsed(argc), char *vtkNotUsed(argv)[])
{
  //Create a renderer, render window, and interactor
  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer.GetPointer());
  vtkNew<vtkRenderWindowInteractor> interactor;
  interactor->SetRenderWindow(renderWindow.GetPointer());

  vtkNew<vtkPoints> points;
  points->InsertPoint(0, 0., 0., 0.);
  points->InsertPoint(1, 200., 0., 0.);
  points->InsertPoint(2, 200., 200., 0.);
  points->InsertPoint(3, 0., 200., 0.);

  vtkNew<vtkCellArray> cells;
  cells->InsertNextCell(4);
  cells->InsertCellPoint(0);
  cells->InsertCellPoint(1);
  cells->InsertCellPoint(2);
  cells->InsertCellPoint(3);

  vtkNew<vtkFloatArray> tcoords;
  tcoords->SetNumberOfComponents(2);
  tcoords->InsertNextTuple2(0.f, 0.f);
  tcoords->InsertNextTuple2(1.f, 0.f);
  tcoords->InsertNextTuple2(1.f, 1.f);
  tcoords->InsertNextTuple2(0.f, 1.f);

  vtkNew<vtkPolyData> textureCoords;
  textureCoords->SetPoints(points.GetPointer());
  textureCoords->SetPolys(cells.GetPointer());
  textureCoords->GetPointData()->SetTCoords(tcoords.GetPointer());

  vtkNew<vtkPolyDataMapper2D> polyDataMapper;
  polyDataMapper->SetInputData( textureCoords.GetPointer() );

  int textureSizes[23][2] =
    {{1,2}, {1,3}, {1,4}, {1,5}, {1,255}, {1,256}, {257,1},
     {2,1}, {3,1}, {4,1}, {5,1}, {255,1}, {256,1}, {257,1},
     {1,1}, {2,2}, {3,3}, {3,3}, {255,255}, {256,256}, {257,257},
     {2047,2047}, {4097,4097}};
  int componentSizes[3] = {1, 3, 4};
  for (int i = 0; i < 23; ++i)
    {
    for (int c = 0; c < 3; ++c)
      {
      int* size = textureSizes[i];
      vtkSmartPointer<vtkImageData> image =
        vtkSmartPointer<vtkImageData>::Take(
          createTexture2D(size[0], size[1], componentSizes[c]));
      if (image.GetPointer() == 0)
        {
        return EXIT_SUCCESS;
        }
      vtkNew<vtkTexture> texture;
      texture->SetInputData(image);
      // You can play with the parameters
      //texture->SetRepeat(false);
      //texture->SetEdgeClamp(true);
      //texture->SetInterpolate(true);

      vtkNew<vtkTexturedActor2D> textureActor;
      textureActor->SetTexture(texture.GetPointer());
      textureActor->SetMapper(polyDataMapper.GetPointer());
      renderer->AddActor(textureActor.GetPointer());

      texture->SetRestrictPowerOf2ImageSmaller(false);
      renderWindow->Render();

      texture->SetRestrictPowerOf2ImageSmaller(true);
      renderWindow->Render();
      }
    }

  return EXIT_SUCCESS;
}