Sign in to follow this  

STL file display problems

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all I'm trying to write a simple viewer that can display stl files. and example of an stl file is:
solid square.stl
  facet normal -1 0 0
    outer loop
      vertex 50 50 0
      vertex 250 250 0
      vertex 50 250 0
    endloop
  endfacet
  facet normal 1 0 0 
    outer loop
      vertex 50 50 0
      vertex 250 50 0
      vertex 250 250 0
    endloop
  endfacet
endsolid square.stl

So far my program can read in all the normals and vertices fine. However when it comes to displaying it sometimes you can see the back parts through the front parts, or parts appear flat instead of being solid. The rendering options I'm using are:
            Dim presentParams As New PresentParameters()
            presentParams.Windowed = True
            presentParams.SwapEffect = SwapEffect.Discard

            ' for lighting
            presentParams.EnableAutoDepthStencil = True
            presentParams.AutoDepthStencilFormat = DepthFormat.D16
            device = New Device(0, DeviceType.Hardware, Me.DirectXScreen, CreateFlags.SoftwareVertexProcessing, presentParams)
            device.RenderState.Lighting = True
            device.RenderState.ZBufferEnable = True
            device.RenderState.CullMode = Cull.CounterClockwise

All the vertices are stored in a vertex buffer and rendered like this:
            setuplights()
            SetupMatrices()

            device.SetStreamSource(0, VertexBuffer, 0)
            device.VertexFormat = CustomVertex.PositionNormal.Format
            device.DrawPrimitives(PrimitiveType.TriangleList, 0, Me.FacetList.Count)

The problem with STL files is that the facets are not declared in any order. i.e. you could have the head declared first, then the toes, then the neck, then fingernail, then torso etc.. Is it likely that because of this there is some problem with the rendering? If anyone needs more code or can already spot an error, I would be really grateful for some advice. Many thanks DRB2k2

Share this post


Link to post
Share on other sites
The order of faces in the buffer should not cause this sort of problem (though it may effect the speed -- if you are worried look at using a mesh and the Optimise/ConvertMeshSubsetToStrips). It is much more likely a problem with the winding order and backface culling. I notice that you have the CullMode set to CounterClockwise, try setting it to Cull.None or Cull.Clockwise.

I don't know about the STL format, but if you have mixed clockwise/counterclockwise faces in your file you may have to recompute the order yourself to use backface culling.

Also make sure that you are clearing the z-buffer when you clear the device, not doing that can cause some odd rendering behavior.

Hope this helps.

Share this post


Link to post
Share on other sites

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this