Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#和OpenTK中的循环细分_C#_Loops_Opentk - Fatal编程技术网

C#和OpenTK中的循环细分

C#和OpenTK中的循环细分,c#,loops,opentk,C#,Loops,Opentk,我正在尝试用C#实现循环细分算法。 如何跟踪发现新顶点的所有边。下面是我加载线框的Json文件的代码。我已经创建了一个新的类细分来细分网格,但它不起作用,因为存在错误。谁能告诉我如何细分网格 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Tex

我正在尝试用C#实现循环细分算法。

如何跟踪发现新顶点的所有边。下面是我加载线框的Json文件的代码。我已经创建了一个新的类细分来细分网格,但它不起作用,因为存在错误。谁能告诉我如何细分网格

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using System.IO;
using Newtonsoft.Json;

namespace MonkeySubdivision
 { 
  public partial class Form1 : Form
  {       
    Mesh[] meshes0;    
    Mesh[] meshes1;
    Mesh[] meshes2;

    bool loaded = false;

    public Form1()
    {
        InitializeComponent();
    }
    private void display()
    {

        if (!loaded)
            return;
        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
        GL.Viewport(0, 0, Width, Height);
        Matrix4 world = Matrix4.LookAt(Vector3.Zero, Vector3.UnitZ, Vector3.UnitY);
        GL.MatrixMode(MatrixMode.Modelview);
        GL.LoadMatrix(ref world);
        GL.LoadIdentity();
        GL.Translate(-0.1f, -0.4f, -5.5f);
        meshes0 = LoadJson();
        meshes1 = Subdivision.subdivsion(meshes0);

        // Subdivide the mesh depending on the numeric value

            if (numericUpDown1.Value == 1)
            {
         //   meshes1 = Subdivision.subdivsion(meshes0);
            Console.Write("Inside 1st subdivision");
            Console.WriteLine("This should be displayed");
            meshes0 = meshes1;
            }
        if (numericUpDown1.Value == 2)
        {
            Console.Write("Inside 2nd subdivision");
        }
        if (numericUpDown1.Value == 3)
        {
            Console.Write("Inside 3rd subdivision");
        }
        if (numericUpDown1.Value == 4)
        {
            Console.Write("Inside 4th subdivision");
        }
        if (numericUpDown1.Value == 5)
        {
            Console.Write("Inside 5th subdivision");
        }

        int vertcount = getnumvertices(meshes0);
        label2.Text = vertcount.ToString();
        int facecount = getnumfaces(meshes0);
        label4.Text = facecount.ToString();


        // Code To Display the triangles on screen
        foreach (Mesh mesh in meshes0)
         {
            foreach (var face in mesh.Faces)
            {                  
                GL.Begin(PrimitiveType.Triangles);
                GL.Color3(Color.Yellow);
                GL.Vertex3(mesh.Vertices[face.A]);
                GL.Vertex3(mesh.Vertices[face.B]);
                GL.Vertex3(mesh.Vertices[face.C]);
                GL.End();
            }

            GL.PolygonMode(MaterialFace.Front, PolygonMode.Line);
            GL.PolygonMode(MaterialFace.Back, PolygonMode.Line);
            GL.Flush();
            glControl1.SwapBuffers();
       }
    }

    //Number of faces in a Mesh
    private int getnumfaces(Mesh[] meshsub)
    {
        int count = 0;
        foreach (Mesh mesh in meshsub)
        {
            foreach (var face in mesh.Faces)
                count = count + 1;
        }
        return count;

    }

    //Number of vertices in a Mesh
    private int getnumvertices(Mesh[] meshsub)
    {
        int count = 0;
        foreach (Mesh mesh in meshsub)
        {
            foreach (var face in mesh.Vertices)
                count = count + 1;
        }
        return count;
    }




    private void glControl1_Resize(object sender, EventArgs e)
    {
        GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);

        Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, Width / (float)Height, 1.0f, 64.0f);

        GL.MatrixMode(MatrixMode.Projection);

        GL.LoadMatrix(ref projection);
        SetupViewport();
        glControl1.Invalidate();

    }
    private void glControl1_Paint(object sender, PaintEventArgs e)
    {

        display();

    }

    private void glControl1_Load(object sender, EventArgs e)
    {
        loaded = true;
        GL.ClearColor(Color.Black);
        GL.ShadeModel(ShadingModel.Smooth);
        GL.ClearColor(Color.Black);
        GL.ClearDepth(1.0f);
        GL.Enable(EnableCap.DepthTest);
        GL.DepthFunc(DepthFunction.Lequal);
        GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
        SetupViewport();
        Application.Idle += Application_Idle; // press TAB twice after +=
    }
    void Application_Idle(object sender, EventArgs e)
    {
        // no guard needed -- we hooked into the event in Load handler
        while (glControl1.IsIdle)
        {
            display();
            SetupViewport();
        }
    }
    private void SetupViewport()
    {
        int w = glControl1.Width;
        int h = glControl1.Height;
        GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);

        Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, w / (float)h, 1.0f, 64.0f);

        GL.MatrixMode(MatrixMode.Projection);

        GL.LoadMatrix(ref projection);

        GL.Viewport(0, 0, w, h); // Use all of the glControl painting area
    }

    // JSON file parser
    private Mesh[] LoadJson()
    {
        var meshes = new List<Mesh>();
        using (StreamReader r = new StreamReader("cube.babylon"))
        {
            string json = r.ReadToEnd();
            dynamic jsonObject = JsonConvert.DeserializeObject(json);

            for (var meshIndex = 0; meshIndex < jsonObject.meshes.Count; meshIndex++)
            {
                var verticesArray = jsonObject.meshes[meshIndex].vertices;
                // Faces
                var indicesArray = jsonObject.meshes[meshIndex].indices;

                var uvCount = jsonObject.meshes[meshIndex].uvCount.Value;
                var verticesStep = 1;

                // Depending of the number of texture's coordinates per vertex
                // we're jumping in the vertices array  by 6, 8 & 10 windows frame
                switch ((int)uvCount)
                {
                    case 0:
                        verticesStep = 6;
                        break;
                    case 1:
                        verticesStep = 8;
                        break;
                    case 2:
                        verticesStep = 10;
                        break;
                }
                // the number of interesting vertices information for us
                var verticesCount = verticesArray.Count / verticesStep;
                // number of faces is logically the size of the array divided by 3 (A, B, C)
                var facesCount = indicesArray.Count / 3;
                var mesh = new Mesh(jsonObject.meshes[meshIndex].name.Value, verticesCount, facesCount);

                // Filling the Vertices array of our mesh first
                for (var index = 0; index < verticesCount; index++)
                {
                    var x = (float)verticesArray[index * verticesStep].Value;
                    var y = (float)verticesArray[index * verticesStep + 1].Value;
                    var z = (float)verticesArray[index * verticesStep + 2].Value;
                    mesh.Vertices[index] = new Vector3(x, y, z);
                }

                // Then filling the Faces array
                for (var index = 0; index < facesCount; index++)
                {
                    var a = (int)indicesArray[index * 3].Value;
                    var b = (int)indicesArray[index * 3 + 1].Value;
                    var c = (int)indicesArray[index * 3 + 2].Value;
                    mesh.Faces[index] = new Face { A = a, B = b, C = c };
                }
                // Getting the position you've set in Blender
                var position = jsonObject.meshes[meshIndex].position;
                mesh.Position = new Vector3((float)position[0].Value, (float)position[1].Value, (float)position[2].Value);
                meshes.Add(mesh);
            }
            return meshes.ToArray();

        }
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        glControl1.Resize += new EventHandler(glControl1_Resize);
        meshes0 = LoadJson();
        meshes1 = Subdivision.subdivsion(meshes0);

    }

    private void numericUpDown1_ValueChanged(object sender, EventArgs e)
    {         

     }


   }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用OpenTK;
