3d model suitable for viewing

How is the 3D model handled in a consistent way?
When I have a random model that I want to put in the viewport I dunno if it's too big or not, if I need to translate it to the middle ...
I think the 3D object might have its own source.

0


a source to share


3 answers


You need to find a bounding volume, a shape that encompasses all the vertices of an object, for your object that is easier to work with than the object itself. Spheres are often used for this. Either the artist can define the sphere as part of the model information, or you can process it at runtime. Calculating the optimal sphere is very difficult, but you can get a good approximation using the following:

determine the min and max value of each point x, y and z
  for each vertex
    min_x = min (min_x, vertex.x)
    max_x = max (max_x, vertex.x)
    min_y = min (min_y, vertex.y)
    max_y = max (max_y, vertex.y)
    min_z = min (min_z, vertex.z)
    max_z = max (max_z, vertex.z)

sphere centre = (max_x + min_x) / 2, (max_y + min_y) / 2, (max_z + min_z) / 2
sphere radius = distance from centre to (max_x, max_y, max_z)

      



Using this sphere, define a position of the world that allows you to view the sphere in full simple geometry, will determine this.

+2


a source


Sorry, your question is very unclear. I assume you want to focus the 3D model on the viewport. You can achieve this by calculating the bounding box of the model. To do this, traverse all polygons and get the min / max X / Y / Z coordinates. The bounding box, defined by the points (min_x,min_y,min_z)

and (max_x,max_y,max_z)

, will contain the entire model. You can now center the model by looking at the center of this box. With some additional calculations (depending on your FOV), you can also get left / right / top / bottom borders inside your viewport.



0


a source


", so I tried to reduce it.

Your best bet in this situation is not to transform your model at all! Leave it. What you want to change is your camera.

First, calculate the bounding box of your model somewhere in 3D space.

Then calculate the radius of it by taking max (aabb.max.x-aabb.min.x, aabb.max.y-aabb.min.y, aabb.max.z-aabb.min.z) .It's raw, but it's does its job.

To center an object in the viewport, place the camera at the object's position. If Y is your front axle, subtract the radius from Y. If Z is the straight axis, then subtract the radius from it. Subtract the fiction factor to get you past the annoying one around the plane so your model doesn't cut out. I am using quaternions in my engine with a nice lookat () method. So call lookat () and go to the center of the bounding box. Voila! The object is focused in the viewport no matter where it is in the world.

This always aligns the camera axis, so you might want to get fancy and instead convert the camera to model space, subtract the radius, and then look at the center again. Then you always look at the back of the model. The key is always lookat ().

Here is some sample code from my engine. It checks if we are trying to create a piece of static terrain when looking down from a height or at a light or static mesh. Everything that is drawn on the scene is visual, and there are dozens of different types. A Visual :: Instance is a copy or drawing of a visual image.

void EnvironmentView::frameSelected(){
  if( m_tSelection.toInstance() ){
    Visual::Instance& I = m_tSelection.toInstance().cast();
    Visual* pVisual = I.toVisual();
    if( pVisual->isa( StaticTerrain::classid )){
      toEditorCamera().toL2W().setPosition( pt3( 0, 0, 50000 ));
      toEditorCamera().lookat( pt3( 0 ));
    }else if( I.toFlags()->bIsLight ){
      Visual::LightInstance& L = static_cast<Visual::LightInstance&>( I );
      qst3& L2W = L.toL2W();
      const sphere s( L2W.toPosition(), L2W.toScale() );
      const f32 y =-(s.toCenter()+s.toRadius()).y();
      const f32 z = (s.toCenter()+s.toRadius()).y();
      qst3& camL2W = toEditorCamera().toL2W();
      camL2W.setPosition(s.toCenter()+pt3( 0, y, z ));//45 deg above
      toEditorCamera().lookat( s.toCenter() );
    }else{
      Mesh::handle hMesh = pVisual->getMesh();
      if( hMesh ){
        qst3& L2W = m_tSelection.toInstance()->toL2W();
        vec4x4 M;
        L2W.getMatrix( M );
        aabb3 b0 = hMesh->toBounds();
        b0.min = M * b0.min;
        b0.max = M * b0.max;
        aabb3 b1;
        b1 += b0.min;
        b1 += b0.max;
        const sphere s( b1.toSphere() );
        const f32 y =-(s.toCenter()+s.toRadius()*2.5f).y();
        const f32 z = (s.toCenter()+s.toRadius()*2.5f).y();
        qst3& camL2W = toEditorCamera().toL2W();
        camL2W.setPosition( L2W.toPosition()+pt3( 0, y, z ));//45 deg above
        toEditorCamera().lookat( b1.toOrigin() );
      }
    }
  }
}

      

0


a source







All Articles