Skip to content

Commit

Permalink
Added ability to set opacity for PDF Special case
Browse files Browse the repository at this point in the history
  • Loading branch information
aashish24 authored and danlipsa committed Dec 20, 2016
1 parent 0ed16ca commit 0a5cbee
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 26 deletions.
25 changes: 18 additions & 7 deletions Rendering/GL2PS/Testing/Cxx/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
# Compile the PNGCompare test, which is used to validate PNG images (read from
# disk, not generated in VTK). The CMake/RasterizePostScript script can be used
# to create a png image from GL2PS output.
set(tests PNGCompare)
vtk_test_cxx_executable(${vtk-module}CxxTests tests
RENDERING_FACTORY
)
## Compile the PNGCompare test, which is used to validate PNG images (read from
## disk, not generated in VTK). The CMake/RasterizePostScript script can be used
## to create a png image from GL2PS output.
#set(tests PNGCompare TestGL2PSAddPolyPrimitive)
#vtk_test_cxx_executable(${vtk-module}CxxTests tests
# RENDERING_FACTORY
# )




set(GL2PSTests
TestGL2PSAddPolyPrimitive.cxx
)

vtk_add_test_cxx(${vtk-module}CxxTests tests NO_VALID ${GL2PSTests})

vtk_test_cxx_executable(${vtk-module}CxxTests tests RENDERING_FACTORY)
193 changes: 193 additions & 0 deletions Rendering/GL2PS/Testing/Cxx/TestGL2PSAddPolyPrimitive.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/*=========================================================================
Program: Visualization Toolkit
Module: TestGL2PSAddPolyPrimitive.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.
=========================================================================*/

// This test checks that we can use GL2PS without an OpenGL context using
// a buffer size of 0, gl2psAddPolyPrimitive, and gl2psForceRasterPos.

#include "vtk_gl2ps.h"

#include "vtkTestingInteractor.h"

#include <iostream>
#include <string>
#include <vector>

namespace {

static inline void setVertex(GL2PSvertex &vert,
float x, float y, float z,
float r, float g, float b, float a)
{
vert.xyz[0] = x;
vert.xyz[1] = y;
vert.xyz[2] = z;
vert.rgba[0] = r;
vert.rgba[1] = g;
vert.rgba[2] = b;
vert.rgba[3] = a;
}

static inline void generatePixelData(std::vector<float> &data,
size_t width, size_t height)
{
data.resize(width * height * 4);
for (size_t h = 0; h < height; ++h)
{
size_t rowOffset = h * width * 4;
for (size_t w = 0; w < width; ++w)
{
size_t pixel = rowOffset + (w * 4);
data[pixel ] = h / static_cast<float>(height);
data[pixel + 1] = 0.f;
data[pixel + 2] = w / static_cast<float>(width);
data[pixel + 3] = 1.f;
}
}
}

} // end anon namespace

#include <vtkOpenGLGL2PSExporter.h>

#include <vtkTextActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkTextActor.h>
#include <vtkTextProperty.h>

#include <vtkPropCollection.h>

