[ create a new paste ] login | about

Link: http://codepad.org/P4gDhN2T    [ raw code | fork ]

Plain Text, pasted on Jun 3:
using Microsoft.DirectX;

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;

using OpenTK;
using OpenTK.Graphics;

namespace HopefullyIWillTesselate
{
    class Program
    {


        static void Main(string[] args)
        {

            Tesselator tesse = new Tesselator();
            

            tesse.BeginPolygon();
            tesse.BeginContour();
            tesse.AddVertex(new Vector3(50.0f, 50.0f, 0.0f));
            tesse.AddVertex(new Vector3(200.0f, 50.0f, 0.0f));
            tesse.AddVertex(new Vector3(200.0f, 200.0f, 0.0f));
            tesse.AddVertex(new Vector3(50.0f, 200.0f, 0.0f));
            tesse.EndContour();
            tesse.BeginContour();
            tesse.AddVertex(new Vector3(75.0f, 75.0f, 0.0f));
            tesse.AddVertex(new Vector3(125.0f, 175.0f, 0.0f));
            tesse.AddVertex(new Vector3(175.0f, 75.0f, 0.0f));
            //tesse.AddVertex(new Vector3(250.0f, 50.0f, 0.0f));
            //tesse.AddVertex(new Vector3(325.0f, 200.0f, 0.0f));
            //tesse.AddVertex(new Vector3(400.0f, 50.0f, 0.0f));
            //tesse.AddVertex(new Vector3(250.0f, 150.0f, 0.0f));
            //tesse.AddVertex(new Vector3(400.0f, 150.0f, 0.0f));
            tesse.EndContour();
            tesse.EndPolygon();

            tesse.PrintVertices();
            tesse.PrintIndicies();

            

           

            tesse.Dispose();
        }
    }

    public class Tesselator : IDisposable
    {
        private OpenTK.GLControl openTK;
        private IntPtr tess;

        public int index = 0;

        private IList<Vector4> vertices;
        private IList<int> indices;

        delegate void BeginCallbackDelegate(BeginMode mode);
        delegate void EndCallbackDelegate();
        delegate void VertexCallbackDelegate(IntPtr v);
        delegate void ErrorCallbackDelegate(GluErrorCode code);
        delegate void EdgeFlagCallbackDelegate(bool flag);
        unsafe delegate void CombineCallbackDelegate(
            [MarshalAs(UnmanagedType.LPArray, SizeConst = 3)]double[] coordinates,
            [MarshalAs(UnmanagedType.LPArray, SizeConst = 4)]double*[] vertexData,
            [MarshalAs(UnmanagedType.LPArray, SizeConst = 4)]float[] weight,
            int* dataOut);

        BeginCallbackDelegate tessBegin;
        EndCallbackDelegate tessEnd;
        ErrorCallbackDelegate tessError;
        VertexCallbackDelegate tessVertex;
        EdgeFlagCallbackDelegate tessEdgeFlag;
        CombineCallbackDelegate tessCombine;

        public Tesselator()
        {
            vertices = new List<Vector4>();
            indices = new List<int>();

            Console.WriteLine("Initializing Tesselator!!!!");

            tessVertex = this.VertexHandler;
            tessBegin = this.BeginHandler;
            tessEnd = this.EndHandler;
            tessError = this.ErrorHandler;
            tessEdgeFlag = this.EdgeFlagHandler;
            unsafe { tessCombine = this.CombineHandler; }

            Trace.Assert(tessVertex != null, "Failed to load tesselator callback function.");
            Trace.Assert(tessBegin != null, "Failed to load tesselator begin callback function.");
            Trace.Assert(tessEnd != null, "Failed to load tesselator end callback function.");
            Trace.Assert(tessError != null, "Failed to load tesselator error callback function.");
            Trace.Assert(tessCombine != null, "Failed to load tesselator combine callback function.");

            openTK = new GLControl();
            tess = Glu.NewTess();

            Glu.TessCallback(tess, TessCallback.TessVertex, tessVertex);
            Glu.TessCallback(tess, TessCallback.TessBegin, tessBegin);
            Glu.TessCallback(tess, TessCallback.TessEnd, tessEnd);
            Glu.TessCallback(tess, TessCallback.TessError, tessError);
            Glu.TessCallback(tess, TessCallback.TessEdgeFlag, tessEdgeFlag);
            Glu.TessCallback(tess, TessCallback.TessCombine, tessCombine);
        }

