Texturing on hard coded mesh not working

Oct 18, 2010 at 4:06 AM

You can download the whole project here: https://fractrace.dev.java.net/files/documents/6137/153623/BalderChildWindow_Oct_17_2010.zip

There are two objects in my scene using the same image for the diffuse map:

                <Geometries:Box x:Name="chamferBox001" Dimension="30,30,30" ItemsSource="{Binding}">
                    <Geometries:Box.Material>
                        <Materials:Material Ambient="Black" Diffuse="White" Shade="Gouraud" DiffuseMap="/BalderChildWindow;component/TestPattern_1024x1024.jpg" DoubleSided="True" />
                    </Geometries:Box.Material>
                </Geometries:Box>
                <Custom:MyBox x:Name="dividers" >
                    <Custom:MyBox.Material>
                        <Materials:Material Ambient="Black" Diffuse="White" Shade="Gouraud" DiffuseMap="/BalderChildWindow;component/TestPattern_1024x1024.jpg" DoubleSided="True" />
                    </Custom:MyBox.Material>
                </Custom:MyBox>

The texture on the first object renders fine, the second object's three planes are all black from any angle (the geometry is there, just no texture), has something changed with the Geometry class that I need to change in my code?

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using Balder.Objects.Geometries;

namespace BalderChildWindow
    {
    public class MyBox : Geometry
        {
        public override void Prepare(Balder.Display.Viewport viewport)
            {
            base.Prepare(viewport);
            GenerateVertices();
            GenerateFaces();
            GenerateTextureCoordinates();
            }
        private void GenerateVertices()
            {
            FullDetailLevel.AllocateVertices(12);
            // X-plane divider
            FullDetailLevel.SetVertex(0, new Vertex(-60, -60, 0));
            FullDetailLevel.SetVertex(1, new Vertex(60, -60, 0));
            FullDetailLevel.SetVertex(2, new Vertex(60, 60, 0));
            FullDetailLevel.SetVertex(3, new Vertex(-60, 60, 0));

            // Y-plane divider
            FullDetailLevel.SetVertex(4, new Vertex(-60, 0, -60));
            FullDetailLevel.SetVertex(5, new Vertex(60, 0, -60));
            FullDetailLevel.SetVertex(6, new Vertex(60, 0, 60));
            FullDetailLevel.SetVertex(7, new Vertex(-60, 0, 60));

            // Z-plane divider
            FullDetailLevel.SetVertex(8, new Vertex(0, -60, -60));
            FullDetailLevel.SetVertex(9, new Vertex(0, 60, -60));
            FullDetailLevel.SetVertex(10, new Vertex(0, 60, 60));
            FullDetailLevel.SetVertex(11, new Vertex(0, -60, 60));
            }
        private void GenerateFaces()
            {
            FullDetailLevel.AllocateFaces(6);
            FullDetailLevel.SetFace(0, new Face(0, 1, 2));
            FullDetailLevel.SetFace(1, new Face(0, 2, 3));
            FullDetailLevel.SetFace(2, new Face(4, 5, 6));
            FullDetailLevel.SetFace(3, new Face(4, 6, 7));
            FullDetailLevel.SetFace(4, new Face(8, 9, 10));
            FullDetailLevel.SetFace(5, new Face(8, 10, 11));
            }
        private void GenerateTextureCoordinates()
            {
            FullDetailLevel.AllocateTextureCoordinates(12);
            // texture mapping
            FullDetailLevel.SetTextureCoordinate(0, new TextureCoordinate(0, 1));
            FullDetailLevel.SetTextureCoordinate(1, new TextureCoordinate(1, 1));
            FullDetailLevel.SetTextureCoordinate(2, new TextureCoordinate(1, 0));
            FullDetailLevel.SetTextureCoordinate(3, new TextureCoordinate(0, 0));

            FullDetailLevel.SetFaceTextureCoordinateIndex(0, 0, 1, 2);
            FullDetailLevel.SetFaceTextureCoordinateIndex(1, 0, 2, 3);
            FullDetailLevel.SetFaceTextureCoordinateIndex(2, 0, 1, 2);
            FullDetailLevel.SetFaceTextureCoordinateIndex(3, 0, 2, 3);
            FullDetailLevel.SetFaceTextureCoordinateIndex(4, 0, 1, 2);
            FullDetailLevel.SetFaceTextureCoordinateIndex(5, 0, 2, 3);
            }
        }
    }

Thanks
Don
Coordinator
Oct 18, 2010 at 12:50 PM

Slight change on this, the renderer need the normals, so if you add the following line of code at the end of the Prepare() method : 

GeometryHelper.CalculateNormals(FullDetailLevel);

You'll see it all.

 

Oct 18, 2010 at 10:33 PM

 

Yes, That fixed it! Thanks Adept :)

Congratulations to the entire team, Balder has beat Silvermotion and Kit3D in handling intersecting and overlapping geometry!!!!!

