Hi all!
So I’m trying to write an arbitrary length array of data to each vertex in a model. The idea is: if you can give a vertex a color with 3 or 4 components, I’m looking to give a vertex a color with any number of components ( a color spectrum of wavelengths ). Is this possible?
Here’s the segment of python code I’ve tried:
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #
# Create custom GeomVertexArrayFormat
spectrum = GeomVertexArrayFormat( )
spectrum.addColumn( "vertex", 3, Geom.NTFloat32, Geom.CPoint )
spectrum.addColumn( "texcoord", 2, Geom.NTFloat32, Geom.CTexcoord)
# num_components is variable
# Geom.C_other?? Maybe a different Geom.___?
spectrum.addColumn( "spectrum", num_components, Geom.NTFloat32, Geom.C_other )
spectrum_format = GeomVertexFormat( )
spectrum_format.add_array( spectrum )
spectrum_format = GeomVertexFormat.registerFormat( spectrum_format )
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #
def fill_custom_vertices( num_rows ):
# Create GeomVertexData with custom format and set up writers
custom_vdata = GeomVertexData( 'custom_vdata', spectrum_format, Geom.UHStatic )
custom_vdata.setNumRows( num_rows )
wvertex = GeomVertexWriter( custom_vdata, 'vertex' )
wtexcoord = GeomVertexWriter( custom_vdata, 'texcoord' )
wspectrum = GeomVertexWriter( custom_vdata, 'spectrum' )
# Probably not the most efficient way to type this out
vertex_arr = np.random.randint( 2, size = 3 )
vertex_vec = LVecBase3f( vertex_arr[ 0 ], vertex_arr[ 1 ], vertex_arr[ 2 ] )
texcoord_arr = np.random.randint( 2, size = 2 )
texcoord_vec = LVecBase2f( texcoord_arr[ 0 ], texcoord_arr[ 1 ] )
for row in range( num_rows ):
wvertex.addData3( vertex_vec )
wtexcoord.addData2( texcoord_vec )
# If num_components == 4, this code works as there's a built in LVecBase4f class and the
# ability to write 4 component data with GeomVertexWriter.addData4
spec_arr = np.random.randint( 2, size = 4 )
spec_vec = LVecBase4f( spec_arr[0], spec_arr[1], spec_arr[2], spec_arr[3] )
for row in ( num_rows ):
wspectrum.addData4( spec_vec )
# Is there a way to do something similar for an arbitrarily long array (arbitrary number of
# components?) I can create a GeomVertexArrayFormat with a column with an arbitrary
# number of components, I'm just not sure how to write data with the same number of
# components to it. For example, I figured it would be something like:
spec_arr = np.random.randint( 2, size = 7 )
spec_vec = LVecBase4f( spec_arr[0], spec_arr[1], spec_arr[2], spec_arr[3], spec_arr[4], spec_arr[5], spec_arr[6] )
wspectrum.addData7( spec_vec )
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #
I’m sure there’s a better way to write data to a vertex, as well as build the LVecBase#f objects from numpy arrays, but I’m more wondering about write that arbitrary component data. Is this the way to do it? Is it possible? Is there a better way that I’m missing? I’m pretty new to working with Panda3D, so I appreciate any help you may have to offer.
Thanks and all the best!