Hi,

I have this simple program that should draw a cube for the user by reading the data in from a text file which looks like this.

8                               
100.0 100.0 -100.0     
-100.0 100.0 -100.0    
-100.0 100.0 100.0      
100.0 100.0 100.0  
100.0 -100.0 -100.0  
-100.0 -100.0 -100.0  
-100.0 -100.0 100.0  
100.0 -100.0 100.0  
12                       
3 0 1 2                  
3 0 2 3  
3 0 4 5  
3 0 5 1  
3 1 5 6  
3 1 6 2  
3 2 6 7  
3 2 7 3  
3 3 7 4  
3 3 4 0  
3 4 7 6  
3 4 6 5  

The first number defines the number of vertices, which is 8 here. There then follow 8 vertices, as z, y, z values. Following that, there is another number, which defines the number of triangles, 12 here, as each face of the cube is made from two triangles. There then follow 12 lines which define the actual triangles in terms of the original vertices. The remaining numbers are the vertex numbers as defined by the previous 8 vertices. Using LH coordinates.

I am successfully being able to read in the data but i am not able to display it correctly. This following code reads in the data and attempts to draw the cube.

        void drawIt()
        {
            txtInfo.Text = "";
            inFile = new StreamReader("Cube.txt");
            String strNumVertices = inFile.ReadLine();

            int numVertices = Convert.ToInt32(strNumVertices);

            vertices = new Vertex[numVertices];

            //read all the vertices
            for (int i = 0; i < numVertices; i++)
            {
                double x, y, z;
                String strXYZ = inFile.ReadLine().TrimStart(); //Remove leading spaces
                String[] strVertices = strXYZ.Split(' ');      //Split with space as a separator.

                x = Convert.ToDouble(strVertices[0]);
                y = Convert.ToDouble(strVertices[1]);
                z = Convert.ToDouble(strVertices[2]);

                vertices[0].x = x;
                vertices[1].y = y;
                vertices[2].z = z;
            }

            String strNumTriangles = inFile.ReadLine();
            int numTriangles = Convert.ToInt32(strNumTriangles);

            int n, index1, index2, index3;
            Npoint p1, p2, p3;
            Nquad[] cubeF = new Nquad[numTriangles];

            //Read all the triangles.
            for (int i = 0; i < numTriangles; i++)
            {
                String str123 = inFile.ReadLine().TrimStart(); //Remove leading spaces
                String[] strVertices = str123.Split(' ');      //Split with space as a separator.

                index1 = Convert.ToInt32(strVertices[1]);
                index2 = Convert.ToInt32(strVertices[2]);
                index3 = Convert.ToInt32(strVertices[3]);

                p1 = new Npoint(vertices[index1].x, vertices[index1].y, vertices[index1].z);
                p2 = new Npoint(vertices[index2].x, vertices[index2].y, vertices[index2].z);
                p3 = new Npoint(vertices[index3].x, vertices[index3].y, vertices[index3].z);

                cubeF[i] = new Nquad(p1, p2, p3);   //change to triangle
            }

            Graphics g = CreateGraphics();

            for (int i = 0; i < numTriangles; i++)
            {
                cubeF[i].draw(g);
            }
        }

