Пример #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            //read the stl file
            objects = new ArrayList();
            StreamReader sr = new StreamReader("stl.txt");

            while (!sr.EndOfStream)
            {
                stlObj currentObj = new stlObj();
                //obj layer
                string currentLine = sr.ReadLine().ToUpper();
                if (currentLine.Contains("SOLID"))
                {
                    currentObj.name = currentLine.Substring(currentLine.IndexOf("SOLID") + 6);  //find name
                }
                else
                {
                    Console.WriteLine("invalid format error");
                }
                while (!currentLine.Contains("ENDSOLID"))
                {
                    facet curretFacet = new facet();
                    currentLine = sr.ReadLine().ToUpper();
                    if (currentLine.Contains("FACET"))
                    {
                        sr.ReadLine();//outer loop
                        //p1
                        currentLine = sr.ReadLine();
                        string[] components = currentLine.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
                        curretFacet.points[0] = new point(components[1], components[2], components[3]);


                        currentLine           = sr.ReadLine();
                        components            = currentLine.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
                        curretFacet.points[1] = new point(components[1], components[2], components[3]);


                        currentLine           = sr.ReadLine();
                        components            = currentLine.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
                        curretFacet.points[2] = new point(components[1], components[2], components[3]);
                        sr.ReadLine(); //end loop
                        sr.ReadLine(); //endfacet

                        //this is to ensure the triangle is facing the correct direction
                    }
                    else
                    {
                        Console.WriteLine("invalid format error");
                    }

                    currentObj.facets.Add(curretFacet);
                    currentObj.facetNum++;
                }
                objects.Add(currentObj);

                loaded = true;
            }
        }
Пример #2
0
        private void draw()
        {
            xPos += 0.1;
            //GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            displayBackground();

            float far = 500, near = 100f;



            OpenTK.Matrix4 RTMat = new OpenTK.Matrix4(
                rotateArr[0], rotateArr[1], rotateArr[2], translateArr[0],
                rotateArr[3], rotateArr[4], rotateArr[5], translateArr[1],
                rotateArr[6], rotateArr[7], rotateArr[8], translateArr[2],
                0, 0, 0, 1);

            OpenTK.Matrix4 reverse = new OpenTK.Matrix4(
                1, 0, 0, 0,
                0, -1, 0, 0,
                0, 0, -1, 0,
                0, 0, 0, 1);

            //not sure if needed
            OpenTK.Matrix4 landscape = new OpenTK.Matrix4(
                0, 1, 0, 0,
                -1, 0, 0, 0,
                0, 0, 1, 0,
                0, 0, 0, 1);

            OpenTK.Matrix4 projection = new OpenTK.Matrix4(
                2 * cameraPar[0] / 816f, 0, 1 - (2 * cameraPar[2] / 816f), 0,
                0, 2 * cameraPar[4] / 612f, -1 + (2 * cameraPar[5] + 2) / 612f, 0,
                0, 0, -(far + near) / (far - near), -2 * far * near / (far - near),
                0, 0, -1, 0);

            OpenTK.Matrix4 projT = new OpenTK.Matrix4(
                2 * cameraPar[0] / 816f, 0, 1 - (2 * cameraPar[2] / 816f), 0,
                0, 2 * cameraPar[4] / 612f, -1 + (2 * cameraPar[5] + 2) / 612f, 0,
                0, 0, -(far + near) / (far - near), -2 * far * near / (far - near),
                0, 0, -1, 0);

            projT.Transpose();
            OpenTK.Matrix4 mvMat    = OpenTK.Matrix4.Mult(reverse, RTMat);
            OpenTK.Matrix4 projMat2 = OpenTK.Matrix4.Mult(landscape, projection);
            OpenTK.Matrix4 mvp      = OpenTK.Matrix4.Mult(projection, mvMat);


            // render graphics

            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            GL.LoadMatrix(ref projT.Row0.X);

            mvMat.Transpose();
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
            GL.LoadMatrix(ref mvMat.Row0.X);



            GL.LineWidth(2.5f);
            GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);
            GL.Color3(Color.Green);
            GL.Begin(PrimitiveType.Lines);
            GL.Vertex3(0, 0, 0);
            GL.Vertex3(0, 0, 100);

            GL.End();

            GL.Color3(Color.Aqua);
            GL.Begin(PrimitiveType.Lines);
            GL.Vertex3(0, 0, 0);
            GL.Vertex3(0, 100, 0);


            GL.Color3(Color.Yellow);
            GL.Begin(PrimitiveType.Lines);
            GL.Vertex3(0, 0, 0);
            GL.Vertex3(100, 0, 0);

            GL.End();



            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
            GL.Enable(EnableCap.Blend);

            Color a = Color.FromArgb(160, 255, 0, 0);
            Color b = Color.FromArgb(160, 225, 25, 25);
            Color c = Color.FromArgb(160, 195, 50, 50);


            Color black = Color.FromArgb(0, 0, 0);

            GL.Enable(EnableCap.DepthTest);
            GL.DepthFunc(DepthFunction.Lequal);

            GL.LineWidth(1.0f);
            foreach (stlObj obj in objects)
            {
                object[] facetArray = obj.facets.ToArray();
                for (int i = 0; i < obj.facetNum - 1; i++)
                {
                    GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
                    Object fcet = (facet)facetArray[i];
                    GL.Begin(PrimitiveType.Triangles);
                    facet fct = (facet)fcet;

                    GL.Color4(a);
                    GL.Vertex3(fct.points[0].x + xPos, fct.points[0].y, fct.points[0].z);
                    GL.Color4(b);
                    GL.Vertex3(fct.points[1].x + xPos, fct.points[1].y, fct.points[1].z);
                    GL.Color4(c);
                    GL.Vertex3(fct.points[2].x + xPos, fct.points[2].y, fct.points[2].z);

                    GL.End();

                    /*
                     * GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);
                     * GL.Color3(black);
                     * GL.Begin(PrimitiveType.Triangles);
                     * GL.Vertex3(fct.points[0].x , fct.points[0].y, fct.points[0].z );
                     * GL.Vertex3(fct.points[1].x , fct.points[1].y, fct.points[1].z );
                     * GL.Vertex3(fct.points[2].x , fct.points[2].y, fct.points[2].z );
                     *
                     * GL.End();
                     */
                }
            }



            GL.Disable(EnableCap.Blend);

            glControl1.Refresh();// redraws and updates



            glControl1.SwapBuffers();
        }