Python OpenGL.GL.glBindBuffer() Examples

The following are 18 code examples of OpenGL.GL.glBindBuffer(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module OpenGL.GL , or try the search function .
Example #1
Source File: glhelpers.py    From bluesky with GNU General Public License v3.0 6 votes vote down vote up
def update_buffer(buf_id, data, offset=0, target=gl.GL_ARRAY_BUFFER):

    global msg1282

    try:
        gl.glBindBuffer(target, buf_id)
        gl.glBufferSubData(target, offset, data.nbytes, data)

    except Exception as err:
        if err.err==1282:
            if not msg1282:
                print("update_buffer: Communication aborted (1282)")
            msg1282 = True
        else:
            print("update_buffer in glhelpers.py: Could not update buffer due to GLError code:",\
                  err.err) 
Example #2
Source File: glhelpers.py    From bluesky with GNU General Public License v3.0 5 votes vote down vote up
def create_empty_buffer(size, target=gl.GL_ARRAY_BUFFER, usage=gl.GL_STATIC_DRAW):
    buf_id = gl.glGenBuffers(1)
    gl.glBindBuffer(target, buf_id)
    gl.glBufferData(target, size, None, usage)
    return buf_id 
Example #3
Source File: hellovr_glfw.py    From pyopenvr with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def set_up_companion_window(self):
        if not self.hmd:
            return
        verts = list()
        # left eye verts
        verts.append(((-1, -1), (0, 0)))
        verts.append(((0, -1), (1, 0)))
        verts.append(((-1, 1), (0, 1)))
        verts.append(((0, 1), (1, 1)))
        # right eye verts
        verts.append(((0, -1), (0, 0)))
        verts.append(((1, -1), (1, 0)))
        verts.append(((0, 1), (0, 1)))
        verts.append(((1, 1), (1, 1)))
        vIndices = numpy.array([0, 1, 3, 0, 3, 2, 4, 5, 7, 4, 7, 6], dtype=numpy.uint16)
        self.companion_window_index_size = len(vIndices)
        self.companion_window_vao = GL.glGenVertexArrays(1)
        GL.glBindVertexArray(self.companion_window_vao)
        #
        self.companion_window_id_vert_buffer = GL.glGenBuffers(1)
        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.companion_window_id_vert_buffer)
        vVerts = numpy.array(verts, dtype=numpy.float32).flatten()
        GL.glBufferData(GL.GL_ARRAY_BUFFER, vVerts, GL.GL_STATIC_DRAW)
        #
        self.companion_window_id_index_buffer = GL.glGenBuffers(1)
        GL.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, self.companion_window_id_index_buffer)
        GL.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER, vIndices, GL.GL_STATIC_DRAW)
        #
        f_size = sizeof(c_float)
        GL.glEnableVertexAttribArray(0)
        GL.glVertexAttribPointer(0, 2, GL.GL_FLOAT, False, 4 * f_size, cast(0 * f_size, c_void_p))
        #
        GL.glEnableVertexAttribArray(1)
        GL.glVertexAttribPointer(1, 2, GL.GL_FLOAT, False, 4 * f_size, cast(2 * f_size, c_void_p))
        #
        GL.glBindVertexArray(0)
        GL.glDisableVertexAttribArray(0)
        GL.glDisableVertexAttribArray(1)
        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, 0)
        GL.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0) 
Example #4
Source File: hellovr_glfw.py    From pyopenvr with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def set_up_scene(self):
        self.vertex_data = []
        v = self.scene_volume_init
        s = self.scale_spacing
        scale = 0.3
        mat_scale = numpy.diag(numpy.array([scale, scale, scale, 1], dtype=numpy.float32))
        t = -0.5 * v * s
        mat_translate = translate(t, t, t)
        mat = mat_translate @ mat_scale
        for z in range(v):
            for y in range(v):
                for x in range(v):
                    self.add_cube_to_scene(mat)
                    mat = translate(s, 0, 0) @ mat
                mat = translate(-v * s, s, 0) @ mat
            mat = translate(0, -v * s, s) @ mat
        vertex_data = numpy.array(self.vertex_data, dtype=numpy.float32).flatten()
        self.vert_count = len(vertex_data) / 5
        self.scene_vao = GL.glGenVertexArrays(1)
        GL.glBindVertexArray(self.scene_vao)
        self.scene_vert_buffer = GL.glGenBuffers(1)
        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.scene_vert_buffer)
        GL.glBufferData(GL.GL_ARRAY_BUFFER, vertex_data, GL.GL_STATIC_DRAW)
        f_size = sizeof(c_float)
        stride = 5 * f_size
        GL.glEnableVertexAttribArray(0)
        GL.glVertexAttribPointer(0, 3, GL.GL_FLOAT, False, stride, cast(0 * f_size, c_void_p))
        GL.glEnableVertexAttribArray(1)
        GL.glVertexAttribPointer(1, 2, GL.GL_FLOAT, False, stride, cast(3 * f_size, c_void_p))
        GL.glBindVertexArray(0)
        GL.glDisableVertexAttribArray(0)
        GL.glDisableVertexAttribArray(1) 
