C# and volume render with glTexImage3D

Hi

Can anyone provide me with any example of volume rendering in C# using glTexImage3D method? I’ve tried to do it myself, but stucked with exception, that nothing says to me… :sorrow:

I’m using Visual Studio 2008 (Windows XP), tried Tao, csgl and NeHe codes…
I’m sure to have support of volume render. My video card is nVidia Quadro FX 1600M.

I found the http://gpwiki.org/index.php/OpenGL:Tutorials:3D_Textures , which is something I need, but I couldn’t rewrite it to C#…

I’d be really grateful for any help.

PS.

Hello to everyone! :slight_smile:

With what kind an exception do you have stucked? And on what code?

Exception:

System.Reflection.TargetInvocationException was unhandled
  Message="Obiekt docelowy wywołania zgłosił wyjątek."
  Source="mscorlib"
  StackTrace:
       w System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
       w System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
       w System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
       w System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       w System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
       w System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args)
       w NeHe.NeHe.SelectExample() w C:\Program Files\TaoFramework\source\examples\NeHe\NeHe.cs:wiersz 73
       w NeHe.NeHe.lstExamples_SelectedIndexChanged(Object sender, EventArgs e) w C:\Program Files\TaoFramework\source\examples\NeHe\NeHe.cs:wiersz 79
       w System.Windows.Forms.Control.OnDoubleClick(EventArgs e)
       w System.Windows.Forms.ListBox.WndProc(Message& m)
       w System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       w System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       w System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       w System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       w System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       w System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       w System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       w System.Windows.Forms.Application.Run(Form mainForm)
       w NeHe.Program.Main() w C:\Program Files\TaoFramework\source\examples\NeHe\Program.cs:wiersz 44
       w System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       w System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       w Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       w System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       w System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       w System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
       Message="BŁĄD!
   w Tao.OpenGl.Gl.Imports.ActiveTexture(Int32 texture)
   w Tao.OpenGl.Gl.glActiveTexture(Int32 texture)
   w NeHe.Lesson05.build_texture() w C:\\Program Files\\TaoFramework\\source\\examples\\NeHe\\Lesson05.cs:wiersz 460"
       Source="NeHe"
       StackTrace:
            w NeHe.Lesson05.build_texture() w C:\Program Files\TaoFramework\source\examples\NeHe\Lesson05.cs:wiersz 476
            w NeHe.Lesson05.InitGL() w C:\Program Files\TaoFramework\source\examples\NeHe\Lesson05.cs:wiersz 420
            w NeHe.Lesson05.CreateGLWindow(String title, Int32 width, Int32 height, Int32 bits, Boolean fullscreenflag) w C:\Program Files\TaoFramework\source\examples\NeHe\Lesson05.cs:wiersz 317
            w NeHe.Lesson05.Run() w C:\Program Files\TaoFramework\source\examples\NeHe\Lesson05.cs:wiersz 136
       InnerException: 

I was editing some NeHe tutorials.
Message = “Target object of the call throws an exception”

Debugger report it here:

private void SelectExample()
        {
            if (lstExamples.SelectedItem != null)
            {
                Type example = Assembly.GetExecutingAssembly().GetType("NeHe." + lstExamples.SelectedItem.ToString(), true, true);
                example.InvokeMember("Run", BindingFlags.InvokeMethod, null, null, null); /*Highlighted by debugger*/
            }
        }

but after investigation I found that problem is line when i try to generate texture3d:


Gl.glTexImage3D(Gl.GL_TEXTURE_3D, 0, (int)Gl.GL_RGB8, 1, 1, 1, 1, Gl.GL_BGR_EXT, Gl.GL_UNSIGNED_BYTE, IntPtr.Zero);

I hope all parameters are alright… Otherwise exception would point it.

But anyway I would prefer just a simple example of glTexImage3D.

Thanks for replying!

