Browse files

Added OpenGL output

  • Loading branch information...
1 parent cdb1891 commit 53fcb3f85f895f7587422db168aba7a509d79c5e @wedesoft committed Oct 12, 2010
View
12 Rakefile
@@ -7,7 +7,7 @@ require 'rake/loaders/makefile'
require 'rbconfig'
PKG_NAME = 'hornetseye-xorg'
-PKG_VERSION = '0.2.1'
+PKG_VERSION = '0.3.0'
CXX = ENV[ 'CXX' ] || 'g++'
STRIP = ENV[ 'STRIP' ] || 'strip'
RB_FILES = FileList[ 'lib/**/*.rb' ]
@@ -45,7 +45,7 @@ desc 'Compile Ruby extension (default)'
task :all => [ SO_FILE ]
file SO_FILE => OBJ do |t|
- sh "#{CXX} -shared -o #{t.name} #{OBJ} -lX11 -lXv #{$LIBRUBYARG}"
+ sh "#{CXX} -shared -o #{t.name} #{OBJ} -lGLU -lGL -lX11 -lXv #{$LIBRUBYARG}"
# sh "#{STRIP} --strip-all #{t.name}"
end
@@ -115,8 +115,8 @@ begin
s.extra_rdoc_files = []
s.rdoc_options = %w{--no-private}
s.add_dependency %<malloc>, [ '~> 1.1' ]
- s.add_dependency %<multiarray>, [ '~> 0.6' ]
- s.add_dependency %<hornetseye-frame>, [ '~> 0.3' ]
+ s.add_dependency %<multiarray>, [ '~> 0.9' ]
+ s.add_dependency %<hornetseye-frame>, [ '~> 0.6' ]
s.add_development_dependency %q{rake}
end
GEM_SOURCE = "#{PKG_NAME}-#{PKG_VERSION}.gem"
@@ -138,8 +138,8 @@ begin
s.extra_rdoc_files = []
s.rdoc_options = %w{--no-private}
s.add_dependency %<malloc>, [ '~> 1.1' ]
- s.add_dependency %<multiarray>, [ '~> 0.6' ]
- s.add_dependency %<hornetseye-frame>, [ '~> 0.3' ]
+ s.add_dependency %<multiarray>, [ '~> 0.9' ]
+ s.add_dependency %<hornetseye-frame>, [ '~> 0.6' ]
end
GEM_BINARY = "#{PKG_NAME}-#{PKG_VERSION}-#{$BINSPEC.platform}.gem"
desc "Build the gem file #{GEM_SOURCE}"
View
5 ext/frame.cc
@@ -60,6 +60,11 @@ char *Frame::data(void)
return ptr;
}
+bool Frame::rgb(void)
+{
+ return rb_funcall( m_frame, rb_intern( "rgb?" ), 0 ) != Qfalse;
+}
+
void Frame::markRubyMember(void)
{
rb_gc_mark( m_frame );
View
1 ext/frame.hh
@@ -30,6 +30,7 @@ public:
int width(void);
int height(void);
char *data(void);
+ bool rgb(void);
VALUE rubyObject(void) { return m_frame; }
void markRubyMember(void);
static int storageSize( const std::string &typecode, int width, int height );
View
2 ext/init.cc
@@ -16,6 +16,7 @@
#include "rubyinc.hh"
#include "x11output.hh"
#include "ximagepainter.hh"
+#include "openglimagepainter.hh"
#include "xvideoimagepainter.hh"
#include "x11display.hh"
#include "x11window.hh"
@@ -39,6 +40,7 @@ extern "C" {
VALUE rbHornetseye = rb_define_module( "Hornetseye" );
X11Output::registerRubyClass( rbHornetseye );
XImagePainter::registerRubyClass( rbHornetseye, X11Output::cRubyClass );
+ OpenGLImagePainter::registerRubyClass( rbHornetseye, X11Output::cRubyClass );
XVideoImagePainter::registerRubyClass( rbHornetseye, X11Output::cRubyClass );
X11Display::registerRubyClass( rbHornetseye );
X11Window::registerRubyClass( rbHornetseye );
View
175 ext/openglimagepainter.cc
@@ -0,0 +1,175 @@
+/* HornetsEye - Computer Vision with Ruby
+ Copyright (C) 2006, 2007, 2008, 2009 Jan Wedekind
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+#define GLX_GLXEXT_PROTOTYPES
+#include <GL/glu.h>
+#include <GL/glx.h>
+#ifndef NDEBUG
+#include <iostream>
+#endif
+#include "openglimagepainter.hh"
+#include "rubytools.hh"
+#include "x11window.hh"
+#include "x11output.hh"
+
+using namespace boost;
+using namespace std;
+
+VALUE OpenGLImagePainter::cRubyClass = Qnil;
+
+OpenGLImagePainter::~OpenGLImagePainter(void)
+{
+ // Seems to be deallocated by X11 or GLX.
+ // if ( m_visualInfo != NULL )
+ // XFree( m_visualInfo );
+}
+
+void OpenGLImagePainter::paint( bool ) throw (Error)
+{
+#ifndef NDEBUG
+ cerr << "OpenGLImagePainter::paint()" << endl;
+#endif
+
+ assert( m_window != NULL );
+
+ if ( m_imageSource != NULL ) {
+
+#ifndef NDEBUG
+ cerr << "Requesting image." << endl;
+#endif
+ FramePtr frame = m_imageSource->frame();
+
+ if ( frame ) {
+ // Create OpenGL rendering context for on-screen rendering.
+ // Direct rendering is enabled (GL_TRUE). Try GL_FALSE if there
+ // are problems with your graphics card and/or driver.
+ // It's also possible to disable direct rendering using an
+ // environment variable "export LIBGL_ALWAYS_INDIRECT=1".
+ GLXContext context =
+ glXCreateContext( m_window->display()->get(),
+ m_window->visualInfo(), 0, GL_TRUE );
+ // Check success.
+ glWrap( context != NULL, "Error creating GLX-context" );
+
+ // Attach context to the window.
+ glWrap( glXMakeCurrent( m_window->display()->get(),
+ m_window->get(), context ),
+ "Error switching GLX-context" );
+
+ // Initialise coordinate system.
+ glLoadIdentity();
+ glViewport( 0, 0, m_window->width(), m_window->height() );
+ glOrtho( 0, m_window->width(), m_window->height(), 0, -1.0, 1.0 );
+
+ // Set up parameters for image transfer.
+ glDisable( GL_DITHER );
+ glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
+ glRasterPos2i( 0, 0 );
+ glPixelZoom( (float)m_window->width() / frame->width() ,
+ -(float)m_window->height() / frame->height() );
+
+ // Wait for vertical retrace
+ // http://www.inb.uni-luebeck.de/~boehme/xvideo_sync.html
+ // The code is commented out for the moment, because it does
+ // not compile on some platforms.
+ // http://rubyforge.org/tracker/index.php?func=detail&aid=11164&group_id=2714&atid=10403
+
+ // unsigned int retraceCount;
+ // glXGetVideoSyncSGI( &retraceCount );
+ // glXWaitVideoSyncSGI( 2, ( retraceCount + 1 ) % 2, &retraceCount );
+
+#ifndef NDEBUG
+ cerr << "Drawing " << frame->width() << 'x' << frame->height()
+ << " image." << endl;
+#endif
+ if ( frame->rgb() ) {
+ glDrawPixels( frame->width(), frame->height(),
+ GL_RGB, GL_UNSIGNED_BYTE, frame->data() );
+ } else {
+ glDrawPixels( frame->width(), frame->height(),
+ GL_LUMINANCE, GL_UNSIGNED_BYTE, frame->data() );
+ };
+ glEnable(GL_DITHER);
+
+ // Finalise.
+ glFinish();
+ glXDestroyContext( m_window->display()->get(), context );
+ };
+
+ };
+}
+
+
+void OpenGLImagePainter::unregisterWindow(void)
+{
+ XFree( m_visualInfo );
+ X11Painter::unregisterWindow();
+}
+
+
+XVisualInfo *OpenGLImagePainter::visualInfo( X11DisplayPtr display )
+ throw (Error)
+{
+ assert( m_visualInfo == NULL );
+ int attributes[] =
+ { GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,
+ GLX_DEPTH_SIZE, 0, None };
+ m_visualInfo =
+ glXChooseVisual( display->get(), DefaultScreen( display->get() ),
+ attributes );
+ ERRORMACRO( m_visualInfo != NULL, Error, ,
+ "Error choosing desired X11 visual." );
+ m_display = display;
+ return m_visualInfo;
+}
+
+void OpenGLImagePainter::glWrap( bool condition, const char *message )
+ throw (Error)
+{
+ if ( !condition ) {
+ Error e;
+ e << message;
+ bool first = true;
+ GLenum error;
+ while ( ( error = glGetError() ) != GL_NO_ERROR ) {
+ e << ( first ? ": " : "; " ) << gluErrorString( error );
+ first = false;
+ };
+ throw e;
+ };
+}
+
+VALUE OpenGLImagePainter::registerRubyClass( VALUE module,
+ VALUE cX11Output )
+{
+ cRubyClass = rb_define_class_under( module, "OpenGLOutput", cX11Output );
+ rb_define_singleton_method( cRubyClass, "new",
+ RUBY_METHOD_FUNC( wrapNew ), 0 );
+ return cRubyClass;
+}
+
+VALUE OpenGLImagePainter::wrapNew( VALUE rbClass )
+{
+ VALUE retVal = Qnil;
+ X11PainterPtr painter( new OpenGLImagePainter );
+ X11OutputPtr ptr( new X11Output( painter ) );
+ retVal = Data_Wrap_Struct( rbClass,
+ X11Output::markRubyObject,
+ X11Output::deleteRubyObject,
+ new X11OutputPtr( ptr ) );
+ return retVal;
+}
+
+
View
44 ext/openglimagepainter.hh
@@ -0,0 +1,44 @@
+/* HornetsEye - Computer Vision with Ruby
+ Copyright (C) 2006, 2007, 2008, 2009 Jan Wedekind
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+#ifndef HORNETSEYE_OPENGLIMAGEPAINTER_HH
+#define HORNETSEYE_OPENGLIMAGEPAINTER_HH
+
+#include <boost/shared_ptr.hpp>
+#include "x11painter.hh"
+#include "x11display.hh"
+
+class X11Window;
+
+class OpenGLImagePainter: public X11Painter
+{
+public:
+ OpenGLImagePainter(void): m_visualInfo(NULL) {}
+ virtual ~OpenGLImagePainter(void);
+ virtual void paint( bool x11Event ) throw (Error);
+ virtual XVisualInfo *visualInfo( X11DisplayPtr display ) throw (Error);
+ virtual void unregisterWindow(void);
+ static VALUE cRubyClass;
+ static VALUE registerRubyClass( VALUE module, VALUE cX11Output );
+ static VALUE wrapNew( VALUE rbClass );
+protected:
+ void glWrap( bool condition, const char *message ) throw (Error);
+ XVisualInfo *m_visualInfo;
+ X11DisplayPtr m_display;
+};
+
+typedef boost::shared_ptr< OpenGLImagePainter > OpenGLImagePainterPtr;
+
+#endif
View
31 lib/hornetseye-xorg/opengloutput.rb
@@ -0,0 +1,31 @@
+# hornetseye-xorg - Graphical output under X.Org
+# Copyright (C) 2010 Jan Wedekind
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# Namespace of Hornetseye computer vision library
+module Hornetseye
+
+ class OpenGLOutput
+
+ def write( frame )
+ target = frame.rgb? ? UBYTERGB : UBYTE
+ frame = frame.to_type target unless frame.typecode == target
+ super frame
+ end
+
+ end
+
+end
+
View
4 lib/hornetseye-xorg/ximageoutput.rb
@@ -19,11 +19,9 @@ module Hornetseye
class XImageOutput
- alias_method :orig_write, :write
-
def write( frame )
frame = frame.to_type UBYTERGB unless frame.typecode == UBYTERGB
- orig_write frame
+ super frame
end
end
View
1 lib/hornetseye_xorg_ext.rb
@@ -18,6 +18,7 @@
require 'hornetseye-xorg/x11display'
require 'hornetseye-xorg/x11output'
require 'hornetseye-xorg/ximageoutput'
+require 'hornetseye-xorg/opengloutput'
require 'hornetseye-xorg/node'
require 'hornetseye-xorg/frame'

0 comments on commit 53fcb3f

Please sign in to comment.