Class: Ray::ImageTarget

Inherits:
Target
  • Object
show all
Defined in:
ext/image_target.c,
lib/ray/image_target.rb,
ext/image_target.c

Overview

Image targets are objects that allow to draw any drawable object on an image instead of doing on-screen rendering. Off-screen rendering can be useful to pre-render some objects.

Notice image targets modify the image they are drawing on directly. You may therefore not want to draw on a cached image, but rather on a copy thereof.

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Target

#[], #clear, #clip, #default_view, #draw, #make_current, #rect, #shader, #size, #to_image, #turtle, #view, #view=, #viewport_for, #with_view

Methods included from PP

#pretty_print_attributes

Constructor Details

#initialize(image = nil) {|target| ... } ⇒ ImageTarget

Returns a new instance of ImageTarget.

Parameters:

  • image (Ray::Image, nil) (defaults to: nil)

    The image to use

Yields:

  • (target)

    Yields itself if a block is given

Yield Parameters:


7
8
9
10
11
12
13
# File 'lib/ray/image_target.rb', line 7

def initialize(image = nil)
  self.image = image if image

  if block_given?
    yield self
  end
end

Class Method Details

.available?true, false

Checks availability of image targets

Image targets are only part of OpenGL core since OpenGL 3 (although they may be supported as an extension in older versions).

Returns:

  • (true, false)

    True when ImageTargets are available


101
102
103
# File 'ext/image_target.c', line 101

VALUE ray_image_target_available(VALUE self) {
  return say_image_target_is_available() ? Qtrue : Qfalse;
}

.unbindObject

Unbinds any image target

This is rarely needed, as this method gets called automatically when a window is bound, and because windows and image targets do not share the same OpenGL context.


83
84
85
86
87
88
89
90
91
# File 'ext/image_target.c', line 83

VALUE ray_image_target_unbind(VALUE self) {
  /*
   * No need for an error when this is not supported, because it just means
   * we don't need to unbind anything.
   */
  if (say_image_target_is_available())
    say_image_target_unbind();
  return Qnil;
}

Instance Method Details

#bindObject

Binds an image target to draw directly on it

This method is only useful when performing low-level OpenGL rendering. It is different from Target#make_current because it doesn't use the target's own OpenGL context. Instead, it will use the current context (ensuring there is one).

Notice binding the image target (and, therefore, making it the current target) clears the depth buffer of the image, so that everything will be rendered over what was already on the image.


71
72
73
74
# File 'ext/image_target.c', line 71

VALUE ray_image_target_bind(VALUE self) {
  say_image_target_bind(ray_rb2image_target(self));
  return self;
}

#imageObject

See Also:


39
40
41
# File 'ext/image_target.c', line 39

VALUE ray_image_target_image(VALUE self) {
  return rb_iv_get(self, "@image");
}

#image=(img) ⇒ Object

Sets the image this object will draw on

Parameters:


31
32
33
34
35
36
# File 'ext/image_target.c', line 31

VALUE ray_image_target_set_image(VALUE self, VALUE img) {
  rb_check_frozen(self);
  say_image_target_set_image(ray_rb2image_target(self), ray_rb2image(img));
  rb_iv_set(self, "@image", img);
  return img;
}

#pretty_print(q) ⇒ Object


15
16
17
# File 'lib/ray/image_target.rb', line 15

def pretty_print(q)
  super q, ["image"]
end

#updateObject

Updates the content of the image target

The pixels of the image will also be updated:

Ray::ImageTarget.new image do |target|
  target.clear Ray::Color.red
  target.update
end

image[0, 0] # => RGBA(255, 0, 0, 255)

54
55
56
57
# File 'ext/image_target.c', line 54

VALUE ray_image_target_update(VALUE self) {
  say_image_target_update(ray_rb2image_target(self));
  return self;
}