Debug callback for rendering

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Debug callback for rendering

Postby Slick » Wed Nov 10, 2021 8:57 am

I had a debug callback for visualization of Newton objects working in 3.1x.

I'm working through re implementing things like this in 4.0.

Is there a simple sample of how to get the face vertices for debug rendering? I do see debug rendering in the samples but looking at the code it looks intertwined. I'll keep looking at it though.
Slick
 
Posts: 330
Joined: Sat Feb 07, 2004 7:24 pm
Location: LA last and France now

Re: Debug callback for rendering

Postby Julio Jerez » Wed Nov 10, 2021 4:31 pm

there are fewer call back now since the cpp class is exposed to the application.
if you look at function

Code: Select all
void ndDemoEntityManager::DrawDebugShapes()

               const ndBodyList& bodyList = m_world->GetBodyList();
      for (ndBodyList::dNode* bodyNode = bodyList.GetFirst(); bodyNode; bodyNode = bodyNode->GetNext())
      {
         ndBodyKinematic* const body = bodyNode->GetInfo();
         if (!body->GetAsBodyTriggerVolume())
         {
            const ndShapeInstance& shapeInstance = body->GetCollisionShape();
            ndDebugMeshCache::dNode* const shapeNode = m_debugShapeCache.Find(shapeInstance.GetShape());
            if (shapeNode)
            {
               dMatrix matrix(shapeInstance.GetScaledTransform(body->GetMatrix()));
               shapeNode->GetInfo().m_flatShaded->Render(this, matrix);
            }
         }
      }



you can get the body list and iterate over it after an update.
the only requirement is that you no use from and engine callback.
and that the engine is no updating , for that you just call Sync() like below
Code: Select all
   if (m_collisionDisplayMode)
   {
      m_world->Sync();
      DrawDebugShapes();
   }
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Debug callback for rendering

Postby Slick » Wed Nov 10, 2021 4:49 pm

Ok thanks for that. I was looking at that function today trying to work out how much of it I needed.
Slick
 
Posts: 330
Joined: Sat Feb 07, 2004 7:24 pm
Location: LA last and France now

Re: Debug callback for rendering

Postby Slick » Fri Nov 12, 2021 3:52 pm

It looks like I got the debug rendering working but I need to test it more and clean up my code.
Slick
 
Posts: 330
Joined: Sat Feb 07, 2004 7:24 pm
Location: LA last and France now

Re: Debug callback for rendering

Postby Slick » Sun Nov 14, 2021 7:35 am

I've hit a problem getting the debug visualisation to show for my static mesh terrain.

I ended up getting the polygon information from this function:
Code: Select all
virtual void DrawPolygon(dInt32 vertexCount, const dVector* const faceVertex, const ndEdgeType* const)

This appears to be called once after a hdDrawShape item is created.

This isn't getting called for my static mesh. Does that mean I didn't create my static mesh correctly or for some reason a static mesh doesn't get this callback? The end of my mesh creation is:
Code: Select all
ndShapeInstance shape(new ndShapeStatic_bvh(meshBuilder));
Slick
 
Posts: 330
Joined: Sat Feb 07, 2004 7:24 pm
Location: LA last and France now

Re: Debug callback for rendering

Postby Julio Jerez » Sun Nov 14, 2021 11:05 am

It is no going to be call automatically you have to mak eteh explicit call to the collsion shape with a class of teh type class ndShapeDebugCallback : public dClassAlloc that implements
function virtual void DrawPolygon

then you have to iterate over the body list to what ever collsion shape you have and calling
collision->DebugShape(...)
there are many example in file ...\newton-4.00\applications\ndSandbox\ndDemoDebugMesh.cpp
like this

