Carlos's picture

Type Initialization Exception in the class Glu when trying to use the tessellator

Project:The Open Toolkit library
Version:1.0-beta-2
Component:Code
Category:support request
Priority:normal
Assigned:Unassigned
Status:closed
Description

I get the following exception when the line
tess = Glu.NewTess();
is executed (sorry my system is in Spanish):
"Se produjo una excepción en el inicializador de tipo de 'OpenTK.Graphics.Glu'."

I'm working with WinXP/VS2008. The same code works OK when I use the 0.9.1 version.

Details/StackTrace:

System.TypeInitializationException was unhandled
Message="Se produjo una excepción en el inicializador de tipo de 'OpenTK.Graphics.Glu'."
Source="OpenTK.Compatibility"
TypeName="OpenTK.Graphics.Glu"
StackTrace:
en OpenTK.Graphics.Glu.NewTess()
en OpenTk_test.J3DTriangulate.Triangulate(Vector2[] Input, Int32[] VertsPerContour) en C:\Documents and Settings\Carlos\Mis documentos\Visual Studio 2008\Projects\OpenTk_test\OpenTk_test\GluTessellation.cs:línea 73
en OpenTk_test.Form1..ctor() en C:\Documents and Settings\Carlos\Mis documentos\Visual Studio 2008\Projects\OpenTk_test\OpenTk_test\Form1.cs:línea 30
en OpenTk_test.Program.Main() en C:\Documents and Settings\Carlos\Mis documentos\Visual Studio 2008\Projects\OpenTk_test\OpenTk_test\Program.cs:línea 18
en System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
en System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
en Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
en System.Threading.ThreadHelper.ThreadStart_Context(Object state)
en System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
en System.Threading.ThreadHelper.ThreadStart()
InnerException: System.TypeInitializationException
Message="Se produjo una excepción en el inicializador de tipo de 'OpenTK.Graphics.GL'."
Source="OpenTK.Compatibility"
TypeName="OpenTK.Graphics.GL"
StackTrace:
en OpenTK.Graphics.GL.GetExtensionDelegate(String name, Type signature)
en OpenTK.Graphics.Glu.LoadDelegate(String name, Type signature) en C:\Users\Athina\Desktop\opentk\opentk-1.0-beta-2\Source\Compatibility\Graphics\Glu\GluHelper.cs:línea 53
en OpenTK.Graphics.Glu.LoadAll() en C:\Users\Athina\Desktop\opentk\opentk-1.0-beta-2\Source\Compatibility\Graphics\Glu\GluHelper.cs:línea 84
en OpenTK.Graphics.Glu..cctor() en C:\Users\Athina\Desktop\opentk\opentk-1.0-beta-2\Source\Compatibility\Graphics\Glu\GluHelper.cs:línea 36
InnerException: System.InvalidOperationException
Message="No GraphicsContext available in the calling thread."
Source="OpenTK.Compatibility"
StackTrace:
en OpenTK.Graphics.GL.LoadAll() en C:\Users\Athina\Desktop\opentk\opentk-1.0-beta-2\Source\Compatibility\Graphics\GL\GLHelper.cs:línea 199
en OpenTK.Graphics.GL..cctor() en C:\Users\Athina\Desktop\opentk\opentk-1.0-beta-2\Source\Compatibility\Graphics\GL\GLHelper.cs:línea 83
InnerException:


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

#1

Status:open» in progress

Thanks for the stack trace, investigating.

the Fiddler's picture

#2

Status:in progress» open

I cannot reproduce this issue here. Can you please provide a simple test case that reproduces the problem?

These lines indicate that you might be calling OpenGL methods before creating an OpenGL context (e.g. via a GameWindow or a GLControl):

InnerException: System.InvalidOperationException
Message="No GraphicsContext available in the calling thread."
the Fiddler's picture

#3

Category:bug report» support request
Status:open» closed

