Class: Ray::GL::Buffer

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

Overview

Buffers are a low-level way to push vertices onto the GPU so you can draw them. They are used interally by higher level classes, such as Ray::BufferRenderer; Ray also keeps global buffers to store the vertices of drawables.

Internally, it uses OpenGL VBOs and VAOs (or just sets vertex attrib pointers if VAOs aren’t available).

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, vtype) ⇒ Object

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



39
40
41
42
43
44
45
46
47
48
49
# File 'ext/gl_buffer.c', line 39

static
VALUE ray_gl_buffer_init(VALUE self, VALUE type, VALUE vtype) {
  rb_iv_set(self, "@vertex_type", vtype);

  say_buffer **ptr = NULL;
  Data_Get_Struct(self, say_buffer*, ptr);

  *ptr = say_buffer_create(ray_get_vtype(vtype), ray_buf_type(type), 256);

  return self;
}

Class Method Details

.unbindObject

Unbinds any buffer that was bound.



52
53
54
55
56
# File 'ext/gl_buffer.c', line 52

static
VALUE ray_gl_buffer_unbind(VALUE self) {
  say_buffer_unbind();
  return Qnil;
}

Instance Method Details

#[](id) ⇒ Ray::GL::Vertex, Ray::Vertex



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'ext/gl_buffer.c', line 70

static
VALUE ray_gl_buffer_get(VALUE self, VALUE i) {
  say_buffer *buf   = ray_rb2buffer(self);
  size_t      size  = say_buffer_get_size(buf);
  size_t      index = NUM2ULONG(i);

  if (index >= size)
    return Qnil;

  VALUE klass  = rb_iv_get(self, "@vertex_type");
  VALUE object = rb_funcall(klass, RAY_METH("allocate"), 0);

  size_t byte_size  = NUM2INT(rb_iv_get(klass, "@vertex_type_size"));

  void *ptr = NULL;
  Data_Get_Struct(object, void, ptr);

  memcpy(ptr, say_buffer_get_vertex(buf, index), byte_size);

  return object;
}

#[]=(id, value) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'ext/gl_buffer.c', line 98

static
VALUE ray_gl_buffer_set(VALUE self, VALUE i, VALUE vertex) {
  rb_check_frozen(self);

  VALUE klass = rb_iv_get(self, "@vertex_type");
  if (!RAY_IS_A(vertex, klass)) {
    rb_raise(rb_eTypeError, "Can't convert %s into %s",
             RAY_OBJ_CLASSNAME(vertex), rb_class2name(klass));
  }

  say_buffer *buf   = ray_rb2buffer(self);
  size_t      size  = say_buffer_get_size(buf);
  size_t      index = NUM2ULONG(i);

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

  size_t byte_size = NUM2INT(rb_iv_get(klass, "@vertex_type_size"));

  void *ptr = NULL;
  Data_Get_Struct(vertex, void, ptr);

  memcpy(say_buffer_get_vertex(buf, index), ptr, byte_size);

  return vertex;
}

#bindObject

Binds the receiver



59
60
61
62
63
# File 'ext/gl_buffer.c', line 59

static
VALUE ray_gl_buffer_bind(VALUE self) {
  say_buffer_bind(ray_rb2buffer(self));
  return self;
}

#resize(size) ⇒ Object

Resizes the buffer, alsos causing it to be updated.



191
192
193
194
195
196
# File 'ext/gl_buffer.c', line 191

static
VALUE ray_gl_buffer_resize(VALUE self, VALUE size) {
  rb_check_frozen(self);
  say_buffer_resize(ray_rb2buffer(self), NUM2ULONG(size));
  return self;
}

#sizeInteger



180
181
182
183
# File 'ext/gl_buffer.c', line 180

static
VALUE ray_gl_buffer_size(VALUE self) {
  return ULONG2NUM(say_buffer_get_size(ray_rb2buffer(self)));
}

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

Overloads:

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

    Updates a part of the buffer.



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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'ext/gl_buffer.c', line 137

static
VALUE ray_gl_buffer_update(int argc, VALUE *argv, VALUE self) {
  say_buffer *buf       = ray_rb2buffer(self);
  size_t      max_index = say_buffer_get_size(buf);

  if (argc == 0)
    say_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_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_buffer_update_part(buf, begin, size);
  }

  return self;
}