コード例 #1
0
ファイル: Wpf3dControl.xaml.cs プロジェクト: mnisl/OD
		///<summary>Objects are added in this step as all gray.  Colors are set in a separate step.</summary>
		public void AddObjectGroup(D3ObjectGroup group) {
			_model3Dgroup=new Model3DGroup();		
			DirectionalLight light=new DirectionalLight(Colors.WhiteSmoke,new Vector3D(-3,-3,-3));
			_model3Dgroup.Children.Add(light);
			DirectionalLight light2=new DirectionalLight(Colors.LightGray,new Vector3D(2.8,-3,-3));
			_model3Dgroup.Children.Add(light2);
			//AmbientLight ambientLight=new AmbientLight(Colors.DarkGray);
			//_model3Dgroup.Children.Add(ambientLight);
			objectNames=new List<string>();
			objectNames.Add("light1");
			objectNames.Add("light2");//to maintain 1:1
			for(int i=0;i<group.D3Objects.Count;i++) {
				MeshGeometry3D meshGeometry3D=new MeshGeometry3D();
				Point3DCollection points=group.D3Objects[i].GenerateVertices();
				meshGeometry3D.Positions=points;
				PointCollection textures=group.D3Objects[i].GenerateTextures();
				meshGeometry3D.TextureCoordinates=textures;
				Vector3DCollection vectors=group.D3Objects[i].GenerateNormals();
				meshGeometry3D.Normals=vectors;
				Int32Collection indices=group.D3Objects[i].GenerateIndices();
				meshGeometry3D.TriangleIndices=indices;
				GeometryModel3D geometryModel3D=new GeometryModel3D();
				geometryModel3D.Geometry=meshGeometry3D;
				//materials
				MaterialGroup materialGroup=new MaterialGroup();
				DiffuseMaterial diffuseMaterial=new DiffuseMaterial();
				if(group.D3Objects[i].TextureMap!=null) {
					ImageBrush imageBrush=new ImageBrush(D3Helper.ConvertImage(group.D3Objects[i].TextureMap));
					imageBrush.ViewportUnits=BrushMappingMode.Absolute;
					ScaleTransform scaleTransform=new ScaleTransform(1,-1);//scale y -1 to flip vertically
					TranslateTransform translateTransform=new TranslateTransform(0,1);//shift up one after flipping
					TransformGroup transformGroup=new TransformGroup();
					transformGroup.Children.Add(scaleTransform);
					transformGroup.Children.Add(translateTransform);
					imageBrush.Transform=transformGroup;
					diffuseMaterial.Brush=imageBrush;
				}
				else if(group.TextureMap!=null && group.D3Objects[i].VertexNormals[0].Texture!=null) {//a group texture is specified and this object uses texture mapping
					ImageBrush imageBrush=new ImageBrush(D3Helper.ConvertImage(group.TextureMap));
					imageBrush.ViewportUnits=BrushMappingMode.Absolute;
					ScaleTransform scaleTransform=new ScaleTransform(1,-1);//scale y -1 to flip vertically
					TranslateTransform translateTransform=new TranslateTransform(0,1);//shift up one after flipping
					TransformGroup transformGroup=new TransformGroup();
					transformGroup.Children.Add(scaleTransform);
					transformGroup.Children.Add(translateTransform);
					imageBrush.Transform=transformGroup;
					diffuseMaterial.Brush=imageBrush;
				}
				else {
					diffuseMaterial.Brush=new SolidColorBrush(Colors.Gray);
					//diffuseMaterial.Color=Colors.Gray;//this didn't work.  Needs brush.
				}
				materialGroup.Children.Add(diffuseMaterial);
				//specular material at 1
				SpecularMaterial specularMaterial=new SpecularMaterial();
				specularMaterial.Brush=new SolidColorBrush(Colors.White);
				specularMaterial.SpecularPower=150;//smaller numbers give more reflection.  150 is minimal specular.
				materialGroup.Children.Add(specularMaterial);
				geometryModel3D.Material=materialGroup;
				_model3Dgroup.Children.Add(geometryModel3D);
				objectNames.Add(group.D3Objects[i].Name);
			}
			ModelVisual3D modelVisual3D=new ModelVisual3D();
			modelVisual3D.Content=_model3Dgroup;
			myViewport.Children.Add(modelVisual3D);
		}
