Click here to Skip to main content
15,789,698 members
Home / Discussions / WPF
   

WPF

 
QuestionCustomControl Styling Question Pin
Kevin Marois4-May-23 18:22
professionalKevin Marois4-May-23 18:22 
AnswerRe: CustomControl Styling Question Pin
Pete O'Hanlon4-May-23 21:29
subeditorPete O'Hanlon4-May-23 21:29 
QuestionNavigationControl - Continued Pin
Kevin Marois2-May-23 15:49
professionalKevin Marois2-May-23 15:49 
AnswerRe: NavigationControl - Continued Pin
Richard Deeming2-May-23 23:19
mveRichard Deeming2-May-23 23:19 
GeneralRe: NavigationControl - Continued Pin
Kevin Marois3-May-23 6:21
professionalKevin Marois3-May-23 6:21 
GeneralRe: NavigationControl - Continued Pin
Kevin Marois3-May-23 16:10
professionalKevin Marois3-May-23 16:10 
GeneralRe: NavigationControl - Continued Pin
Richard Deeming3-May-23 22:35
mveRichard Deeming3-May-23 22:35 
QuestionWPF 3D rendering Pin
Kenneth Haugland1-May-23 5:52
professionalKenneth Haugland1-May-23 5:52 
I have started on a project that involves using loading resources on the new glTF format. Namely it from NASAs 3D models of the Solar system:
Sun 3D Model | NASA Solar System Exploration[^]

The glTF format seems to be easy to load using the glTFLoader that can be downloaded from github. The file itself is just a mix of some header info, a JSON part, and a Binary data part that is pretty well described in the documentation. glTF-Tutorials/gltfTutorial_002_BasicGltfStructure.md at master · KhronosGroup/glTF-Tutorials · GitHub[^]

But it is not working exactly. First issue is that the images are loaded, but the texture coordinates seems to be wrong. I have asked this question over at the site that monitors the standard. Load glTF into WPF 3D with a simple viewer - glTF - Khronos Forums[^].

The second issue, which I hope some of you might help me with, is that the diffuse material does not seem to register correctly. So I have to set this via a name property on the diffuse material itself. Then it loads. Any ideas on why that is?

using glTFLoader.Schema;
using glTFLoader;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using System.Windows;

namespace Wpf_glTF_testing
{
    public class Earth
    {
        public UInt32 Magic { get; private set; }

        public UInt32 Version { get; private set; }

        public UInt32 TotalFileLength { get; private set; }

        public UInt32 JsonChuckLength { get; private set; }
        public UInt32 BinChuckLength { get; private set; }

        public byte[]  JSON_data { get; private set; }
        public byte[] BIN_data { get; private set; }

        UInt32 JSON_hex = 0x4E4F534A;
        UInt32 BIN_hex = 0x004E4942;

        public Point3DCollection MaterialPoints { get; private set; } = new Point3DCollection();    

        public Vector3DCollection NormalPoints { get; private set; } = new Vector3DCollection();  

        public PointCollection TexturePoints { get; private set; } = new PointCollection();

        public Int32Collection Indecies { get; private set; } = new Int32Collection();

        public MeshGeometry3D Model3D { get; private set; }

        public GeometryModel3D GeoModel3D { get;set; } = new GeometryModel3D();

        public List<BitmapImage> Images { get; private set; } = new List<BitmapImage>();

