Class: Ray::GL::IndexBuffer

Inherits:
Object
  • Object
show all
Defined in:
ext/gl_index_buffer.c,
ext/gl_index_buffer.c

Overview

Index buffers are very similar to Ray::GL::Buffer. They are buffers used to contain indices, which allows to avoid sending the same vertex twice when drawing.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Object

Creates a new index buffer with an arbitrary small size (256).



38
39
40
41
42
43
44
45
46
# File 'ext/gl_index_buffer.c', line 38

static
VALUE ray_gl_index_buffer_init(VALUE self, VALUE type) {
  say_index_buffer **ptr = NULL;
  Data_Get_Struct(self, say_index_buffer*, ptr);

  *ptr = say_index_buffer_create(ray_buf_type(type), 256);

  return self;
}

Class Method Details

.unbindObject

Unbinds any buffer that was bound.



50
51
52
53
54
# File 'ext/gl_index_buffer.c', line 50

static
VALUE ray_gl_index_buffer_unbind(VALUE self) {
  say_index_buffer_unbind();
  return Qnil;
}

Instance Method Details

#[](id) ⇒ Index

Returns The indexed stored at that index.

Parameters:

  • id (Integer)

Returns:

  • (Index)

    The indexed stored at that index



77
78
79
80
81
82
83
84
85
86
87
# File 'ext/gl_index_buffer.c', line 77

static
VALUE ray_gl_index_buffer_get(VALUE self, VALUE i) {
  say_index_buffer *buf   = ray_rb2index_buffer(self);
  size_t            size  = say_index_buffer_get_size(buf);
  size_t            index = NUM2ULONG(i);

  if (index >= size)
    return Qnil;

  return ULONG2NUM(*say_index_buffer_get(buf, index));
}

#[]=(id, value) ⇒ Object

Parameters:

  • id (Integer)
  • value (Integer)

    The index to store in the buffer



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'ext/gl_index_buffer.c', line 94

static
VALUE ray_gl_index_buffer_set(VALUE self, VALUE i, VALUE val) {
  rb_check_frozen(self);

  say_index_buffer *buf   = ray_rb2index_buffer(self);
  size_t            index = NUM2ULONG(i);
  size_t            size  = say_index_buffer_get_size(buf);

  if (index >= size) {
    rb_raise(rb_eRangeError, "%zu is outside of range 0...%zu",
             size, index);
  }

  *say_index_buffer_get(buf, index) = NUM2ULONG(val);

  return val;
}

#bindObject

Binds the receiver



57
58
59
60
61
# File 'ext/gl_index_buffer.c', line 57

static
VALUE ray_gl_index_buffer_bind(VALUE self) {
  say_index_buffer_bind(ray_rb2index_buffer(self));
  return self;
}

#iboInteger

buffer.

Returns:

  • (Integer)

    The identifier of the OpenGL buffer used by the index



67
68
69
70
# File 'ext/gl_index_buffer.c', line 67

static
VALUE ray_gl_index_buffer_ibo(VALUE self) {
  return ULONG2NUM(say_index_buffer_get_ibo(ray_rb2index_buffer(self)));
}

#resize(size) ⇒ Object

Resizes the index buffer, alsos causing it to be updated.

Parameters:

  • size (Integere)

    New size



175
176
177
178
179
180
# File 'ext/gl_index_buffer.c', line 175

static
VALUE ray_gl_index_buffer_resize(VALUE self, VALUE size) {
  rb_check_frozen(self);
  say_index_buffer_resize(ray_rb2index_buffer(self), NUM2ULONG(size));
  return self;
}

#sizeInteger

Returns Size of the buffer (amount of indices it contains).

Returns:

  • (Integer)

    Size of the buffer (amount of indices it contains)



165
166
167
168
# File 'ext/gl_index_buffer.c', line 165

static
VALUE ray_gl_index_buffer_size(VALUE self) {
  return ULONG2NUM(say_index_buffer_get_size(ray_rb2index_buffer(self)));
}

#update(range = 0...size) ⇒ Object #update(first, size) ⇒ Object

Overloads:

  • #update(range = 0...size) ⇒ Object

    Updates a part of the buffer.

    Parameters:

    • range (Range<Integer>) (defaults to: 0...size)

      Indices of indices to update

  • #update(first, size) ⇒ Object

    Parameters:

    • first (Integer)

      First index to update

    • size (Integer)

      Amount of indices to update



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'ext/gl_index_buffer.c', line 122

static
VALUE ray_gl_index_buffer_update(int argc, VALUE *argv, VALUE self) {
  say_index_buffer *buf       = ray_rb2index_buffer(self);
  size_t            max_index = say_index_buffer_get_size(buf);

  if (argc == 0)
    say_index_buffer_update(buf);
  else if (argc == 2) {
    size_t begin = NUM2ULONG(argv[0]);
    size_t end   = NUM2ULONG(argv[1]);

    if (end > max_index)
      end = max_index;

    if (begin > end || begin > max_index)
      return self;

    size_t size = (end - begin) + 1;

    say_index_buffer_update_part(buf, begin, size);
  }
  else {
    VALUE range;
    rb_scan_args(argc, argv, "1", &range); /* raise exception */

    size_t begin = NUM2ULONG(rb_funcall(range, RAY_METH("begin"), 0));
    size_t end   = NUM2ULONG(rb_funcall(range, RAY_METH("end"), 0));

    if (end > max_index)
      end = max_index;

    if (begin > end || begin > max_index)
      return self;

    size_t size = (end - begin) + 1;

    say_index_buffer_update_part(buf, begin, size);
  }

  return self;
}