Class: Ray::GL::Vertex
- Inherits:
-
Object
- Object
- Ray::GL::Vertex
- Defined in:
- lib/ray/gl/vertex.rb,
ext/gl_vertex.c
Constant Summary collapse
- TypeMap =
ray_gl_vertex_types
Class Method Summary collapse
- .layout ⇒ Object
-
.make(layout) ⇒ Object
Creates a new Vertex class with a custom layout.
- .make_type(types) ⇒ Object
- .offset_of(vtype, elem_id) ⇒ Object
- .size(vtype) ⇒ Object
Instance Method Summary collapse
Class Method Details
.layout ⇒ Object
80 81 82 |
# File 'lib/ray/gl/vertex.rb', line 80 def self.layout @vertex_type_layout end |
.make(layout) ⇒ Object
Creates a new Vertex class with a custom layout. Layout is an array of arrays, where each row contains 3 elements:
-
Attribute name in Ruby
-
Attribute name in GLSL shaders
-
Attribute type, one of the following symbols: float, int, ubyte, bool, color, vector2, vector3.
Getters and setters are created for all of the attributes.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ray/gl/vertex.rb', line 28 def self.make(layout) layout.each do |_, _, type| unless TypeMap.has_key? type raise ArgumentError, "unknown type in a vertex: #{type.inspect}" end end vtype = make_type layout.map { |_, *rest| rest } @vertex_classes[vtype] = Class.new self do # Be *very* careful with those values. @vertex_type_id = vtype @vertex_type_size = Vertex.size(vtype) @vertex_type_layout = layout class << self undef make undef make_type undef size undef offset_of end define_method :initialize do |*args| if args.size > layout.size raise ArgumentError, "wrong number of arguments: %d for %d", args.size, layout.size end layout.each_with_index do |(attr, _, type), i| send("#{attr}=", args[i] || default_for(type)) end end layout.each_with_index do |(attr, _, type), i| offset = Vertex.offset_of(vtype, i) # Faster than define_method. module_eval(<<-eom, __FILE__, __LINE__) def #{attr} element(#{offset}, #{type.inspect}) end def #{attr}=(val) set_element(#{offset}, #{type.inspect}, val) end eom alias_method "#{attr}?", attr if type == :bool end end end |
.make_type(types) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'ext/gl_vertex.c', line 29
static
VALUE ray_gl_vertex_make_type(VALUE self, VALUE types) {
size_t size = RARRAY_LEN(types);
if (size == 0) {
rb_raise(rb_eArgError, "can't create empty vertex type");
}
size_t vtype_id = say_vertex_type_make_new();
say_vertex_type *vtype = say_get_vertex_type(vtype_id);
for (size_t i = 0; i < size; i++) {
VALUE element = RAY_ARRAY_AT(types, i);
VALUE name = RAY_ARRAY_AT(element, 0);
VALUE type = RAY_ARRAY_AT(element, 1);
char *c_name = StringValuePtr(name);
say_vertex_elem_type c_type = NUM2INT(rb_hash_aref(ray_gl_vertex_types,
type));
say_vertex_elem c_elem = {c_type, c_name};
say_vertex_type_push(vtype, c_elem);
}
return INT2FIX(vtype_id);
}
|
.offset_of(vtype, elem_id) ⇒ Object
56 57 58 59 60 |
# File 'ext/gl_vertex.c', line 56
static
VALUE ray_gl_vertex_offset_of(VALUE self, VALUE vtype, VALUE elem_id) {
say_vertex_type *type = say_get_vertex_type(NUM2ULONG(vtype));
return INT2FIX(say_vertex_type_get_offset(type, NUM2INT(elem_id)));
}
|
.size(vtype) ⇒ Object
62 63 64 65 66 |
# File 'ext/gl_vertex.c', line 62
static
VALUE ray_gl_vertex_size(VALUE self, VALUE vtype) {
say_vertex_type *type = say_get_vertex_type(NUM2ULONG(vtype));
return INT2FIX(say_vertex_type_get_size(type));
}
|
Instance Method Details
#pairs ⇒ Object
88 89 90 91 92 |
# File 'lib/ray/gl/vertex.rb', line 88 def pairs self.class.layout.map { |key, _, _| "#{key}=#{send(key)}" }.join " " end |
#to_s ⇒ Object
84 85 86 |
# File 'lib/ray/gl/vertex.rb', line 84 def to_s "#<#{self.class} #{pairs}>" end |