コード例 #2
0
ファイル: D3Helper.cs プロジェクト: mnisl/OD
		public static D3ObjectGroup ImportObjectsFromObjFile(string strObjData) {
			D3ObjectGroup d3ObjectGroup=new D3ObjectGroup();
			string[] lines=strObjData.Split(new string[] {"\r\n"},StringSplitOptions.None);
			int i=0;
			List<Vertex3f> listV=new List<Vertex3f>();
			List<Vertex3f> listT=new List<Vertex3f>();
			List<Vertex3f> listN=new List<Vertex3f>();
			Vertex3f vertex;
			Vertex3f texture;
			Vertex3f normal;
			VertexNormal vertnorm;
			D3Object d3Object=null;
			string[] items;
			string[] subitems;
			int idxV;
			int idxT;
			int idxN;
			Face face;
			while(i<lines.Length){
				//Debug.WriteLine(i.ToString());
				if(lines[i].StartsWith("#")//comment
						|| lines[i].StartsWith("mtllib")//material library.  We build our own.
						|| lines[i].StartsWith("usemtl"))//use material
				{
					i++;
					continue;//we don't care about any of these
				}
				if(lines[i].StartsWith("o")) {//object.  For example "o cube1"
					//vertices and normals are per object as they are pulled out of the file,
					//but we need to regroup them per d3Object.
					i++;
					continue;
				}
				if(lines[i].StartsWith("v ")) {//vertex.  For example "v -0.34671119 0.22176000 0.83703486"
					items=lines[i].Split(' ');
					vertex=new Vertex3f();
					vertex.X=Convert.ToSingle(items[1],CultureInfo.InvariantCulture);
					vertex.Y=Convert.ToSingle(items[2],CultureInfo.InvariantCulture);
					vertex.Z=Convert.ToSingle(items[3],CultureInfo.InvariantCulture);
					listV.Add(vertex);
					i++;
					continue;
				}
				if(lines[i].StartsWith("vt")) {//vertex.  For example "vt 0.72553915 0.63685900"
					items=lines[i].Split(' ');
					texture=new Vertex3f();
					texture.X=Convert.ToSingle(items[1],CultureInfo.InvariantCulture);
					texture.Y=Convert.ToSingle(items[2],CultureInfo.InvariantCulture);
					//texture.Z=Convert.ToSingle(items[3],CultureInfo.InvariantCulture);
					listT.Add(texture);
					i++;
					continue;
				}
				if(lines[i].StartsWith("vn")) {//normal.  For example "vn -0.32559605 -0.84121753 -0.43167149"
					items=lines[i].Split(' ');
					normal=new Vertex3f();
					normal.X=Convert.ToSingle(items[1],CultureInfo.InvariantCulture);
					normal.Y=Convert.ToSingle(items[2],CultureInfo.InvariantCulture);
					normal.Z=Convert.ToSingle(items[3],CultureInfo.InvariantCulture);
					listN.Add(normal);
					i++;
					continue;
				}
				if(lines[i].StartsWith("g")) {//group.  For example "g cylinder2_default"
					if(d3Object!=null) {
						d3ObjectGroup.D3Objects.Add(d3Object);//add the previous object to the group
					}
					d3Object=new D3Object();//start the new one, including the new list of VNs
					d3Object.Name=lines[i].Substring(2);
					i++;
					continue;
				}
				if(lines[i].StartsWith("f")) {//face.  For example "f 12//384 51//443 384//336 383//335".  Format is v//n
					items=lines[i].Split(' ');//5 items in this example
					//create triangle fan, preserving counterclockwise order
					for(int f=2;f<items.Length-1;f++) {//one face/triangle per loop, 2 triangles in this case
						face=new Face();
						//only grab three vertices
						//first vertex is always the 0 for the fan
						subitems=items[1].Split('/');//12,,384 for the first triangle
						idxV=Convert.ToInt32(subitems[0])-1;//11 for the first triangle
						if(subitems[1]=="") {//no texture
							idxT=-1;
						}
						else {
							idxT=Convert.ToInt32(subitems[1])-1;
						}
						idxN=Convert.ToInt32(subitems[2])-1;//383 for the first triangle
						vertnorm=new VertexNormal();
						vertnorm.Vertex=listV[idxV];
						if(idxT!=-1) {
							vertnorm.Texture=listT[idxT];
						}
						vertnorm.Normal=listN[idxN];
						face.IndexList.Add(d3Object.GetIndexForVertNorm(vertnorm));
						//second vertex
						subitems=items[f].Split('/');//51,,443 for the first triangle
						idxV=Convert.ToInt32(subitems[0])-1;//50 for the first triangle
						if(subitems[1]=="") {//no texture
							idxT=-1;
						}
						else {
							idxT=Convert.ToInt32(subitems[1])-1;
						}
						idxN=Convert.ToInt32(subitems[2])-1;//442 for the first triangle
						vertnorm=new VertexNormal();
						vertnorm.Vertex=listV[idxV];
						if(idxT!=-1) {
							vertnorm.Texture=listT[idxT];
						}
						vertnorm.Normal=listN[idxN];
						face.IndexList.Add(d3Object.GetIndexForVertNorm(vertnorm));
						//third vertex
						subitems=items[f+1].Split('/');//384,,336 for the first triangle
						idxV=Convert.ToInt32(subitems[0])-1;//383 for the first triangle
						if(subitems[1]=="") {//no texture
							idxT=-1;
						}
						else {
							idxT=Convert.ToInt32(subitems[1])-1;
						}
						idxN=Convert.ToInt32(subitems[2])-1;//335 for the first triangle
						vertnorm=new VertexNormal();
						vertnorm.Vertex=listV[idxV];
						if(idxT!=-1) {
							vertnorm.Texture=listT[idxT];
						}
						vertnorm.Normal=listN[idxN];
						face.IndexList.Add(d3Object.GetIndexForVertNorm(vertnorm));
						d3Object.Faces.Add(face);
					}
					i++;
					continue;
				}
				//might be another kind of row that we missed above
				i++;
			}
			if(d3Object!=null) {
				d3ObjectGroup.D3Objects.Add(d3Object);//add the last object to the group
			}
			return d3ObjectGroup;
		}
