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 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_initialize(self, event): gl.glClearColor(0, 0, 0, 1) # Enable blending gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE) # Note: normal GL requires these lines, ES 2.0 does not from OpenGL import GL gl.glEnable(GL.GL_VERTEX_PROGRAM_POINT_SIZE) gl.glEnable(GL.GL_POINT_SPRITE) # 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 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_LUMINANCE, im1.shape[1], im1.shape[0], 0, gl.GL_LUMINANCE, gl.GL_FLOAT, im1.astype(np.float32)) 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) # Create vertex buffer self._vbo_handle = gl.glGenBuffers(1)
def on_initialize(self, event): gl.glClearColor(0,0,0,1); # Enable blending gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE) # Note: normal GL requires these lines, ES 2.0 does not from OpenGL import GL gl.glEnable(GL.GL_VERTEX_PROGRAM_POINT_SIZE) gl.glEnable(GL.GL_POINT_SPRITE) # 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 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_LUMINANCE, im1.shape[1], im1.shape[0], 0, gl.GL_LUMINANCE, gl.GL_FLOAT, im1.astype(np.float32)) 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) # Create vertex buffer self._vbo_handle = gl.glGenBuffers(1)
def _process_pending_data(self, data, offset, level, format, clim): """ Process the pending data. Uploading the data (i.e. create a new texture) or updating it (a subsection). """ if isinstance(data, np.ndarray): # Convert data type to one supported by OpenGL data = convert_data(data, clim) # Set shape shape = data.shape # If data is of same shape as current texture, update is much faster if not offset: MAP = {gl.GL_TEXTURE_2D: 2, gl.ext.GL_TEXTURE_3D: 3} ndim = MAP.get(self._target, 0) if data.shape == self._texture_shape and self._valid: offset = [0 for i in self._texture_shape[:ndim]] elif isinstance(data, tuple): # Set shape shape = data else: raise TextureError('data is not a valid type, should not happen!') # Determine format (== internalformat) if format is None: format = get_format(shape, self._target) if offset: # Update: fast! gl.glBindTexture(self._target, self._handle) if self._handle <= 0 or not gl.glIsTexture(self._handle): raise TextureError( 'Cannot update texture if there is no texture.') self._upload_subdata(data, offset, format, level) else: # (re)upload: slower if self._valid: # We delete the existing texture first. In theory this # should not be necessary, but some implementations cause # memory leaks otherwise. self.delete() self._create() # Upload! self._activate() if isinstance(data, tuple): self._allocate_storage(shape, format, level) else: self._upload_data(data, format, level) # Set all parameters that the user set for param, value in self._texture_params.items(): gl.glTexParameter(self._target, param, value) self._pending_params = {} # We just applied all
def _process_pending_data(self, data, offset, level, format, clim): """ Process the pending data. Uploading the data (i.e. create a new texture) or updating it (a subsection). """ if isinstance(data, np.ndarray): # Convert data type to one supported by OpenGL data = convert_data(data, clim) # Set shape shape = data.shape # If data is of same shape as current texture, update is much faster if not offset: MAP = {gl.GL_TEXTURE_2D:2, gl.ext.GL_TEXTURE_3D:3} ndim = MAP.get(self._target, 0) if data.shape == self._texture_shape and self._valid: offset = [0 for i in self._texture_shape[:ndim]] elif isinstance(data, tuple): # Set shape shape = data else: raise TextureError('data is not a valid type, should not happen!') # Determine format (== internalformat) if format is None: format = get_format(shape, self._target) if offset: # Update: fast! gl.glBindTexture(self._target, self._handle) if self._handle <= 0 or not gl.glIsTexture(self._handle): raise TextureError('Cannot update texture if there is no texture.') self._upload_subdata(data, offset, format, level) else: # (re)upload: slower if self._valid: # We delete the existing texture first. In theory this # should not be necessary, but some implementations cause # memory leaks otherwise. self.delete() self._create() # Upload! self._activate() if isinstance(data, tuple): self._allocate_storage(shape, format, level) else: self._upload_data(data, format, level) # Set all parameters that the user set for param, value in self._texture_params.items(): gl.glTexParameter(self._target, param, value) self._pending_params = {} # We just applied all
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 _update(self): # If we use a 3D texture, we need an extension if self._target == gl.ext.GL_TEXTURE_3D: if not ext_available('GL_texture_3D'): raise TextureError('3D Texture not available.') # For each level ... for texLevel in self._levels.values(): # Need to resize? if texLevel.need_resize: texLevel.need_resize = False new_texture_created = False if self._valid and len(self._levels) == 1: # We delete the existing texture first. In theory this # should not be necessary, but some implementations cause # memory leaks otherwise. new_texture_created = True self.delete() self._create() # Allocate texture on GPU gl.glBindTexture(self._target, self._handle) #self._activate() self._allocate_shape(texLevel) # If not ok, warn (one time) if not gl.glIsTexture(self._handle): self._handle = 0 print('Warning enabling texture, the texture is not valid.') return if new_texture_created: # We have a new texture: apply all parameters that were set for param, value in self._texture_params.items(): gl.glTexParameter(self._target, param, value) self._pending_params = {} # We just applied all # Need to update some data? while texLevel.pending_data: data, clim, offset = texLevel.pending_data.pop(0) # Apply clim and convert data type to one supported by OpenGL data = convert_data(data, clim) # Upload gl.glBindTexture(self._target, self._handle) # self._activate() self._upload_data(data, texLevel, offset) # Check #if not gl.glIsTexture(self._handle): # raise TextureError('This should not happen (texture is invalid)') # Need to update any parameters? gl.glBindTexture(self._target, self._handle) # self._activate() while self._pending_params: param, value = self._pending_params.popitem() gl.glTexParameter(self._target, param, value)
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 _deactivate(self): gl.glBindTexture(self._target, 0)
def _activate(self): gl.glBindTexture(self._target, self._handle)