Example #5
Source File: fullscreen_quad.py    From dm_control with Apache License 2.0 5 votes vote down vote up
def _init_geometry(self):
    """Initializes the fullscreen quad geometry."""
    vertex_buffer = GL.glGenBuffers(1)
    GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vertex_buffer)
    GL.glBufferData(
        GL.GL_ARRAY_BUFFER,
        _FULLSCREEN_QUAD_VERTEX_POSITONS_AND_TEXTURE_COORDS.nbytes,
        _FULLSCREEN_QUAD_VERTEX_POSITONS_AND_TEXTURE_COORDS, GL.GL_STATIC_DRAW) 
Example #6
Source File: glhelpers.py    From bluesky with GNU General Public License v3.0 5 votes vote down vote up
def bind_attrib(self, attrib_id, size, data, storagetype=gl.GL_STATIC_DRAW, instance_divisor=0, datatype=gl.GL_FLOAT, stride=0, offset=None, normalize=False):
        if RenderObject.bound_vao is not self.vao_id:
            gl.glBindVertexArray(self.vao_id)
            RenderObject.bound_vao = self.vao_id

        # Keep track of max instance divisor
        self.max_instance_divisor = max(instance_divisor, self.max_instance_divisor)

        # If the input is an array create a new GL buffer, otherwise assume the buffer already exists and a buffer ID is passed
        if type(data) is np.ndarray:
            # Get an index to one new buffer in GPU mem, bind it, and copy the array data to it
            buf_id = gl.glGenBuffers(1)
            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, buf_id)
            gl.glBufferData(gl.GL_ARRAY_BUFFER, data.nbytes, data, storagetype)
        else:
            # Assume that a GLuint is passed which means that the buffer is already in GPU memory
            buf_id = data
            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, buf_id)

        # Assign this buffer to one of the attributes in the shader
        gl.glEnableVertexAttribArray(attrib_id)
        gl.glVertexAttribPointer(attrib_id, size, datatype, normalize, stride, offset)
        # For instanced data, indicate per how many instances we move a step in the buffer (1=per instance)
        if instance_divisor > 0:
            gl.glVertexAttribDivisor(attrib_id, instance_divisor)
        # Clean up
        gl.glDisableVertexAttribArray(attrib_id)

        self.enabled_attributes[attrib_id] = [size, buf_id, instance_divisor, datatype]

        return buf_id 
Example #7
Source File: glhelpers.py    From bluesky with GNU General Public License v3.0 5 votes vote down vote up
def update(self, offset=0, size=None):
        if size is None:
            size = self.nbytes
        gl.glBindBuffer(gl.GL_UNIFORM_BUFFER, self.ubo)
        gl.glBufferSubData(gl.GL_UNIFORM_BUFFER, offset, size, self.pdata) 
Example #8
Source File: glhelpers.py    From bluesky with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, data):
        if isinstance(data, np.ndarray):
            self.pdata  = data
            self.nbytes = data.nbytes
        else:
            self.pdata  = pointer(data)
            self.nbytes = sizeof(data)
        self.ubo = gl.glGenBuffers(1)
        self.binding = self.max_binding
        gl.glBindBuffer(gl.GL_UNIFORM_BUFFER, self.ubo)
        gl.glBufferData(gl.GL_UNIFORM_BUFFER, self.nbytes, self.pdata, gl.GL_STREAM_DRAW)
        gl.glBindBufferBase(gl.GL_UNIFORM_BUFFER, self.binding, self.ubo)
        UniformBuffer.max_binding += 1 
Example #9
Source File: glhelpers.py    From bluesky with GNU General Public License v3.0 5 votes vote down vote up
def update_buffer(buf_id, data, offset=0, target=gl.GL_ARRAY_BUFFER):
    gl.glBindBuffer(target, buf_id)
    gl.glBufferSubData(target, offset, data.nbytes, data) 
