STL file display problems

Started by
0 comments, last by turnpast 19 years, 8 months ago
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
Advertisement
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.

This topic is closed to new replies.

Advertisement