It took me a while to get here, but intersections, are maintained exactly where they are supposed to be, and textures do not distort as the view changes. Neither Silvermotion or Kit3D were able to do it right.

Here is a link to the test project if anyone is interested: https://fractrace.dev.java.net/files/documents/6137/153635/BalderChildWindow_Oct_18_2010.zip

Thank you all for your excellent work!

Don

Oct 19, 2010 at 1:32 PM

One problem with the latest results is the planes are two sided, however the texture is only applied to one side.

I expected when viewing a polygon from the back side the texture would be visible, just backwards, and I remember this being the case with version 0.8.8.8.

V-0.8.8.8 had the same texture distortion and z-depth issues as Kit3D and Silvermotion, but both sides of a two sided polygon were textured.

Do I need to add something to my code to texture both sides of a 2-sided polygon?

Thanks

Don

Coordinator
Oct 19, 2010 at 4:45 PM

That was a bit of a mind-boggle for me, but after freaking out for 2 minutes my mind started thinking rationally again. I think it has to do with how lighting is done. In version 0.8.8.8 normals for the vertices were smoothed out, so even for a plane the normals were not pointing in the direction of the plane necessarily. In 0.8.8.9 this has changed and its doing the math correctly for this and the result is that since the normals for these planes are always for the backside pointing in the
wrong direction, lighting will cause it to become black. It is in fact textured, just that it is black. Turning the light off and you should see it.

This is what I'd call a bug in Balder - just something I haven't thought about. I'll create a work item for it, as it needs to be fixed (handled).

But as a work around, what you can do; turn off doublesided on the material and generate the faces needed for the flipside yourself and set the first set of faces with SmoothingGroup=0 and the second set with SmoothingGroup=1

        private void GenerateFaces()
            {
            FullDetailLevel.AllocateFaces(12);
            FullDetailLevel.SetFace(0, new Face(0, 1, 2) {SmoothingGroup = 0});
			FullDetailLevel.SetFace(1, new Face(0, 2, 3) { SmoothingGroup = 0 });
			FullDetailLevel.SetFace(2, new Face(4, 5, 6) { SmoothingGroup = 0 });
			FullDetailLevel.SetFace(3, new Face(4, 6, 7) { SmoothingGroup = 0 });
			FullDetailLevel.SetFace(4, new Face(8, 9, 10) { SmoothingGroup = 0 });
			FullDetailLevel.SetFace(5, new Face(8, 10, 11) { SmoothingGroup = 0 });

			FullDetailLevel.SetFace(6, new Face(2, 1, 0) { SmoothingGroup = 1 });
			FullDetailLevel.SetFace(7, new Face(3, 2, 0) { SmoothingGroup = 1 });
			FullDetailLevel.SetFace(8, new Face(6, 5, 4) { SmoothingGroup = 1 });
			FullDetailLevel.SetFace(9, new Face(7, 6, 4) { SmoothingGroup = 1 });
			FullDetailLevel.SetFace(10, new Face(10, 9, 8) { SmoothingGroup = 1 });
			FullDetailLevel.SetFace(11, new Face(11, 10, 8) { SmoothingGroup = 1 });

            }
        private void GenerateTextureCoordinates()
            {
            FullDetailLevel.AllocateTextureCoordinates(12);
            // texture mapping
            FullDetailLevel.SetTextureCoordinate(0, new TextureCoordinate(0, 1));
            FullDetailLevel.SetTextureCoordinate(1, new TextureCoordinate(1, 1));
            FullDetailLevel.SetTextureCoordinate(2, new TextureCoordinate(1, 0));
            FullDetailLevel.SetTextureCoordinate(3, new TextureCoordinate(0, 0));

            FullDetailLevel.SetFaceTextureCoordinateIndex(0, 0, 1, 2);
            FullDetailLevel.SetFaceTextureCoordinateIndex(1, 0, 2, 3);
            FullDetailLevel.SetFaceTextureCoordinateIndex(2, 0, 1, 2);
            FullDetailLevel.SetFaceTextureCoordinateIndex(3, 0, 2, 3);
            FullDetailLevel.SetFaceTextureCoordinateIndex(4, 0, 1, 2);
            FullDetailLevel.SetFaceTextureCoordinateIndex(5, 0, 2, 3);

			FullDetailLevel.SetFaceTextureCoordinateIndex(6, 2, 1, 0);
			FullDetailLevel.SetFaceTextureCoordinateIndex(7, 3, 2, 0);
			FullDetailLevel.SetFaceTextureCoordinateIndex(8, 2, 1, 0);
			FullDetailLevel.SetFaceTextureCoordinateIndex(9, 3, 2, 0);
			FullDetailLevel.SetFaceTextureCoordinateIndex(10, 2, 1, 0);
			FullDetailLevel.SetFaceTextureCoordinateIndex(11, 3, 2, 0);

            }
        }

 

Coordinator
Oct 19, 2010 at 4:46 PM

Btw: Thanks for discovering this. !

Coordinator
Oct 19, 2010 at 4:48 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.