Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Native Arrays #51

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions examples/14_sprite_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
scene = Mittsu::Scene.new
scene_ortho = Mittsu::Scene.new

amount = 20
amount = 200
radius = 500

map_a = Mittsu::ImageUtils.load_texture(File.join File.dirname(__FILE__), 'sprite0.png')
Expand Down Expand Up @@ -106,7 +106,7 @@
image_width = material.map.image.width
image_height = material.map.image.height

sprite.material.rotation += 0.1 * (i / 1.0)
sprite.material.rotation += 0.01 * i.to_f
sprite.scale.set(scale * image_width, scale * image_height, 1.0)

if material.map != material_c
Expand All @@ -116,10 +116,10 @@
group.rotation.x = time * 0.5
group.rotation.y = time * 0.75
group.rotation.z = time * 1.0

renderer.clear
renderer.render(scene, camera)
renderer.clear_depth
renderer.render(scene_ortho, camera_ortho)
end

renderer.clear
renderer.render(scene, camera)
renderer.clear_depth
renderer.render(scene_ortho, camera_ortho)
end
1 change: 1 addition & 0 deletions lib/mittsu/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
require "mittsu/core/object_3d"
require "mittsu/core/raycaster"
require "mittsu/core/hash_array"
require "mittsu/core/native_array"
10 changes: 5 additions & 5 deletions lib/mittsu/core/buffer_geometry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ def compute_offsets(size = 65535)
# puts "Faces to process: #{(indices.length/3)}"
# puts "Reordering #{verticesCount} vertices."

sorted_indices = Array.new(indices.length) # 16-bit (Uint16Array in THREE.js)
sorted_indices = UInt16Array.new(indices.length)
index_ptr = 0
vertex_ptr = 0

Expand All @@ -449,9 +449,9 @@ def compute_offsets(size = 65535)

duplicated_vertices = 0
new_vertice_maps = 0
face_vertices = Array.new(6) # (Int32Array)
vertex_map = Array.new(vertices.length) # (Int32Array)
rev_vertex_map = Array.new(vertices.length) # (Int32Array)
face_vertices = Int32Array.new(6)
vertex_map = Int32Array.new(vertices.length)
rev_vertex_map = Int32Array.new(vertices.length)
vertices.each_index do |j|
vertex_map[j] = -1
rev_vertex_map[j] = -1
Expand Down Expand Up @@ -569,7 +569,7 @@ def normalize_normals

# reoderBuffers:
# Reorder attributes based on a new indexBuffer and indexMap.
# indexBuffer - Uint16Array of the new ordered indices.
# indexBuffer - UInt16Array of the new ordered indices.
# indexMap - Int32Array where the position is the new vertex ID and the value the old vertex ID for each vertex.
# vertexCount - Amount of total vertices considered in this reordering (in case you want to grow the vertice stack).
def reorder_buffers(index_buffer, index_map, vertex_count)
Expand Down
117 changes: 117 additions & 0 deletions lib/mittsu/core/native_array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
require 'fiddle'

module Mittsu
class NativeArray
FREEFUNC = Fiddle::Function.new(Fiddle::RUBY_FREE, [Fiddle::TYPE_VOIDP], Fiddle::TYPE_VOID)
include Enumerable

attr_reader :size, :ptr, :bytesize
alias count size
alias length size

def initialize(size, initial_value=nil)
@size = size
@bytesize = size * self.class::SIZEOF_ELEMENT
@ptr = Fiddle::Pointer.malloc(@bytesize, FREEFUNC)

if !initial_value.nil?
self[0, length] = [initial_value].pack(self.class::PACK_DIRECTIVE)*length
end
end

def [](index, length=nil)
if length.nil?
@ptr[index * self.class::SIZEOF_ELEMENT, self.class::SIZEOF_ELEMENT].unpack(self.class::PACK_DIRECTIVE)[0]
else
@ptr[index * self.class::SIZEOF_ELEMENT, length * self.class::SIZEOF_ELEMENT].unpack("#{self.class::PACK_DIRECTIVE}#{length}")
end
end

