查看: 9821|回复: 9

osgswig-csharp(C#)

[复制链接]

该用户从未签到

发表于 2013-3-12 22:21:21 | 显示全部楼层 |阅读模式
本帖最后由 reziarfg 于 2013-3-13 21:50 编辑

使用osgswig生成了OpenSceneGraph3.0.1的C#代码,结果如下,欢迎讨论。

  • 嵌套类
未全部支持
  • 多重继承
C#不支持多重继承,通过接口继承或多重内部代理指针实现,代码量大。
  • 操作符重载
C#和C++支持的操作符不尽相同,C#不支持的部分通过函数实现。
  • C++友元
不支持
  • 模板类
支持
  • 名字空间
C#与C++使用的名字空间相同
  • 智能指针
通过在构造函数中调用Reference.ref(),Dispose()中调用Reference.unref()支持osg的智能指针。
  • C++回调
未支持

示例应用程序:
osgFormExample.PNG

OSGWinInitializer.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Diagnostics;
  5. using System.Windows.Forms;

  6. namespace osgFormExample
  7. {
  8.     public class OSGWinInitializer
  9.     {
  10.         /// <summary>
  11.         /// 初始化控件,使其作为osgViewer.Viewer的输出窗口
  12.         /// </summary>
  13.         /// <param name="viewer">osgViewer.Viewer</param>
  14.         /// <param name="ctrl">控件</param>
  15.         /// <returns>是否成功</returns>
  16.         public static bool Init(osgViewer.Viewer viewer, Control ctrl)
  17.         {
  18.             try
  19.             {
  20.                 osgViewer.GraphicsWindowWin32.Traits traits = new osgViewer.GraphicsWindowWin32.Traits();

  21.                 Debug.Assert(viewer != null);

  22.                 osgViewer.GraphicsWindowWin32.WindowData windata = new osgViewer.GraphicsWindowWin32.WindowData(ctrl.Handle.ToInt32(),true);

  23.                 traits.x = 0;
  24.                 traits.y = 0;
  25.                 traits.width = ctrl.Width;
  26.                 traits.height = ctrl.Height;
  27.                 // Setup the traits parameters
  28.                 traits.windowDecoration = false;
  29.                 traits.doubleBuffer = true;
  30.                 traits.setInheritedWindowPixelFormat = true;
  31.                 traits.inheritedWindowData = windata;
  32.                 traits.quadBufferStereo = false; // stereo

  33.                 // Create the Graphics Context
  34.                 osg.GraphicsContext gc = osg.GraphicsContext.createGraphicsContext(traits);

  35.                 // Init a new Camera (Master for this View)
  36.                 osg.Camera camera = viewer.asView().getCamera();

  37.                 // Assign Graphics Context to the Camera
  38.                 camera.setGraphicsContext(gc);

  39.                 // Set the viewport for the Camera
  40.                 osg.Viewport vp = new osg.Viewport(traits.x, traits.y, traits.width, traits.height);
  41.                 camera.setViewport(vp);
  42.                 camera.setProjectionMatrixAsPerspective(30.0, (double)vp.getWidth() / (double)vp.getHeight(), 0.1, 1000.0);

  43.                 // don't exit if press ESC key
  44.                 viewer.setQuitEventSetsDone(false);
  45.                 viewer.setKeyEventSetsDone(9999); // 屏蔽任何键

  46.                 // Realize the Viewer
  47.                 viewer.realize();
  48.                 return true;
  49.             }
  50.             catch (System.Exception ex)
  51.             {
  52.                 MessageBox.Show(ex.Message);
  53.                 return false;
  54.             }
  55.         }


  56.     }
  57. }
复制代码
BaseOSGControl.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;

  5. namespace osgFormExample
  6. {
  7.     /// <summary>
  8.     /// 示例控件
  9.     /// </summary>
  10.     public class BaseOSGControl : UserControl
  11.     {
  12.         #region Members

  13.         Timer _timer = new Timer();

  14.         osgViewer.Viewer _viewer = new osgViewer.Viewer();

  15.         public osgViewer.Viewer Viewer
  16.         {
  17.             get { return _viewer; }
  18.             set { _viewer = value; }
  19.         }
  20.         #endregion // Members

  21.         public BaseOSGControl()
  22.         {
  23.             InitializeComponent();
  24.         }

  25.         void _timer_Tick(object sender, EventArgs e)
  26.         {
  27.             lock(_viewer){
  28.             _viewer.frame();
  29.             }
  30.         }

  31.         private void InitializeComponent()
  32.         {
  33.             this.SuspendLayout();
  34.             //
  35.             // BaseOSGControl
  36.             //
  37.             this.BackColor = System.Drawing.Color.Green;
  38.             this.Name = "BaseOSGControl";
  39.             this.Size = new System.Drawing.Size(605, 424);
  40.             this.Load += new System.EventHandler(this.BaseOSGControl_Load);
  41.             this.ResumeLayout(false);
  42.         }

  43.         private void BaseOSGControl_Load(object sender, EventArgs e)
  44.         {
  45.             InitOSG();

  46.             _timer.Interval = 10;
  47.             _timer.Enabled = true;
  48.             _timer.Tick += new EventHandler(_timer_Tick);
  49.         }

  50.         private void InitOSG()
  51.         {
  52.             OSGWinInitializer.Init(_viewer, this);
  53.             osgGA.CameraManipulator cam = new osgGA.TrackballManipulator();
  54.             _viewer.setCameraManipulator(cam, true);
  55.             _viewer.addEventHandler(new osgViewer.HelpHandler());
  56.             _viewer.addEventHandler(new osgViewer.StatsHandler());
  57.         }

  58.         /// <summary>
  59.         /// Call this when this control is closing
  60.         /// </summary>
  61.         public void Destroy()
  62.         {
  63.             _viewer.setDone(true);
  64.             _viewer.Dispose();
  65.         }

  66.     }
  67. }
复制代码
SampleForm.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;

  8. namespace osgFormExample
  9. {
  10.     public partial class SampleForm : Form
  11.     {
  12.         public SampleForm()
  13.         {
  14.             InitializeComponent();
  15.         }

  16.         BaseOSGControl _ctrl = new BaseOSGControl();

  17.         private void Form1_Load(object sender, EventArgs e)
  18.         {
  19.             _ctrl = new BaseOSGControl();
  20.             this.panel1.Controls.Add(_ctrl);
  21.             _ctrl.Dock = DockStyle.Fill;
  22.             // sample model
  23.             shapesToolStripMenuItem_Click(this,null);
  24.         }

  25.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  26.         {
  27.             _ctrl.Destroy();
  28.         }

  29.         private void openOToolStripMenuItem_Click(object sender, EventArgs e)
  30.         {
  31.             if (openFileDialog1.ShowDialog() == DialogResult.OK)
  32.             {
  33.                 using (ScopedWait wait = new ScopedWait(this))
  34.                 {
  35.                     osg.Node root = null;
  36.                     // more than one file
  37.                     if (openFileDialog1.FileNames.Length >= 1)
  38.                     {
  39.                         osg.Group gp = new osg.Group();
  40.                         foreach (string file in openFileDialog1.FileNames)
  41.                         {
  42.                             osg.Node n = osgDB.osgDB.readNodeFile(file);
  43.                             if (n == null)
  44.                                 MessageBox.Show(string.Format("Cannot open file {0}", openFileDialog1.FileName));
  45.                             else
  46.                                 gp.addChild(n);
  47.                         }
  48.                         root = gp;
  49.                     }
  50.                     else if (openFileDialog1.FileNames.Length == 1)
  51.                     {
  52.                         osg.Node n = osgDB.osgDB.readNodeFile(openFileDialog1.FileName);
  53.                         if (n == null)
  54.                             MessageBox.Show(string.Format("Cannot open file {0}", openFileDialog1.FileName));
  55.                         else
  56.                             root = n;
  57.                     }
  58.                     if(root!=null)
  59.                         _ctrl.Viewer.setSceneData(root);
  60.                 } // using
  61.             }
  62.         }

  63.         private void saveSToolStripMenuItem_Click(object sender, EventArgs e)
  64.         {
  65.             if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  66.             {
  67.                 using (ScopedWait wait = new ScopedWait(this))
  68.                 {
  69.                     try
  70.                     {
  71.                         osg.Node n = _ctrl.Viewer.getSceneData();
  72.                         if (n == null)
  73.                             throw new Exception("Invalid node");
  74.                         else
  75.                         {
  76.                             if (osgDB.osgDB.writeNodeFile(n, saveFileDialog1.FileName) == false)
  77.                                 throw new Exception(string.Format("Cannot save file {0}", saveFileDialog1.FileName));
  78.                         }
  79.                     }
  80.                     catch (System.Exception ex)
  81.                     {
  82.                         MessageBox.Show(ex.Message);
  83.                     }
  84.                 }
  85.             }
  86.         }

  87.         private void backgroundToolStripMenuItem_Click(object sender, EventArgs e)
  88.         {
  89.             if (colorDialog1.ShowDialog() == DialogResult.OK)
  90.             {
  91.                 osg.Vec4f bk = new osg.Vec4f(colorDialog1.Color.R/255.0f,colorDialog1.Color.G/255.0f,colorDialog1.Color.B/255.0f,1.0f);
  92.                 _ctrl.Viewer.asView().getCamera().setClearColor(bk);
  93.             }
  94.         }

  95.         private void shapesToolStripMenuItem_Click(object sender, EventArgs e)
  96.         {
  97.             using (ScopedWait wait = new ScopedWait(this))
  98.             {
  99.                 _ctrl.Viewer.setSceneData(ShapeCreator.create());
  100.             }
  101.         }
  102.     }
  103. }
复制代码



ShapeCreator.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;

  4. namespace osgFormExample
  5. {
  6.     class ShapeCreator
  7.     {
  8.         public static osg.Node create()
  9.         {
  10.             osg.Geode geode = new osg.Geode();


  11.             // ---------------------------------------
  12.             // Set up a StateSet to texture the objects
  13.             // ---------------------------------------
  14.             osg.StateSet stateset = new osg.StateSet();

  15.             osg.Image image = osgDB.osgDB.readImageFile("Images/lz1.rgb");
  16.             if (image != null)
  17.             {
  18.                 osg.Texture2D texture = new osg.Texture2D();
  19.                 texture.setImage(image);
  20.                 texture.setFilter(osg.Texture.FilterParameter.MIN_FILTER, osg.Texture.FilterMode.LINEAR);
  21.                 stateset.setTextureAttributeAndModes(0, texture, (uint)osg.StateAttribute.Values.ON);
  22.             }

  23.             stateset.setMode((uint)osg.osgPINVOKE.GL_LIGHTING_get(), (uint)osg.StateAttribute.Values.ON);

  24.             geode.setStateSet(stateset);

  25.             float radius = 0.8f;
  26.             float height = 1.0f;

  27.             osg.TessellationHints hints = new osg.TessellationHints();
  28.             hints.setDetailRatio(0.5f);

  29.             geode.addDrawable(new osg.ShapeDrawable(new osg.Sphere(new osg.Vec3f(0.0f, 0.0f, 0.0f), radius), hints));
  30.             geode.addDrawable(new osg.ShapeDrawable(new osg.Box(new osg.Vec3f(2.0f, 0.0f, 0.0f), 2 * radius), hints));
  31.             geode.addDrawable(new osg.ShapeDrawable(new osg.Cone(new osg.Vec3f(4.0f, 0.0f, 0.0f), radius, height), hints));
  32.             geode.addDrawable(new osg.ShapeDrawable(new osg.Cylinder(new osg.Vec3f(6.0f, 0.0f, 0.0f), radius, height), hints));
  33.             geode.addDrawable(new osg.ShapeDrawable(new osg.Capsule(new osg.Vec3f(8.0f, 0.0f, 0.0f), radius, height), hints));
  34.   
  35.             return geode;
  36.         }
  37.     }
  38. }
复制代码

该用户从未签到

 楼主| 发表于 2013-3-12 22:35:20 | 显示全部楼层
本帖最后由 reziarfg 于 2013-3-12 22:46 编辑

基本原理:
基于C++类编写C包装器,通过PINVOKE由C#类调用。
C++类 ->  C包装器-> PINVOKE-> C#类

示例1 生存周期
顺序图:
life_seq.png
示例2 操作符==
顺序图:
op_equal.png

1、C++端代码片段(osg.dll)

  1. namespace osg {

  2. class Vec3f // 部分代码
  3. {
  4.     public:
  5.         /** Data type of vector components.*/
  6.         typedef float value_type;
  7.         /** Constructor that sets all components of the vector to zero */
  8.         Vec3f() { _v[0]=0.0f; _v[1]=0.0f; _v[2]=0.0f;}
  9.         Vec3f(value_type x,value_type y,value_type z);
  10.         Vec3f(const Vec2f& v2,value_type zz);
  11.         /** Number of vector components. */
  12.         enum { num_components = 3 };
  13.         value_type _v[3];
  14.         inline value_type x() const;
  15.         inline value_type y() const;
  16.         inline value_type z() const;
  17.         /** Returns true if all components have values that are not NaN. */
  18.         inline bool valid();
  19.         inline bool operator == (const Vec3f& v) const;
  20.         inline bool operator <  (const Vec3f& v) const;
  21.         inline bool operator +  (const Vec3f& v) const;
  22.         /** Negation operator. Returns the negative of the Vec3f. */
  23.         inline const Vec3f operator - () const;
  24.         /** Length of the vector = sqrt( vec . vec ) */
  25.         inline value_type length() const;
  26.         /** Normalize the vector so that it has length unity.
  27.           * Returns the previous length of the vector.
  28.         */
  29.         inline value_type normalize();
  30. };    // end of class Vec3f

  31. }    // end of namespace osg
复制代码



2、C包装器代码片段(osg_wrapper(d).dll)
  1. #ifndef SWIGEXPORT
  2. # if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
  3. #   if defined(STATIC_LINKED)
  4. #     define SWIGEXPORT
  5. #   else
  6. #     define SWIGEXPORT __declspec(dllexport)
  7. #   endif
  8. # else
  9. #   if defined(__GNUC__) && defined(GCC_HASCLASSVISIBILITY)
  10. #     define SWIGEXPORT __attribute__ ((visibility("default")))
  11. #   else
  12. #     define SWIGEXPORT
  13. #   endif
  14. # endif
  15. #endif

  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif

  19. SWIGEXPORT void * __stdcall CSharp_new_Vec3f__SWIG_0() {
  20.   return new osg::Vec3f();
  21. }

  22. SWIGEXPORT void * __stdcall CSharp_new_Vec3f__SWIG_1(float jarg1, float jarg2, float jarg3) {
  23.   return new osg::Vec3f(jarg1, jarg2, jarg3);
  24. }

  25. SWIGEXPORT void * __stdcall CSharp_new_Vec3f__SWIG_2(void * jarg1, float jarg2) {
  26.   osg::Vec2f *arg1 = (osg::Vec2f *)jarg1;
  27.   if (!arg1) {
  28.     SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "osg::Vec2f const & type is null", 0);
  29.     return 0;
  30.   }
  31.   return new osg::Vec3f((osg::Vec2f const &)*arg1, jarg2);
  32. }

  33. SWIGEXPORT void __stdcall CSharp_delete_Vec3f(void * jarg1) {
  34.   osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
  35.   delete arg1;
  36. }

  37. SWIGEXPORT int __stdcall CSharp_Vec3f_num_components_get() {
  38.   int result = (int)osg::Vec3f::num_components;
  39.   return jresult;
  40. }

  41. SWIGEXPORT void __stdcall CSharp_Vec3f__v_set(void * jarg1, void * jarg2) {
  42.   osg::Vec3f *arg1 = (osg::Vec3f *) 0 ;
  43.   osg::Vec3f::value_type *arg2 ;
  44.   arg1 = (osg::Vec3f *)jarg1;
  45.   arg2 = (osg::Vec3f::value_type *)jarg2;
  46.   {
  47.     size_t ii;
  48.     osg::Vec3f::value_type *b = (osg::Vec3f::value_type *) arg1->_v;
  49.     for (ii = 0; ii < (size_t)3; ii++) b[ii] = *((osg::Vec3f::value_type *) arg2 + ii);
  50.   }
  51. }

  52. SWIGEXPORT float __stdcall CSharp_Vec3f_x(void * jarg1) {
  53.   osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
  54.   osg::Vec3f::value_type result;  
  55.   result = (osg::Vec3f::value_type)(arg1->x());
  56.   return result;
  57. }

  58. SWIGEXPORT void __stdcall CSharp_Vec3f_set_x(void * jarg1,float jarg2) {
  59.         osg::Vec3f *arg1 = (osg::Vec3f *) 0 ;
  60.         arg1 = (osg::Vec3f *)jarg1;
  61.         arg1->x()=jarg2;
  62. }

  63. SWIGEXPORT float __stdcall CSharp_Vec3f_y(void * jarg1) {
  64.   osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
  65.   osg::Vec3f::value_type result;  
  66.   result = (osg::Vec3f::value_type)(arg1->y());
  67.   return result;
  68. }

  69. SWIGEXPORT void __stdcall CSharp_Vec3f_set_y(void * jarg1,float jarg2) {
  70.         osg::Vec3f *arg1 = (osg::Vec3f *) 0 ;
  71.         arg1 = (osg::Vec3f *)jarg1;
  72.         arg1->y()=jarg2;
  73. }

  74. SWIGEXPORT float __stdcall CSharp_Vec3f_z(void * jarg1) {
  75.   osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
  76.   osg::Vec3f::value_type result;  
  77.   result = (osg::Vec3f::value_type)(arg1->z());
  78.   return result;
  79. }

  80. SWIGEXPORT void __stdcall CSharp_Vec3f_set_z(void * jarg1,float jarg2) {
  81.         osg::Vec3f *arg1 = (osg::Vec3f *) 0 ;
  82.         arg1 = (osg::Vec3f *)jarg1;
  83.         arg1->z()=jarg2;
  84. }

  85. SWIGEXPORT unsigned int __stdcall CSharp_Vec3f_valid(void * jarg1) {
  86.   unsigned int jresult ;
  87.   osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
  88.   bool result;
  89.   result = (bool)( arg1->valid());
  90.   jresult = result;
  91.   return jresult;
  92. }

  93. SWIGEXPORT unsigned int SWIGSTDCALL CSharp_Vec3f_Equal(void * jarg1, void * jarg2) {
  94.   unsigned int jresult ;
  95.   osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
  96.   osg::Vec3f *arg2 = arg2 = (osg::Vec3f *)jarg2 ;
  97.   if (!arg2) {
  98.     SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "osg::Vec3f const & type is null", 0);
  99.     return 0;
  100.   }
  101.   bool result = arg1->operator ==((osg::Vec3f const &)*arg2);
  102.   jresult = (unsigned int)result;
  103.   return jresult;
  104. }

  105. SWIGEXPORT unsigned int SWIGSTDCALL CSharp_Vec3f_Less(void * jarg1, void * jarg2) {
  106.   unsigned int jresult ;
  107.   osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
  108.   osg::Vec3f *arg2 = arg2 = (osg::Vec3f *)jarg2 ;
  109.   if (!arg2) {
  110.     SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "osg::Vec3f const & type is null", 0);
  111.     return 0;
  112.   }
  113.   bool result = arg1->operator <((osg::Vec3f const &)*arg2);
  114.   jresult = (unsigned int)result;
  115.   return jresult;
  116. }

  117. SWIGEXPORT void * SWIGSTDCALL CSharp_Vec3f_Add(void * jarg1, void * jarg2) {
  118.   void * jresult ;
  119.   osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
  120.   osg::Vec3f *arg2 = arg2 = (osg::Vec3f *)jarg2 ;
  121.   if (!arg2) {
  122.     SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "osg::Vec3f const & type is null", 0);
  123.     return 0;
  124.   }
  125.   osg::Vec3f result = arg1->operator +((osg::Vec3f const &)*arg2);
  126.   jresult = new osg::Vec3f((const osg::Vec3f &)result);
  127.   return jresult;
  128. }

  129. SWIGEXPORT void * SWIGSTDCALL CSharp_Vec3f_Neg(void * jarg1) {
  130.   void * jresult ;
  131.   osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
  132.   osg::Vec3f result = arg1->operator -();
  133.   jresult = new osg::Vec3f((const osg::Vec3f &)result);
  134.   return jresult;
  135. }

  136. SWIGEXPORT float __stdcall CSharp_Vec3f_length(void * jarg1) {
  137.   osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
  138.   osg::Vec3f::value_type result = arg1)->length();
  139.   return result;
  140. }

  141. SWIGEXPORT float __stdcall CSharp_Vec3f_normalize(void * jarg1) {
  142.   osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
  143.   return arg1->normalize();
  144. }
  145. #ifdef __cplusplus
  146. }
  147. #endif
