[vtkusers] Draw a 2D cross on vtkImageActor

Jochen K. jochen.kling at email.de
Sun Jun 24 08:36:40 EDT 2012


Hi Jerry,


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


Note: This example has been written just to demonstrate mixing VTK with
OpenGL is working, but it has to be extended in many aspects.

Resizing is not handled properly. Perspective projection isn't tested.

Anyhow I hope it's a good starting point.


using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;
using Kitware.VTK;

using SharpGL;
using SharpGL.Enumerations;

namespace TestActiViz {
   public partial class Form1 : Form {

      protected OpenGL _GL;
      protected RenderContextType _RenderContextType;
      vtkRenderWindow _RenderWindow;
      vtkRenderer _Renderer;
      IntPtr _Context;
      bool _IsOpenGLInitialized = false;


      public Form1() {
         InitializeComponent();
         _GL = new OpenGL();
         _RenderContextType = RenderContextType.NativeWindow;
      }


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


      private void renderWindowControl1_Load(object sender, EventArgs e) {
         vtkSphereSource sphere = vtkSphereSource.New();
         vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
         mapper.SetInputConnection(sphere.GetOutputPort());
         // _Actor
         vtkActor actor = vtkActor.New();
         actor.SetMapper(mapper);
         actor.GetProperty().SetColor(1, 0, 0);

         // get a reference to the renderwindow of our renderWindowControl1
         _RenderWindow = renderWindowControl1.RenderWindow;
         _Renderer = _RenderWindow.GetRenderers().GetFirstRenderer();
         _Renderer.EndEvt += new
vtkObject.vtkObjectEventHandler(renderer_EndEvt);
         _Renderer.StartEvt += new
vtkObject.vtkObjectEventHandler(renderer_StartEvt);
         // set background color
         _Renderer.SetBackground(0.2, 0.3, 0.4);
         // add our actor to the _Renderer
         _Renderer.AddActor(actor);
      }


      void renderer_StartEvt(vtkObject sender, vtkObjectEventArgs e) {
         if(!_IsOpenGLInitialized) {
            //Debug.WriteLine("renderer_StartEvt");
            if(InitOpenGL())
               _RenderWindow.SwapBuffersOff();
         }
      }


      void renderer_EndEvt(vtkObject sender, vtkObjectEventArgs e) {
         if(_IsOpenGLInitialized) {
            //Debug.WriteLine("renderer_EndEvt");

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


      private void Draw2DGrid() {
         int[] size = _RenderWindow.GetSize();
         int boxSize = 10;
         float z = 0.0f;
         int n = size[0] / boxSize;
         int m = size[1] / boxSize;
         _GL.LineWidth(1.0f);
         _GL.Begin(SharpGL.Enumerations.BeginMode.Lines);
         // vertical grid lines
         for(int i = 0; i < n; i++) {
            _GL.Vertex(i * boxSize + 1, 0, z);
            _GL.Vertex(i * boxSize + 1, size[1], z);
         }
         // horizontal grid lines
         for(int i = 0; i < m; i++) {
            _GL.Vertex(0, i * boxSize, z);
            _GL.Vertex(size[0], i * boxSize, z);
         }
         _GL.End();
      }
   }
}


with kind regards

Jochen

--
View this message in context: http://vtk.1045678.n5.nabble.com/Draw-a-2D-cross-on-vtkImageActor-tp5714166p5714181.html
Sent from the VTK - Users mailing list archive at Nabble.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120624/88be3142/attachment.htm>


More information about the vtkusers mailing list