        //Sun_1_1391000
        //Earth_1_12756
        public Earth(string filename = "Earth_1_12756.glb")
        {
            // Get all metadata
            Gltf test = Interface.LoadModel(filename);

            // Load all byte arrays from the Binary file glTF version 2
            using (var stream = File.Open(filename, FileMode.Open))
            {
                using (var reader = new BinaryReader(stream, Encoding.UTF8, false))
                {
                    // Reading the inital data that determines the file type
                    Magic = reader.ReadUInt32();
                    Version = reader.ReadUInt32();
                    TotalFileLength  = reader.ReadUInt32();

                    // Read the JSON data
                    JsonChuckLength = reader.ReadUInt32();
                    UInt32 chunckType = reader.ReadUInt32();
                    // Should be equal to JSON_hex
                    string hexValue = chunckType.ToString("X");

                    JSON_data = reader.ReadBytes((int)JsonChuckLength);

                    // Read teh binary data
                    BinChuckLength = reader.ReadUInt32();
                    UInt32 chunckType2 = reader.ReadUInt32();
                    
                    // Should be equal to BIN_hex
                    string hexValue2 = chunckType2.ToString("X");
                    
                    BIN_data = reader.ReadBytes((int)BinChuckLength);
                }
            }

            int PosisionOrNormals = 0;

            // Here I assume all the indecies, vertextes and normalized vectors are given
            foreach (glTFLoader.Schema.Accessor item in test.Accessors)
            {

                // Read the byte positions and offsets for each accessors
                var BufferViewIndex = item.BufferView;
                BufferView BufferView = test.BufferViews[(int)BufferViewIndex];
                var Offset = BufferView.ByteOffset;
                var Length = BufferView.ByteLength;

                // Convert the appropriate format
                if (item.ComponentType == Accessor.ComponentTypeEnum.FLOAT)
                {
                    if (item.Type == Accessor.TypeEnum.VEC3)
                    {
                        // Used to scale all planets to +/- 1
                        float[] ScalingFactorForVariables = new float[3];

                        if (item.Max == null)
                            ScalingFactorForVariables = new float[3] { 1.0f, 1.0f, 1.0f };
                        else
                            ScalingFactorForVariables = item.Max;

                        // Upscaling factor
                        float UpscalingFactor = 1.5f;

                        // Point 3D for posisions and Vector 3D for normals
                        Point3DCollection PointsPosisions = new Point3DCollection();
                        Vector3DCollection NormalsForPosisions = new Vector3DCollection();

                        for (int n = Offset; n < Offset + Length; n += 4)
                        {
                            float x = BitConverter.ToSingle(BIN_data, n) / ScalingFactorForVariables[0] * UpscalingFactor;
                            n += 4;
                            float y = BitConverter.ToSingle(BIN_data, n)  / ScalingFactorForVariables[1] * UpscalingFactor;
                            n += 4;
                            float z = BitConverter.ToSingle(BIN_data, n)  / ScalingFactorForVariables[2] * UpscalingFactor;

                            PointsPosisions.Add(new Point3D(x, y, z));
                            NormalsForPosisions.Add(new Vector3D(x, y, z));
                        }

                        // Assuing that the posisions are index 0 and 
                        if (PosisionOrNormals == 0)
                            MaterialPoints = PointsPosisions;
                        else
                            NormalPoints = NormalsForPosisions;

                        PosisionOrNormals++;
                    }
                    if (item.Type == Accessor.TypeEnum.VEC2)
                    {
                        // Assuming texture posisions
                        PointCollection vec2 = new PointCollection();
                        for (int n = Offset; n < Offset + Length; n += 4)
                        {
                            double x = Math.Round((double)BitConverter.ToSingle(BIN_data, n),2);
                            n += 4;
                            double y = Math.Round((double)BitConverter.ToSingle(BIN_data, n), 2);

                            vec2.Add(new Point((double)x, (double)y));
                        }
                        TexturePoints = vec2;
                    }
                }
                else if (item.ComponentType == Accessor.ComponentTypeEnum.UNSIGNED_SHORT)
                {
                    List<UInt16> TriangleIncidence = new List<UInt16>();
                    for (int n = Offset; n < Offset + Length; n += 2)
                    {
                        UInt16 TriangleItem = BitConverter.ToUInt16(BIN_data, n);                      
                        Indecies.Add((Int32)TriangleItem);

                    }
                }
            }

            // Showing the image
            foreach (glTFLoader.Schema.Image item in test.Images)
            {
                //var ImageType = item.MimeType;
                int BufferViewIndex = (int)item.BufferView;
                BufferView BufferView = test.BufferViews[BufferViewIndex];
                var Offset = BufferView.ByteOffset;
                var Length = BufferView.ByteLength;

                // Copy the relevant data from binary part
                byte[] ImageBytes = new byte[Length];
                Array.Copy(BIN_data, Offset, ImageBytes, 0, Length);

                // Conmvert to image
                MemoryStream ms = new MemoryStream(ImageBytes);
                BitmapImage Img = new BitmapImage();
                Img.BeginInit();
                Img.StreamSource = ms;
                Img.EndInit();
                Images.Add(Img);
              
            }
            Model3D = new MeshGeometry3D();

            Model3D.TriangleIndices = Indecies;
            Model3D.Positions = MaterialPoints;
            Model3D.Normals = NormalPoints;
            Model3D.TextureCoordinates = TexturePoints;

            GeoModel3D.Geometry = Model3D;

            // I cant get this to work properly
            GeoModel3D.Material = new DiffuseMaterial() { Brush = Brushes.Blue };
        }
    }
}

