2021-04-16 23:21:06 +08:00
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
# pyglet
|
|
|
|
# Copyright (c) 2006-2008 Alex Holkner
|
2022-04-30 13:56:57 +08:00
|
|
|
# Copyright (c) 2008-2022 pyglet contributors
|
2021-04-16 23:21:06 +08:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions
|
|
|
|
# are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer in
|
|
|
|
# the documentation and/or other materials provided with the
|
|
|
|
# distribution.
|
|
|
|
# * Neither the name of pyglet nor the names of its
|
|
|
|
# contributors may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written
|
|
|
|
# permission.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
"""Manage related vertex attributes within a single vertex domain.
|
|
|
|
|
|
|
|
A vertex "domain" consists of a set of attribute descriptions that together
|
|
|
|
describe the layout of one or more vertex buffers which are used together to
|
|
|
|
specify the vertices in a primitive. Additionally, the domain manages the
|
|
|
|
buffers used to store the data and will resize them as necessary to accommodate
|
|
|
|
new vertices.
|
|
|
|
|
|
|
|
Domains can optionally be indexed, in which case they also manage a buffer
|
|
|
|
containing vertex indices. This buffer is grown separately and has no size
|
|
|
|
relation to the attribute buffers.
|
|
|
|
|
|
|
|
Applications can create vertices (and optionally, indices) within a domain
|
|
|
|
with the :py:meth:`VertexDomain.create` method. This returns a
|
|
|
|
:py:class:`VertexList` representing the list of vertices created. The vertex
|
|
|
|
attribute data within the group can be modified, and the changes will be made
|
|
|
|
to the underlying buffers automatically.
|
|
|
|
|
|
|
|
The entire domain can be efficiently drawn in one step with the
|
|
|
|
:py:meth:`VertexDomain.draw` method, assuming all the vertices comprise
|
|
|
|
primitives of the same OpenGL primitive mode.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import ctypes
|
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
import pyglet
|
|
|
|
|
2021-04-16 23:21:06 +08:00
|
|
|
from pyglet.gl import *
|
2022-03-05 23:10:18 +08:00
|
|
|
from pyglet.graphics import allocation, vertexattribute, vertexarray
|
|
|
|
from pyglet.graphics.vertexbuffer import BufferObject, MappableBufferObject
|
2021-04-16 23:21:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
def _nearest_pow2(v):
|
|
|
|
# From http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
|
|
|
|
# Credit: Sean Anderson
|
|
|
|
v -= 1
|
|
|
|
v |= v >> 1
|
|
|
|
v |= v >> 2
|
|
|
|
v |= v >> 4
|
|
|
|
v |= v >> 8
|
|
|
|
v |= v >> 16
|
|
|
|
return v + 1
|
|
|
|
|
|
|
|
|
2022-03-05 23:10:18 +08:00
|
|
|
_c_types = {
|
|
|
|
GL_BYTE: ctypes.c_byte,
|
|
|
|
GL_UNSIGNED_BYTE: ctypes.c_ubyte,
|
|
|
|
GL_SHORT: ctypes.c_short,
|
|
|
|
GL_UNSIGNED_SHORT: ctypes.c_ushort,
|
|
|
|
GL_INT: ctypes.c_int,
|
|
|
|
GL_UNSIGNED_INT: ctypes.c_uint,
|
|
|
|
GL_FLOAT: ctypes.c_float,
|
|
|
|
GL_DOUBLE: ctypes.c_double,
|
|
|
|
}
|
2021-04-16 23:21:06 +08:00
|
|
|
|
|
|
|
|
2022-03-05 23:10:18 +08:00
|
|
|
_gl_types = {
|
|
|
|
'b': GL_BYTE,
|
|
|
|
'B': GL_UNSIGNED_BYTE,
|
|
|
|
's': GL_SHORT,
|
|
|
|
'S': GL_UNSIGNED_SHORT,
|
|
|
|
'i': GL_INT,
|
|
|
|
'I': GL_UNSIGNED_INT,
|
|
|
|
'f': GL_FLOAT,
|
|
|
|
'd': GL_DOUBLE,
|
|
|
|
}
|
2021-04-16 23:21:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
class VertexDomain:
|
|
|
|
"""Management of a set of vertex lists.
|
|
|
|
|
|
|
|
Construction of a vertex domain is usually done with the
|
|
|
|
:py:func:`create_domain` function.
|
|
|
|
"""
|
2021-09-23 06:34:23 +08:00
|
|
|
version = 0
|
2021-04-16 23:21:06 +08:00
|
|
|
_initial_count = 16
|
|
|
|
|
2022-03-05 23:10:18 +08:00
|
|
|
def __init__(self, program, attribute_meta):
|
|
|
|
self.program = program
|
|
|
|
self.attribute_meta = attribute_meta
|
2021-04-16 23:21:06 +08:00
|
|
|
self.allocator = allocation.Allocator(self._initial_count)
|
2022-03-05 23:10:18 +08:00
|
|
|
self.vao = vertexarray.VertexArray()
|
2021-04-16 23:21:06 +08:00
|
|
|
|
2022-03-05 23:10:18 +08:00
|
|
|
self.attributes = []
|
2021-09-23 06:34:23 +08:00
|
|
|
self.buffer_attributes = [] # list of (buffer, attributes)
|
2022-03-05 23:10:18 +08:00
|
|
|
|
|
|
|
for name, meta in attribute_meta.items():
|
|
|
|
location = meta['location']
|
|
|
|
count = meta['count']
|
|
|
|
gl_type = _gl_types[meta['format'][0]]
|
|
|
|
normalize = 'n' in meta['format']
|
|
|
|
attribute = vertexattribute.VertexAttribute(name, location, count, gl_type, normalize)
|
|
|
|
self.attributes.append(attribute)
|
|
|
|
# Create buffer:
|
2022-07-16 20:20:23 +08:00
|
|
|
attribute.buffer = MappableBufferObject(attribute.stride * self.allocator.capacity)
|
2022-03-05 23:10:18 +08:00
|
|
|
attribute.buffer.element_size = attribute.stride
|
|
|
|
attribute.buffer.attributes = (attribute,)
|
|
|
|
self.buffer_attributes.append((attribute.buffer, (attribute,)))
|
2021-04-16 23:21:06 +08:00
|
|
|
|
|
|
|
# Create named attributes for each attribute
|
|
|
|
self.attribute_names = {}
|
2022-03-05 23:10:18 +08:00
|
|
|
for attribute in self.attributes:
|
|
|
|
self.attribute_names[attribute.name] = attribute
|
2021-04-16 23:21:06 +08:00
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
# Break circular refs that Python GC seems to miss even when forced
|
|
|
|
# collection.
|
|
|
|
for attribute in self.attributes:
|
|
|
|
try:
|
|
|
|
del attribute.buffer
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
def safe_alloc(self, count):
|
2021-04-16 23:21:06 +08:00
|
|
|
"""Allocate vertices, resizing the buffers if necessary."""
|
|
|
|
try:
|
|
|
|
return self.allocator.alloc(count)
|
|
|
|
except allocation.AllocatorMemoryException as e:
|
|
|
|
capacity = _nearest_pow2(e.requested_capacity)
|
2021-09-23 06:34:23 +08:00
|
|
|
self.version += 1
|
2021-04-16 23:21:06 +08:00
|
|
|
for buffer, _ in self.buffer_attributes:
|
|
|
|
buffer.resize(capacity * buffer.element_size)
|
|
|
|
self.allocator.set_capacity(capacity)
|
|
|
|
return self.allocator.alloc(count)
|
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
def safe_realloc(self, start, count, new_count):
|
2021-04-16 23:21:06 +08:00
|
|
|
"""Reallocate vertices, resizing the buffers if necessary."""
|
|
|
|
try:
|
|
|
|
return self.allocator.realloc(start, count, new_count)
|
|
|
|
except allocation.AllocatorMemoryException as e:
|
|
|
|
capacity = _nearest_pow2(e.requested_capacity)
|
2021-09-23 06:34:23 +08:00
|
|
|
self.version += 1
|
2021-04-16 23:21:06 +08:00
|
|
|
for buffer, _ in self.buffer_attributes:
|
|
|
|
buffer.resize(capacity * buffer.element_size)
|
|
|
|
self.allocator.set_capacity(capacity)
|
|
|
|
return self.allocator.realloc(start, count, new_count)
|
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
def create(self, count, index_count=None):
|
2021-04-16 23:21:06 +08:00
|
|
|
"""Create a :py:class:`VertexList` in this domain.
|
|
|
|
|
|
|
|
:Parameters:
|
|
|
|
`count` : int
|
|
|
|
Number of vertices to create.
|
2021-09-23 06:34:23 +08:00
|
|
|
`index_count`: None
|
|
|
|
Ignored for non indexed VertexDomains
|
2021-04-16 23:21:06 +08:00
|
|
|
|
|
|
|
:rtype: :py:class:`VertexList`
|
|
|
|
"""
|
2021-09-23 06:34:23 +08:00
|
|
|
start = self.safe_alloc(count)
|
2021-04-16 23:21:06 +08:00
|
|
|
return VertexList(self, start, count)
|
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
def draw(self, mode):
|
|
|
|
"""Draw all vertices in the domain.
|
2021-04-16 23:21:06 +08:00
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
All vertices in the domain are drawn at once. This is the
|
|
|
|
most efficient way to render primitives.
|
2021-04-16 23:21:06 +08:00
|
|
|
|
|
|
|
:Parameters:
|
|
|
|
`mode` : int
|
|
|
|
OpenGL drawing mode, e.g. ``GL_POINTS``, ``GL_LINES``, etc.
|
|
|
|
|
|
|
|
"""
|
2022-03-05 23:10:18 +08:00
|
|
|
self.vao.bind()
|
|
|
|
|
2021-04-16 23:21:06 +08:00
|
|
|
for buffer, attributes in self.buffer_attributes:
|
|
|
|
buffer.bind()
|
|
|
|
for attribute in attributes:
|
|
|
|
attribute.enable()
|
|
|
|
attribute.set_pointer(attribute.buffer.ptr)
|
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
starts, sizes = self.allocator.get_allocated_regions()
|
|
|
|
primcount = len(starts)
|
|
|
|
if primcount == 0:
|
|
|
|
pass
|
|
|
|
elif primcount == 1:
|
|
|
|
# Common case
|
|
|
|
glDrawArrays(mode, starts[0], sizes[0])
|
2021-04-16 23:21:06 +08:00
|
|
|
else:
|
2021-09-23 06:34:23 +08:00
|
|
|
starts = (GLint * primcount)(*starts)
|
|
|
|
sizes = (GLsizei * primcount)(*sizes)
|
|
|
|
glMultiDrawArrays(mode, starts, sizes, primcount)
|
2021-04-16 23:21:06 +08:00
|
|
|
|
|
|
|
for buffer, _ in self.buffer_attributes:
|
|
|
|
buffer.unbind()
|
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
def draw_subset(self, mode, vertex_list):
|
|
|
|
"""Draw a specific VertexList in the domain.
|
|
|
|
|
|
|
|
The `vertex_list` parameter specifies a :py:class:`VertexList`
|
|
|
|
to draw. Only primitives in that list will be drawn.
|
|
|
|
|
|
|
|
:Parameters:
|
|
|
|
`mode` : int
|
|
|
|
OpenGL drawing mode, e.g. ``GL_POINTS``, ``GL_LINES``, etc.
|
|
|
|
`vertex_list` : `VertexList`
|
|
|
|
Vertex list to draw.
|
|
|
|
|
|
|
|
"""
|
2022-03-05 23:10:18 +08:00
|
|
|
self.vao.bind()
|
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
for buffer, attributes in self.buffer_attributes:
|
|
|
|
buffer.bind()
|
|
|
|
for attribute in attributes:
|
|
|
|
attribute.enable()
|
|
|
|
attribute.set_pointer(attribute.buffer.ptr)
|
|
|
|
|
|
|
|
glDrawArrays(mode, vertex_list.start, vertex_list.count)
|
|
|
|
|
|
|
|
for buffer, _ in self.buffer_attributes:
|
|
|
|
buffer.unbind()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_empty(self):
|
2021-04-16 23:21:06 +08:00
|
|
|
return not self.allocator.starts
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<%s@%x %s>' % (self.__class__.__name__, id(self), self.allocator)
|
|
|
|
|
|
|
|
|
|
|
|
class VertexList:
|
|
|
|
"""A list of vertices within a :py:class:`VertexDomain`. Use
|
|
|
|
:py:meth:`VertexDomain.create` to construct this list.
|
|
|
|
"""
|
|
|
|
def __init__(self, domain, start, count):
|
|
|
|
self.domain = domain
|
|
|
|
self.start = start
|
|
|
|
self.count = count
|
2021-09-23 06:34:23 +08:00
|
|
|
self._caches = {}
|
|
|
|
self._cache_versions = {}
|
2021-04-16 23:21:06 +08:00
|
|
|
|
|
|
|
def draw(self, mode):
|
|
|
|
"""Draw this vertex list in the given OpenGL mode.
|
|
|
|
|
|
|
|
:Parameters:
|
|
|
|
`mode` : int
|
|
|
|
OpenGL drawing mode, e.g. ``GL_POINTS``, ``GL_LINES``, etc.
|
|
|
|
|
|
|
|
"""
|
2022-03-05 23:10:18 +08:00
|
|
|
self.domain.draw_subset(mode, self)
|
2021-04-16 23:21:06 +08:00
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
def resize(self, count, index_count=None):
|
2021-04-16 23:21:06 +08:00
|
|
|
"""Resize this group.
|
|
|
|
|
|
|
|
:Parameters:
|
|
|
|
`count` : int
|
|
|
|
New number of vertices in the list.
|
2021-09-23 06:34:23 +08:00
|
|
|
`index_count`: None
|
|
|
|
Ignored for non indexed VertexDomains
|
2021-04-16 23:21:06 +08:00
|
|
|
|
|
|
|
"""
|
2021-09-23 06:34:23 +08:00
|
|
|
new_start = self.domain.safe_realloc(self.start, self.count, count)
|
2021-04-16 23:21:06 +08:00
|
|
|
if new_start != self.start:
|
|
|
|
# Copy contents to new location
|
|
|
|
for attribute in self.domain.attributes:
|
2021-09-23 06:34:23 +08:00
|
|
|
old = attribute.get_region(attribute.buffer, self.start, self.count)
|
|
|
|
new = attribute.get_region(attribute.buffer, new_start, self.count)
|
2021-04-16 23:21:06 +08:00
|
|
|
new.array[:] = old.array[:]
|
|
|
|
new.invalidate()
|
|
|
|
self.start = new_start
|
|
|
|
self.count = count
|
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
for version in self._cache_versions:
|
|
|
|
self._cache_versions[version] = None
|
2021-04-16 23:21:06 +08:00
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
"""Delete this group."""
|
|
|
|
self.domain.allocator.dealloc(self.start, self.count)
|
|
|
|
|
|
|
|
def migrate(self, domain):
|
|
|
|
"""Move this group from its current domain and add to the specified
|
|
|
|
one. Attributes on domains must match. (In practice, used to change
|
|
|
|
parent state of some vertices).
|
|
|
|
|
|
|
|
:Parameters:
|
|
|
|
`domain` : `VertexDomain`
|
|
|
|
Domain to migrate this vertex list to.
|
|
|
|
|
|
|
|
"""
|
2021-09-23 06:34:23 +08:00
|
|
|
assert list(domain.attribute_names.keys()) == list(self.domain.attribute_names.keys()),\
|
|
|
|
'Domain attributes must match.'
|
2021-04-16 23:21:06 +08:00
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
new_start = domain.safe_alloc(self.count)
|
2021-04-16 23:21:06 +08:00
|
|
|
for key, old_attribute in self.domain.attribute_names.items():
|
|
|
|
old = old_attribute.get_region(old_attribute.buffer, self.start, self.count)
|
|
|
|
new_attribute = domain.attribute_names[key]
|
|
|
|
new = new_attribute.get_region(new_attribute.buffer, new_start, self.count)
|
|
|
|
new.array[:] = old.array[:]
|
|
|
|
new.invalidate()
|
|
|
|
|
|
|
|
self.domain.allocator.dealloc(self.start, self.count)
|
|
|
|
self.domain = domain
|
|
|
|
self.start = new_start
|
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
for version in self._cache_versions:
|
|
|
|
self._cache_versions[version] = None
|
2021-04-16 23:21:06 +08:00
|
|
|
|
2022-03-22 23:20:07 +08:00
|
|
|
def set_attribute_data(self, name, data):
|
|
|
|
attribute = self.domain.attribute_names[name]
|
2022-03-05 23:10:18 +08:00
|
|
|
attribute.set_region(attribute.buffer, self.start, self.count, data)
|
2021-04-16 23:21:06 +08:00
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
def __getattr__(self, name):
|
|
|
|
"""dynamic access to vertex attributes, for backwards compatibility.
|
|
|
|
"""
|
|
|
|
domain = self.domain
|
2022-02-22 23:22:11 +08:00
|
|
|
if self._cache_versions.get(name, None) != domain.version:
|
2021-09-23 06:34:23 +08:00
|
|
|
attribute = domain.attribute_names[name]
|
|
|
|
self._caches[name] = attribute.get_region(attribute.buffer, self.start, self.count)
|
2022-02-22 23:22:11 +08:00
|
|
|
self._cache_versions[name] = domain.version
|
2021-04-16 23:21:06 +08:00
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
region = self._caches[name]
|
2021-04-16 23:21:06 +08:00
|
|
|
region.invalidate()
|
|
|
|
return region.array
|
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
def __setattr__(self, name, value):
|
|
|
|
# Allow setting vertex attributes directly without overwriting them:
|
|
|
|
if 'domain' in self.__dict__ and name in self.__dict__['domain'].attribute_names:
|
|
|
|
getattr(self, name)[:] = value
|
|
|
|
return
|
|
|
|
super().__setattr__(name, value)
|
2021-04-16 23:21:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
class IndexedVertexDomain(VertexDomain):
|
|
|
|
"""Management of a set of indexed vertex lists.
|
|
|
|
|
|
|
|
Construction of an indexed vertex domain is usually done with the
|
2021-09-23 06:34:23 +08:00
|
|
|
:py:func:`create_domain` function.
|
2021-04-16 23:21:06 +08:00
|
|
|
"""
|
|
|
|
_initial_index_count = 16
|
|
|
|
|
2022-03-05 23:10:18 +08:00
|
|
|
def __init__(self, program, attribute_meta, index_gl_type=GL_UNSIGNED_INT):
|
|
|
|
super(IndexedVertexDomain, self).__init__(program, attribute_meta)
|
2021-04-16 23:21:06 +08:00
|
|
|
|
|
|
|
self.index_allocator = allocation.Allocator(self._initial_index_count)
|
|
|
|
|
|
|
|
self.index_gl_type = index_gl_type
|
|
|
|
self.index_c_type = vertexattribute._c_types[index_gl_type]
|
|
|
|
self.index_element_size = ctypes.sizeof(self.index_c_type)
|
2022-07-16 20:20:23 +08:00
|
|
|
self.index_buffer = BufferObject(self.index_allocator.capacity * self.index_element_size)
|
2021-04-16 23:21:06 +08:00
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
def safe_index_alloc(self, count):
|
2021-04-16 23:21:06 +08:00
|
|
|
"""Allocate indices, resizing the buffers if necessary."""
|
|
|
|
try:
|
|
|
|
return self.index_allocator.alloc(count)
|
|
|
|
except allocation.AllocatorMemoryException as e:
|
|
|
|
capacity = _nearest_pow2(e.requested_capacity)
|
2021-09-23 06:34:23 +08:00
|
|
|
self.version += 1
|
2021-04-16 23:21:06 +08:00
|
|
|
self.index_buffer.resize(capacity * self.index_element_size)
|
|
|
|
self.index_allocator.set_capacity(capacity)
|
|
|
|
return self.index_allocator.alloc(count)
|
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
def safe_index_realloc(self, start, count, new_count):
|
2021-04-16 23:21:06 +08:00
|
|
|
"""Reallocate indices, resizing the buffers if necessary."""
|
|
|
|
try:
|
|
|
|
return self.index_allocator.realloc(start, count, new_count)
|
|
|
|
except allocation.AllocatorMemoryException as e:
|
|
|
|
capacity = _nearest_pow2(e.requested_capacity)
|
2021-09-23 06:34:23 +08:00
|
|
|
self.version += 1
|
2021-04-16 23:21:06 +08:00
|
|
|
self.index_buffer.resize(capacity * self.index_element_size)
|
|
|
|
self.index_allocator.set_capacity(capacity)
|
|
|
|
return self.index_allocator.realloc(start, count, new_count)
|
|
|
|
|
|
|
|
def create(self, count, index_count):
|
|
|
|
"""Create an :py:class:`IndexedVertexList` in this domain.
|
|
|
|
|
|
|
|
:Parameters:
|
|
|
|
`count` : int
|
|
|
|
Number of vertices to create
|
|
|
|
`index_count`
|
|
|
|
Number of indices to create
|
|
|
|
|
|
|
|
"""
|
2021-09-23 06:34:23 +08:00
|
|
|
start = self.safe_alloc(count)
|
|
|
|
index_start = self.safe_index_alloc(index_count)
|
2021-04-16 23:21:06 +08:00
|
|
|
return IndexedVertexList(self, start, count, index_start, index_count)
|
|
|
|
|
|
|
|
def get_index_region(self, start, count):
|
2022-03-05 23:10:18 +08:00
|
|
|
"""Get a data from a region of the index buffer.
|
2021-04-16 23:21:06 +08:00
|
|
|
|
|
|
|
:Parameters:
|
|
|
|
`start` : int
|
|
|
|
Start of the region to map.
|
|
|
|
`count` : int
|
|
|
|
Number of indices to map.
|
|
|
|
|
|
|
|
:rtype: Array of int
|
|
|
|
"""
|
|
|
|
byte_start = self.index_element_size * start
|
|
|
|
byte_count = self.index_element_size * count
|
|
|
|
ptr_type = ctypes.POINTER(self.index_c_type * count)
|
2022-03-05 23:10:18 +08:00
|
|
|
map_ptr = self.index_buffer.map_range(byte_start, byte_count, ptr_type)
|
|
|
|
data = map_ptr[:]
|
|
|
|
self.index_buffer.unmap()
|
|
|
|
return data
|
|
|
|
|
|
|
|
def set_index_region(self, start, count, data):
|
|
|
|
byte_start = self.index_element_size * start
|
|
|
|
byte_count = self.index_element_size * count
|
|
|
|
ptr_type = ctypes.POINTER(self.index_c_type * count)
|
|
|
|
map_ptr = self.index_buffer.map_range(byte_start, byte_count, ptr_type)
|
|
|
|
map_ptr[:] = data
|
|
|
|
self.index_buffer.unmap()
|
2021-04-16 23:21:06 +08:00
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
def draw(self, mode):
|
|
|
|
"""Draw all vertices in the domain.
|
|
|
|
|
|
|
|
All vertices in the domain are drawn at once. This is the
|
|
|
|
most efficient way to render primitives.
|
|
|
|
|
|
|
|
:Parameters:
|
|
|
|
`mode` : int
|
|
|
|
OpenGL drawing mode, e.g. ``GL_POINTS``, ``GL_LINES``, etc.
|
|
|
|
|
|
|
|
"""
|
2022-03-05 23:10:18 +08:00
|
|
|
self.vao.bind()
|
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
for buffer, attributes in self.buffer_attributes:
|
|
|
|
buffer.bind()
|
|
|
|
for attribute in attributes:
|
|
|
|
attribute.enable()
|
|
|
|
attribute.set_pointer(attribute.buffer.ptr)
|
2022-07-16 20:20:23 +08:00
|
|
|
self.index_buffer.bind_to_index_buffer()
|
2021-09-23 06:34:23 +08:00
|
|
|
|
|
|
|
starts, sizes = self.index_allocator.get_allocated_regions()
|
|
|
|
primcount = len(starts)
|
|
|
|
if primcount == 0:
|
|
|
|
pass
|
|
|
|
elif primcount == 1:
|
|
|
|
# Common case
|
|
|
|
glDrawElements(mode, sizes[0], self.index_gl_type,
|
|
|
|
self.index_buffer.ptr + starts[0] * self.index_element_size)
|
|
|
|
else:
|
|
|
|
starts = [s * self.index_element_size + self.index_buffer.ptr for s in starts]
|
|
|
|
starts = (ctypes.POINTER(GLvoid) * primcount)(*(GLintptr * primcount)(*starts))
|
|
|
|
sizes = (GLsizei * primcount)(*sizes)
|
|
|
|
glMultiDrawElements(mode, sizes, self.index_gl_type, starts, primcount)
|
|
|
|
|
|
|
|
self.index_buffer.unbind()
|
|
|
|
for buffer, _ in self.buffer_attributes:
|
|
|
|
buffer.unbind()
|
2021-04-16 23:21:06 +08:00
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
def draw_subset(self, mode, vertex_list):
|
|
|
|
"""Draw a specific IndexedVertexList in the domain.
|
2021-04-16 23:21:06 +08:00
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
The `vertex_list` parameter specifies a :py:class:`IndexedVertexList`
|
|
|
|
to draw. Only primitives in that list will be drawn.
|
2021-04-16 23:21:06 +08:00
|
|
|
|
|
|
|
:Parameters:
|
|
|
|
`mode` : int
|
|
|
|
OpenGL drawing mode, e.g. ``GL_POINTS``, ``GL_LINES``, etc.
|
|
|
|
`vertex_list` : `IndexedVertexList`
|
2021-09-23 06:34:23 +08:00
|
|
|
Vertex list to draw.
|
2021-04-16 23:21:06 +08:00
|
|
|
|
|
|
|
"""
|
2022-03-05 23:10:18 +08:00
|
|
|
self.vao.bind()
|
|
|
|
|
2021-04-16 23:21:06 +08:00
|
|
|
for buffer, attributes in self.buffer_attributes:
|
|
|
|
buffer.bind()
|
|
|
|
for attribute in attributes:
|
|
|
|
attribute.enable()
|
|
|
|
attribute.set_pointer(attribute.buffer.ptr)
|
2022-07-16 20:20:23 +08:00
|
|
|
self.index_buffer.bind_to_index_buffer()
|
2021-04-16 23:21:06 +08:00
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
glDrawElements(mode, vertex_list.index_count, self.index_gl_type,
|
|
|
|
self.index_buffer.ptr +
|
|
|
|
vertex_list.index_start * self.index_element_size)
|
2021-04-16 23:21:06 +08:00
|
|
|
|
|
|
|
self.index_buffer.unbind()
|
|
|
|
for buffer, _ in self.buffer_attributes:
|
|
|
|
buffer.unbind()
|
|
|
|
|
|
|
|
|
|
|
|
class IndexedVertexList(VertexList):
|
|
|
|
"""A list of vertices within an :py:class:`IndexedVertexDomain` that are
|
|
|
|
indexed. Use :py:meth:`IndexedVertexDomain.create` to construct this list.
|
|
|
|
"""
|
|
|
|
_indices_cache = None
|
|
|
|
_indices_cache_version = None
|
|
|
|
|
|
|
|
def __init__(self, domain, start, count, index_start, index_count):
|
2021-09-23 06:34:23 +08:00
|
|
|
super().__init__(domain, start, count)
|
2021-04-16 23:21:06 +08:00
|
|
|
self.index_start = index_start
|
|
|
|
self.index_count = index_count
|
|
|
|
|
|
|
|
def resize(self, count, index_count):
|
|
|
|
"""Resize this group.
|
|
|
|
|
|
|
|
:Parameters:
|
|
|
|
`count` : int
|
|
|
|
New number of vertices in the list.
|
|
|
|
`index_count` : int
|
|
|
|
New number of indices in the list.
|
|
|
|
|
|
|
|
"""
|
|
|
|
old_start = self.start
|
2021-09-23 06:34:23 +08:00
|
|
|
super().resize(count)
|
2021-04-16 23:21:06 +08:00
|
|
|
|
|
|
|
# Change indices (because vertices moved)
|
|
|
|
if old_start != self.start:
|
|
|
|
diff = self.start - old_start
|
|
|
|
self.indices[:] = [i + diff for i in self.indices]
|
|
|
|
|
|
|
|
# Resize indices
|
2022-03-05 23:10:18 +08:00
|
|
|
new_start = self.domain.safe_index_realloc(self.index_start, self.index_count, index_count)
|
2021-04-16 23:21:06 +08:00
|
|
|
if new_start != self.index_start:
|
2022-03-05 23:10:18 +08:00
|
|
|
old = self.domain.get_index_region(self.index_start, self.index_count)
|
|
|
|
new = self.domain.get_index_region(self.index_start, self.index_count)
|
2021-04-16 23:21:06 +08:00
|
|
|
new.array[:] = old.array[:]
|
|
|
|
new.invalidate()
|
2022-03-05 23:10:18 +08:00
|
|
|
|
2021-04-16 23:21:06 +08:00
|
|
|
self.index_start = new_start
|
|
|
|
self.index_count = index_count
|
|
|
|
self._indices_cache_version = None
|
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
"""Delete this group."""
|
2021-09-23 06:34:23 +08:00
|
|
|
super().delete()
|
2021-04-16 23:21:06 +08:00
|
|
|
self.domain.index_allocator.dealloc(self.index_start, self.index_count)
|
|
|
|
|
|
|
|
def migrate(self, domain):
|
2021-09-23 06:34:23 +08:00
|
|
|
"""Move this group from its current indexed domain and add to the
|
2021-04-16 23:21:06 +08:00
|
|
|
specified one. Attributes on domains must match. (In practice, used
|
|
|
|
to change parent state of some vertices).
|
|
|
|
|
|
|
|
:Parameters:
|
|
|
|
`domain` : `IndexedVertexDomain`
|
|
|
|
Indexed domain to migrate this vertex list to.
|
|
|
|
|
|
|
|
"""
|
|
|
|
old_start = self.start
|
|
|
|
old_domain = self.domain
|
2021-09-23 06:34:23 +08:00
|
|
|
super().migrate(domain)
|
2021-04-16 23:21:06 +08:00
|
|
|
|
|
|
|
# Note: this code renumber the indices of the *original* domain
|
|
|
|
# because the vertices are in a new position in the new domain
|
|
|
|
if old_start != self.start:
|
|
|
|
diff = self.start - old_start
|
2022-03-05 23:10:18 +08:00
|
|
|
old_indices = old_domain.get_index_region(self.index_start, self.index_count)
|
|
|
|
old_domain.set_index_region(self.index_start, self.index_count, [i + diff for i in old_indices])
|
2021-04-16 23:21:06 +08:00
|
|
|
|
|
|
|
# copy indices to new domain
|
2022-03-05 23:10:18 +08:00
|
|
|
old_array = old_domain.get_index_region(self.index_start, self.index_count)
|
2021-04-16 23:21:06 +08:00
|
|
|
# must delloc before calling safe_index_alloc or else problems when same
|
|
|
|
# batch is migrated to because index_start changes after dealloc
|
|
|
|
old_domain.index_allocator.dealloc(self.index_start, self.index_count)
|
2022-03-05 23:10:18 +08:00
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
new_start = self.domain.safe_index_alloc(self.index_count)
|
2022-03-05 23:10:18 +08:00
|
|
|
self.domain.set_index_region(new_start, self.index_count, old_array)
|
2021-09-23 06:34:23 +08:00
|
|
|
|
2021-04-16 23:21:06 +08:00
|
|
|
self.index_start = new_start
|
|
|
|
self._indices_cache_version = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def indices(self):
|
|
|
|
"""Array of index data."""
|
2022-02-22 23:22:11 +08:00
|
|
|
if self._indices_cache_version != self.domain.version:
|
2021-04-16 23:21:06 +08:00
|
|
|
domain = self.domain
|
2021-09-23 06:34:23 +08:00
|
|
|
self._indices_cache = domain.get_index_region(self.index_start, self.index_count)
|
2022-02-22 23:22:11 +08:00
|
|
|
self._indices_cache_version = domain.version
|
2021-04-16 23:21:06 +08:00
|
|
|
|
2022-03-05 23:10:18 +08:00
|
|
|
return self._indices_cache
|
2021-04-16 23:21:06 +08:00
|
|
|
|
|
|
|
@indices.setter
|
|
|
|
def indices(self, data):
|
2022-03-05 23:10:18 +08:00
|
|
|
self.domain.set_index_region(self.index_start, self.index_count, data)
|