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 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 _mark_active_attributes(self): """ Mark which attributes are active and set the location. Called after linking. """ count = gl.glGetProgramiv(self.handle, gl.GL_ACTIVE_ATTRIBUTES) # This match a name of the form "name[size]" (= array) regex = re.compile("""(?P<name>\w+)\s*(\[(?P<size>\d+)\])""") # Find active attributes self._active_attributes = {} for i in range(count): name, size, gtype = gl.glGetActiveAttrib(self.handle, i) loc = gl.glGetAttribLocation(self._handle, name) name = name.decode('utf-8') # This checks if the attribute is an array # Name will be something like xxx[0] instead of xxx m = regex.match(name) # When attribute is an array, size corresponds to the highest used index if m: name = m.group('name') if size >= 1: for i in range(size): name = '%s[%d]' % (m.group('name'),i) self._active_attributes[name] = loc else: self._active_attributes[name] = loc # Mark these as active (loc non-None means active) for attribute in self._attributes.values(): attribute._loc = self._active_attributes.get(attribute.name, None)
def _mark_active_attributes(self): """ Mark which attributes are active and set the location. Called after linking. """ count = gl.glGetProgramiv(self.handle, gl.GL_ACTIVE_ATTRIBUTES) # This match a name of the form "name[size]" (= array) regex = re.compile("""(?P<name>\w+)\s*(\[(?P<size>\d+)\])""") # Find active attributes self._active_attributes = {} for i in range(count): name, size, gtype = gl.glGetActiveAttrib(self.handle, i) loc = gl.glGetAttribLocation(self._handle, name) name = name.decode('utf-8') # This checks if the attribute is an array # Name will be something like xxx[0] instead of xxx m = regex.match(name) # When attribute is an array, size corresponds to the highest used index if m: name = m.group('name') if size >= 1: for i in range(size): name = '%s[%d]' % (m.group('name'), i) self._active_attributes[name] = loc else: self._active_attributes[name] = loc # Mark these as active (loc non-None means active) for attribute in self._attributes.values(): attribute._loc = self._active_attributes.get(attribute.name, None)
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 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])