Home > front end >  How to create a PyGLM array of float32 type from a list of python floats
How to create a PyGLM array of float32 type from a list of python floats

Time:01-24

I have this function to modify it:

def buffers_init_pnt() -> None:
    global mPoints
    global POINTS_VAOID
    global POINTS_VBOID 
    global glmverts_pnt_init 
    floatlist = []
    for item in sphere_nodes:
        floatlist.append(float(item[1]))
        floatlist.append(float(item[2]))
        floatlist.append(float(item[3]))
        glmverts_pnt_001 = glm.array(glm.float32,
            float(item[1]), float(item[2]), float(item[3])
            )          
        mPoints.append(Point(glmverts_pnt_001, POINTS_VAOID, POINTS_VBOID)) 
            
    buf = struct.pack('%sf' % len(floatlist), *floatlist)
    
    #I would like to create a glm array of float32 type from the list floatlist
    #glmtest = glm.array(glm.float32, 0.0, 0.0, 0.0)
    #glmtest = glm.array.from_bytes(buf)
    #glmtest.dtype = glm.float32 

    POINTS_VAOID = gl.glGenVertexArrays(1)
    POINTS_VBOID = gl.glGenBuffers(1)
    gl.glBindVertexArray(POINTS_VAOID)
    gl.glBindBuffer(gl.GL_ARRAY_BUFFER, POINTS_VBOID)
    gl.glBufferData(gl.GL_ARRAY_BUFFER, glmverts_pnt_init.nbytes, None, gl.GL_DYNAMIC_DRAW)
    gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, gl.GL_FALSE, 3 * glm.sizeof(glm.float32), None)
    gl.glEnableVertexAttribArray(0) 

I know there is a from_bytes() however I tried to use it and the dtype was showing Uint8 and I couldn't change it since it was write protected. Is there a way to create a glm array from a regular list of python floats?

CodePudding user response:

Create a ctypes array:

import ctypes
floatArray = (ctypes.c_float * len(floatlist))(*floatlist) 

This array can be directly used with PyOpenGLs glBufferData overload:

gl.glBufferData(gl.GL_ARRAY_BUFFER, floatArray, gl.GL_DYNAMIC_DRAW)
  • Related