question

Mischa Spelt avatar image
1 Like"
Mischa Spelt asked Phil BoBo edited

OnDraw in module vs FlexScript: different results?

Please help, I think I am going crazy. A while back, I figured out the recipe for drawing a text with a fixed size regardless of the size of the object. I tried to do the same thing right now, from a C++ module, and it didn't work. So I tried to draw a simple sphere and it didn't work. Then I put the same code in the OnDraw trigger in FlexSim and .. it worked!

As far as I was able to figure out, the FlexScript trigger and the C++ method are called from the same OpenGL state (there is no intermediate transformation going on) so the code should have the same result.

Expected result

The red sphere and the "FlexSim" text as shown in the screenshot below (as produced by FlexScript's OnDraw)

Actual result

The green sphere and the "Module!" text in the screenshot below (the current output of the module's OnDraw).

1657136404431.pngBoth are produced using (the FlexScript and C++ equivalent, respectively, of)

  fglPushMatrix();
  drawtomodelscale( current );

  drawsphere( 0, 0, 0, 0.5, 255, 0, 0, 0.5 );
 
  spacetranslate( 0, 0, current.size.z );
  spacescale( current.size.x, current.size.y, current.size.z );
  drawtext( view, "FlexSim",
    /* loc: */ 0, 0, 0,
    /* size: */ 0, /* textSize: */ 0.75, 0,
    /* rot: */  0, 0, 0,
    /* rgba: */ 0, 0, 0, 1 );

  fglPopMatrix();

A Minimal Complete Reproducible Example including the module and a test model is here: Test.zip

FlexSim 22.0.4
module sdkondraw
1657136404431.png (367.4 KiB)
test.zip (3.7 MiB)
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

Jordan Johnson avatar image
2 Likes"
Jordan Johnson answered Phil BoBo edited

I'm no expert; it may be that @Phil BoBo has more information. I was able to fix the issue by applying a rotation in the C++ code:

FlexSim::fglPushMatrix();
FlexSim::fglRotate(90, 1, 0, 0);
FlexSim::drawtomodelscale( this->holder );
// ...

My guess is that there is in fact a transform that is applied before calling the OnDraw trigger, apparently the same transform as applied here. Here's my result with no OnDraw trigger:
1657143843757.png

Also, a side note. You can use the size property in C++ just like in FlexScript:

auto s = this->size; // since TestObject inherits FlexSimObject
FlexSim::spacetranslate( 0, 0, s.z );
FlexSim::spacescale( s.x, s.y, s.z );

1657143843757.png (260.7 KiB)
· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Phil BoBo avatar image Phil BoBo ♦♦ commented ·

The ondrawtrigger of on object is purposefully called with a coordinate space that the user expects, with Z being up.

FlexSim's native drawing is done with Z pointing at the camera, which is looking forward at the object from the front:

1657145060759.png

To get into the coordinate space you expect from a trigger that's in FlexSim's native drawing coordinate space, rotate 90 degrees around the X axis, as Jordan explained.

Depending on the function you are in, you could be in either coordinate space system, but for backwards compatibility of large amounts of existing OpenGL and mesh code that's been written assuming one space or the other, we aren't going to change this to try to make it consistent. Just do a rotation if you are in one space and want to write your code in the other.

The OnDraw event function, an object's On Draw trigger, and the onDraw() method of a FlexSim class are three different functions called in different ways at different times. They aren't all the same thing.

1 Like 1 ·
1657145060759.png (55.0 KiB)