无法在我的程序中找到错误

时间:2013-04-28 10:43:06

标签: c# opengl opentk

我一直在研究一个可以解析.obj模型文件的程序但是每次运行我的程序时都会收到这个礼物:

Image of error

以下是我的课程:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void glControl1_Load(object sender, EventArgs e)
    {
        GL.ClearColor(Color.Black);
        glControl1.Paint += glControl1_Paint;
    }


    private void glControl1_Paint(object sender, PaintEventArgs e)
    {
        GL.Clear(ClearBufferMask.ColorBufferBit);
        GL.Clear(ClearBufferMask.DepthBufferBit);

        //'Basic Setup for viewing
        Matrix4d perspective = Matrix4d.CreatePerspectiveFieldOfView(1.04, 4 / 3, 1, 10000); //'Setup Perspective
        Matrix4 lookAt = Matrix4.LookAt(100, 20, 0, 0, 0, 0, 0, 1, 0);//'Setup camera
        GL.MatrixMode(MatrixMode.Projection); //'Load Perspective
        GL.LoadIdentity();
        GL.LoadMatrix(ref perspective);
        GL.MatrixMode(MatrixMode.Modelview);// 'Load Camera
        GL.LoadIdentity();
        GL.LoadMatrix(ref lookAt);
        GL.Viewport(0, 0, glControl1.Width, glControl1.Height); //'Size of window
        GL.Enable(EnableCap.DepthTest); //'Enable correct Z Drawings
        GL.DepthFunc(DepthFunction.Less); //'Enable correct Z Drawings

        //'Rotating
        GL.Rotate(float.Parse(numericUpDown1.Value.ToString()), 0, 0, 1.0f);
        GL.Rotate(float.Parse(numericUpDown2.Value.ToString()), 0, 1.0f, 0);

        GL.PolygonMode(MaterialFace.FrontAndBack,PolygonMode.Fill);

        int objectDisplayList = GL.GenLists(1);
        GL.NewList(objectDisplayList, ListMode.Compile);
        {
            Model m = null;
            m = OBJLoader.loadModel(@"\bunny.obj");

            GL.Begin(BeginMode.Triangles);

            foreach (Face face in m.faces){
                //MessageBox.Show((m.normals[0]).ToString());
                Vector3 n1 = m.normals[(int)face.normal.X - 1];
                GL.Normal3(n1.X, n1.Y, n1.Z);
                Vector3 v1 = m.vertices[(int)face.vertex.X - 1];
                GL.Vertex3(v1.X, v1.Y, v1.Z);

                Vector3 n2 = m.normals[(int)face.normal.Y - 1];
                GL.Normal3(n1.X, n1.Y, n1.Z);
                Vector3 v2 = m.vertices[(int)face.vertex.Y - 1];
                GL.Vertex3(v2.X, v2.Y, v2.Z);

                Vector3 n3 = m.normals[(int)face.normal.Z - 1];
                GL.Normal3(n1.X, n1.Y, n1.Z);
                Vector3 v3 = m.vertices[(int)face.vertex.Z - 1];
                GL.Vertex3(v3.X, v3.Y, v3.Z);
            }
            GL.End();

        }

        //Draw pyramid, Y is up, Z is twards you, X is left and right
        //'Vertex goes (X,Y,Z)
      /*  GL.Begin(BeginMode.Triangles);
        // 'Face 1
        GL.Color3(Color.Red);
        GL.Vertex3(50, 0, 0);
        GL.Color3(Color.White);
        GL.Vertex3(0, 25, 0);
        GL.Color3(Color.Blue);
        GL.Vertex3(0, 0, 50);
        //  'Face 2
        GL.Color3(Color.Green);
        GL.Vertex3(-50, 0, 0);
        GL.Color3(Color.White);
        GL.Vertex3(0, 25, 0);
        GL.Color3(Color.Blue);
        GL.Vertex3(0, 0, 50);
        //  'Face 3
        GL.Color3(Color.Red);
        GL.Vertex3(50, 0, 0);
        GL.Color3(Color.White);
        GL.Vertex3(0, 25, 0);
        GL.Color3(Color.Blue);
        GL.Vertex3(0, 0, -50);
        //   'Face 4
        GL.Color3(Color.Green);
        GL.Vertex3(-50, 0, 0);
        GL.Color3(Color.White);
        GL.Vertex3(0, 25, 0);
        GL.Color3(Color.Blue);
        GL.Vertex3(0, 0, -50);

        //   'Finish the begin mode with "end"
        GL.End();*/

        // 'Finally...
        GraphicsContext.CurrentContext.VSync = true; // 'Caps frame rate as to not over run GPU
        glControl1.SwapBuffers(); //'Takes from the 'GL' and puts into control
    }
}

class Model
{
    public IList<Vector3> vertices = new List<Vector3>();
    public IList<Vector3> normals = new List<Vector3>();
    public IList<Face> faces = new List<Face>();

}
class Face
{
    public Vector3 vertex = new Vector3(); //Three indices, not vertices or normals!
    public Vector3 normal = new Vector3();
    public Face(Vector3 vertex, Vector3 normal)
    {
        this.vertex = vertex;
        this.normal = normal;
    }
}
class OBJLoader
{
    public static Model loadModel(String f) {

        StreamReader reader = new StreamReader(f); 

    Model m = new Model();
    String line;
    while ((line = reader.ReadLine()) != null){

        if(line.StartsWith("v ")){
            float x = float.Parse(line.Split(' ')[1]);
            float y = float.Parse(line.Split(' ')[2]);
            float z = float.Parse(line.Split(' ')[3]);
            m.vertices.Add(new Vector3(x,y,z));
        }
        else if(line.StartsWith("vn ")){
            float x = float.Parse(line.Split(' ')[1]);
            float y = float.Parse(line.Split(' ')[2]);
            float z = float.Parse(line.Split(' ')[3]);
            m.vertices.Add(new Vector3(x,y,z));
        }
        else if(line.StartsWith("f ")){
            Vector3 vertexIndices = new Vector3(float.Parse(line.Split(' ')[1].Split('/')[0]),
                                                  float.Parse(line.Split(' ')[2].Split('/')[0]),
                                                  float.Parse(line.Split(' ')[3].Split('/')[0]));

            Vector3 normalIndices = new Vector3(float.Parse(line.Split(' ')[1].Split('/')[2]),
                                                  float.Parse(line.Split(' ')[2].Split('/')[2]),
                                                  float.Parse(line.Split(' ')[3].Split('/')[2]));
            m.faces.Add(new Face(vertexIndices, normalIndices));
        }

    }
    reader.Close();
    return m;
}
}

0 个答案:

没有答案