Hi Jerry,<br /><br />

in this example I draw for demonstration purposes a grid of lines into the scene.<br /><br />

Note: This example has been written just to demonstrate mixing VTK with OpenGL is working, but it has to be extended in many aspects.<br />
Resizing is not handled properly. Perspective projection isn't tested.<br />
Anyhow I hope it's a good starting point.<br />

<pre><span style=' color: Blue;'>using</span> System;
<span style=' color: Blue;'>using</span> System.ComponentModel;
<span style=' color: Blue;'>using</span> System.Windows.Forms;
<span style=' color: Blue;'>using</span> System.Diagnostics;
<span style=' color: Blue;'>using</span> Kitware.VTK;

<span style=' color: Blue;'>using</span> SharpGL;
<span style=' color: Blue;'>using</span> SharpGL.Enumerations;

<span style=' color: Blue;'>namespace</span> TestActiViz {
   <span style=' color: Blue;'>public</span> <span style=' color: Blue;'>partial</span> <span style=' color: Blue;'>class</span> Form1 : Form {

      <span style=' color: Blue;'>protected</span> OpenGL _GL;
      <span style=' color: Blue;'>protected</span> RenderContextType _RenderContextType;
      vtkRenderWindow _RenderWindow;
      vtkRenderer _Renderer;
      IntPtr _Context;
      <span style=' color: Blue;'>bool</span> _IsOpenGLInitialized = <span style=' color: Maroon;'>false</span>;


      <span style=' color: Blue;'>public</span> Form1() {
         InitializeComponent();
         _GL = <span style=' color: Blue;'>new</span> OpenGL();
         _RenderContextType = RenderContextType.NativeWindow;
      }


      <span style=' color: Blue;'>private</span> <span style=' color: Blue;'>bool</span> InitOpenGL() {
         <span style=' color: Blue;'>if</span>(!_IsOpenGLInitialized) {
            vtkWin32OpenGLRenderWindow rw1 = vtkWin32OpenGLRenderWindow.SafeDownCast(_RenderWindow);
            _Context = rw1.GetGenericContext();
            IntPtr windowId = rw1.GetGenericWindowId();
            <span style=' color: Blue;'>int</span>[] size = _RenderWindow.GetSize();
            <span style=' color: Blue;'>int</span> bitdepth = _RenderWindow.GetDepthBufferSize();
            Debug.WriteLine(<span style=' color: Maroon;'>"Bit depth: "</span> + _RenderWindow.GetDepthBufferSize());
            <span style=' color: Blue;'>bool</span> ret = _GL.Create(_RenderContextType, size[<span style=' color: Maroon;'>0</span>], size[<span style=' color: Maroon;'>1</span>], bitdepth, windowId);
            <span style=' color: Blue;'>if</span>(ret) {
               _IsOpenGLInitialized = <span style=' color: Maroon;'>true</span>;
            }
            <span style=' color: Blue;'>return</span> ret;
         }
         <span style=' color: Blue;'>return</span> <span style=' color: Maroon;'>true</span>;
      }


      <span style=' color: Blue;'>private</span> <span style=' color: Blue;'>void</span> renderWindowControl1_Load(<span style=' color: Blue;'>object</span> sender, EventArgs e) {
         vtkSphereSource sphere = vtkSphereSource.New();
         vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
         mapper.SetInputConnection(sphere.GetOutputPort());
         <span style=' color: Green;'>// _Actor</span>
         vtkActor actor = vtkActor.New();
         actor.SetMapper(mapper);
         actor.GetProperty().SetColor(<span style=' color: Maroon;'>1</span>, <span style=' color: Maroon;'>0</span>, <span style=' color: Maroon;'>0</span>);

         <span style=' color: Green;'>// get a reference to the renderwindow of our renderWindowControl1</span>
         _RenderWindow = renderWindowControl1.RenderWindow;
         _Renderer = _RenderWindow.GetRenderers().GetFirstRenderer();
         _Renderer.EndEvt += <span style=' color: Blue;'>new</span> vtkObject.vtkObjectEventHandler(renderer_EndEvt);
         _Renderer.StartEvt += <span style=' color: Blue;'>new</span> vtkObject.vtkObjectEventHandler(renderer_StartEvt);
         <span style=' color: Green;'>// set background color</span>
         _Renderer.SetBackground(<span style=' color: Maroon;'>0</span><span style=' color: Maroon;'>.2</span>, <span style=' color: Maroon;'>0</span><span style=' color: Maroon;'>.3</span>, <span style=' color: Maroon;'>0</span><span style=' color: Maroon;'>.4</span>);
         <span style=' color: Green;'>// add our actor to the _Renderer</span>
         _Renderer.AddActor(actor);
      }


      <span style=' color: Blue;'>void</span> renderer_StartEvt(vtkObject sender, vtkObjectEventArgs e) {
         <span style=' color: Blue;'>if</span>(!_IsOpenGLInitialized) {
            <span style=' color: Green;'>//Debug.WriteLine("renderer_StartEvt");</span>
            <span style=' color: Blue;'>if</span>(InitOpenGL())
               _RenderWindow.SwapBuffersOff();
         }
      }


      <span style=' color: Blue;'>void</span> renderer_EndEvt(vtkObject sender, vtkObjectEventArgs e) {
         <span style=' color: Blue;'>if</span>(_IsOpenGLInitialized) {
            <span style=' color: Green;'>//Debug.WriteLine("renderer_EndEvt");</span>

            <span style=' color: Green;'>// next commented line has the same effect as _GL.MakeCurrent()</span>
            <span style=' color: Green;'>//renderWindowControl1.RenderWindow.MakeCurrent();</span>
            _GL.MakeCurrent();
            _GL.MatrixMode(MatrixMode.Projection);
            _GL.LoadIdentity();
            _GL.Ortho(<span style=' color: Maroon;'>0</span>, renderWindowControl1.Width, renderWindowControl1.Height, <span style=' color: Maroon;'>0</span>, -<span style=' color: Maroon;'>10</span>, <span style=' color: Maroon;'>10</span>);
            _GL.Clear(SharpGL.OpenGL.GL_DEPTH_BUFFER_BIT); <span style=' color: Green;'>// really needed ???</span>
            <span style=' color: Green;'>// draw our own stuff</span>
            Draw2DGrid();
            _GL.Flush();
            _GL.Blit(_Context);
         }
      }


      <span style=' color: Blue;'>private</span> <span style=' color: Blue;'>void</span> Draw2DGrid() {
         <span style=' color: Blue;'>int</span>[] size = _RenderWindow.GetSize();
         <span style=' color: Blue;'>int</span> boxSize = <span style=' color: Maroon;'>10</span>;
         <span style=' color: Blue;'>float</span> z = <span style=' color: Maroon;'>0</span><span style=' color: Maroon;'>.0f</span>;
         <span style=' color: Blue;'>int</span> n = size[<span style=' color: Maroon;'>0</span>] / boxSize;
         <span style=' color: Blue;'>int</span> m = size[<span style=' color: Maroon;'>1</span>] / boxSize;
         _GL.LineWidth(<span style=' color: Maroon;'>1</span><span style=' color: Maroon;'>.0f</span>);
         _GL.Begin(SharpGL.Enumerations.BeginMode.Lines);
         <span style=' color: Green;'>// vertical grid lines</span>
         <span style=' color: Blue;'>for</span>(<span style=' color: Blue;'>int</span> i = <span style=' color: Maroon;'>0</span>; i &lt; n; i++) {
            _GL.Vertex(i * boxSize + <span style=' color: Maroon;'>1</span>, <span style=' color: Maroon;'>0</span>, z);
            _GL.Vertex(i * boxSize + <span style=' color: Maroon;'>1</span>, size[<span style=' color: Maroon;'>1</span>], z);
         }
         <span style=' color: Green;'>// horizontal grid lines</span>
         <span style=' color: Blue;'>for</span>(<span style=' color: Blue;'>int</span> i = <span style=' color: Maroon;'>0</span>; i &lt; m; i++) {
            _GL.Vertex(<span style=' color: Maroon;'>0</span>, i * boxSize, z);
            _GL.Vertex(size[<span style=' color: Maroon;'>0</span>], i * boxSize, z);
         }
         _GL.End();
      }
   }
}
</pre>
<br />
with kind regards<br />
Jochen
        
<br/><hr align="left" width="300" />
View this message in context: <a href="http://vtk.1045678.n5.nabble.com/Draw-a-2D-cross-on-vtkImageActor-tp5714166p5714181.html">Re: Draw a 2D cross on vtkImageActor</a><br/>
Sent from the <a href="http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html">VTK - Users mailing list archive</a> at Nabble.com.<br/>