复制代码


3、C# PINVOKE代码片段(osg_cs.dll)
  1. public class osgPINVOKE
  2. {
  3. #if DEBUG
  4.     public const string LIBNAME = "osg_cs_wrapperd";
  5. #else
  6.     public const string LIBNAME = "osg_cs_wrapper";
  7. #endif
  8.   [DllImport(LIBNAME, EntryPoint="CSharp_Vec3f_num_components_get")]
  9.   public static extern int Vec3f_num_components_get();

  10.   [DllImport(LIBNAME, EntryPoint="CSharp_new_Vec3f__SWIG_0")]
  11.   public static extern IntPtr new_Vec3f__SWIG_0();

  12.   [DllImport(LIBNAME, EntryPoint="CSharp_new_Vec3f__SWIG_1")]
  13.   public static extern IntPtr new_Vec3f__SWIG_1(float jarg1, float jarg2, float jarg3);

  14.   [DllImport(LIBNAME, EntryPoint="CSharp_new_Vec3f__SWIG_2")]
  15.   public static extern IntPtr new_Vec3f__SWIG_2(HandleRef jarg1, float jarg2);

  16.   [DllImport(LIBNAME, EntryPoint="CSharp_delete_Vec3f")]
  17.   public static extern void delete_Vec3f(HandleRef jarg1);

  18.   [DllImport(LIBNAME, EntryPoint="CSharp_Vec3f_x")]
  19.   public static extern float Vec3f_x(HandleRef jarg1);

  20.   [DllImport(LIBNAME, EntryPoint = "CSharp_Vec3f_set_x")]
  21.   public static extern void Vec3f_set_x(HandleRef jarg1,float jarg2);

  22.   [DllImport(LIBNAME, EntryPoint="CSharp_Vec3f_y")]
  23.   public static extern float Vec3f_y(HandleRef jarg1);

  24.   [DllImport(LIBNAME, EntryPoint = "CSharp_Vec3f_set_y")]
  25.   public static extern void Vec3f_set_y(HandleRef jarg1, float jarg2);

  26.   [DllImport(LIBNAME, EntryPoint="CSharp_Vec3f_z")]
  27.   public static extern float Vec3f_z(HandleRef jarg1);

  28.   [DllImport(LIBNAME, EntryPoint = "CSharp_Vec3f_set_z")]
  29.   public static extern void Vec3f_set_z(HandleRef jarg1, float jarg2);

  30.   [DllImport(LIBNAME, EntryPoint="CSharp_Vec3f_valid")]
  31.   public static extern bool Vec3f_valid(HandleRef jarg1);

  32.   [DllImport(LIBNAME, EntryPoint="CSharp_Vec3f_Equals")]
  33.   public static extern bool Vec3f_Equals(HandleRef jarg1, HandleRef jarg2);

  34.   [DllImport(LIBNAME, EntryPoint="CSharp_Vec3f_Less")]
  35.   public static extern bool Vec3f_Less(HandleRef jarg1, HandleRef jarg2);

  36.   [DllImport(LIBNAME, EntryPoint="CSharp_Vec3f_Add")]
  37.   public static extern IntPtr Vec3f_Add(HandleRef jarg1, HandleRef jarg2);

  38.   [DllImport(LIBNAME, EntryPoint="CSharp_Vec3f_Add")]
  39.   public static extern IntPtr Vec3f_Neg(HandleRef jarg1);

  40.   [DllImport(LIBNAME, EntryPoint="CSharp_Vec3f_length")]
  41.   public static extern float Vec3f_length(HandleRef jarg1);

  42.   [DllImport(LIBNAME, EntryPoint="CSharp_Vec3f_normalize")]
  43.   public static extern float Vec3f_normalize(HandleRef jarg1);

  44. } // end of class osgPINVIKKE