I am closing this issue due to lack of response but feel free to reopen if the problem persists!

snowcat99's picture

#4

Hey Fiddler,

I am experience the exact same bug as above...

Visual Studio Express 2010. Vista.

snowcat99's picture

#5

Status:closed» open

Here is some source. Taken from another post.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System;
using System.Collections.Generic;
using System.Xml;
using OpenTK.Graphics.OpenGL; //for triangulating paths with the OpenTK.Graphics.Glu tesselator
using OpenTK.Graphics;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Forms;
 
namespace WindowsFormsApplication1
{
 
 
 
 
 
 
        //
        //This class use the OpenGL OpenTK.Graphics.Glu tessellation
        //functions to triangulate a simple polygon.
        //
        //This code is based largely on Michael Foetsch
        //triangulation tutorial that can be found, as of
        //February 2, 2008, at http://www.geocities.com/foetsch/articles.htm.
        //
        //It interfaces with OpenGL through OpenTK.
        //
 
        public static class Tessellate
        {
            // Some of this code is lifted from the OpenTK tesselation sample:
            // Define the signatures for the callback functions, and declare the callbacks.
            delegate void BeginCallbackDelegate(int mode);
            delegate void EndCallbackDelegate();
            delegate void VertexCallbackDelegate(IntPtr v);
            delegate void EdgeFlagCallbackDelegate(int flag);
            delegate void ErrorCallbackDelegate(int code);
            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,
                double** dataOut);
 
            //static OpenTK.GLControl glControl = null;
 
            // Triangulate
            // "Input" is a list of Vector2's that hold the point data to be triangulated.
            //(Vector2 is not shown but is a structure that has two floating points members X and Y).
            // "VertsPerContour" is an array that determines where to break up the contours in the
            //list of points in Input. VertsPerContour must sum to the number of entries in Input.
            //
            //Note: Outer contours must be given in counter-clockwise order while inner contours
            //      (holes) must be given in clock-wise order.
            //
            //Returns true on successfull triangulation, false on failure or error
            //
 
            unsafe static public bool Triangulate(List<double> Vertexes)
            {
                OpenTK.Vector3d[] vectors = new OpenTK.Vector3d[(Vertexes.Count / 3)];
 
                for (int i = 0; i < Vertexes.Count / 3; i++)
                {
                    vectors[i].X = Vertexes[i * 3 + 0];
                    vectors[i].Y = Vertexes[i * 3 + 1];
                    vectors[i].Z = Vertexes[i * 3 + 2];
                }
                int[] size = new int[1] { vectors.Count() };
 
                return Triangulate(vectors, size);
            }
 
