def paint(): n = 100 data = np.hstack( (.2 * np.random.randn(n, 2), np.random.rand(n, 4))).astype(np.float32) vs = compile_shader(VS, gl.GL_VERTEX_SHADER) fs = compile_shader(FS, gl.GL_FRAGMENT_SHADER) shaders_program = link_shader_program(vs, fs) buffer = gl.glGenBuffers(1) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, buffer) gl.glBufferData(gl.GL_ARRAY_BUFFER, data, gl.GL_STATIC_DRAW) l = gl.glGetAttribLocation(shaders_program, "position") gl.glVertexAttribPointer(l, 2, gl.GL_FLOAT, gl.GL_FALSE, 0, None) gl.glEnableVertexAttribArray(l) lc = gl.glGetAttribLocation(shaders_program, "color") gl.glVertexAttribPointer(lc, 4, gl.GL_FLOAT, gl.GL_FALSE, 2 * 4, None) gl.glEnableVertexAttribArray(lc) gl.glUseProgram(shaders_program) gl.glViewport(0, 0, 500, 500) gl.glClearColor(0., 0., 0., 1.) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, buffer) gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, data.shape[0])
def _update(self): """ Upload all pending data to GPU. """ # Bind buffer now gl.glBindBuffer(self._target, self._handle) # Allocate new size if necessary if self._need_resize: # This will only allocate the buffer on GPU # WARNING: we should check if this operation is ok gl.glBufferData(self._target, self._nbytes, None, self._usage) # debug #print("Creating a new buffer (%d) of %d bytes" # % (self._handle, self._nbytes)) self._need_resize = False # Upload data while self._pending_data: data, nbytes, offset = self._pending_data.pop(0) # debug # print("Uploading %d bytes at offset %d to buffer (%d)" # % (nbytes, offset, self._handle)) try: gl.glBufferSubData(self._target, offset, nbytes, data) except Exception as error: # This might be due to a driver error (seen on ATI), issue #64. # We try to detect this, and if we can use glBufferData instead if ( hasattr(error, 'err') and error.err == gl.GL_INVALID_VALUE and offset == 0 and nbytes == self._nbytes ): gl.glBufferData(self._target, nbytes, data, self._usage) else: raise
def on_paint(self, event): # Technically, we would only need to set u_time on every draw, # because the program is enabled at the beginning and never disabled. # In vispy, the program is re-enabled at each draw though and we # want to keep the code similar. gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) # Activate program and texture gl.glUseProgram(self._prog_handle) gl.glBindTexture(gl.GL_TEXTURE_2D, self._tex_handle) # Update VBO gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self._vbo_handle) gl.glBufferData(gl.GL_ARRAY_BUFFER, vertex_data.nbytes, vertex_data, gl.GL_DYNAMIC_DRAW) # Set attributes (again, the loc can be cached) loc = gl.glGetAttribLocation(self._prog_handle, 'a_lifetime'.encode('utf-8')) gl.glEnableVertexAttribArray(loc) gl.glVertexAttribPointer(loc, 1, gl.GL_FLOAT, False, 7 * 4, ctypes.c_voidp(0)) # loc = gl.glGetAttribLocation(self._prog_handle, 'a_startPosition'.encode('utf-8')) gl.glEnableVertexAttribArray(loc) gl.glVertexAttribPointer(loc, 3, gl.GL_FLOAT, False, 7 * 4, ctypes.c_voidp(1 * 4)) # loc = gl.glGetAttribLocation(self._prog_handle, 'a_endPosition'.encode('utf-8')) gl.glEnableVertexAttribArray(loc) gl.glVertexAttribPointer(loc, 3, gl.GL_FLOAT, False, 7 * 4, ctypes.c_voidp(4 * 4)) # loc = gl.glGetUniformLocation(self._prog_handle, 'u_color'.encode('utf-8')) gl.glUniform4f(loc, *self._color) # Set unforms loc = gl.glGetUniformLocation(self._prog_handle, 'u_time'.encode('utf-8')) gl.glUniform1f(loc, time.time() - self._starttime) # loc = gl.glGetUniformLocation(self._prog_handle, 'u_centerPosition'.encode('utf-8')) gl.glUniform3f(loc, *self._centerpos) # Draw gl.glDrawArrays(gl.GL_POINTS, 0, N) # New explosion? if time.time() - self._starttime > 1.5: self._new_explosion() # Redraw as fast as we can self.update()
def on_paint(self, event): # Technically, we would only need to set u_time on every draw, # because the program is enabled at the beginning and never disabled. # In vispy, the program is re-enabled at each draw though and we # want to keep the code similar. gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) # Activate program and texture gl.glUseProgram(self._prog_handle) gl.glBindTexture(gl.GL_TEXTURE_2D, self._tex_handle) # Update VBO gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self._vbo_handle) gl.glBufferData(gl.GL_ARRAY_BUFFER, vertex_data.nbytes, vertex_data, gl.GL_DYNAMIC_DRAW) # Set attributes (again, the loc can be cached) loc = gl.glGetAttribLocation(self._prog_handle, 'a_lifetime'.encode('utf-8')) gl.glEnableVertexAttribArray(loc) gl.glVertexAttribPointer(loc, 1, gl.GL_FLOAT, False, 7*4, ctypes.c_voidp(0)) # loc = gl.glGetAttribLocation(self._prog_handle, 'a_startPosition'.encode('utf-8')) gl.glEnableVertexAttribArray(loc) gl.glVertexAttribPointer(loc, 3, gl.GL_FLOAT, False, 7*4, ctypes.c_voidp(1*4)) # loc = gl.glGetAttribLocation(self._prog_handle, 'a_endPosition'.encode('utf-8')) gl.glEnableVertexAttribArray(loc) gl.glVertexAttribPointer(loc, 3, gl.GL_FLOAT, False, 7*4, ctypes.c_voidp(4*4)) # loc = gl.glGetUniformLocation(self._prog_handle, 'u_color'.encode('utf-8')) gl.glUniform4f(loc, *self._color) # Set unforms loc = gl.glGetUniformLocation(self._prog_handle, 'u_time'.encode('utf-8')) gl.glUniform1f(loc, time.time()-self._starttime) # loc = gl.glGetUniformLocation(self._prog_handle, 'u_centerPosition'.encode('utf-8')) gl.glUniform3f(loc, *self._centerpos) # Draw gl.glDrawArrays(gl.GL_POINTS, 0, N) # New explosion? if time.time() - self._starttime > 1.5: self._new_explosion() # Redraw as fast as we can self.update()
def on_paint(self, event): gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) # Activate program and texture gl.glUseProgram(self._prog_handle) gl.glBindTexture(gl.GL_TEXTURE_2D, self._tex_handle) # Set attributes (again, the loc can be cached) loc = gl.glGetAttribLocation(self._prog_handle, 'a_position'.encode('utf-8')) gl.glEnableVertexAttribArray(loc) if use_buffers: gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self._positions_handle) gl.glVertexAttribPointer(loc, 3, gl.GL_FLOAT, False, 0, None) else: gl.glBindBuffer(gl.GL_ARRAY_BUFFER, 0) # 0 means do not use buffer gl.glVertexAttribPointer(loc, 3, gl.GL_FLOAT, False, 0, positions) # loc = gl.glGetAttribLocation(self._prog_handle, 'a_texcoord'.encode('utf-8')) gl.glEnableVertexAttribArray(loc) if use_buffers: gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self._texcoords_handle) gl.glVertexAttribPointer(loc, 2, gl.GL_FLOAT, False, 0, None) else: gl.glBindBuffer(gl.GL_ARRAY_BUFFER, 0) # 0 means do not use buffer gl.glVertexAttribPointer(loc, 2, gl.GL_FLOAT, False, 0, texcoords) # Set uniforms (note that you could cache the locations) loc = gl.glGetUniformLocation(self._prog_handle, 'u_view'.encode('utf-8')) gl.glUniformMatrix4fv(loc, 1, False, self.view) loc = gl.glGetUniformLocation(self._prog_handle, 'u_model'.encode('utf-8')) gl.glUniformMatrix4fv(loc, 1, False, self.model) loc = gl.glGetUniformLocation(self._prog_handle, 'u_projection'.encode('utf-8')) gl.glUniformMatrix4fv(loc, 1, False, self.projection) # Draw if use_buffers: gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, self._faces_handle) gl.glDrawElements(gl.GL_TRIANGLES, faces.size, gl.GL_UNSIGNED_INT, None) else: gl.glDrawElements(gl.GL_TRIANGLES, faces.size, gl.GL_UNSIGNED_INT, faces)
def on_initialize(self, event): gl.glClearColor(1,1,1,1) gl.glEnable(gl.GL_DEPTH_TEST) # Create shader program self._prog_handle = gl.glCreateProgram() # Create vertex shader shader = gl.glCreateShader(gl.GL_VERTEX_SHADER) gl.glShaderSource(shader, VERT_CODE) gl.glCompileShader(shader) status = gl.glGetShaderiv(shader, gl.GL_COMPILE_STATUS) if not status: # We could show more useful info here, but that takes a few lines raise RuntimeError('Vertex shader did not compile.') else: gl.glAttachShader(self._prog_handle, shader) # Create fragment shader shader = gl.glCreateShader(gl.GL_FRAGMENT_SHADER) gl.glShaderSource(shader, FRAG_CODE) gl.glCompileShader(shader) status = gl.glGetShaderiv(shader, gl.GL_COMPILE_STATUS) if not status: # We could show more useful info here, but that takes a few lines raise RuntimeError('Fragment shader did not compile.') else: gl.glAttachShader(self._prog_handle, shader) # Link gl.glLinkProgram(self._prog_handle) status = gl.glGetProgramiv(self._prog_handle, gl.GL_LINK_STATUS) if not status: # We could show more useful info here, but that takes a few lines raise RuntimeError('Program did not link.') # Create texture im = io.crate() self._tex_handle = gl.glGenTextures(1) gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1) gl.glBindTexture(gl.GL_TEXTURE_2D, self._tex_handle) gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGB, im.shape[1], im.shape[0], 0, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, im) gl.glTexParameter(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR) gl.glTexParameter(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR) if use_buffers: # Create vertex buffer self._positions_handle = gl.glGenBuffers(1) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self._positions_handle) gl.glBufferData(gl.GL_ARRAY_BUFFER, positions.nbytes, positions, gl.GL_DYNAMIC_DRAW) # self._texcoords_handle = gl.glGenBuffers(1) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self._texcoords_handle) gl.glBufferData(gl.GL_ARRAY_BUFFER, texcoords.nbytes, texcoords, gl.GL_DYNAMIC_DRAW) # Create buffer for faces self._faces_handle = gl.glGenBuffers(1) gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, self._faces_handle) gl.glBufferData(gl.GL_ELEMENT_ARRAY_BUFFER, faces.nbytes, faces, gl.GL_DYNAMIC_DRAW)
def paint(): n = 100 data = np.hstack(( .2 * np.random.randn(n, 2), np.random.rand(n, 4) )).astype(np.float32) vs = compile_shader(VS, gl.GL_VERTEX_SHADER) fs = compile_shader(FS, gl.GL_FRAGMENT_SHADER) shaders_program = link_shader_program(vs, fs) buffer = gl.glGenBuffers(1) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, buffer) gl.glBufferData(gl.GL_ARRAY_BUFFER, data, gl.GL_STATIC_DRAW) l = gl.glGetAttribLocation(shaders_program, "position") gl.glVertexAttribPointer(l, 2, gl.GL_FLOAT, gl.GL_FALSE, 0, None) gl.glEnableVertexAttribArray(l); lc = gl.glGetAttribLocation(shaders_program, "color") gl.glVertexAttribPointer(lc, 4, gl.GL_FLOAT, gl.GL_FALSE, 2 * 4, None) gl.glEnableVertexAttribArray(lc); gl.glUseProgram(shaders_program) gl.glViewport(0, 0, 500, 500) gl.glClearColor(0., 0., 0., 1.) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, buffer) gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, data.shape[0])
def _update(self): """ Upload all pending data to GPU. """ # Bind buffer now gl.glBindBuffer(self._target, self._handle) # Allocate new size if necessary if self._need_resize: # This will only allocate the buffer on GPU # WARNING: we should check if this operation is ok gl.glBufferData(self._target, self._nbytes, None, self._usage) # debug #print("Creating a new buffer (%d) of %d bytes" # % (self._handle,self._nbytes)) self._need_resize = False # Upload data while self._pending_data: data, nbytes, offset = self._pending_data.pop(0) # debug # print("Uploading %d bytes at offset %d to buffer (%d)" # % (size, offset, self._handle)) gl.glBufferSubData(self._target, offset, nbytes, data)
def on_initialize(self, event): gl.glClearColor(1, 1, 1, 1) gl.glEnable(gl.GL_DEPTH_TEST) # Create shader program self._prog_handle = gl.glCreateProgram() # Create vertex shader shader = gl.glCreateShader(gl.GL_VERTEX_SHADER) gl.glShaderSource(shader, VERT_CODE) gl.glCompileShader(shader) status = gl.glGetShaderiv(shader, gl.GL_COMPILE_STATUS) if not status: # We could show more useful info here, but that takes a few lines raise RuntimeError('Vertex shader did not compile.') else: gl.glAttachShader(self._prog_handle, shader) # Create fragment shader shader = gl.glCreateShader(gl.GL_FRAGMENT_SHADER) gl.glShaderSource(shader, FRAG_CODE) gl.glCompileShader(shader) status = gl.glGetShaderiv(shader, gl.GL_COMPILE_STATUS) if not status: # We could show more useful info here, but that takes a few lines raise RuntimeError('Fragment shader did not compile.') else: gl.glAttachShader(self._prog_handle, shader) # Link gl.glLinkProgram(self._prog_handle) status = gl.glGetProgramiv(self._prog_handle, gl.GL_LINK_STATUS) if not status: # We could show more useful info here, but that takes a few lines raise RuntimeError('Program did not link.') # Create texture im = io.crate() self._tex_handle = gl.glGenTextures(1) gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1) gl.glBindTexture(gl.GL_TEXTURE_2D, self._tex_handle) gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGB, im.shape[1], im.shape[0], 0, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, im) gl.glTexParameter(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR) gl.glTexParameter(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR) if use_buffers: # Create vertex buffer self._positions_handle = gl.glGenBuffers(1) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self._positions_handle) gl.glBufferData(gl.GL_ARRAY_BUFFER, positions.nbytes, positions, gl.GL_DYNAMIC_DRAW) # self._texcoords_handle = gl.glGenBuffers(1) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self._texcoords_handle) gl.glBufferData(gl.GL_ARRAY_BUFFER, texcoords.nbytes, texcoords, gl.GL_DYNAMIC_DRAW) # Create buffer for faces self._faces_handle = gl.glGenBuffers(1) gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, self._faces_handle) gl.glBufferData(gl.GL_ELEMENT_ARRAY_BUFFER, faces.nbytes, faces, gl.GL_DYNAMIC_DRAW)
def upload(self, program): """ Actual upload of data to GPU memory """ # If there is not data, there is no point in uploading if self._data is None: if self._show_warning_notset: print("Value for attribute '%s' is not set." % self.name) self._show_warning_notset = False return # Check active status (mandatory) if self._loc is None: raise VariableError("Attribute is not active") # Generic vertex attribute (all vertices receive the same value) if self._generic: # Tell OpenGL to use the constant value gl.glDisableVertexAttribArray(self._loc) # Early exit if not self._dirty: return # Apply self._afunction(self._loc, *self._data) # Client side array elif isinstance(self._data, ClientVertexBuffer): # Tell OpenGL to use the array and not the glVertexAttrib* value gl.glEnableVertexAttribArray(self._loc) # Disable any VBO gl.glBindBuffer(gl.GL_ARRAY_BUFFER, 0) # Early exit (pointer to CPU-data is still known by Program) if not self._dirty: return # Get numpy array from its container data = self._data.data # Check attribute format against data format size, gtype, _ = gl_typeinfo[self._gtype] #if self._gtype != self._data._gtype: # raise ValueError("Data not compatible with attribute type") offset = 0 stride = self._data.stride # Apply (first disable any previous VertexBuffer) gl.glVertexAttribPointer(self._loc, size, gtype, False, stride, data) # Regular vertex buffer or vertex buffer view else: data = self._data # todo: check offset = -1? # Tell OpenGL to use the array and not the glVertexAttrib* value gl.glEnableVertexAttribArray(self._loc) # Enable the VBO program.activate_object(data) # Early exit if not self._dirty: return # Check attribute format against data format size, gtype, _ = gl_typeinfo[self._gtype] #if self._gtype != self._data._gtype: # raise ValueError("Data not compatible with attribute type") offset = self._data.offset stride = self._data.stride #size, gtype, dtype = gl_typeinfo[self._gtype] #offset, stride = data._offset, data._stride # view_params not on VertexBuffer # Make offset a pointer, or it will be interpreted as a small array offset = ctypes.c_void_p(offset) # Apply gl.glVertexAttribPointer(self._loc, size, gtype, False, stride, offset) # Mark as uploaded self._dirty = False
def _activate(self): """ Bind the buffer to some target """ gl.glBindBuffer(self._target, self._handle)
def _deactivate(self): """ Unbind the current bound buffer """ gl.glBindBuffer(self._target, 0)
def upload(self, program): """ Actual upload of data to GPU memory """ # If there is not data, there is no point in uploading if self._data is None: if self._show_warning_notset: print("Value for attribute '%s' is not set." % self.name) self._show_warning_notset = False return # Check active status (mandatory) if self._loc is None: raise VariableError("Attribute is not active") # Note: It has been seen (on my (AK) machine) that attributes # are *not* local to the program object; the splitscreen example # is broken if we use the early exits here. # todo: instead of setting dirty to true, remove early exits # (leaving for now for testing on other machines) self._dirty = True # Generic vertex attribute (all vertices receive the same value) if self._generic: # Tell OpenGL to use the constant value gl.glDisableVertexAttribArray(self._loc) # Early exit if not self._dirty: return # Apply self._afunction(self._loc, *self._data) # Client side array elif isinstance(self._data, ClientVertexBuffer): # Tell OpenGL to use the array and not the glVertexAttrib* value gl.glEnableVertexAttribArray(self._loc) # Disable any VBO gl.glBindBuffer(gl.GL_ARRAY_BUFFER, 0) # Early exit (pointer to CPU-data is still known by Program) if not self._dirty: return # Get numpy array from its container data = self._data.data # Check attribute format against data format size, gtype, _ = gl_typeinfo[self._gtype] #if self._gtype != self._data._gtype: # raise ValueError("Data not compatible with attribute type") offset = 0 stride = self._data.stride # Apply (first disable any previous VertexBuffer) gl.glVertexAttribPointer(self._loc, size, gtype, False, stride, data) # Regular vertex buffer or vertex buffer view else: data = self._data # todo: check offset = -1? # Tell OpenGL to use the array and not the glVertexAttrib* value gl.glEnableVertexAttribArray(self._loc) # Enable the VBO program.activate_object(data) # Early exit if not self._dirty: return # Check attribute format against data format size, gtype, _ = gl_typeinfo[self._gtype] #if self._gtype != self._data._gtype: # raise ValueError("Data not compatible with attribute type") offset = self._data.offset stride = self._data.stride #size, gtype, dtype = gl_typeinfo[self._gtype] #offset, stride = data._offset, data._stride # view_params not on VertexBuffer # Make offset a pointer, or it will be interpreted as a small array offset = ctypes.c_void_p(offset) # Apply gl.glVertexAttribPointer(self._loc, size, gtype, False, stride, offset) # Mark as uploaded self._dirty = False