0
\$\begingroup\$

Gah! I have a scene with 2 3D objects - a plane and a sphere;

the plane sits on the xy axis with a depth if -100 and diameter of 200;

the sphere diameter is 32 and sits on the xyz = 0 position;

when rendering using indexedprimitive all appears fine. the sphere appears in front of the plane.

however as i decrease the world z component of the sphere and the sphere passes through the plane and becomes hidden it shows the front of the sphere passing through first.

as if the plane were moving down over the sphere but only displaying the parts of the sphere that should be hidden.

the most puzzling aspect is that this problem only appears as one object passes through the other. when behind it is hidden, when in front visible?

Help please :)

Edit 20/12/2015

Has been suggested I upload a sequence of pics to better illustrate my problem so... Hope these help

Sequential Rendertarget images .

        GlobeTarget = new RenderTarget2D(graphicsDevice, GlobeDiameter, GlobeDiameter, false,
                          SurfaceFormat.Color, DepthFormat.Depth24, 0, RenderTargetUsage.PreserveContents);
        graphicsDevice.SetRenderTarget(GlobeTarget);
        graphicsDevice.BlendState = BlendState.Opaque;
        graphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
        graphicsDevice.DepthStencilState = new DepthStencilState
            { DepthBufferEnable = true, DepthBufferFunction = CompareFunction.Less };
        graphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1, 0);
\$\endgroup\$
  • \$\begingroup\$ Thanks, the pictures help a lot. At a glance, this looks like it could be a winding issue. If you're accidentally rendering the back faces of the sphere instead of the front faces, then this is exactly what we'd expect to see, the the center of the eye disappearing first, followed by the edges (because the center we see would then actually be the back of the eye, the furthest part from us). If you turn off back/front face culling and render all faces, is the effect still present? \$\endgroup\$ – DMGregory Dec 20 '15 at 3:33
  • 1
    \$\begingroup\$ Thanks for the speedy response :) Did as you suggested and reversed the indexBuffer order for each triangle of the eye. This solved the depth problem but left me with a purely white eyeball. As I type this I suspect that the eye is simply facing away from the camera. Back in five :) \$\endgroup\$ –  Stoort Dec 20 '15 at 12:13
  • 1
    \$\begingroup\$ OK 20 minutes later and all is well. I am usling Blender to create my objects then Exporting them in the Wavefront.obj format. This gives me a text file with vertex, texture position and triangle face info. All I needed to do was reverse the 3 face vertex pointers. Easy peasy once you know the problem exists. Thanks SO MUCH. It looks great now :) Stoort \$\endgroup\$ –  Stoort Dec 20 '15 at 12:48
  • \$\begingroup\$ Good fix! Want to write that up as an answer that other Blender & XNA users can refer to if they encounter a similar problem? \$\endgroup\$ – DMGregory Dec 20 '15 at 14:07
0
\$\begingroup\$

All thanks to DMGregory for showing me the way.

Because I was using an Orthographic view and looking straight down at objects directly facing me with NO rotation I fell foul of a really old optical illusion. I was indeed looking at the inside of my objects but my brain didn`t realise this so interpreted the visual info as facing out. Doh!

The camera :-

    Projection = Matrix.CreateOrthographic(GlobeTarget.Width, GlobeTarget.Height, -500, 1000);

and the solution :-

        graphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
        graphicsDevice.DepthStencilState = new DepthStencilState
            { DepthBufferEnable = true, DepthBufferFunction = CompareFunction.Less };
        graphicsDevice.Clear(ClearOptions.DepthBuffer, Color.Black, 1, 0);

I only clear the DepthBuffer and NOT the Target (The texture) as before setting the graphicsDevice here I use the SpriteBatch to Draw a texture onto the RenderTarget2D that I use to build the full image. This worked beautifully and allowed me to create the following character :D

Cel Shaded Lit and Dropped in a Ball

Here are two snapshots I took using the SaveAsPng texture function :-

        FileStream setStream = File.Open(fileName, FileMode.Create);
        StreamWriter writer = new StreamWriter(setStream);
        textureToSave.SaveAsPng(setStream, textureToSave.Width, textureToSave.Height);
        setStream.Dispose();
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.