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 = "";

int numVertices = Convert.ToInt32(strNumVertices);

vertices = new Vertex[numVertices];

for (int i = 0; i < numVertices; i++)
{
double x, y, z;
String[] strVertices = strXYZ.Split(' ');      //Split with space as a separator.

x = Convert.ToDouble(strVertices);
y = Convert.ToDouble(strVertices);
z = Convert.ToDouble(strVertices);

vertices.x = x;
vertices.y = y;
vertices.z = z;
}

int numTriangles = Convert.ToInt32(strNumTriangles);

int n, index1, index2, index3;
Npoint p1, p2, p3;

for (int i = 0; i < numTriangles; i++)
{
String[] strVertices = str123.Split(' ');      //Split with space as a separator.

index1 = Convert.ToInt32(strVertices);
index2 = Convert.ToInt32(strVertices);
index3 = Convert.ToInt32(strVertices);

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 //
//                                                                    //
//********************************************************************//

{
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 = v1;
points = v2;
points = v3;
}

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

public Npoint[] points = new Npoint;
private Npoint p1;
private Npoint p2;
private Npoint p3;

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

{

for (int i = 0; i < 3; i++)
{
}
}

//********************************************************************//
// 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, (int)points[i].point,
(int)points[i + 1].point, (int)points[i + 1].point);

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

}
}
}
``````

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 = 1;
}
}

public Npoint(double x, double y, double z)
{
point = x;  point = y; point = z; point = 1;
}

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

public Npoint(double x, double y, double z, String name)
{
point = x;  point = y; point = z; point = 1;
pointName = name;
}

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

public double[] point = new double;
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;

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

Wow..this is crazy!! I gotta try it! Thanx mate

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.