Example #10
Source File: glhelpers.py    From bluesky with GNU General Public License v3.0 5 votes vote down vote up
def bind_attrib(self, attrib_id, size, data, storagetype=gl.GL_STATIC_DRAW, instance_divisor=0, datatype=gl.GL_FLOAT, stride=0, offset=None, normalize=False):
        if RenderObject.bound_vao is not self.vao_id:
            gl.glBindVertexArray(self.vao_id)
            RenderObject.bound_vao = self.vao_id

        # Keep track of max instance divisor
        self.max_instance_divisor = max(instance_divisor, self.max_instance_divisor)

        # If the input is an array create a new GL buffer, otherwise assume the buffer already exists and a buffer ID is passed
        if type(data) is np.ndarray:
            # Get an index to one new buffer in GPU mem, bind it, and copy the array data to it
            buf_id = gl.glGenBuffers(1)
            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, buf_id)
            gl.glBufferData(gl.GL_ARRAY_BUFFER, data.nbytes, data, storagetype)
        else:
            # Assume that a GLuint is passed which means that the buffer is already in GPU memory
            buf_id = data
            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, buf_id)

        # Assign this buffer to one of the attributes in the shader
        gl.glEnableVertexAttribArray(attrib_id)
        gl.glVertexAttribPointer(attrib_id, size, datatype, normalize, stride, offset)
        # For instanced data, indicate per how many instances we move a step in the buffer (1=per instance)
        if instance_divisor > 0:
            gl.glVertexAttribDivisor(attrib_id, instance_divisor)
        # Clean up
        gl.glDisableVertexAttribArray(attrib_id)

        self.enabled_attributes[attrib_id] = [size, buf_id, instance_divisor, datatype]

        return buf_id 
Example #11
Source File: glhelpers.py    From bluesky with GNU General Public License v3.0 5 votes vote down vote up
def update(self, offset=0, size=None):
        if size is None:
            size = self.nbytes
        gl.glBindBuffer(gl.GL_UNIFORM_BUFFER, self.ubo)
        gl.glBufferSubData(gl.GL_UNIFORM_BUFFER, offset, size, pointer(self.data)) 
Example #12
Source File: glhelpers.py    From bluesky with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, data):
        self.data = data
        self.nbytes = sizeof(data)
        self.ubo = gl.glGenBuffers(1)
        self.binding = self.max_binding
        gl.glBindBuffer(gl.GL_UNIFORM_BUFFER, self.ubo)
        gl.glBufferData(gl.GL_UNIFORM_BUFFER, self.nbytes, pointer(self.data), gl.GL_STREAM_DRAW)
        gl.glBindBufferBase(gl.GL_UNIFORM_BUFFER, self.binding, self.ubo)
        UniformBuffer.max_binding += 1 
Example #13
Source File: glhelpers.py    From bluesky with GNU General Public License v3.0 5 votes vote down vote up
def create_empty_buffer(size, target=gl.GL_ARRAY_BUFFER, usage=gl.GL_STATIC_DRAW):
    buf_id = gl.glGenBuffers(1)
    gl.glBindBuffer(target, buf_id)
    gl.glBufferData(target, size, None, usage)
    return buf_id 
Example #14
Source File: glow.py    From semantic-kitti-api with MIT License 5 votes vote down vote up
def release(self):
    gl.glBindBuffer(self.target_, 0) 
Example #15
Source File: glow.py    From semantic-kitti-api with MIT License 5 votes vote down vote up
def bind(self):
    gl.glBindBuffer(self.target_, self.id_) 
Example #16
Source File: glow.py    From semantic-kitti-api with MIT License 5 votes vote down vote up
def assign(self, array):
    gl.glBindBuffer(self.target_, self.id_)
    gl.glBufferData(self.target_, array, self.usage_)
    gl.glBindBuffer(self.target_, 0) 
