reziarfg 发表于 2013-3-12 22:21:21

osgswig-csharp(C#)

本帖最后由 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++回调
未支持

示例应用程序:


OSGWinInitializer.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;

namespace osgFormExample
{
    public class OSGWinInitializer
    {
      /// <summary>
      /// 初始化控件,使其作为osgViewer.Viewer的输出窗口
      /// </summary>
      /// <param name="viewer">osgViewer.Viewer</param>
      /// <param name="ctrl">控件</param>
      /// <returns>是否成功</returns>
      public static bool Init(osgViewer.Viewer viewer, Control ctrl)
      {
            try
            {
                osgViewer.GraphicsWindowWin32.Traits traits = new osgViewer.GraphicsWindowWin32.Traits();

                Debug.Assert(viewer != null);

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

                traits.x = 0;
                traits.y = 0;
                traits.width = ctrl.Width;
                traits.height = ctrl.Height;
                // Setup the traits parameters
                traits.windowDecoration = false;
                traits.doubleBuffer = true;
                traits.setInheritedWindowPixelFormat = true;
                traits.inheritedWindowData = windata;
                traits.quadBufferStereo = false; // stereo

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

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

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

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

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

                // Realize the Viewer
                viewer.realize();
                return true;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
      }


    }
}
BaseOSGControl.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace osgFormExample
{
    /// <summary>
    /// 示例控件
    /// </summary>
    public class BaseOSGControl : UserControl
    {
      #region Members

      Timer _timer = new Timer();

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

      public osgViewer.Viewer Viewer
      {
            get { return _viewer; }
            set { _viewer = value; }
      }
      #endregion // Members

      public BaseOSGControl()
      {
            InitializeComponent();
      }

      void _timer_Tick(object sender, EventArgs e)
      {
            lock(_viewer){
            _viewer.frame();
            }
      }

      private void InitializeComponent()
      {
            this.SuspendLayout();
            //
            // BaseOSGControl
            //
            this.BackColor = System.Drawing.Color.Green;
            this.Name = "BaseOSGControl";
            this.Size = new System.Drawing.Size(605, 424);
            this.Load += new System.EventHandler(this.BaseOSGControl_Load);
            this.ResumeLayout(false);
      }

      private void BaseOSGControl_Load(object sender, EventArgs e)
      {
            InitOSG();

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

      private void InitOSG()
      {
            OSGWinInitializer.Init(_viewer, this);
            osgGA.CameraManipulator cam = new osgGA.TrackballManipulator();
            _viewer.setCameraManipulator(cam, true);
            _viewer.addEventHandler(new osgViewer.HelpHandler());
            _viewer.addEventHandler(new osgViewer.StatsHandler());
      }

      /// <summary>
      /// Call this when this control is closing
      /// </summary>
      public void Destroy()
      {
            _viewer.setDone(true);
            _viewer.Dispose();
      }

    }
}
SampleForm.csusing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace osgFormExample
{
    public partial class SampleForm : Form
    {
      public SampleForm()
      {
            InitializeComponent();
      }

      BaseOSGControl _ctrl = new BaseOSGControl();

      private void Form1_Load(object sender, EventArgs e)
      {
            _ctrl = new BaseOSGControl();
            this.panel1.Controls.Add(_ctrl);
            _ctrl.Dock = DockStyle.Fill;
            // sample model
            shapesToolStripMenuItem_Click(this,null);
      }

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

      private void openOToolStripMenuItem_Click(object sender, EventArgs e)
      {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                using (ScopedWait wait = new ScopedWait(this))
                {
                  osg.Node root = null;
                  // more than one file
                  if (openFileDialog1.FileNames.Length >= 1)
                  {
                        osg.Group gp = new osg.Group();
                        foreach (string file in openFileDialog1.FileNames)
                        {
                            osg.Node n = osgDB.osgDB.readNodeFile(file);
                            if (n == null)
                              MessageBox.Show(string.Format("Cannot open file {0}", openFileDialog1.FileName));
                            else
                              gp.addChild(n);
                        }
                        root = gp;
                  }
                  else if (openFileDialog1.FileNames.Length == 1)
                  {
                        osg.Node n = osgDB.osgDB.readNodeFile(openFileDialog1.FileName);
                        if (n == null)
                            MessageBox.Show(string.Format("Cannot open file {0}", openFileDialog1.FileName));
                        else
                            root = n;
                  }
                  if(root!=null)
                        _ctrl.Viewer.setSceneData(root);
                } // using
            }
      }

      private void saveSToolStripMenuItem_Click(object sender, EventArgs e)
      {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                using (ScopedWait wait = new ScopedWait(this))
                {
                  try
                  {
                        osg.Node n = _ctrl.Viewer.getSceneData();
                        if (n == null)
                            throw new Exception("Invalid node");
                        else
                        {
                            if (osgDB.osgDB.writeNodeFile(n, saveFileDialog1.FileName) == false)
                              throw new Exception(string.Format("Cannot save file {0}", saveFileDialog1.FileName));
                        }
                  }
                  catch (System.Exception ex)
                  {
                        MessageBox.Show(ex.Message);
                  }
                }
            }
      }

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

      private void shapesToolStripMenuItem_Click(object sender, EventArgs e)
      {
            using (ScopedWait wait = new ScopedWait(this))
            {
                _ctrl.Viewer.setSceneData(ShapeCreator.create());
            }
      }
    }
}



ShapeCreator.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace osgFormExample
{
    class ShapeCreator
    {
      public static osg.Node create()
      {
            osg.Geode geode = new osg.Geode();


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

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

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

            geode.setStateSet(stateset);

            float radius = 0.8f;
            float height = 1.0f;

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

            geode.addDrawable(new osg.ShapeDrawable(new osg.Sphere(new osg.Vec3f(0.0f, 0.0f, 0.0f), radius), hints));
            geode.addDrawable(new osg.ShapeDrawable(new osg.Box(new osg.Vec3f(2.0f, 0.0f, 0.0f), 2 * radius), hints));
            geode.addDrawable(new osg.ShapeDrawable(new osg.Cone(new osg.Vec3f(4.0f, 0.0f, 0.0f), radius, height), hints));
            geode.addDrawable(new osg.ShapeDrawable(new osg.Cylinder(new osg.Vec3f(6.0f, 0.0f, 0.0f), radius, height), hints));
            geode.addDrawable(new osg.ShapeDrawable(new osg.Capsule(new osg.Vec3f(8.0f, 0.0f, 0.0f), radius, height), hints));

            return geode;
      }
    }
}

reziarfg 发表于 2013-3-12 22:35:20

本帖最后由 reziarfg 于 2013-3-12 22:46 编辑

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

示例1 生存周期
顺序图:

示例2 操作符==顺序图:
1、C++端代码片段(osg.dll):

namespace osg {

class Vec3f // 部分代码
{
    public:
      /** Data type of vector components.*/
      typedef float value_type;
      /** Constructor that sets all components of the vector to zero */
      Vec3f() { _v=0.0f; _v=0.0f; _v=0.0f;}
      Vec3f(value_type x,value_type y,value_type z);
      Vec3f(const Vec2f& v2,value_type zz);
      /** Number of vector components. */
      enum { num_components = 3 };
      value_type _v;
      inline value_type x() const;
      inline value_type y() const;
      inline value_type z() const;
      /** Returns true if all components have values that are not NaN. */
      inline bool valid();
      inline bool operator == (const Vec3f& v) const;
      inline bool operator <(const Vec3f& v) const;
      inline bool operator +(const Vec3f& v) const;
      /** Negation operator. Returns the negative of the Vec3f. */
      inline const Vec3f operator - () const;
      /** Length of the vector = sqrt( vec . vec ) */
      inline value_type length() const;
      /** Normalize the vector so that it has length unity.
          * Returns the previous length of the vector.
      */
      inline value_type normalize();
};    // end of class Vec3f

}    // end of namespace osg



2、C包装器代码片段(osg_wrapper(d).dll):#ifndef SWIGEXPORT
# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
#   if defined(STATIC_LINKED)
#   define SWIGEXPORT
#   else
#   define SWIGEXPORT __declspec(dllexport)
#   endif
# else
#   if defined(__GNUC__) && defined(GCC_HASCLASSVISIBILITY)
#   define SWIGEXPORT __attribute__ ((visibility("default")))
#   else
#   define SWIGEXPORT
#   endif
# endif
#endif

#ifdef __cplusplus
extern "C" {
#endif

SWIGEXPORT void * __stdcall CSharp_new_Vec3f__SWIG_0() {
return new osg::Vec3f();
}

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

SWIGEXPORT void * __stdcall CSharp_new_Vec3f__SWIG_2(void * jarg1, float jarg2) {
osg::Vec2f *arg1 = (osg::Vec2f *)jarg1;
if (!arg1) {
    SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "osg::Vec2f const & type is null", 0);
    return 0;
}
return new osg::Vec3f((osg::Vec2f const &)*arg1, jarg2);
}

SWIGEXPORT void __stdcall CSharp_delete_Vec3f(void * jarg1) {
osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
delete arg1;
}

SWIGEXPORT int __stdcall CSharp_Vec3f_num_components_get() {
int result = (int)osg::Vec3f::num_components;
return jresult;
}

SWIGEXPORT void __stdcall CSharp_Vec3f__v_set(void * jarg1, void * jarg2) {
osg::Vec3f *arg1 = (osg::Vec3f *) 0 ;
osg::Vec3f::value_type *arg2 ;
arg1 = (osg::Vec3f *)jarg1;
arg2 = (osg::Vec3f::value_type *)jarg2;
{
    size_t ii;
    osg::Vec3f::value_type *b = (osg::Vec3f::value_type *) arg1->_v;
    for (ii = 0; ii < (size_t)3; ii++) b = *((osg::Vec3f::value_type *) arg2 + ii);
}
}

SWIGEXPORT float __stdcall CSharp_Vec3f_x(void * jarg1) {
osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
osg::Vec3f::value_type result;
result = (osg::Vec3f::value_type)(arg1->x());
return result;
}

SWIGEXPORT void __stdcall CSharp_Vec3f_set_x(void * jarg1,float jarg2) {
        osg::Vec3f *arg1 = (osg::Vec3f *) 0 ;
        arg1 = (osg::Vec3f *)jarg1;
        arg1->x()=jarg2;
}

SWIGEXPORT float __stdcall CSharp_Vec3f_y(void * jarg1) {
osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
osg::Vec3f::value_type result;
result = (osg::Vec3f::value_type)(arg1->y());
return result;
}

SWIGEXPORT void __stdcall CSharp_Vec3f_set_y(void * jarg1,float jarg2) {
        osg::Vec3f *arg1 = (osg::Vec3f *) 0 ;
        arg1 = (osg::Vec3f *)jarg1;
        arg1->y()=jarg2;
}

SWIGEXPORT float __stdcall CSharp_Vec3f_z(void * jarg1) {
osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
osg::Vec3f::value_type result;
result = (osg::Vec3f::value_type)(arg1->z());
return result;
}

SWIGEXPORT void __stdcall CSharp_Vec3f_set_z(void * jarg1,float jarg2) {
        osg::Vec3f *arg1 = (osg::Vec3f *) 0 ;
        arg1 = (osg::Vec3f *)jarg1;
        arg1->z()=jarg2;
}

SWIGEXPORT unsigned int __stdcall CSharp_Vec3f_valid(void * jarg1) {
unsigned int jresult ;
osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
bool result;
result = (bool)( arg1->valid());
jresult = result;
return jresult;
}

SWIGEXPORT unsigned int SWIGSTDCALL CSharp_Vec3f_Equal(void * jarg1, void * jarg2) {
unsigned int jresult ;
osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
osg::Vec3f *arg2 = arg2 = (osg::Vec3f *)jarg2 ;
if (!arg2) {
    SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "osg::Vec3f const & type is null", 0);
    return 0;
}
bool result = arg1->operator ==((osg::Vec3f const &)*arg2);
jresult = (unsigned int)result;
return jresult;
}