Are you sure it is glTexImage3D which is guilty?
Your exception has following information on InnerException member:

       Message="BŁĄD!
   w Tao.OpenGl.Gl.Imports.ActiveTexture(Int32 texture)
   w Tao.OpenGl.Gl.glActiveTexture(Int32 texture)
   w NeHe.Lesson05.build_texture() w C:\\Program Files\\TaoFramework\\source\\examples\\NeHe\\Lesson05.cs:wiersz 460"
       Source="NeHe"
       StackTrace:
            w NeHe.Lesson05.build_texture() w C:\Program Files\TaoFramework\source\examples\NeHe\Lesson05.cs:wiersz 476

What is on 476 line in Lesson05.cs ?
I think you have there some problem with glActiveTexture call.

Oh… sorry… I’v been doing some changes and put wrong exception stack trace… :stupid:

This one comes exacly when I uncomment the line with glTexImage3D


System.Reflection.TargetInvocationException was unhandled
  Message="Obiekt docelowy wywołania zgłosił wyjątek."
  Source="mscorlib"
  StackTrace:
       w System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
       w System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
       w System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
       w System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       w System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
       w System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args)
       w NeHe.NeHe.SelectExample() w C:\Program Files\TaoFramework\source\examples\NeHe\NeHe.cs:wiersz 73
       w NeHe.NeHe.lstExamples_SelectedIndexChanged(Object sender, EventArgs e) w C:\Program Files\TaoFramework\source\examples\NeHe\NeHe.cs:wiersz 79
       w System.Windows.Forms.Control.OnDoubleClick(EventArgs e)
       w System.Windows.Forms.ListBox.WndProc(Message& m)
       w System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       w System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       w System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       w System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       w System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       w System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       w System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       w System.Windows.Forms.Application.Run(Form mainForm)
       w NeHe.Program.Main() w C:\Program Files\TaoFramework\source\examples\NeHe\Program.cs:wiersz 44
       w System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       w System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       w Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       w System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       w System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       w System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
       Message="BŁĄD!
   w Tao.OpenGl.Gl.Imports.TexImage3D(Int32 target, Int32 level, Int32 internalformat, Int32 width, Int32 height, Int32 depth, Int32 border, Int32 format, Int32 type, IntPtr pixels)
   w Tao.OpenGl.Gl.glTexImage3D(Int32 target, Int32 level, Int32 internalformat, Int32 width, Int32 height, Int32 depth, Int32 border, Int32 format, Int32 type, IntPtr pixels)
   w NeHe.Lesson05.build_texture() w C:\\Program Files\\TaoFramework\\source\\examples\\NeHe\\Lesson05.cs:wiersz 461"
       Source="NeHe"
       StackTrace:
            w NeHe.Lesson05.build_texture() w C:\Program Files\TaoFramework\source\examples\NeHe\Lesson05.cs:wiersz 476
            w NeHe.Lesson05.InitGL() w C:\Program Files\TaoFramework\source\examples\NeHe\Lesson05.cs:wiersz 420
            w NeHe.Lesson05.CreateGLWindow(String title, Int32 width, Int32 height, Int32 bits, Boolean fullscreenflag) w C:\Program Files\TaoFramework\source\examples\NeHe\Lesson05.cs:wiersz 317
            w NeHe.Lesson05.Run() w C:\Program Files\TaoFramework\source\examples\NeHe\Lesson05.cs:wiersz 136
       InnerException: 

Realy noone have done some 3d texturing??

OK, I manage to compile program with TexImage3D in C++, but I realy hope somehow it’s possible also in C#. Any ideas?

Maybe the problem is the last parameter, texel data, IntPtr pixels? But would it throw different exception?

Sorry, szamil, but what was your question again?

There’s lots of info on interop marshaling in the MSDN docs if you’re having trouble in that area. There are also several open source .NET libraries that wrap all this stuff up for you, like Tao.

Make sure you are using the latest version of Tao.OpenGl.dll (2.1.0.12, IIRC), because older ones didn’t marshal TexImage3D correctly.