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 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 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_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 _create(self): """ Create buffer on GPU """ if not self._handle: self._handle = gl.glGenBuffers(1)