1
votes

I tried to render my surface with using alpha channel, but when I setting alpha value, it renders with random colors and not semi-transparent

// Init memory
Q3DSurface *poSurface = new Q3DSurface();
QSurface3DSeries *poSeries = new QSurface3DSeries();
QSurfaceDataArray *poDataArray = new QSurfaceDataArray();

// Generating test surface series
for ( int i = 0, k = 0; i < 10; ++i)
{
  QSurfaceDataRow *poRow = new QSurfaceDataRow();

  for ( int j = 0; j < 10; ++j )
  {
    float x = j;
    float y = i;
    float z = k;
    poRow->append( QSurfaceDataItem( QVector3D( x, y, z ) ) );
  }
  poDataArray->append( poRow );

  if ( i % 2 == 0 )
  {
    ++k;
  }
}

//
poSeries->dataProxy()->resetArray( poDataArray );
poSurface->addSeries( poSeries );

// Setting color with alpha value
poSeries->setBaseColor( QColor( 100, 100, 100, 100 ));

// Show surface widget
QWidget *poWidget = QWidget::createWindowContainer( poSurface );
poWidget->setWindowTitle( "test ");
poWidget->show();

What am I doing wrong?

1

1 Answers

1
votes

I'm not sure what you mean by "random colours", but at a guess, are you accounting for the default lighting? The effect of the 3d lighting can make colours look differently to what they are explicitly set to. With regard to your transparency setting, I think this code looks fine. You are setting the RGBA values as R=100, G=100, B=100, A=100 which will produce a grey colour. Under the default light this may look like light/dark patches because of the function you have graphed and the way the light "bounces" off the edges. Try changing your code slightly to see if this is really what is happening:

    poSeries->dataProxy()->resetArray( poDataArray );
    poSurface->addSeries( poSeries );
    //PICK A DARK THEME THAT WILL HELP TO ILLUSTRATE THE EFFECT
    poSurface->activeTheme()->setType(Q3DTheme::ThemeEbony);
    //TURN THE AMBIENT LIGHTING UP TO FULL
    poSurface->activeTheme()->setAmbientLightStrength(1.0f);

   // Setting color with alpha value
   //SET IT TO RED WITH A FULL ALPHA CHANNEL
   poSeries->setBaseColor( QColor( 100, 0, 0, 255 ));



// Show surface widget
QWidget *poWidget = QWidget::createWindowContainer( poSurface );
poWidget->setWindowTitle( "test ");
poWidget->show();

This should produce a dark red image of your graph with a dark background (just to make things clearer). Now put the alpha value back to what you wanted originally and you will see what effect this has on the colouring:

    // Setting color with alpha value: "washed out" red colour
poSeries->setBaseColor( QColor( 100, 0, 0, 100 ));

You can probably see that it is the colour (rather than the mesh) that is being rendered at the transparency setting set through "setBaseColor".

Unfortunately I cannot tell you how to render transparently the Q3DSurface itself, but I hope that helps a little.