复制代码


4、C#类代码片段(osg_cs.dll)
  1. namespace osg
  2. {
  3.     using System;
  4.     using System.Runtime.InteropServices;
  5.     public class Vec3f : IDisposable
  6.     {
  7.         private HandleRef swigCPtr;
  8.         protected bool swigCMemOwn;
  9.         public Vec3f(IntPtr cPtr, bool cMemoryOwn)
  10.         {
  11.             swigCMemOwn = cMemoryOwn;
  12.             swigCPtr = new HandleRef(this, cPtr);
  13.         }
  14.         public static HandleRef getCPtr(Vec3f obj)
  15.         {
  16.             return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr;
  17.         }
  18.         ~Vec3f()
  19.         {
  20.             Dispose();
  21.         }
  22.         public virtual void Dispose()
  23.         {
  24.             lock (this)
  25.             {
  26.                 if (swigCPtr.Handle != IntPtr.Zero)
  27.                 {
  28.                     if (swigCMemOwn)
  29.                     {
  30.                         swigCMemOwn = false;
  31.                         osgPINVOKE.delete_Vec3f(swigCPtr);
  32.                     }
  33.                     swigCPtr = new HandleRef(null, IntPtr.Zero);
  34.                 }
  35.                 GC.SuppressFinalize(this);
  36.             }
  37.         }
  38.         public Vec3f()
  39.             : this(osgPINVOKE.new_Vec3f__SWIG_0(), true)
  40.         {
  41.         }
  42.         public Vec3f(float x, float y, float z)
  43.             : this(osgPINVOKE.new_Vec3f__SWIG_1(x, y, z), true)
  44.         {
  45.         }
  46.         public Vec3f(Vec2f v2, float zz)
  47.             : this(osgPINVOKE.new_Vec3f__SWIG_2(Vec2f.getCPtr(v2), zz), true)
  48.         {
  49.             if (osgPINVOKE.SWIGPendingException.Pending) throw osgPINVOKE.SWIGPendingException.Retrieve();
  50.         }

  51.         public float x
  52.         {
  53.             get { return osgPINVOKE.Vec3f_x(swigCPtr); }
  54.             set { osgPINVOKE.Vec3f_set_x(swigCPtr, value); }
  55.         }

  56.         public float y
  57.         {
  58.             get { return osgPINVOKE.Vec3f_y(swigCPtr); }
  59.             set { osgPINVOKE.Vec3f_set_y(swigCPtr, value); }
  60.         }

  61.         public float z
  62.         {
  63.             get { return osgPINVOKE.Vec3f_z(swigCPtr); }
  64.             set { osgPINVOKE.Vec3f_set_z(swigCPtr, value); }
  65.         }

  66.         public bool valid()
  67.         {
  68.             bool ret = osgPINVOKE.Vec3f_valid(swigCPtr);
  69.             return ret;
  70.         }

  71.         public bool Equals(Vec3f v)
  72.         {
  73.             bool ret = osgPINVOKE.Vec3f_Equals(swigCPtr, Vec3f.getCPtr(v));
  74.             if (osgPINVOKE.SWIGPendingException.Pending) throw osgPINVOKE.SWIGPendingException.Retrieve();
  75.             return ret;
  76.         }

  77.         public bool Less(Vec3f v)
  78.         {
  79.             bool ret = osgPINVOKE.Vec3f_Less(swigCPtr, Vec3f.getCPtr(v));
  80.             if (osgPINVOKE.SWIGPendingException.Pending) throw osgPINVOKE.SWIGPendingException.Retrieve();
  81.             return ret;
  82.         }

  83.         public Vec3f Add(Vec3f rhs)
  84.         {
  85.             Vec3f ret = new Vec3f(osgPINVOKE.Vec3f_Add(swigCPtr, Vec3f.getCPtr(rhs)), true);
  86.             if (osgPINVOKE.SWIGPendingException.Pending) throw osgPINVOKE.SWIGPendingException.Retrieve();
  87.             return ret;
  88.         }

  89.         public Vec3f Neg(Vec3f rhs)
  90.         {
  91.             Vec3f ret = new Vec3f(osgPINVOKE.Vec3f_Neg(swigCPtr, Vec3f.getCPtr(rhs)), true);
  92.         }

  93.         public float length()
  94.         {
  95.             float ret = osgPINVOKE.Vec3f_length(swigCPtr);
  96.             return ret;
  97.         }

  98.         public float normalize()
  99.         {
  100.             float ret = osgPINVOKE.Vec3f_normalize(swigCPtr);
  101.             return ret;
  102.         }

  103.         public static readonly int num_components = osgPINVOKE.Vec3f_num_components_get();

  104.         #region Operators

  105.         public float this[int index]
  106.         {
  107.             get
  108.             { //检查索引范围
  109.                 if (index == 0)
  110.                     return this.x;
  111.                 else if (index == 1)
  112.                     return this.y;
  113.                 else if (index == 2)
  114.                     return this.z;
  115.                 else
  116.                     throw new ArgumentOutOfRangeException("[]",index.ToString());
  117.             }
  118.             set
  119.             {
  120.                 if (index == 0)
  121.                     x = value;
  122.                 else if (index == 1)
  123.                     y = value;
  124.                 else if (index == 2)
  125.                     z = value;
  126.             }
  127.         }

  128.         public override bool Equals(object obj)
  129.         {
  130.             if (obj == null || GetType() != obj.GetType()) return false;
  131.             Vec3f v2 = (Vec3f)obj;
  132.             return this.Equals(v2);
  133.         }

  134.         public override int GetHashCode()
  135.         {
  136.             return x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode();
  137.         }

  138.         public static bool operator ==(Vec3f v1, Vec3f v2)
  139.         {
  140.             return Object.Equals(v1, v2);
  141.         }
  142.         public static bool operator !=(Vec3f v1, Vec3f v2)
  143.         {
  144.             return !Object.Equals(v1, v2);
  145.         }
  146.         public static bool operator <(Vec3f v1, Vec3f v2)
  147.         {
  148.             return v1.Less(v2);
  149.         }
  150.         public static Vec3f operator +(Vec3f v1, Vec3f v2)
  151.         {
  152.             return v1.Add(v2);
  153.         }
  154.         public static Vec3f operator -(Vec3f v)
  155.         {
  156.             return v.Neg();
  157.         }
  158.         #endregion // Operators
  159.     } // end of class Vec3f

  160. } // end of namespace osg