SWIGEXPORT unsigned int SWIGSTDCALL CSharp_Vec3f_Less(void * jarg1, void * jarg2) {
unsigned int jresult ;
osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
osg::Vec3f *arg2 = arg2 = (osg::Vec3f *)jarg2 ;
if (!arg2) {
    SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "osg::Vec3f const & type is null", 0);
    return 0;
}
bool result = arg1->operator <((osg::Vec3f const &)*arg2);
jresult = (unsigned int)result;
return jresult;
}

SWIGEXPORT void * SWIGSTDCALL CSharp_Vec3f_Add(void * jarg1, void * jarg2) {
void * jresult ;
osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
osg::Vec3f *arg2 = arg2 = (osg::Vec3f *)jarg2 ;
if (!arg2) {
    SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "osg::Vec3f const & type is null", 0);
    return 0;
}
osg::Vec3f result = arg1->operator +((osg::Vec3f const &)*arg2);
jresult = new osg::Vec3f((const osg::Vec3f &)result);
return jresult;
}

SWIGEXPORT void * SWIGSTDCALL CSharp_Vec3f_Neg(void * jarg1) {
void * jresult ;
osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
osg::Vec3f result = arg1->operator -();
jresult = new osg::Vec3f((const osg::Vec3f &)result);
return jresult;
}