使用OpenTK.Graphics;
使用OpenTK.Graphics.OpenGL;
使用System.IO;
使用Newtonsoft.Json;
名称空间MonkeySubdivision
{ 
公共部分类Form1:Form
{       
网格[]网格0;
网格[]网格1;
网格[]网格2;
bool-loaded=false;
公共表格1()
{
初始化组件();
}
专用void display()
{
如果(!已加载)
回来
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
总图视口(0,0,宽度,高度);
Matrix4-world=Matrix4.LookAt(Vector3.Zero,Vector3.UnitZ,Vector3.UnitY);
GL.MatrixMode(MatrixMode.Modelview);
总帐负荷矩阵(参考世界);
GL.LoadIdentity();
GL.平移(-0.1f,-0.4f,-5.5f);
meshes0=LoadJson();
meshes1=细分。细分(meshes0);
//根据数值细分网格
if(numericUpDown1.Value==1)
{
//meshes1=细分。细分(meshes0);
控制台。写入(“第1分区内部”);
Console.WriteLine(“应显示此项”);
网格0=网格1;
}
if(numericUpDown1.Value==2)
{
控制台。写入(“第二分区内部”);
}
如果(numericUpDown1.Value==3)
{
控制台。写入(“第三分区内部”);
}
如果(numericUpDown1.Value==4)
{
控制台。写入(“第四分区内部”);
}
如果(numericUpDown1.Value==5)
{
控制台。写入(“第五分区内部”);
}
int vertcount=getnumvertices(网格0);
label2.Text=vertcount.ToString();
int facecount=getnumfaces(meshes0);
label4.Text=facecount.ToString();
//在屏幕上显示三角形的代码
foreach(网格中的网格0)
{
foreach(网格面中的变量面)
{                  
GL.Begin(基本类型三角形);
GL.Color3(颜色为黄色);
GL.Vertex3(网格顶点[face.A]);
GL.Vertex3(网格顶点[face.B]);
GL.Vertex3(网格顶点[face.C]);
GL.End();
}
GL.PolygonMode(MaterialFace.Front,PolygonMode.Line);
GL.PolygonMode(MaterialFace.Back,PolygonMode.Line);
GL.Flush();
glControl1.SwapBuffers();
}
}
//网格中的面数
私有int getnumfaces(Mesh[]meshsub)
{
整数计数=0;
foreach(meshsub中的网格)
{
foreach(网格面中的变量面)
计数=计数+1;
}
返回计数;
}
//网格中的顶点数
私有int getnumvertices(Mesh[]meshsub)
{
整数计数=0;
foreach(meshsub中的网格)
{
foreach(网格顶点中的变量面)
计数=计数+1;
}
返回计数;
}
私有void glControl1_Resize(对象发送方,事件参数e)
{
GL.视口(ClientRectangle.X、ClientRectangle.Y、ClientRectangle.Width、ClientRectangle.Height);
Matrix4 projection=Matrix4.CreatePerspectiveFieldOfView((float)Math.PI/4,宽度/(float)高度,1.0f,64.0f);
GL.MatrixMode(MatrixMode.Projection);
总帐负荷矩阵(参考投影);
SetupViewport();
glControl1.Invalidate();
}
私有void glControl1_Paint(对象发送器,PaintEventArgs e)
{
显示();
}
私有void glControl1_加载(对象发送方,事件参数e)
{
加载=真;
GL.ClearColor(颜色为黑色);
GL.ShadeModel(ShadingModel.Smooth);
GL.ClearColor(颜色为黑色);
GL.净空深度(1.0f);
总账启用(启用CAP.深度测试);
总账DepthFunc(DepthFunction.Lequal);
GL.Hint(HintTarget.PerspectiveCorrectionHint,HintMode.Nicest);
SetupViewport();
Application.Idle+=Application\u Idle;//之后按TAB两次+=
}
无效应用程序\u空闲(对象发送器、事件参数)
{
//不需要保护——我们在LoadHandler中钩住了事件
while(glControl1.IsIdle)
{
显示();
SetupViewport();
}
}
私有void SetupViewport()
{
int w=glControl1.宽度;
int h=glControl1.高度;
GL.视口(ClientRectangle.X、ClientRectangle.Y、ClientRectangle.Width、ClientRectangle.Height);
Matrix4 projection=Matrix4.CreatePerspectiveFieldOfView((float)Math.PI/4,w/(float)h,1.0f,64.0f);
GL.MatrixMode(MatrixMode.Projection);
总帐负荷矩阵(参考投影);
GL.Viewport(0,0,w,h);//使用所有glControl绘制区域
}
//JSON文件解析器
私有网格[]加载JSON()
{
var mesh=新列表();
使用(StreamReader r=新StreamReader(“cube.babylon”))
{
字符串json=r.ReadToEnd();
动态jsonObject=JsonConvert.DeserializeObject(json);
对于(var-meshIndex=0;meshIndex using OpenTK;
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Collections;

 namespace MonkeySubdivision
{
class Subdivision
{
    public static Mesh[] subdivsion(Mesh[] meshsub)
    {
        var meshes = new List<Mesh>();
        //Vertices of a mesh
        //Hashtable edges = new Hashtable();


        int verticescount = getnumvertices(meshsub);
        Console.WriteLine(verticescount);
        int facecount = getnumfaces(meshsub);
        int edgecount = verticescount + facecount - 2;
        int newvercount = verticescount + edgecount;
        int newfacecount = facecount * 4;
        Vector3[] NewVertices = new Vector3[newvercount];
        var meshnew = new Mesh("subdmesh", newvercount, newfacecount);


        foreach (Mesh mesh in meshsub)
        {

            //for (var j = 0; j < verticescount; j++)
            //{
            //    Console.WriteLine(mesh.Vertices[j]);
            //    NewVertices[j] = mesh.Vertices[j];
            //}



            foreach (Mesh mesh2 in meshsub)
            {
                //for (var index = 0; index < facecount; index++)
                //{
                //    foreach (var faces in mesh.Faces)
                //    {
                //        meshnew.Faces[index] = mesh.Faces[index];
                //    }
                //}
                int i = 0;
                foreach (var face in mesh.Faces)
                {
                    var P0 = face.A;
                    var P1 = face.B;
                    var P2 = face.C;
                    Console.WriteLine("Faces");
                    Console.WriteLine(P0);
                    Console.WriteLine(P1);
                    Console.WriteLine(P2);

                     NewVertices[i] = getfourthvert(P0, P1, P2, meshsub);
                     NewVertices[i + 1 ] = getfourthvert(P1, P2, P0, meshsub);
                     NewVertices[i + 2] = getfourthvert(P2, P0, P1,meshsub);
                     i = i + 3;





                    for (var index = verticescount; index < newvercount; index++)
                    {
                        meshnew.Vertices[index] = NewVertices[index];
                    }
                    /*         for(var index = facecount; index < newfacecount; index++)
                             {
                                 var a = face.A;
                                 var b = (int)indicesArray[index * 3 + 1].Value;
                                 var c = (int)indicesArray[index * 3 + 2].Value;
                                 mesh.Faces[index] = new Face { A = a, B = b, C = c };
                             }*/


                    meshes.Add(meshnew);
                }


                int n = 6;
                double num = (3.0 + 2.0 * Math.Cos(2.0 * Math.PI / n));
                double beta = 1.0 / n * (5.0 / 8.0 - num * num / 64.0);

            }
            }
            return meshes.ToArray();
        }


    private static int getnumfaces(Mesh[] meshsub)
    {
        int count = 0;
        foreach (Mesh mesh in meshsub)
        {
            foreach (var face in mesh.Faces)
                count = count + 1;
        }
        return count;

    }

    private static int getnumvertices(Mesh[] meshsub)
    {
        int count = 0;
        foreach (Mesh mesh in meshsub)
        {
            foreach (var vert in mesh.Vertices)
                count = count + 1;
        }
        return count;
    }

    private static Vector3 getfourthvert(int X0, int X1, int X2, Mesh[] meshsub)
    {
        int X3;
        Vector3 V4 = new Vector3(0, 0, 0);
        foreach (Mesh mesh in meshsub)
            {

            foreach (var face2 in mesh.Faces)
             {
                var V0 = mesh.Vertices[X0];
                var V1 = mesh.Vertices[X1];
                var V2 = mesh.Vertices[X2];
                var V3 = mesh.Vertices[0];

                if ((X0 == face2.A) && (X1 == face2.B))
                {
                    var temp = face2.C;

                    if (temp != X2)
                    {
                        X3 = temp;
                        V3 = mesh.Vertices[X3];
                        V4 = (3 * V0 + 3 * V1 + V2 + V3) / 8;
                    }

                }

            }

        }

        Console.WriteLine(V4);
        return V4;
    }



}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace WindowsFormsApplication1
{

    public class Mesh
    {
        static List<Mesh> meshes { get; set; }
        static List<PolyGon> polygons { get; set; }
    }
    public class PolyGon
    {
        List<Edge> edges { get; set; } 
    }
    public class Edge
    {
        List<PointF> points { get; set; }  //two points
        List<PolyGon> parents { get; set; } // two parents
    }
}
​