int TestGL2PSAddPolyPrimitive(int , char * [])
{
vtkNew<vtkOpenGLGL2PSExporter> exporter;

vtkNew<vtkTextActor> ta;
ta->SetPosition(0, 0);
ta->SetInput("Hello");
ta->GetTextProperty()->SetColor(0.0, 1.0, 0.0);
ta->GetTextProperty()->SetOpacity(0.5);
exporter->SetCompress(0);
exporter->DrawBackgroundOff();

vtkNew<vtkRenderer> ren;
vtkNew<vtkRenderWindow> renWin;

ren->AddActor(ta.GetPointer());
renWin->AddRenderer(ren.GetPointer());


vtkPropCollection* ac = ren->GetViewProps();
ac->InitTraversal();
vtkProp* act = ac->GetNextProp();
vtkIndent ind;
while(act) {
act->PrintSelf(std::cout, ind);
act = ac->GetNextProp();
}


exporter->SetTextAsPath(1);
exporter->SetInput(renWin.GetPointer());
exporter->SetFileFormatToPDF();
exporter->SetFilePrefix("bad");
exporter->Write();





// std::string filename = vtkTestingInteractor::TempDirectory +
// std::string("/TestGL2PSAddPolyPrimitive.pdf");
// FILE *stream = fopen(filename.c_str(), "wb");
// if (stream == NULL)
// {
// std::cerr << "Error opening output file." << std::endl;
// return EXIT_FAILURE;
// }

// GLint viewport[4] = { 0, 0, 400, 400 };
// GLint result = gl2psBeginPage("AddPolyPrimitive Test", "VTK", viewport,
// GL2PS_PDF, GL2PS_SIMPLE_SORT,
// GL2PS_NO_OPENGL_CONTEXT | GL2PS_NO_BLENDING,
// GL_RGBA, 0, NULL, 0, 0, 0, 0, stream, 0);
// if (result != GL2PS_SUCCESS)
// {
// std::cerr << "gl2psBeginPage failed." << std::endl;
// return EXIT_FAILURE;
// }

// // AddPolyPrimitive arguments:
// GL2PSvertex vertices[3]; // Vertices.
// GLint offset = 0; // line offset
// GLushort pattern = 0xffff; // glLineStipple pattern
// GLint factor = 1; // glLineStipple repeat factor
// GLfloat ofactor = 0.f; // glPolygonOffset factor
// GLfloat ounits = 0.f; // glPolygonOffset units
// GLfloat width = 1; // linewidth or pointsize
// // Something to do with gl2psEnable(GL2PS_POLYGON_BOUNDARY), which is not
// // implemented according to the docs.
// char boundary = 0;

// // Point:
// setVertex(vertices[0], 200, 307.5, 0, 0.f, 0.f, 1.f, 1.f);
// gl2psAddPolyPrimitive(GL2PS_POINT, 1, vertices, offset, ofactor, ounits,
// pattern, factor, /*width=*/15, boundary);

// // Line:
// // Note that the first vertex's color is used for the entire line.
// setVertex(vertices[0], 100, 50, 0, 1.f, 0.f, 0.f, 1.f);
// setVertex(vertices[1], 300, 50, 0, 0.f, 0.f, 1.f, 1.f);
// gl2psAddPolyPrimitive(GL2PS_LINE, 2, vertices, offset, ofactor, ounits,
// pattern, factor, width, boundary);

// // Triangle:
// setVertex(vertices[0], 100, 100, 0, 1.f, 0.f, 0.f, 1.f);
// setVertex(vertices[1], 300, 100, 0, 0.f, 1.f, 0.f, 1.f);
// setVertex(vertices[2], 200, 300, 0, 0.f, 0.f, 1.f, 1.f);
// gl2psAddPolyPrimitive(GL2PS_TRIANGLE, 3, vertices, offset, ofactor, ounits,
// pattern, factor, width, boundary);

// // Text:
// setVertex(vertices[0], 200, 325, 0, 0.f, 0.f, 0.f, 1.f);
// gl2psForceRasterPos(vertices);
// float rgba[4] = {1.0, 0.0, 0.0, 1.0};
//// gl2psTextOptColor("1", "Helvetica", 12, GL2PS_TEXT_C, 0.f, rgba);
//// gl2psTextOptColor("2", "Helvetica", 12, GL2PS_TEXT_CL, 0.f, rgba);
//// gl2psTextOptColor("3", "Helvetica", 12, GL2PS_TEXT_CR, 0.f, rgba);
//// gl2psTextOptColor("4", "Helvetica", 12, GL2PS_TEXT_B, 0.f, rgba);
//// gl2psTextOptColor("5", "Helvetica", 12, GL2PS_TEXT_BL, 0.f, rgba);
//// gl2psTextOptColor("6", "Helvetica", 12, GL2PS_TEXT_BR, 0.f, rgba);
//// gl2psTextOptColor("7", "Helvetica", 12, GL2PS_TEXT_T, 0.f, rgba);
//// gl2psTextOptColor("8", "Helvetica", 12, GL2PS_TEXT_TL, 0.f, rgba);
//// gl2psTextOptColor("9", "Helvetica", 12, GL2PS_TEXT_TR, 0.f, rgba);

// // DrawPixels:
// std::vector<float> pixelData;
// generatePixelData(pixelData, 100, 100);
// setVertex(vertices[0], 275, 275, 0, 0.f, 0.f, 0.f, 0.f);
// gl2psForceRasterPos(vertices);
// gl2psDrawPixels(100, 100, 0, 0, GL_RGBA, GL_FLOAT, &pixelData[0]);

// result = gl2psEndPage();
// if (result != GL2PS_SUCCESS)
// {
// std::cerr << "gl2psEndPage failed." << std::endl;
// return EXIT_FAILURE;
// }

// fclose(stream);

return EXIT_SUCCESS;
}
17 changes: 14 additions & 3 deletions Rendering/GL2PS/module.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
if(ANDROID OR APPLE_IOS) # No GL2PS on mobile
return()
if(ANDROID OR APPLE_IOS) # No gl2ps on mobile
set(gl2ps_depends)
set(gl2ps_test_depends)
elseif(VTK_RENDERING_BACKEND STREQUAL "OpenGL")
set(gl2ps_depends vtkRenderingGL2PS)
set(gl2ps_test_depends vtkIOExportOpenGL)
elseif(VTK_RENDERING_BACKEND STREQUAL "OpenGL2")
set(gl2ps_depends vtkRenderingGL2PSOpenGL2)
set(gl2ps_test_depends vtkIOExportOpenGL2)
endif()
vtk_module(vtkRenderingGL2PS
TCL_NAME vtkRenderingGLtoPS
Expand All @@ -8,6 +15,10 @@ vtk_module(vtkRenderingGL2PS
TEST_DEPENDS
vtkTestingRendering
vtkInteractionStyle
vtkIOExport
vtkRendering${VTK_RENDERING_BACKEND}
vtkRenderingGL2PS
vtkIOExportOpenGL
KIT
vtkOpenGL
DEPENDS
Expand All @@ -21,4 +32,4 @@ vtk_module(vtkRenderingGL2PS
vtkRenderingCore
vtkRenderingFreeType
vtkgl2ps
)
)
17 changes: 11 additions & 6 deletions Rendering/GL2PS/vtkGL2PSUtilities.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ void vtkGL2PSUtilities::DrawPathPS(vtkPath *path, double rasterPos[3],
out << "grestore" << endl;

glRasterPos3dv(rasterPos);
gl2psSpecial(gl2psGetFileFormat(), out.str().c_str());
gl2psSpecial(gl2psGetFileFormat(), out.str().c_str(), NULL);
}

void vtkGL2PSUtilities::DrawPathPDF(vtkPath *path, double rasterPos[3],
Expand Down Expand Up @@ -544,8 +544,8 @@ void vtkGL2PSUtilities::DrawPathPDF(vtkPath *path, double rasterPos[3],
static_cast<float>(rgba[2])/255.f <<
(strokeWidth > 1e-5 ? " RG" : " rg") << endl;
// opacity
out << static_cast<float>(rgba[3])/255.f
<< (strokeWidth > 1e-5 ? " CA" : " ca") << endl;
// out << static_cast<float>(rgba[3])/255.f
// << (strokeWidth > 1e-5 ? " CA" : " ca") << endl;
// translate
out << 1.f << " " << 0.f << " " << 0.f << " " << 1.f << " "
<< windowPos[0] << " " << windowPos[1] << " cm" << endl;
Expand Down Expand Up @@ -644,7 +644,13 @@ void vtkGL2PSUtilities::DrawPathPDF(vtkPath *path, double rasterPos[3],
out << "Q" << endl; // Pop state

glRasterPos3dv(rasterPos);
gl2psSpecial(gl2psGetFileFormat(), out.str().c_str());
GL2PSrgba colorRgba;
colorRgba[0] = rgba[0]/255.0;
colorRgba[1] = rgba[1]/255.0;
colorRgba[2] = rgba[2]/255.0;
colorRgba[3] = rgba[3]/255.0;

gl2psSpecial(gl2psGetFileFormat(), out.str().c_str(), colorRgba);
}

void vtkGL2PSUtilities::DrawPathSVG(vtkPath *path, double rasterPos[3],
Expand Down Expand Up @@ -805,7 +811,7 @@ void vtkGL2PSUtilities::DrawPathSVG(vtkPath *path, double rasterPos[3],
<< "</g>" << endl;

glRasterPos3dv(rasterPos);
gl2psSpecial(gl2psGetFileFormat(), out.str().c_str());
gl2psSpecial(gl2psGetFileFormat(), out.str().c_str(), NULL);
}


Expand All @@ -823,7 +829,6 @@ inline void vtkGL2PSUtilities::ProjectPoint(double point[4],
vtkNew<vtkMatrix4x4> modelviewMatrix;
modelviewMatrix->DeepCopy(glMatrix);
modelviewMatrix->Transpose();

vtkNew<vtkMatrix4x4> transformMatrix;
vtkMatrix4x4::Multiply4x4(projectionMatrix.GetPointer(),
modelviewMatrix.GetPointer(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static inline void generatePixelData(std::vector<float> &data,
int TestGL2PSAddPolyPrimitive(int , char * [])
{
std::string filename = vtkTestingInteractor::TempDirectory +
std::string("/TestGL2PSAddPolyPrimitive.ps");
std::string("/TestGL2PSAddPolyPrimitive.pdf");
FILE *stream = fopen(filename.c_str(), "wb");
if (stream == NULL)
{
Expand All @@ -72,7 +72,7 @@ int TestGL2PSAddPolyPrimitive(int , char * [])

GLint viewport[4] = { 0, 0, 400, 400 };
GLint result = gl2psBeginPage("AddPolyPrimitive Test", "VTK", viewport,
GL2PS_PS, GL2PS_SIMPLE_SORT,
GL2PS_PDF, GL2PS_SIMPLE_SORT,
GL2PS_NO_OPENGL_CONTEXT | GL2PS_NO_BLENDING,
GL_RGBA, 0, NULL, 0, 0, 0, 0, stream, 0);
if (result != GL2PS_SUCCESS)
Expand Down
Loading

0 comments on commit 0a5cbee

Please sign in to comment.