コード例 #3
0
        public static D3ObjectGroup ImportObjectsFromObjFile(string strObjData)
        {
            D3ObjectGroup d3ObjectGroup = new D3ObjectGroup();

            string[]        lines = strObjData.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            int             i     = 0;
            List <Vertex3f> listV = new List <Vertex3f>();
            List <Vertex3f> listT = new List <Vertex3f>();
            List <Vertex3f> listN = new List <Vertex3f>();
            Vertex3f        vertex;
            Vertex3f        texture;
            Vertex3f        normal;
            VertexNormal    vertnorm;
            D3Object        d3Object = null;

            string[] items;
            string[] subitems;
            int      idxV;
            int      idxT;
            int      idxN;
            Face     face;

            while (i < lines.Length)
            {
                //Debug.WriteLine(i.ToString());
                if (lines[i].StartsWith("#") ||            //comment
                    lines[i].StartsWith("mtllib") ||                            //material library.  We build our own.
                    lines[i].StartsWith("usemtl"))                               //use material
                {
                    i++;
                    continue;                    //we don't care about any of these
                }
                if (lines[i].StartsWith("o"))    //object.  For example "o cube1"
                //vertices and normals are per object as they are pulled out of the file,
                //but we need to regroup them per d3Object.
                {
                    i++;
                    continue;
                }
                if (lines[i].StartsWith("v "))                 //vertex.  For example "v -0.34671119 0.22176000 0.83703486"
                {
                    items    = lines[i].Split(' ');
                    vertex   = new Vertex3f();
                    vertex.X = Convert.ToSingle(items[1], CultureInfo.InvariantCulture);
                    vertex.Y = Convert.ToSingle(items[2], CultureInfo.InvariantCulture);
                    vertex.Z = Convert.ToSingle(items[3], CultureInfo.InvariantCulture);
                    listV.Add(vertex);
                    i++;
                    continue;
                }
                if (lines[i].StartsWith("vt"))                 //vertex.  For example "vt 0.72553915 0.63685900"
                {
                    items     = lines[i].Split(' ');
                    texture   = new Vertex3f();
                    texture.X = Convert.ToSingle(items[1], CultureInfo.InvariantCulture);
                    texture.Y = Convert.ToSingle(items[2], CultureInfo.InvariantCulture);
                    //texture.Z=Convert.ToSingle(items[3],CultureInfo.InvariantCulture);
                    listT.Add(texture);
                    i++;
                    continue;
                }
                if (lines[i].StartsWith("vn"))                 //normal.  For example "vn -0.32559605 -0.84121753 -0.43167149"
                {
                    items    = lines[i].Split(' ');
                    normal   = new Vertex3f();
                    normal.X = Convert.ToSingle(items[1], CultureInfo.InvariantCulture);
                    normal.Y = Convert.ToSingle(items[2], CultureInfo.InvariantCulture);
                    normal.Z = Convert.ToSingle(items[3], CultureInfo.InvariantCulture);
                    listN.Add(normal);
                    i++;
                    continue;
                }
                if (lines[i].StartsWith("g"))                 //group.  For example "g cylinder2_default"
                {
                    if (d3Object != null)
                    {
                        d3ObjectGroup.D3Objects.Add(d3Object);  //add the previous object to the group
                    }
                    d3Object      = new D3Object();             //start the new one, including the new list of VNs
                    d3Object.Name = lines[i].Substring(2);
                    i++;
                    continue;
                }
                if (lines[i].StartsWith("f"))                  //face.  For example "f 12//384 51//443 384//336 383//335".  Format is v//n
                {
                    items = lines[i].Split(' ');               //5 items in this example
                    //create triangle fan, preserving counterclockwise order
                    for (int f = 2; f < items.Length - 1; f++) //one face/triangle per loop, 2 triangles in this case
                    {
                        face = new Face();
                        //only grab three vertices
                        //first vertex is always the 0 for the fan
                        subitems = items[1].Split('/');              //12,,384 for the first triangle
                        idxV     = Convert.ToInt32(subitems[0]) - 1; //11 for the first triangle
                        if (subitems[1] == "")                       //no texture
                        {
                            idxT = -1;
                        }
                        else
                        {
                            idxT = Convert.ToInt32(subitems[1]) - 1;
                        }
                        idxN            = Convert.ToInt32(subitems[2]) - 1;         //383 for the first triangle
                        vertnorm        = new VertexNormal();
                        vertnorm.Vertex = listV[idxV];
                        if (idxT != -1)
                        {
                            vertnorm.Texture = listT[idxT];
                        }
                        vertnorm.Normal = listN[idxN];
                        face.IndexList.Add(d3Object.GetIndexForVertNorm(vertnorm));
                        //second vertex
                        subitems = items[f].Split('/');              //51,,443 for the first triangle
                        idxV     = Convert.ToInt32(subitems[0]) - 1; //50 for the first triangle
                        if (subitems[1] == "")                       //no texture
                        {
                            idxT = -1;
                        }
                        else
                        {
                            idxT = Convert.ToInt32(subitems[1]) - 1;
                        }
                        idxN            = Convert.ToInt32(subitems[2]) - 1;         //442 for the first triangle
                        vertnorm        = new VertexNormal();
                        vertnorm.Vertex = listV[idxV];
                        if (idxT != -1)
                        {
                            vertnorm.Texture = listT[idxT];
                        }
                        vertnorm.Normal = listN[idxN];
                        face.IndexList.Add(d3Object.GetIndexForVertNorm(vertnorm));
                        //third vertex
                        subitems = items[f + 1].Split('/');          //384,,336 for the first triangle
                        idxV     = Convert.ToInt32(subitems[0]) - 1; //383 for the first triangle
                        if (subitems[1] == "")                       //no texture
                        {
                            idxT = -1;
                        }
                        else
                        {
                            idxT = Convert.ToInt32(subitems[1]) - 1;
                        }
                        idxN            = Convert.ToInt32(subitems[2]) - 1;         //335 for the first triangle
                        vertnorm        = new VertexNormal();
                        vertnorm.Vertex = listV[idxV];
                        if (idxT != -1)
                        {
                            vertnorm.Texture = listT[idxT];
                        }
                        vertnorm.Normal = listN[idxN];
                        face.IndexList.Add(d3Object.GetIndexForVertNorm(vertnorm));
                        d3Object.Faces.Add(face);
                    }
                    i++;
                    continue;
                }
                //might be another kind of row that we missed above
                i++;
            }
            if (d3Object != null)
            {
                d3ObjectGroup.D3Objects.Add(d3Object);                //add the last object to the group
            }
            return(d3ObjectGroup);
        }