def []=(index, length=nil, value)
if length.nil?
@ptr[index * self.class::SIZEOF_ELEMENT, self.class::SIZEOF_ELEMENT] = [value].pack(self.class::PACK_DIRECTIVE)
else
string = value.is_a?(Array) ? value.pack("#{self.class::PACK_DIRECTIVE}#{length}") : value
@ptr[index * self.class::SIZEOF_ELEMENT, length * self.class::SIZEOF_ELEMENT] = string
end
end

def each
return enum_for(:each) unless block_given?

@size.times do |index|
yield self[index]
end
end

def ==(other)
return false if length != other.length
each_with_index do |element, index|
return false if element != other[index]
end
true
end

def to_a
each.to_a
end
alias to_ary to_a

def to_s
to_a.to_s
end

def dup
self.class.new(length).tap do |array|
array[0, length] = self[0, length]
end
end

def self.from_array(array, length = nil)
length ||= array.length
from_string(array.pack("#{self::PACK_DIRECTIVE}#{length}"), length)
end

def self.from_string(string, length = nil)
length ||= string.bytesize / self::SIZEOF_ELEMENT
new(length).tap do |array|
array[0, length] = string
end
end
end

class Float32Array < NativeArray
SIZEOF_ELEMENT = Fiddle::SIZEOF_FLOAT
PACK_DIRECTIVE = 'F'
end

class Int32Array < NativeArray
SIZEOF_ELEMENT = Fiddle::SIZEOF_INT
PACK_DIRECTIVE = 'l'
end

class Int16Array < NativeArray
SIZEOF_ELEMENT = Fiddle::SIZEOF_SHORT
PACK_DIRECTIVE = 's'
end

class Int8Array < NativeArray
SIZEOF_ELEMENT = Fiddle::SIZEOF_CHAR
PACK_DIRECTIVE = 'c'
end

class UInt32Array < NativeArray
SIZEOF_ELEMENT = Fiddle::SIZEOF_INT
PACK_DIRECTIVE = 'L'
end

class UInt16Array < NativeArray
SIZEOF_ELEMENT = Fiddle::SIZEOF_SHORT
PACK_DIRECTIVE = 'S'
end

class UInt8Array < NativeArray
SIZEOF_ELEMENT = Fiddle::SIZEOF_CHAR
PACK_DIRECTIVE = 'C'
end
end
2 changes: 1 addition & 1 deletion lib/mittsu/extras/image_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def get_normal_map(image, depth)

def generate_data_texture(width, height, color)
size = width * height
data = Array.new(3 * size) # Uint8Array
data = UInt8Array.new(3 * size)

r = (color.r * 255).floor
g = (color.g * 255).floor
Expand Down
22 changes: 12 additions & 10 deletions lib/mittsu/objects/sprite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ module Mittsu
class Sprite < Object3D
attr_accessor :material, :z

INDICES = [0, 1, 2,
0, 2, 3] # Uint16Array
VERTICES = [-0.5, -0.5, 0.0,
0.5, -0.5, 0.0,
0.5, 0.5, 0.0,
-0.5, 0.5, 0.0] # Float32Array
UVS = [0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0] # Float32Array
INDICES = UInt16Array.from_array [0, 1, 2,
0, 2, 3]

VERTICES = Float32Array.from_array [-0.5, -0.5, 0.0,
0.5, -0.5, 0.0,
0.5, 0.5, 0.0,
-0.5, 0.5, 0.0]

UVS = Float32Array.from_array [0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0]

GEOMETRY = BufferGeometry.new
GEOMETRY[:index] = BufferAttribute.new(INDICES, 1)
Expand Down
24 changes: 12 additions & 12 deletions lib/mittsu/renderers/opengl/core/geometry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ def init_geometry_groups(object)
def init_line_buffers(object)
nvertices = @vertices.length