SWIGEXPORT float __stdcall CSharp_Vec3f_length(void * jarg1) {
osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
osg::Vec3f::value_type result = arg1)->length();
return result;
}

SWIGEXPORT float __stdcall CSharp_Vec3f_normalize(void * jarg1) {
osg::Vec3f *arg1 = (osg::Vec3f *)jarg1;
return arg1->normalize();
}
#ifdef __cplusplus
}
#endif


3、C# PINVOKE代码片段(osg_cs.dll):
public class osgPINVOKE
{
#if DEBUG
    public const string LIBNAME = "osg_cs_wrapperd";
#else
    public const string LIBNAME = "osg_cs_wrapper";
#endif

public static extern int Vec3f_num_components_get();


public static extern IntPtr new_Vec3f__SWIG_0();


public static extern IntPtr new_Vec3f__SWIG_1(float jarg1, float jarg2, float jarg3);


public static extern IntPtr new_Vec3f__SWIG_2(HandleRef jarg1, float jarg2);


public static extern void delete_Vec3f(HandleRef jarg1);


public static extern float Vec3f_x(HandleRef jarg1);


public static extern void Vec3f_set_x(HandleRef jarg1,float jarg2);


public static extern float Vec3f_y(HandleRef jarg1);


public static extern void Vec3f_set_y(HandleRef jarg1, float jarg2);


public static extern float Vec3f_z(HandleRef jarg1);


public static extern void Vec3f_set_z(HandleRef jarg1, float jarg2);


public static extern bool Vec3f_valid(HandleRef jarg1);


public static extern bool Vec3f_Equals(HandleRef jarg1, HandleRef jarg2);


public static extern bool Vec3f_Less(HandleRef jarg1, HandleRef jarg2);


public static extern IntPtr Vec3f_Add(HandleRef jarg1, HandleRef jarg2);


public static extern IntPtr Vec3f_Neg(HandleRef jarg1);


public static extern float Vec3f_length(HandleRef jarg1);


public static extern float Vec3f_normalize(HandleRef jarg1);

} // end of class osgPINVIKKE