        public void PrintVertices()
        {
            foreach (Vector4 vec in vertices)
            {
                Console.WriteLine(vec.ToString());
            }
        }

        public void PrintIndicies()
        {
            foreach (int inde in indices)
            {
                Console.Write(inde.ToString() + " ");            
            }
            Console.WriteLine(" ");
        }

        void EdgeFlagHandler(bool flag)
        {

        }

        void BeginHandler(BeginMode mode)
        {
            
        }

        void EndHandler()
        {
            
        }

        void VertexHandler(IntPtr v)
        {
            Console.WriteLine("Did we ever get here?");
            int value = Marshal.ReadInt32(v);

            indices.Add(value);
            //Console.WriteLine
        }

        void ErrorHandler(GluErrorCode code)
        {
            System.Windows.Forms.MessageBox.Show(
                String.Format("GLU Error {0}: {1}", code.ToString(), Glu.ErrorString(code)),
                "An error occured while tesselating.");
            //this.Exit();
        }

        //unsafe double*[] combineData;
        //int data_index = 0;
        unsafe void CombineHandler(double[] coordinates, double*[] data, float[] weight, int* dataOut)
        {
            // Workaround Mono 1.2.6 bug with unsafe inline initializers
            /*if (combineData == null)
                combineData = new double*[16];

            double* out_data = combineData[data_index] = (double*)Marshal.AllocHGlobal(6 * sizeof(double));
            int i;

            out_data[0] = coordinates[0];
            out_data[1] = coordinates[1];
            out_data[2] = coordinates[2];

            for (i = 3; i < 6; i++)
            {
                double* real_data = (double*)data[i - 3];
                out_data[i] = weight[0] * real_data[0] +
                              weight[1] * real_data[1] +
                              weight[2] * real_data[2] +
                              weight[3] * real_data[3];
            }
            data_index++;*/

            int newIndex = index;
            index++;

            Vector4 newVert = new Vector4((float)coordinates[0], (float)coordinates[1], (float)coordinates[2], (float)newIndex);
            vertices.Add(newVert);

            dataOut =  &newIndex;

            //int flossy = *dataOut;

            //Console.WriteLine(flossy.ToString());

            //index++;
        }

 

        public void BeginPolygon()
        {
            Glu.TessBeginPolygon(tess, IntPtr.Zero);
        }

        public void EndPolygon()
        {
            Trace.Assert(tessCombine != null, "Failed to load tesselator combine callback function.");
            Glu.TessEndPolygon(tess);
        }

        public void BeginContour()
        {
            Glu.TessBeginContour(tess);
        }

        public void EndContour()
        {
            Glu.TessEndContour(tess);
        }

        public void AddVertex(Vector3 vert)
        {
            Vector4 newVert = new Vector4(vert.X, vert.Y, vert.Z, (float)index);
            vertices.Add(newVert);
            double[] input = new double[3];

            input[0] = (double)vert.X;
            input[1] = (double)vert.Y;
            input[2] = (double)vert.Z;

            //Console.WriteLine("THE VALUE PASSED HERE IS " + index.ToString());

            Glu.TessVertex(tess, input, (int)index);
            index++;

        }

        #region IDisposable Members

        public void Dispose()
        {
            Glu.DeleteTess(tess);
            openTK.Dispose();
            //throw new NotImplementedException();
        }

        #endregion
    }
}



Create a new paste based on this one


Comments: