70001964 Create a vdPolyface with material image and mapping TexCoords

Article 70001964
Type HowTo
Product Engine
Version 8
Date Added 5/6/2021 12:00:00 AM
Fixed 9.9002.0.1 (5/6/2021 12:00:00 AM)
Submitted by VectorDraw Team

Summary

Create a vdPolyface with material image and mapping TexCoords

Solution

Example:

Suppose we have create an image 'textcoords.png' looks like
with widthxheigth = 2598x1732 (aspect width/height = 1.5)

Following a code that create a new vdPolyface that represent a box 1x1x1 size
and maps  the parts of the image to each face
NOTE: Assume the image coords to be lowerleft = 0,0 and upperright = 1,1
               //points of a box corners
                gPoints pts = new gPoints();
                //bottom points
                pts.Add(new gPoint(0, 0, 0));
                pts.Add(new gPoint(1, 0, 0));
                pts.Add(new gPoint(1, 1, 0));
                pts.Add(new gPoint(0, 1, 0));
                //top points
                pts.Add(new gPoint(0, 0, 1));
                pts.Add(new gPoint(1, 0, 1));
                pts.Add(new gPoint(1, 1, 1));
                pts.Add(new gPoint(0, 1, 1));
                Int32Array facelist = new Int32Array(new int[] {
                    //note: change the 
                    1,2,3,4,-1,//bottom
                    5,6,7,8,-1,//top
                    1,2,6,5,-1,//front
                    3,4,8,7,-1,//back
                    4,1,5,8,-1,//left
                    2,3,7,6,-1,//right
                });

                //NOTE: Assume the image coords to be lowerleft = 0,0 and upperright = 1,1
                //--->(texture coords relative to the image lower - left)
                //  0.0  0.33 0.66 1.0 
                //
                //1  |----|----|----|
                //   |-A--|-B--|-C--|
                //0.5|----|----|----|
                //   |-D--|-E--|-F--|
                //0  |----|----|----|


                DoubleArray texcoords = new DoubleArray(new double[] {
                    0.0,0.5,0.33,0.5,0.33,1.0,0.0,1.0,  //bottom  A letter
                    0.33,0.5,0.66,0.5,0.66,1.0,0.33,1.0,//top    B letter
                    0.66,0.5,1.0,0.5,1.0,1.0,0.66,1.0,  //front  C letter
                    0.0,0.0,0.33,0.0,0.33,0.5,0.0,0.5,  //back   D letter
                    0.33,0.0,0.66,0.0,0.66,0.5,0.33,0.5,//left   E letter
                    0.66,0.0,1.0,0.0,1.0,0.5,0.66,0.5,  //right  F letter
                });
                vdPolyface pf = new vdPolyface(doc, pts, facelist) { TexCoords = texcoords };
                vdColor pfcolor = new vdColor(Color.Blue);
                pfcolor.MaterialImage = doc.Images.Add(@"textcoords.png");
                pf.PenColor = pfcolor;
                doc.Model.Entities.AddItem(pf);
                doc.RenderMode = vdRender.Mode.Render;

                doc.Redraw(true);

Send comments on this topic.