コード例 #4
0
        ///<summary>Objects are added in this step as all gray.  Colors are set in a separate step.</summary>
        public void AddObjectGroup(D3ObjectGroup group)
        {
            _model3Dgroup = new Model3DGroup();
            DirectionalLight light = new DirectionalLight(Colors.WhiteSmoke, new Vector3D(-3, -3, -3));

            _model3Dgroup.Children.Add(light);
            DirectionalLight light2 = new DirectionalLight(Colors.LightGray, new Vector3D(2.8, -3, -3));

            _model3Dgroup.Children.Add(light2);
            //AmbientLight ambientLight=new AmbientLight(Colors.DarkGray);
            //_model3Dgroup.Children.Add(ambientLight);
            objectNames = new List <string>();
            objectNames.Add("light1");
            objectNames.Add("light2");            //to maintain 1:1
            for (int i = 0; i < group.D3Objects.Count; i++)
            {
                MeshGeometry3D    meshGeometry3D = new MeshGeometry3D();
                Point3DCollection points         = group.D3Objects[i].GenerateVertices();
                meshGeometry3D.Positions = points;
                PointCollection textures = group.D3Objects[i].GenerateTextures();
                meshGeometry3D.TextureCoordinates = textures;
                Vector3DCollection vectors = group.D3Objects[i].GenerateNormals();
                meshGeometry3D.Normals = vectors;
                Int32Collection indices = group.D3Objects[i].GenerateIndices();
                meshGeometry3D.TriangleIndices = indices;
                GeometryModel3D geometryModel3D = new GeometryModel3D();
                geometryModel3D.Geometry = meshGeometry3D;
                //materials
                MaterialGroup   materialGroup   = new MaterialGroup();
                DiffuseMaterial diffuseMaterial = new DiffuseMaterial();
                if (group.D3Objects[i].TextureMap != null)
                {
                    ImageBrush imageBrush = new ImageBrush(D3Helper.ConvertImage(group.D3Objects[i].TextureMap));
                    imageBrush.ViewportUnits = BrushMappingMode.Absolute;
                    ScaleTransform     scaleTransform     = new ScaleTransform(1, -1);         //scale y -1 to flip vertically
                    TranslateTransform translateTransform = new TranslateTransform(0, 1);      //shift up one after flipping
                    TransformGroup     transformGroup     = new TransformGroup();
                    transformGroup.Children.Add(scaleTransform);
                    transformGroup.Children.Add(translateTransform);
                    imageBrush.Transform  = transformGroup;
                    diffuseMaterial.Brush = imageBrush;
                }
                else if (group.TextureMap != null && group.D3Objects[i].VertexNormals[0].Texture != null)             //a group texture is specified and this object uses texture mapping
                {
                    ImageBrush imageBrush = new ImageBrush(D3Helper.ConvertImage(group.TextureMap));
                    imageBrush.ViewportUnits = BrushMappingMode.Absolute;
                    ScaleTransform     scaleTransform     = new ScaleTransform(1, -1);         //scale y -1 to flip vertically
                    TranslateTransform translateTransform = new TranslateTransform(0, 1);      //shift up one after flipping
                    TransformGroup     transformGroup     = new TransformGroup();
                    transformGroup.Children.Add(scaleTransform);
                    transformGroup.Children.Add(translateTransform);
                    imageBrush.Transform  = transformGroup;
                    diffuseMaterial.Brush = imageBrush;
                }
                else
                {
                    diffuseMaterial.Brush = new SolidColorBrush(Colors.Gray);
                    //diffuseMaterial.Color=Colors.Gray;//this didn't work.  Needs brush.
                }
                materialGroup.Children.Add(diffuseMaterial);
                //specular material at 1
                SpecularMaterial specularMaterial = new SpecularMaterial();
                specularMaterial.Brush         = new SolidColorBrush(Colors.White);
                specularMaterial.SpecularPower = 150;              //smaller numbers give more reflection.  150 is minimal specular.
                materialGroup.Children.Add(specularMaterial);
                geometryModel3D.Material = materialGroup;
                _model3Dgroup.Children.Add(geometryModel3D);
                objectNames.Add(group.D3Objects[i].Name);
            }
            ModelVisual3D modelVisual3D = new ModelVisual3D();

            modelVisual3D.Content = _model3Dgroup;
            myViewport.Children.Add(modelVisual3D);
        }