Code: Select all
ndFlatShadedDebugMesh::ndFlatShadedDebugMesh(const ndShaderPrograms& shaderCache, const ndShapeInstance* const collision)
   :ndDemoMeshInterface()
   ,m_indexCount(0)
   ,m_shadeColorLocation(0)
   ,m_normalMatrixLocation(0)
   ,m_projectMatrixLocation(0)
   ,m_viewModelMatrixLocation(0)
   ,m_shader(0)
   ,m_vertexBuffer(0)
   ,m_vertextArrayBuffer(0)
   ,m_triangleIndexBuffer(0)
{
   class ndDrawShape : public ndShapeDebugCallback
   {
      public:
      ndDrawShape()
         :ndShapeDebugCallback()
         ,m_triangles(1024)
      {
      }

      virtual void DrawPolygon(dInt32 vertexCount, const dVector* const faceVertex, const ndEdgeType* const)
      {
         dVector p0(faceVertex[0]);
         dVector p1(faceVertex[1]);
         dVector p2(faceVertex[2]);
         
         dVector normal((p1 - p0).CrossProduct(p2 - p0));
         normal = normal.Normalize();
         for (dInt32 i = 2; i < vertexCount; i++)
         {
            glPositionNormal point;
            point.m_posit.m_x = GLfloat(faceVertex[0].m_x);
            point.m_posit.m_y = GLfloat(faceVertex[0].m_y);
            point.m_posit.m_z = GLfloat(faceVertex[0].m_z);
            point.m_normal.m_x = GLfloat(normal.m_x);
            point.m_normal.m_y = GLfloat(normal.m_y);
            point.m_normal.m_z = GLfloat(normal.m_z);
            m_triangles.PushBack(point);

            point.m_posit.m_x = GLfloat(faceVertex[i - 1].m_x);
            point.m_posit.m_y = GLfloat(faceVertex[i - 1].m_y);
            point.m_posit.m_z = GLfloat(faceVertex[i - 1].m_z);
            point.m_normal.m_x = GLfloat(normal.m_x);
            point.m_normal.m_y = GLfloat(normal.m_y);
            point.m_normal.m_z = GLfloat(normal.m_z);
            m_triangles.PushBack(point);

            point.m_posit.m_x = GLfloat(faceVertex[i].m_x);
            point.m_posit.m_y = GLfloat(faceVertex[i].m_y);
            point.m_posit.m_z = GLfloat(faceVertex[i].m_z);
            point.m_normal.m_x = GLfloat(normal.m_x);
            point.m_normal.m_y = GLfloat(normal.m_y);
            point.m_normal.m_z = GLfloat(normal.m_z);
            m_triangles.PushBack(point);
         }
      }
            
      dArray<glPositionNormal> m_triangles;
   };

   ndDrawShape drawShapes;
   collision->DebugShape(dGetIdentityMatrix(), drawShapes);


the funtion take a pointe to a collsion shape, and them make and notification object to get the call back to DrawPolygon.
This is similar to what lambdas function is is newton C++ but I like it far better, more readable, and more flexible , and more practical too.

to what class did you add
virtual void DrawPolygon(dInt32 vertexCount, const dVector* const faceVertex, const ndEdgeType* const)
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Debug callback for rendering

Postby Slick » Sun Nov 14, 2021 11:42 am

Ok thanks for the update. I will investigate. I didn't derive a class from ndDrawShape() : ndShapeDebugCallback() I just put the implementation in and created an object. Now I think I understand a bit more I will work on it.
Last edited by Slick on Sun Nov 14, 2021 1:05 pm, edited 1 time in total.
Slick
 
Posts: 330
Joined: Sat Feb 07, 2004 7:24 pm
Location: LA last and France now

Re: Debug callback for rendering

Postby Julio Jerez » Sun Nov 14, 2021 1:05 pm

is is not class ndDrawShape it is it is class ndShapeDebugCallback : public dClassAlloc.
ndDrawShape is just one example I made for rendering debug but in teh same file there few more,
for example there are solid rendering, wire frame rending with diffrent color, hidden line removed rendering, point and so on, you can make it however you want.


I just renamed the class to ndShapeDebugNotify : public dClassAlloc
for consistency with the other notification call backs classes
it is this class now.
Code: Select all
D_MSV_NEWTON_ALIGN_32
class ndShapeDebugNotify : public dClassAlloc
{
   public:
   enum ndEdgeType
   {
      m_shared,
      m_open,
   };

   ndShapeDebugNotify()
      :m_instance(nullptr)
   {
   }

   virtual ~ndShapeDebugNotify()
   {
   }

   virtual void DrawPolygon(dInt32 vertexCount, const dVector* const faceArray, const ndEdgeType* const edgeType) = 0;

   const ndShapeInstance* m_instance;
} D_GCC_NEWTON_ALIGN_32;


please sync to get the last change
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Debug callback for rendering

Postby Slick » Sun Nov 14, 2021 1:07 pm

Will do and will work on it tomorrow.
Slick
 
Posts: 330
Joined: Sat Feb 07, 2004 7:24 pm
Location: LA last and France now

Re: Debug callback for rendering

Postby Slick » Tue Nov 16, 2021 4:50 am

I made a lot of progress and got more debug rendering working. Although my flat plan debug lines are rendering strangely.

Maybe it is the order of faces that I am creating in Drawpolygon although when I create a plane using your function BuildGridPlane using mesheffect the debug lines are correct so it suggests an error in my parsing of vertices and indices although my parsing function was working in 3.1x but I had to make a few changes for 4.0.

Code: Select all
// add the 3 lines that make up this face from the 3 vertices
         p_ManObj->position(p2.m_x, p2.m_y, p2.m_z);
         p_ManObj->position(p0.m_x, p0.m_y, p0.m_z);
         p_ManObj->position(p0.m_x, p0.m_y, p0.m_z);
         p_ManObj->position(p1.m_x, p1.m_y, p1.m_z);
         p_ManObj->position(p1.m_x, p1.m_y, p1.m_z);
         p_ManObj->position(p2.m_x, p2.m_y, p2.m_z);


That is my code in Drawpolygon. Sometimes I see the m_y is sometimes 50.0f (in this example) although all of the y points should be zero.
Image
Slick
 
Posts: 330
Joined: Sat Feb 07, 2004 7:24 pm
Location: LA last and France now

Re: Debug callback for rendering

Postby Slick » Tue Nov 16, 2021 6:17 am

I finally got it working.

Code: Select all
meshBuilder->AddFace(&poly_verts[0][0], sizeof(Real) * 12, 3, materialindex);


My error was using sizeof(Real) instead of sizeof(Real) * 12.
Slick
 
Posts: 330
Joined: Sat Feb 07, 2004 7:24 pm
Location: LA last and France now


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 50 guests