0

I am new to vtk and Qt. I have created a cube in blender with 3 faces red and 1 face green and exported as cube.ply to preserve color information:

ply
format ascii 1.0
comment Created by Blender 2.69 (sub 0) - www.blender.org, source file: ''
element vertex 24
property float x
property float y
property float z
property float nx
property float ny
property float nz
element face 6
property list uchar uint vertex_indices
end_header
1.000000 1.000000 -1.000000 0.000000 0.000000 -1.000000
1.000000 -1.000000 -1.000000 0.000000 0.000000 -1.000000
-1.000000 -1.000000 -1.000000 0.000000 0.000000 -1.000000
-1.000000 1.000000 -1.000000 0.000000 0.000000 -1.000000
1.000000 0.999999 1.000000 0.000000 -0.000000 1.000000
-1.000000 1.000000 1.000000 0.000000 -0.000000 1.000000
-1.000000 -1.000000 1.000000 0.000000 -0.000000 1.000000
0.999999 -1.000001 1.000000 0.000000 -0.000000 1.000000
1.000000 1.000000 -1.000000 1.000000 -0.000000 0.000000
1.000000 0.999999 1.000000 1.000000 -0.000000 0.000000
0.999999 -1.000001 1.000000 1.000000 -0.000000 0.000000
1.000000 -1.000000 -1.000000 1.000000 -0.000000 0.000000
1.000000 -1.000000 -1.000000 -0.000000 -1.000000 -0.000000
0.999999 -1.000001 1.000000 -0.000000 -1.000000 -0.000000
-1.000000 -1.000000 1.000000 -0.000000 -1.000000 -0.000000
-1.000000 -1.000000 -1.000000 -0.000000 -1.000000 -0.000000
-1.000000 -1.000000 -1.000000 -1.000000 0.000000 -0.000000
-1.000000 -1.000000 1.000000 -1.000000 0.000000 -0.000000
-1.000000 1.000000 1.000000 -1.000000 0.000000 -0.000000
-1.000000 1.000000 -1.000000 -1.000000 0.000000 -0.000000
1.000000 0.999999 1.000000 0.000000 1.000000 0.000000
1.000000 1.000000 -1.000000 0.000000 1.000000 0.000000
-1.000000 1.000000 -1.000000 0.000000 1.000000 0.000000
-1.000000 1.000000 1.000000 0.000000 1.000000 0.000000
4 0 1 2 3
4 4 5 6 7
4 8 9 10 11
4 12 13 14 15
4 16 17 18 19
4 20 21 22 23

I have created a QVTK Widget in Qt Creator and my code draws the cube in grey. Following is the my mainwindow.cpp file (after various modifications to get colors to show):

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QVTKInteractor.h"
#include <vtkSTLReader.h>
#include <vtkPLYReader.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);



   vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
   vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
   vtkSmartPointer<vtkRenderWindow> renwin = vtkSmartPointer<vtkRenderWindow>::New();
   vtkSmartPointer<QVTKInteractor> interactor= vtkSmartPointer<QVTKInteractor>::New();
   vtkSmartPointer<vtkPLYReader> reader = vtkSmartPointer<vtkPLYReader>::New();
   vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();


    reader->SetFileName("../vtk2/media/cube.ply");
    reader->Update();
    mapper->SetColorModeToMapScalars();
    mapper->SetInputConnection(reader->GetOutputPort());

    mapper->ScalarVisibilityOn();
    actor->SetMapper(mapper);
    renderer->AddActor(actor);

       ui->qvtkWidget->SetRenderWindow(renwin);
       ui->qvtkWidget->GetRenderWindow()->AddRenderer(renderer);
       renderer->SetBackground(.3,.6,.3);
       actor->GetProperty()->SetFrontfaceCulling(true);
       QVTKInteractor* inter=ui->qvtkWidget->GetInteractor();
}

MainWindow::~MainWindow()
{
    delete ui;
}

It does not show any colors. Having tried various VTK examples I have not found one which shows how to map colors from imported 3d file. I am sure I am missing a piece of code.

A side-issue, the cube loads looking asymmetrical in QVTKWidget. I wonder if that is an issue with QVTK. I am attaching an image ..

Visit http://postimg.org/image/r8093cwbt/ for an image

Maelstorm
  • 580
  • 2
  • 10
  • 29
  • Update: Reading up on the format I realise my .ply file (at the top) does not contain color information at all. The materials I laid on the object in Blender are NOT vertex colors. For that, Blender has a separate 'vertex paint' mode which colors the vertex and the .ply output file then has color info. Unfortunately the vtkPLYReader reference states it does not read vertex color info by default (instead 'face color' information is read). For now I am working different examples to get a hang of VTK before I revisit the issue. – Maelstorm Aug 06 '14 at 00:13

1 Answers1

0

You can try by accessing the vtkPolyData, then the pointData (or cellData) and the setting the color, something like this

mapper->GetInput()->GetPointData()->SetScalars(colors);

In this case color is a vtkUnsignedCharArray with colors in r,g,b (more details Here).

Or also you can try modifing the actors properties like this:

actor->GetProperty()SetColor(double r, double g, double b);

In this case remember to add the line #include <vtkProperty.h> to be able to access actor's properties

cycopepe
  • 531
  • 8
  • 21
  • Thanks I am looking into it. Can you also look at that image I linked at the bottom of my post? The cube imported in QVTKWidget is quite wonky. Maybe you can give an idea why it looks so? – Maelstorm Aug 04 '14 at 22:21