AnswerRe: WPF 3D rendering Pin
Gerry Schmitz2-May-23 7:14
mveGerry Schmitz2-May-23 7:14 
GeneralRe: WPF 3D rendering Pin
Kenneth Haugland2-May-23 10:59
professionalKenneth Haugland2-May-23 10:59 
AnswerRe: WPF 3D rendering Pin
RedDk2-May-23 9:03
RedDk2-May-23 9:03 
GeneralRe: WPF 3D rendering Pin
Kenneth Haugland2-May-23 11:01
professionalKenneth Haugland2-May-23 11:01 
QuestionExpander in ListBoxItem Width Problem Pin
Kevin Marois27-Apr-23 8:19
professionalKevin Marois27-Apr-23 8:19 
AnswerRe: Expander in ListBoxItem Width Problem Pin
Richard Deeming2-May-23 0:09
mveRichard Deeming2-May-23 0:09 
GeneralRe: Expander in ListBoxItem Width Problem Pin
Kevin Marois2-May-23 8:12
professionalKevin Marois2-May-23 8:12 
QuestionSpinning Indicator Control Error Pin
Kevin Marois26-Apr-23 15:32
professionalKevin Marois26-Apr-23 15:32 
AnswerRe: Spinning Indicator Control Error Pin
Gerry Schmitz27-Apr-23 8:01
mveGerry Schmitz27-Apr-23 8:01 
GeneralRe: Spinning Indicator Control Error Pin
Kevin Marois27-Apr-23 8:27
professionalKevin Marois27-Apr-23 8:27 
GeneralRe: Spinning Indicator Control Error Pin
Gerry Schmitz28-Apr-23 5:46
mveGerry Schmitz28-Apr-23 5:46 
GeneralRe: Spinning Indicator Control Error Pin
Kevin Marois28-Apr-23 8:24
professionalKevin Marois28-Apr-23 8:24 
QuestionFlat Button Style Problem Pin
Kevin Marois22-Apr-23 19:14
professionalKevin Marois22-Apr-23 19:14 
AnswerRe: Flat Button Style Problem Pin
Gerry Schmitz23-Apr-23 9:55
mveGerry Schmitz23-Apr-23 9:55 
GeneralRe: Flat Button Style Problem Pin
Kevin Marois23-Apr-23 10:15
professionalKevin Marois23-Apr-23 10:15 
GeneralRe: Flat Button Style Problem Pin
Gerry Schmitz23-Apr-23 16:28
mveGerry Schmitz23-Apr-23 16:28 
GeneralRe: Flat Button Style Problem Pin
Kevin Marois25-Apr-23 20:08
professionalKevin Marois25-Apr-23 20:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.