复制代码



该用户从未签到

发表于 2013-3-13 07:54:56 | 显示全部楼层
呵呵 不错哦,移动到教程里面了

该用户从未签到

发表于 2013-4-2 15:56:56 | 显示全部楼层
我也试着编译了一下osgswig c#,编译osg接口库的时候出现了很多SwigDirector_DrawCallback的问题,
2>osgCSHARP_wrap.cxx(2420): error C2146: 语法错误: 缺少“;”(在标识符“__SWIG_0”的前面)
2>osgCSHARP_wrap.cxx(2420): error C3861: “__SWIG_0”: 找不到标识符
2>osgCSHARP_wrap.cxx(2427): error C2662: “SwigDirector_DrawCallback::swig_callbackoperator”: 不能将“this”指针从“const SwigDirector_DrawCallback”转换为“SwigDirector_DrawCallback &”
2>          转换丢失限定符
2>osgCSHARP_wrap.cxx(2427): error C2146: 语法错误: 缺少“)”(在标识符“__SWIG_1”的前面)
2>osgCSHARP_wrap.cxx(2427): error C2059: 语法错误:“)”
2>osgCSHARP_wrap.cxx(2427): error C2065: “__SWIG_1”: 未声明的标识符
2>osgCSHARP_wrap.cxx(2427): error C2143: 语法错误 : 缺少“;”(在“{”的前面)
2>osgCSHARP_wrap.cxx(2430): error C2181: 没有匹配 if 的非法 else
2>osgCSHARP_wrap.cxx(2432): error C2662: “SwigDirector_DrawCallback::swig_callbackoperator”: 不能将“this”指针从“const SwigDirector_DrawCallback”转换为“SwigDirector_DrawCallback &”
2>          转换丢失限定符
2>osgCSHARP_wrap.cxx(2432): error C2146: 语法错误: 缺少“;”(在标识符“__SWIG_1”的前面)
2>osgCSHARP_wrap.cxx(2432): error C3861: “__SWIG_1”: 找不到标识符
2>osgCSHARP_wrap.cxx(2436): error C3646: “__SWIG_0”: 未知重写说明符
2>osgCSHARP_wrap.cxx(2436): error C3646: “__SWIG_1”: 未知重写说明符
2>osgCSHARP_wrap.cxx(2436): error C2086: “SwigDirector_DrawCallback::SWIG_Callback0_t (__cdecl *__cdecl callbackoperator)(void)”: 重定义
2>          osgCSHARP_wrap.cxx(2436) : 参见“callbackoperator”的声明
2>osgCSHARP_wrap.cxx(2437): error C2146: 语法错误: 缺少“;”(在标识符“__SWIG_0”的前面)
2>osgCSHARP_wrap.cxx(2437): error C2065: “__SWIG_0”: 未声明的标识符
2>osgCSHARP_wrap.cxx(2437): error C2146: 语法错误: 缺少“;”(在标识符“__SWIG_0”的前面)
2>osgCSHARP_wrap.cxx(2437): error C2065: “__SWIG_0”: 未声明的标识符
2>osgCSHARP_wrap.cxx(2438): error C2146: 语法错误: 缺少“;”(在标识符“__SWIG_1”的前面)
2>osgCSHARP_wrap.cxx(2438): error C2065: “__SWIG_1”: 未声明的标识符
2>osgCSHARP_wrap.cxx(2438): error C2146: 语法错误: 缺少“;”(在标识符“__SWIG_1”的前面)
2>osgCSHARP_wrap.cxx(2438): error C2065: “__SWIG_1”: 未声明的标识符
2>osgCSHARP_wrap.cxx(2442): error C2146: 语法错误: 缺少“;”(在标识符“__SWIG_0”的前面)
2>osgCSHARP_wrap.cxx(2442): error C2065: “__SWIG_0”: 未声明的标识符
2>osgCSHARP_wrap.cxx(2443): error C2146: 语法错误: 缺少“;”(在标识符“__SWIG_1”的前面)
2>osgCSHARP_wrap.cxx(2443): error C2065: “__SWIG_1”: 未声明的标识符
2>osgCSHARP_wrap.cxx(8440): error C2660: “SwigDirector_DrawCallback::swig_connect_director”: 函数不接受 2 个参数
大侠们,怎么回事啊,给点提示吧,

该用户从未签到

发表于 2013-4-2 15:59:03 | 显示全部楼层
而且生成的camera.cs文件里出现了"::"域作用符,如下:
  public enum ImplicitBufferAttachment {
    IMPLICIT_DEPTH_BUFFER_ATTACHMENT = osg:isplaySettings::IMPLICIT_DEPTH_BUFFER_ATTACHMENT,
    IMPLICIT_STENCIL_BUFFER_ATTACHMENT = osg::DisplaySettings::IMPLICIT_STENCIL_BUFFER_ATTACHMENT,
    IMPLICIT_COLOR_BUFFER_ATTACHMENT = osg::DisplaySettings::IMPLICIT_COLOR_BUFFER_ATTACHMENT,
    USE_DISPLAY_SETTINGS_MASK = (~0)
  }

该用户从未签到

发表于 2013-4-2 21:34:50 | 显示全部楼层
  不错啊,支持一个~~

该用户从未签到

 楼主| 发表于 2013-4-11 21:07:21 | 显示全部楼层
zwxscu 发表于 2013-4-2 15:59
而且生成的camera.cs文件里出现了"::"域作用符,如下:
  public enum ImplicitBufferAttachment {
    IMP ...

谢谢支持。
改成这样:  
public enum ImplicitBufferAttachment {
    IMPLICIT_DEPTH_BUFFER_ATTACHMENT = DisplaySettings.ImplicitBufferAttachment.IMPLICIT_DEPTH_BUFFER_ATTACHMENT,
    IMPLICIT_STENCIL_BUFFER_ATTACHMENT = DisplaySettings.ImplicitBufferAttachment.IMPLICIT_STENCIL_BUFFER_ATTACHMENT,
    IMPLICIT_COLOR_BUFFER_ATTACHMENT = DisplaySettings.ImplicitBufferAttachment.IMPLICIT_COLOR_BUFFER_ATTACHMENT,
    USE_DISPLAY_SETTINGS_MASK = (~0)
  }

该用户从未签到

 楼主| 发表于 2013-4-11 21:11:16 | 显示全部楼层
本帖最后由 reziarfg 于 2013-4-16 22:19 编辑
zwxscu 发表于 2013-4-2 15:56
我也试着编译了一下osgswig c#,编译osg接口库的时候出现了很多SwigDirector_DrawCallback的问题,
2>osgCS ...

修改osgCSHARP_wrap.h文件如下,osgCSHARP_wrap.cxx里也要做相应修改。

struct SwigDirector_DrawCallback : public DrawCallback, public Swig:: Director {

public:
    SwigDirector_DrawCallback();
    SwigDirector_DrawCallback(DrawCallback const &arg0, CopyOp const &arg1);
    virtual void operator ()(osg::RenderInfo &renderInfo) const;
    virtual void operator ()(osg::Camera const &arg0) const;

    typedef void (SWIGSTDCALL* SWIG_Callback0_t)(void *);
    typedef void (SWIGSTDCALL* SWIG_Callback1_t)(void *);
    void swig_connect_director(SWIG_Callback0_t callbackoperator0, SWIG_Callback1_t callbackoperator1);

private:
    SWIG_Callback0_t swig_callbackoperator0;
    SWIG_Callback1_t swig_callbackoperator1;
    void swig_init_callbacks();
};

该用户从未签到

发表于 2013-4-12 07:51:50 | 显示全部楼层
这么做 是不是有点多此一举了

该用户从未签到

发表于 2013-8-23 11:32:56 | 显示全部楼层
我也想做osgswig c#编译,可好久找不到相关资料,楼主是如何编译的,有相关网址或文档能否提供下,谢谢
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表