Example #17
Source File: opengl.py    From pyimgui with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def _create_device_objects(self):
        # save state
        last_texture = gl.glGetIntegerv(gl.GL_TEXTURE_BINDING_2D)
        last_array_buffer = gl.glGetIntegerv(gl.GL_ARRAY_BUFFER_BINDING)

        last_vertex_array = gl.glGetIntegerv(gl.GL_VERTEX_ARRAY_BINDING)

        self._shader_handle = gl.glCreateProgram()
        # note: no need to store shader parts handles after linking
        vertex_shader = gl.glCreateShader(gl.GL_VERTEX_SHADER)
        fragment_shader = gl.glCreateShader(gl.GL_FRAGMENT_SHADER)

        gl.glShaderSource(vertex_shader, self.VERTEX_SHADER_SRC)
        gl.glShaderSource(fragment_shader, self.FRAGMENT_SHADER_SRC)
        gl.glCompileShader(vertex_shader)
        gl.glCompileShader(fragment_shader)

        gl.glAttachShader(self._shader_handle, vertex_shader)
        gl.glAttachShader(self._shader_handle, fragment_shader)

        gl.glLinkProgram(self._shader_handle)

        # note: after linking shaders can be removed
        gl.glDeleteShader(vertex_shader)
        gl.glDeleteShader(fragment_shader)

        self._attrib_location_tex = gl.glGetUniformLocation(self._shader_handle, "Texture")
        self._attrib_proj_mtx = gl.glGetUniformLocation(self._shader_handle, "ProjMtx")
        self._attrib_location_position = gl.glGetAttribLocation(self._shader_handle, "Position")
        self._attrib_location_uv = gl.glGetAttribLocation(self._shader_handle, "UV")
        self._attrib_location_color = gl.glGetAttribLocation(self._shader_handle, "Color")

        self._vbo_handle = gl.glGenBuffers(1)
        self._elements_handle = gl.glGenBuffers(1)

        self._vao_handle = gl.glGenVertexArrays(1)
        gl.glBindVertexArray(self._vao_handle)
        gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self._vbo_handle)

        gl.glEnableVertexAttribArray(self._attrib_location_position)
        gl.glEnableVertexAttribArray(self._attrib_location_uv)
        gl.glEnableVertexAttribArray(self._attrib_location_color)

        gl.glVertexAttribPointer(self._attrib_location_position, 2, gl.GL_FLOAT, gl.GL_FALSE, imgui.VERTEX_SIZE, ctypes.c_void_p(imgui.VERTEX_BUFFER_POS_OFFSET))
        gl.glVertexAttribPointer(self._attrib_location_uv, 2, gl.GL_FLOAT, gl.GL_FALSE, imgui.VERTEX_SIZE, ctypes.c_void_p(imgui.VERTEX_BUFFER_UV_OFFSET))
        gl.glVertexAttribPointer(self._attrib_location_color, 4, gl.GL_UNSIGNED_BYTE, gl.GL_TRUE, imgui.VERTEX_SIZE, ctypes.c_void_p(imgui.VERTEX_BUFFER_COL_OFFSET))

        # restore state
        gl.glBindTexture(gl.GL_TEXTURE_2D, last_texture)
        gl.glBindBuffer(gl.GL_ARRAY_BUFFER, last_array_buffer)
        gl.glBindVertexArray(last_vertex_array) 
Example #18
Source File: hellovr_glfw.py    From pyopenvr with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
def __init__(self, name, vr_model, vr_diffuse_texture):
        self.name = name
        # create and bind a VAO to hold state for this model
        self.vertex_array = GL.glGenVertexArrays(1)
        GL.glBindVertexArray(self.vertex_array)
        # Populate a vertex buffer
        self.vertex_buffer = GL.glGenBuffers(1)
        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.vertex_buffer)
        GL.glBufferData(GL.GL_ARRAY_BUFFER,
                        sizeof(openvr.RenderModel_Vertex_t) * vr_model.unVertexCount,
                        vr_model.rVertexData, GL.GL_STATIC_DRAW)
        # Identify the components in the vertex buffer
        GL.glEnableVertexAttribArray(0)
        hv3sz = sizeof(openvr.HmdVector3_t)
        GL.glVertexAttribPointer(0, 3, GL.GL_FLOAT, False, sizeof(openvr.RenderModel_Vertex_t),
                                 cast(0 * hv3sz, c_void_p))
        GL.glEnableVertexAttribArray(1)
        GL.glVertexAttribPointer(1, 3, GL.GL_FLOAT, False, sizeof(openvr.RenderModel_Vertex_t),
                                 cast(1 * hv3sz, c_void_p))
        GL.glEnableVertexAttribArray(2)
        GL.glVertexAttribPointer(2, 2, GL.GL_FLOAT, False, sizeof(openvr.RenderModel_Vertex_t),
                                 cast(2 * hv3sz, c_void_p))
        # Create and populate the index buffer
        self.index_buffer = GL.glGenBuffers(1)
        GL.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, self.index_buffer)
        GL.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER,
                        sizeof(c_uint16) * vr_model.unTriangleCount * 3,
                        vr_model.rIndexData, GL.GL_STATIC_DRAW)
        GL.glBindVertexArray(0)
        # create and populate the texture
        self.texture = GL.glGenTextures(1)
        GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture)
        GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, vr_diffuse_texture.unWidth, vr_diffuse_texture.unHeight,
                        0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, vr_diffuse_texture.rubTextureMapData)
        # If this renders black ask McJohn what's wrong.
        GL.glGenerateMipmap(GL.GL_TEXTURE_2D)
        GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE)
        GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE)
        GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR)
        GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR_MIPMAP_LINEAR)
        f_largest = GL.glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT)
        GL.glTexParameterf(GL.GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, f_largest)
        GL.glBindTexture(GL.GL_TEXTURE_2D, 0)
        self.vertex_count = vr_model.unTriangleCount * 3