def __init__(self, vtype, utype): self.dash_atlas = None # Convert types to lists (in case they were already dtypes) such that # we can append new fields vtype = eval(str(np.dtype(vtype))) utype = eval(str(np.dtype(utype))) # We add a uniform index to access uniform data from texture vtype.append(('a_index', 'f4')) # Check if given utype is made of float32 only rutype = dtype_reduce(utype) if type(rutype[0]) is not str or rutype[2] != 'float32': raise CollectionError( "Uniform type cannot de reduced to float32 only") else: count = rutype[1] size = count // 4 if count % 4: size += 1 utype.append(('unnused', 'f4', size * 4 - count)) count = size * 4 self.utype = utype self._vbuffer = VertexBuffer(vtype) self._ubuffer = DynamicBuffer(utype) self._ubuffer_id = 0 self._ubuffer_shape = [0, count] self._dirty = True
def __init__(self, vtype, utype): # Convert types to lists (in case they were already dtypes) such that # we can append new fields vtype = eval(str(np.dtype(vtype))) utype = eval(str(np.dtype(utype))) # We add a uniform index to access uniform data from texture vtype.append( ('a_index', 'f4') ) # Check if given utype is made of float32 only rutype = dtype_reduce(utype) if type(rutype[0]) is not str or rutype[2] != 'float32': raise CollectionError("Uniform type cannot de reduced to float32 only") else: count = rutype[1] count2 = int(math.pow(2, math.ceil(math.log(count, 2)))) if (count2 - count) > 0: utype.append(('unused', 'f4', count2-count)) self._count = count2 self.utype = utype self._vbuffer = VertexBuffer(vtype) self._ubuffer = DynamicBuffer( utype ) self._ubuffer_id = 0 self._max_texture_size = gl.glGetInteger(gl.GL_MAX_TEXTURE_SIZE) self._compute_ushape(1) self._ubuffer.reserve( self._ushape[1] / (count/4) ) self._dirty = True
def build(self): self.vertices = np.zeros(len(self._text) * 4, dtype=[('position', 'f4', 3), ('tex_coord', 'f4', 2), ('color', 'f4', 4)]) self.indices = np.zeros((len(self._text) * 6, ), dtype=np.uint32) prev = None x, y = 0, 0 for i, charcode in enumerate(self._text): glyph = self._font[charcode] kerning = glyph.get_kerning(prev) / 64.0 x0 = int(x + glyph.offset[0] + kerning) y0 = int(y + glyph.offset[1]) x1 = x0 + glyph.size[0] y1 = y0 - glyph.size[1] u0 = glyph.texcoords[0] v0 = glyph.texcoords[1] u1 = glyph.texcoords[2] v1 = glyph.texcoords[3] index = i * 4 indices = [ index, index + 1, index + 2, index, index + 2, index + 3 ] vertices = [[x0, y0, 0], [x0, y1, 0], [x1, y1, 0], [x1, y0, 0]] texcoords = [[u0, v0], [u0, v1], [u1, v1], [u1, v0]] colors = [ self._color, ] * 4 self.indices[i * 6:i * 6 + 6] = indices self.vertices['position'][i * 4:i * 4 + 4] = vertices self.vertices['tex_coord'][i * 4:i * 4 + 4] = texcoords self.vertices['color'][i * 4:i * 4 + 4] = colors x += glyph.advance[0] / 64.0 + kerning y += glyph.advance[1] / 64.0 prev = charcode width = x + glyph.advance[0] / 64.0 + glyph.size[0] if self._anchor_y == 'top': dy = -round(self._font.ascender) elif self._anchor_y == 'center': dy = +round(-self._font.height / 2 - self._font.descender) elif self._anchor_y == 'bottom': dy = -round(self._font.descender) else: dy = 0 if self._anchor_x == 'right': dx = -width / 1.0 elif self._anchor_x == 'center': dx = -width / 2.0 else: dx = 0 self.vertices['position'] += self._x + round(dx), self._y + round( dy), self._z self.buffer = VertexBuffer(self.vertices, self.indices)
def __init__ (self, segments, linewidths = 1.0, colors = (0,0,0,1), caps = (1,1), transforms = (0,0,1,0), antialiased = 0.75): """ """ Collection.__init__(self) self.dtype = np.dtype( [('position', 'f4', 3), ('tex_coord', 'f4', 2), ('color', 'f4', 4), ('transform', 'f4', 4), ('tangent', 'f4', 2), ('cap', 'f4', 2), ('length', 'f4', 1), ('thickness', 'f4', 1), ('support', 'f4', 1)] ) vertices = np.zeros(0, dtype = self.dtype) self._buffer = VertexBuffer(vertices) self.append(segments, linewidths, colors, caps, transforms, antialiased) vert = open('./line.vert').read() frag = open('./line.frag').read() self._shader = Shader(vert,frag)
def __init__ (self, centers, radii, linewidths = 1.0, edgecolors = (0,0,0,1), facecolors = (0,0,0,1), transforms = (0,0,1,0), antialiased = 0.75): """ """ Collection.__init__(self) self.dtype = np.dtype( [('position', 'f4', 3), ('tex_coord', 'f4', 2), ('color', 'f4', 4), ('facecolor', 'f4', 4), ('transform', 'f4', 4), ('radius', 'f4', 2), ('thickness', 'f4', 1), ('support', 'f4', 1)] ) vertices = np.zeros(0, dtype = self.dtype) self._buffer = VertexBuffer(vertices) self.append(centers, radii, linewidths, edgecolors, facecolors, transforms, antialiased) vert = open('./ellipse.vert').read() frag = open('./ellipse.frag').read() self._shader = Shader(vert,frag)
# vertices v = [ [ 1, 1, 1], [-1, 1, 1], [-1,-1, 1], [ 1,-1, 1], [ 1,-1,-1], [ 1, 1,-1], [-1, 1,-1], [-1,-1,-1] ] # normals n = [ [ 0, 0, 1], [ 1, 0, 0], [ 0, 1, 0] , [-1, 0, 1], [ 0,-1, 0], [ 0, 0,-1] ] # colors c = [ [0, 1, 1, 1], [0, 0, 1, 1], [0, 0, 0, 1], [0, 1, 0, 1], [1, 1, 0, 1], [1, 1, 1, 1], [1, 0, 1, 1], [1, 0, 0, 1] ]; V = np.array([(v[0],n[0],c[0]), (v[1],n[0],c[1]), (v[2],n[0],c[2]), (v[3],n[0],c[3]), (v[0],n[1],c[0]), (v[3],n[1],c[3]), (v[4],n[1],c[4]), (v[5],n[1],c[5]), (v[0],n[2],c[0]), (v[5],n[2],c[5]), (v[6],n[2],c[6]), (v[1],n[2],c[1]), (v[1],n[3],c[1]), (v[6],n[3],c[6]), (v[7],n[3],c[7]), (v[2],n[3],c[2]), (v[7],n[4],c[7]), (v[4],n[4],c[4]), (v[3],n[4],c[3]), (v[2],n[4],c[2]), (v[4],n[5],c[4]), (v[7],n[5],c[7]), (v[6],n[5],c[6]), (v[5],n[5],c[5]) ], dtype = vtype) I = np.resize( np.array([0,1,2,0,2,3], dtype=np.uint32), 6*(2*3)) I += np.repeat( 4*np.arange(2*3), 6) cube = VertexBuffer(vtype) cube.append(V,I) I = np.resize( np.array([0,1,1,2,2,3,3,0], dtype=np.uint32), 6*(2*4)) I += np.repeat( 4*np.arange(6), 8) outline = VertexBuffer(vtype) outline.append(V,I) shader = Shader(open("./cube.vert").read(), open("./cube.frag").read()) glut.glutMainLoop()
t0 = glut.glutGet(glut.GLUT_ELAPSED_TIME) glut.glutInit(sys.argv) glut.glutInitDisplayMode(glut.GLUT_DOUBLE | glut.GLUT_RGB | glut.GLUT_DEPTH) glut.glutCreateWindow("Thick lines") glut.glutReshapeWindow(800, 800) glut.glutDisplayFunc(on_display) glut.glutReshapeFunc(on_reshape) glut.glutKeyboardFunc(on_keyboard) glut.glutIdleFunc(on_idle) shader = Shader( open('line-uniform.vert').read(), open('line-uniform.frag').read() ) S = star() vtype = np.dtype([('prev2', 'f4', 2), ('prev', 'f4', 2), ('curr', 'f4', 2), ('next', 'f4', 2), ('next2', 'f4', 2), ('vid', 'f4', 1) ] ) line = VertexBuffer(vtype) np.random.seed(1) for i in range(1000): s = np.random.uniform(0.025,0.100) x,y = np.random.uniform(0,800,2) V,I = bake( S*s + (x,y), closed=True) line.append(V,I) glut.glutMainLoop()
u_projection = np.eye(4, dtype=np.float32) u_view = np.eye(4, dtype=np.float32) theta, phi = 0, 0 vtype = [('position', np.float32, 3), ('normal', np.float32, 3), ('color', np.float32, 4)] vertices, normals, faces = load_obj('obj/icosahedron.obj') V = np.zeros(len(vertices), dtype=vtype) V['position'] = vertices * .99 V['color'] = 1, 1, 1, 1 V['normal'] = normals obj = VertexBuffer(vtype) obj.append(V, faces) outline = DashLines() F = np.roll(faces.repeat(2, axis=1), -1, axis=1) F = F.reshape(len(F) * 3, 2) F = np.sort(F, axis=1) G = F.view(dtype=[('p0', F.dtype), ('p1', F.dtype)]) G = np.unique(G) for face in G: p0 = vertices[face['p0']] p1 = vertices[face['p1']] outline.append([p0, p1], fg_color=(0, 0, 0, 1), linewidth=0.01, dash_pattern="dashed",