@vertex_array = Array.new(nvertices * 3, 0.0) # Float32Array
@color_array = Array.new(nvertices * 3, 0.0) # Float32Array
@line_distance_array = Array.new(nvertices, 0.0) # Float32Array
@vertex_array = Float32Array.new(nvertices * 3, 0.0)
@color_array = Float32Array.new(nvertices * 3, 0.0)
@line_distance_array = Float32Array.new(nvertices, 0.0)

@line_count = nvertices

Expand All @@ -62,8 +62,8 @@ def init_line_buffers(object)
def init_particle_buffers(object)
nvertices = @vertices.length

@vertex_array = Array.new(nvertices * 3, 0.0) # Float32Array
@color_array = Array.new(nvertices * 3, 0.0) # Float32Array
@vertex_array = Float32Array.new(nvertices * 3, 0.0)
@color_array = Float32Array.new(nvertices * 3, 0.0)

@particle_count = nvertices

Expand Down Expand Up @@ -100,7 +100,7 @@ def set_line_buffers(hint)
end

glBindBuffer(GL_ARRAY_BUFFER, @vertex_buffer)
glBufferData_easy(GL_ARRAY_BUFFER, @vertex_array, hint)
glBufferData(GL_ARRAY_BUFFER, @vertex_array.bytesize, @vertex_array.ptr, hint)
end

if @colors_need_update
Expand All @@ -113,7 +113,7 @@ def set_line_buffers(hint)
end

glBindBuffer(GL_ARRAY_BUFFER, @color_buffer)
glBufferData_easy(GL_ARRAY_BUFFER, @color_array, hint)
glBufferData(GL_ARRAY_BUFFER, @color_array.bytesize, @color_array.ptr, hint)
end

if @line_distances_need_update
Expand All @@ -122,7 +122,7 @@ def set_line_buffers(hint)
end

glBindBuffer(GL_ARRAY_BUFFER, @line_distance_buffer)
glBufferData_easy(GL_ARRAY_BUFFER, @line_distance_array, hint)
glBufferData(GL_ARRAY_BUFFER, @line_distance_array.bytesize, @line_distance_array.ptr, hint)
end

if @custom_attributes
Expand Down Expand Up @@ -173,7 +173,7 @@ def set_line_buffers(hint)
end

glBindBuffer(GL_ARRAY_BUFFER, custom_attribute.buffer)
glBufferData_easy(GL_ARRAY_BUFFER, custom_attribute.array, hint)
glBufferData(GL_ARRAY_BUFFER, custom_attribute.array.bytesize, custom_attribute.array.ptr, hint)

custom_attribute.needs_update = false
end
Expand All @@ -192,7 +192,7 @@ def set_particle_buffers(hint)


glBindBuffer(GL_ARRAY_BUFFER, @vertex_buffer)
glBufferData_easy(GL_ARRAY_BUFFER, @vertex_array, hint)
glBufferData(GL_ARRAY_BUFFER, @vertex_array.bytesize, @vertex_array.ptr, hint)
end

if @colors_need_update
Expand All @@ -205,7 +205,7 @@ def set_particle_buffers(hint)
end

glBindBuffer(GL_ARRAY_BUFFER, @color_buffer)
glBufferData_easy(GL_ARRAY_BUFFER, @color_array, hint)
glBufferData(GL_ARRAY_BUFFER, @color_array.bytesize, @color_array.ptr, hint)
end

if @custom_attribute
Expand Down Expand Up @@ -330,7 +330,7 @@ def init_custom_attributes(object)

attribute.size = size

attribute.array = Array.new(nvertices * size) # Float32Array
attribute.array = Float32Array.new(nvertices * size)

attribute.buffer = glCreateBuffer
attribute.buffer.belongs_to_attribute = name
Expand Down
2 changes: 1 addition & 1 deletion lib/mittsu/renderers/opengl/objects/mesh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Mesh
attr_accessor :renderer

def render_buffer(camera, lights, fog, material, geometry_group, update_buffers)
type = GL_UNSIGNED_INT # geometry_group.type_array == Uint32Array ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT
type = GL_UNSIGNED_INT

# wireframe
if material.wireframe
Expand Down
Loading