This next code where i think the issue is attempts to draw the cube via drawing a quad. However my text file only deals with triangles and i not sure how to change it so it draws the cube properly.

    class Nquad
    {

//********************************************************************//
//                                                                    //
// Constructors.  The section allows you to specify the 4 vertices of //
// the quad.                                                          //
//                                                                    //
//********************************************************************//

        public Nquad()
            {
            for (int i = 0; i < 3; i++) points[i] = new Npoint(0, 0, 0);
            }

            public Nquad(Npoint v1, Npoint v2, Npoint v3)
            {
                // TODO: Complete member initialization
                points[0] = v1;
                points[1] = v2;
                points[2] = v3;
            }

        //********************************************************************//
        //                                                                    //
        // Instance variables.                                                //
        //                                                                    //
        //********************************************************************//

        public Npoint[] points = new Npoint[3];
        public String  quadNameName = "Triangle";
        private Npoint p1;
        private Npoint p2;
        private Npoint p3;


        //********************************************************************//
        // Transform the quad by a transformation matrix.   Return the        //
        // transformed quad.                                                  //
        //********************************************************************//

        public Nquad transform(Nmatrix matrix)
        {
            Nquad transformedQuad = new Nquad();

            for (int i = 0; i < 3; i++)
            {
                transformedQuad.points[i] = points[i].transform(matrix);
                transformedQuad.points[i] = transformedQuad.points[i].rescale();
            }
            return transformedQuad;
        }


        //********************************************************************//
        // Draw the quad.   This assumes that the quad has heen transformed   //
        // first.                                                             //
        //********************************************************************//

        public void draw(Graphics g)
        {
            Pen pen = new Pen(Color.Black, 1);

            for (int i = 0; i < 2; i++)     // Draw the three lines as a loop.
                g.DrawLine(pen,
                    (int)points[i].point[0], (int)points[i].point[1], 
                    (int)points[i + 1].point[0], (int)points[i + 1].point[1]); 

            //And back to the first point for the last line.
            g.DrawLine(pen,
                (int)points[2].point[0], (int)points[2].point[1],
                (int)points[0].point[0], (int)points[0].point[1]);

        }
    }
}

Finally this code handles the coordinates of the cube.

    class Npoint
    {

//********************************************************************//
//                                                                    //
// Constructors.                                                      //
//                                                                    //
//********************************************************************//

    public Npoint()
        {
            for (int i = 0; i < 3; i++)
            {
                point[i] = 0; point[3] = 1;
            }
        }

    public Npoint(double x, double y, double z)
        {
            point[0] = x;  point[1] = y; point[2] = z; point[3] = 1;
        }

    public Npoint(String name)
        {
            for (int i = 0; i < 3; i++)
            {
                point[i] = 0; point[3] = 1;
                pointName = name;
            }
        }

    public Npoint(double x, double y, double z, String name)
        {
            point[0] = x;  point[1] = y; point[2] = z; point[3] = 1;
            pointName = name;
        }

//********************************************************************//
//                                                                    //
// Instance variables.                                                //
//                                                                    //
//********************************************************************//

    public double[] point = new double[4];
    public String pointName = "Point";

//*********************************************************************//
//                                                                     //
// Transform this point by multiplying it by a transformation matrix.  //
// The transformed point is returned as a homogeneous point...         //
//                                                                     //
// ...And re-scale the point to a unit scale...                        //
//                                                                     //
//*********************************************************************//

        public Npoint transform(Nmatrix matrix)
        {
        double total;
        Npoint newPoint = new Npoint(pointName);

        for (int col = 0; col < 4; col++)
            {
            total = 0;
            for (int row = 0; row < 4; row++) 
                total = total + point[row] * matrix.matrix[col, row];

            newPoint.point[col] = total;
            }

        return newPoint;
        }



        public Npoint rescale()
            {
            Npoint newPoint = new Npoint(pointName);
            for (int i = 0; i < 4; i++)
                newPoint.point[i] = point[i]/point[3];

            return newPoint;
            }




//*********************************************************************//
//                                                                     //
// toString() method.                                                  //
//                                                                     //
//*********************************************************************//

        public String toString()
        {
            String s = pointName + "\t";

            for (int i = 0; i < 4; i++)
            {
                s += Convert.ToString(point[i] + "\t");
            }

        return s;
        }

    }
}

I now this is kinda a long post now, but i have also uploaded my .sln file to drpbox if you would like to download it to see better what im a talking about.

http://dl.dropbox.com/u/59043100/Cube%20Drawer.zip

If anyone could help me alter this program i will be very greatful.

Thanks

I can't see anywhere in your solution where you project your 3d point to the 2d screen co-ordinates.

My 3D mathematics is a little rusty, so I will assume your matrix mathematics work correctly. I don't really have time to re-learn matrix and vector mathematics whilst I'm at work ;)

To do your projection, you need your field of view, and a viewport. Also remember that Y goes down the screen on Winforms, not up it.

This article has been dead for over six months. Start a new discussion instead.