4、C#类代码片段(osg_cs.dll):
namespace osg
{
    using System;
    using System.Runtime.InteropServices;
    public class Vec3f : IDisposable
    {
      private HandleRef swigCPtr;
      protected bool swigCMemOwn;
      public Vec3f(IntPtr cPtr, bool cMemoryOwn)
      {
            swigCMemOwn = cMemoryOwn;
            swigCPtr = new HandleRef(this, cPtr);
      }
      public static HandleRef getCPtr(Vec3f obj)
      {
            return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr;
      }
      ~Vec3f()
      {
            Dispose();
      }
      public virtual void Dispose()
      {
            lock (this)
            {
                if (swigCPtr.Handle != IntPtr.Zero)
                {
                  if (swigCMemOwn)
                  {
                        swigCMemOwn = false;
                        osgPINVOKE.delete_Vec3f(swigCPtr);
                  }
                  swigCPtr = new HandleRef(null, IntPtr.Zero);
                }
                GC.SuppressFinalize(this);
            }
      }
      public Vec3f()
            : this(osgPINVOKE.new_Vec3f__SWIG_0(), true)
      {
      }
      public Vec3f(float x, float y, float z)
            : this(osgPINVOKE.new_Vec3f__SWIG_1(x, y, z), true)
      {
      }
      public Vec3f(Vec2f v2, float zz)
            : this(osgPINVOKE.new_Vec3f__SWIG_2(Vec2f.getCPtr(v2), zz), true)
      {
            if (osgPINVOKE.SWIGPendingException.Pending) throw osgPINVOKE.SWIGPendingException.Retrieve();
      }