            unsafe static public bool Triangulate(OpenTK.Vector3d[] Input, int[] VertsPerContour)
            {
                InvalidPolygon = false;
                Indices = new List<int>();
 
                IntPtr tess = IntPtr.Zero;
 
                // create tesselation object
 
                    OpenTK.GLControl glControl = new OpenTK.GLControl();
 
 
                tess = OpenTK.Graphics.Glu.NewTess();
 
 
                // register tesselation callbacks
                OpenTK.Graphics.Glu.TessCallback(tess, OpenTK.Graphics.TessCallback.TessBegin, new BeginCallbackDelegate(BeginCallback));
                OpenTK.Graphics.Glu.TessCallback(tess, OpenTK.Graphics.TessCallback.TessEdgeFlag, new EdgeFlagCallbackDelegate(EdgeCallback));
                OpenTK.Graphics.Glu.TessCallback(tess, OpenTK.Graphics.TessCallback.TessVertex, new VertexCallbackDelegate(VertexCallback));
                OpenTK.Graphics.Glu.TessCallback(tess, OpenTK.Graphics.TessCallback.TessEnd, new EndCallbackDelegate(EndCallback));
                OpenTK.Graphics.Glu.TessCallback(tess, OpenTK.Graphics.TessCallback.TessCombine, new CombineCallbackDelegate(CombineCallback));
                OpenTK.Graphics.Glu.TessCallback(tess, OpenTK.Graphics.TessCallback.TessError, new ErrorCallbackDelegate(ErrorCallback));
 
                //copy input into a linear array of floats
                double[] Vertices = new double[3 * Input.Length];
                for (int v = 0; v < Input.Length; v++)
                {
                    Vertices[v * 3 + 0] = Input[v].X;
                    Vertices[v * 3 + 1] = Input[v].Y;
                    Vertices[v * 3 + 2] = Input[v].Z;
                }
 
                // begin polygon
                OpenTK.Graphics.Glu.TessBeginPolygon(tess, IntPtr.Zero);
 
                // go through the contours
                int CurrentContour = 0;
                int VertsThisContour = 0;
 
                for (int v = 0; v < Input.Length; v++)
                {
                    if (v == 0)
                        OpenTK.Graphics.Glu.TessBeginContour(tess);
 
                    // pass the corresponding vertex to the tesselator object
                    double[] VertsToPass = new double[3];
                    VertsToPass[0] = Vertices[v * 3 + 0];
                    VertsToPass[1] = Vertices[v * 3 + 1];
                    VertsToPass[2] = Vertices[v * 3 + 2];
                    OpenTK.Graphics.Glu.TessVertex(tess, VertsToPass, v);
 
                    if (InvalidPolygon)
                        break;
 
                    VertsThisContour++;
 
                    if (VertsThisContour >= VertsPerContour[CurrentContour])
                    {
                        VertsThisContour = 0;
                        OpenTK.Graphics.Glu.TessEndContour(tess);
 
                        CurrentContour++;
 
                        if (CurrentContour < (long)VertsPerContour.Length)
                        {
                            OpenTK.Graphics.Glu.TessBeginContour(tess);
                        }
                    }
                }
 
                if (InvalidPolygon)
                {
                    // destroy the tesselation object
                    OpenTK.Graphics.Glu.DeleteTess(tess);
                    tess = IntPtr.Zero;
 
                    return false; //error in polygon definition
                }
                else
                {
                    // end polygon
                    OpenTK.Graphics.Glu.TessEndPolygon(tess);
 
                    // destroy the tessellation object
                    OpenTK.Graphics.Glu.DeleteTess(tess);
                    tess = IntPtr.Zero;
 
                    //The Indices object is now valid.
                    return true;
                }
            }
 
            //After a successful call to Triangulate, this public list holds the indices
            //for the given input.
            public static List<int> Indices;
            static bool InvalidPolygon;
 
            //GLU callback functions
            static void BeginCallback(int type)
            {
            }
            static void EdgeCallback(int flag)
            {
            }
            static void VertexCallback(IntPtr vertexIndex)
            {
                unsafe
                {
                    Indices.Add(*((int*)vertexIndex));
                }
            }
            static void EndCallback()
            {
            }
            unsafe static void CombineCallback(double[] coords, double*[] vertexData, float[] weight, double** outData)
            {
                //This means the polygon is self-intersecting.
                //See the OpenTK tesselation example for a working version of the
                //combine callback.
                InvalidPolygon = true;
            }
            static void ErrorCallback(int errno)
            {
                //some error ocurred, mark this triangulation as invalid
                InvalidPolygon = true;
            }
 
        }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            double[] points = new double[12] { 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1 };
            List<double> oints = new List<double> { 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1 };
 
            Tessellate.Triangulate(oints);
 
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
the Fiddler's picture

#6

Status:open» closed

You need to create an OpenGL context before using any GL or Glu functions. Your code works correctly if you add a call to glControl.CreateControl() right after you construct the GLControl. This is not normally necessary when you add the GLControl directly into a Form, as the Form will create all child controls automatically - however, you need to call it when you create a bare GLControl outside the normal WinForms environment.

Closing (but please reopen if the issue perists :) )