      public float x
      {
            get { return osgPINVOKE.Vec3f_x(swigCPtr); }
            set { osgPINVOKE.Vec3f_set_x(swigCPtr, value); }
      }

      public float y
      {
            get { return osgPINVOKE.Vec3f_y(swigCPtr); }
            set { osgPINVOKE.Vec3f_set_y(swigCPtr, value); }
      }

      public float z
      {
            get { return osgPINVOKE.Vec3f_z(swigCPtr); }
            set { osgPINVOKE.Vec3f_set_z(swigCPtr, value); }
      }

      public bool valid()
      {
            bool ret = osgPINVOKE.Vec3f_valid(swigCPtr);
            return ret;
      }

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

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

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

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

      public float length()
      {
            float ret = osgPINVOKE.Vec3f_length(swigCPtr);
            return ret;
      }

      public float normalize()
      {
            float ret = osgPINVOKE.Vec3f_normalize(swigCPtr);
            return ret;
      }

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

      #region Operators

      public float this
      {
            get
            { //检查索引范围
                if (index == 0)
                  return this.x;
                else if (index == 1)
                  return this.y;
                else if (index == 2)
                  return this.z;
                else
                  throw new ArgumentOutOfRangeException("[]",index.ToString());
            }
            set
            {
                if (index == 0)
                  x = value;
                else if (index == 1)
                  y = value;
                else if (index == 2)
                  z = value;
            }
      }

      public override bool Equals(object obj)
      {
            if (obj == null || GetType() != obj.GetType()) return false;
            Vec3f v2 = (Vec3f)obj;
            return this.Equals(v2);
      }

      public override int GetHashCode()
      {
            return x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode();
      }

      public static bool operator ==(Vec3f v1, Vec3f v2)
      {
            return Object.Equals(v1, v2);
      }
      public static bool operator !=(Vec3f v1, Vec3f v2)
      {
            return !Object.Equals(v1, v2);
      }
      public static bool operator <(Vec3f v1, Vec3f v2)
      {
            return v1.Less(v2);
      }
      public static Vec3f operator +(Vec3f v1, Vec3f v2)
      {
            return v1.Add(v2);
      }
      public static Vec3f operator -(Vec3f v)
      {
            return v.Neg();
      }
      #endregion // Operators
    } // end of class Vec3f

} // end of namespace osg



liuzhiyu123 发表于 2013-3-13 07:54:56

呵呵 不错哦,移动到教程里面了

zwxscu 发表于 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 个参数
大侠们,怎么回事啊,给点提示吧,

zwxscu 发表于 2013-4-2 15:59:03

而且生成的camera.cs文件里出现了"::"域作用符,如下:
public enum ImplicitBufferAttachment {
    IMPLICIT_DEPTH_BUFFER_ATTACHMENT = osg::DisplaySettings::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)
}

tianxiao888 发表于 2013-4-2 21:34:50

不错啊,支持一个~~

reziarfg 发表于 2013-4-11 21:07:21

zwxscu 发表于 2013-4-2 15:59 static/image/common/back.gif
而且生成的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)
}

reziarfg 发表于 2013-4-11 21:11:16

本帖最后由 reziarfg 于 2013-4-16 22:19 编辑

zwxscu 发表于 2013-4-2 15:56 static/image/common/back.gif
我也试着编译了一下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();
};

liuzhiyu123 发表于 2013-4-12 07:51:50

这么做 是不是有点多此一举了

wangshen5962 发表于 2013-8-23 11:32:56

我也想做osgswig c#编译,可好久找不到相关资料,楼主是如何编译的,有相关网址或文档能否提供下,谢谢
页: [1]
查看完整版本: osgswig-csharp(C#)