def bind(self, co, buf_id, offset): bgl.glUniform1f(self.unif_offset, float(offset)) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, co[0]) bgl.glVertexAttribPointer(self.attr_pos, 3, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL.buf) bgl.glEnableVertexAttribArray(self.attr_pos) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, buf_id[0]) bgl.glVertexAttribPointer(self.attr_primitive_id, 1, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL.buf) bgl.glEnableVertexAttribArray(self.attr_primitive_id)
def assign(self, varName, varValue): assert varName in self.shaderVars, 'Variable %s not found' % varName try: v = self.shaderVars[varName] q, l, t = v['qualifier'], v['location'], v['type'] if l == -1: if not v['reported']: dprint('ASSIGNING TO UNUSED ATTRIBUTE (%s): %s = %s' % (self.name, varName, str(varValue))) v['reported'] = True return if DEBUG_PRINT: print('%s (%s,%d,%s) = %s' % (varName, q, l, t, str(varValue))) if q in {'in', 'attribute'}: if t == 'float': bgl.glVertexAttrib1f(l, varValue) elif t == 'int': bgl.glVertexAttrib1i(l, varValue) elif t == 'vec2': bgl.glVertexAttrib2f(l, *varValue) elif t == 'vec3': bgl.glVertexAttrib3f(l, *varValue) elif t == 'vec4': bgl.glVertexAttrib4f(l, *varValue) else: assert False, 'Unhandled type %s for attrib %s' % (t, varName) if self.checkErrors: self.drawing.glCheckError('assign attrib %s = %s' % (varName, str(varValue))) elif q in {'uniform'}: # cannot set bools with BGL! :( if t == 'float': bgl.glUniform1f(l, varValue) elif t == 'vec2': bgl.glUniform2f(l, *varValue) elif t == 'vec3': bgl.glUniform3f(l, *varValue) elif t == 'vec4': bgl.glUniform4f(l, *varValue) elif t == 'mat3': bgl.glUniformMatrix3fv(l, 1, bgl.GL_TRUE, varValue) elif t == 'mat4': bgl.glUniformMatrix4fv(l, 1, bgl.GL_TRUE, varValue) else: assert False, 'Unhandled type %s for uniform %s' % ( t, varName) if self.checkErrors: self.drawing.glCheckError( 'assign uniform %s (%s %d) = %s' % (varName, t, l, str(varValue))) else: assert False, 'Unhandled qualifier %s for variable %s' % ( q, varName) except Exception as e: print('ERROR Shader.assign(%s, %s)): %s' % (varName, str(varValue), str(e)))
def assign(self, varName, varValue): assert varName in self.shaderVars, 'Variable %s not found' % varName try: v = self.shaderVars[varName] q,l,t = v['qualifier'],v['location'],v['type'] if l == -1: if not v['reported']: dprint('ASSIGNING TO UNUSED ATTRIBUTE (%s): %s = %s' % (self.name, varName,str(varValue))) v['reported'] = True return if DEBUG_PRINT: print('%s (%s,%d,%s) = %s' % (varName, q, l, t, str(varValue))) if q in {'in','attribute'}: if t == 'float': bgl.glVertexAttrib1f(l, varValue) elif t == 'int': bgl.glVertexAttrib1i(l, varValue) elif t == 'vec2': bgl.glVertexAttrib2f(l, *varValue) elif t == 'vec3': bgl.glVertexAttrib3f(l, *varValue) elif t == 'vec4': bgl.glVertexAttrib4f(l, *varValue) else: assert False, 'Unhandled type %s for attrib %s' % (t, varName) if self.checkErrors: self.drawing.glCheckError('assign attrib %s = %s' % (varName, str(varValue))) elif q in {'uniform'}: # cannot set bools with BGL! :( if t == 'float': bgl.glUniform1f(l, varValue) elif t == 'vec2': bgl.glUniform2f(l, *varValue) elif t == 'vec3': bgl.glUniform3f(l, *varValue) elif t == 'vec4': bgl.glUniform4f(l, *varValue) elif t == 'mat3': bgl.glUniformMatrix3fv(l, 1, bgl.GL_TRUE, varValue) elif t == 'mat4': bgl.glUniformMatrix4fv(l, 1, bgl.GL_TRUE, varValue) else: assert False, 'Unhandled type %s for uniform %s' % (t, varName) if self.checkErrors: self.drawing.glCheckError('assign uniform %s (%s %d) = %s' % (varName, t, l, str(varValue))) else: assert False, 'Unhandled qualifier %s for variable %s' % (q, varName) except Exception as e: print('ERROR (assign): ' + str(e))
def assign(self, varName, varValue): assert varName in self.shaderVars, 'Variable %s not found' % varName v = self.shaderVars[varName] q,l,t = v['qualifier'],v['location'],v['type'] # print(varName + '=' + str(varValue)) if q in {'in','attribute'}: if t == 'float': bgl.glVertexAttrib1f(l, varValue) else: assert False, 'Unhandled type %s for attrib %s' % (t, varName) elif q in {'uniform'}: if t == 'float': bgl.glUniform1f(l, varValue) elif t == 'bool': bgl.glUniform1i(l, 1 if varValue else 0) else: assert False, 'Unhandled type %s for uniform %s' % (t, varName) else: assert False, 'Unhandled qualifier %s for variable %s' % (q, varName)
def Draw(self, index_offset): self.first_index = index_offset bgl.glUseProgram(self.shader.program) bgl.glBindVertexArray(self.vao[0]) bgl.glUniformMatrix4fv(self.unif_MV, 1, bgl.GL_TRUE, self.MV) bgl.glUniformMatrix4fv(self.unif_MVP, 1, bgl.GL_TRUE, self.MVP) if self.draw_tris: bgl.glUniform1f(self.unif_offset, float(index_offset)) # bgl has no glUniform1ui :\ bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_tris[0]) bgl.glEnableVertexAttribArray(self.attr_pos) bgl.glVertexAttribPointer(self.attr_pos, 3, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_tri_indices[0]) bgl.glEnableVertexAttribArray(self.attr_primitive_id) bgl.glVertexAttribPointer(self.attr_primitive_id, 1, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL) bgl.glDrawArrays(bgl.GL_TRIANGLES, 0, self.num_tris * 3) index_offset += self.num_tris bgl.glDepthRange(-0.00005, 0.99995) if self.draw_edges: bgl.glUniform1f(self.unif_offset, float(index_offset)) #TODO: use glUniform1ui bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_edges[0]) bgl.glVertexAttribPointer(self.attr_pos, 3, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL) bgl.glEnableVertexAttribArray(self.attr_pos) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_edge_indices[0]) bgl.glVertexAttribPointer(self.attr_primitive_id, 1, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL) bgl.glEnableVertexAttribArray(self.attr_primitive_id) bgl.glDrawArrays(bgl.GL_LINES, 0, self.num_edges * 2) index_offset += self.num_edges if self.draw_verts: bgl.glUniform1f(self.unif_offset, float(index_offset)) #TODO: use glUniform1ui bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_verts[0]) bgl.glVertexAttribPointer(self.attr_pos, 3, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL) bgl.glEnableVertexAttribArray(self.attr_pos) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_looseverts_indices[0]) bgl.glVertexAttribPointer(self.attr_primitive_id, 1, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL) bgl.glEnableVertexAttribArray(self.attr_primitive_id) bgl.glDrawArrays(bgl.GL_POINTS, 0, self.num_verts) bgl.glDepthRange(0.0, 1.0)
def execute(self, refholder): # pass tr = self.nodeTree.properties.TextureResolution print("begining execution " + str(tr)) # compute A' mask = np.array([[0.05, 0.2, 0.05], [0.2, -1, 0.2], [0.05, 0.2, 0.05]]) # Input data pixels A = self.inputs[0].getPixels() B = self.inputs[1].getPixels() # print(A) bgl.glGetIntegerv(bgl.GL_CURRENT_PROGRAM, self.rdData[self.name]["prev_program"]) bgl.glUseProgram(self.rdData[self.name]["program"]) #set any uniforms needed bgl.glUniform1f(self.rdData[self.name]["feed_loc"], self.inputs[2].value) bgl.glUniform1f(self.rdData[self.name]["kill_loc"], self.inputs[3].value) bgl.glUniform1f(self.rdData[self.name]["da_loc"], self.inputs[4].value) bgl.glUniform1f(self.rdData[self.name]["db_loc"], self.inputs[5].value) bgl.glUniform1f(self.rdData[self.name]["dt_loc"], self.inputs[6].value) bgl.glUniform1f(self.rdData[self.name]["step_loc"], 1 / tr) bgl.glDisable(bgl.GL_SCISSOR_TEST) bgl.glViewport(0, 0, tr, tr) bgl.glMatrixMode(bgl.GL_MODELVIEW) bgl.glPushMatrix() bgl.glLoadIdentity() bgl.glMatrixMode(bgl.GL_PROJECTION) bgl.glPushMatrix() bgl.glLoadIdentity() bgl.gluOrtho2D(0, 1, 0, 1) bgl.glEnable(bgl.GL_TEXTURE_2D) bgl.glActiveTexture(bgl.GL_TEXTURE0) channels = [] if self.channels == '0': channels = [0] elif self.channels == '1': channels = [0, 1, 2] for j in channels: self.rdData[self.name]["npArray"][:, :, 0] = A[:, :, j] self.rdData[self.name]["npArray"][:, :, 1] = B[:, :, j] # Caution: Interfacing with Cython requires toList() self.rdData[self.name]["image"].pixels = self.rdData[ self.name]["npArray"].flatten() self.rdData[self.name]["image"].gl_load(0, bgl.GL_LINEAR, bgl.GL_LINEAR) bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.rdData[self.name]["image"].bindcode[0]) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_S, bgl.GL_REPEAT) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_T, bgl.GL_REPEAT) for i in range(self.inputs[7].value): bgl.glClearDepth(1.0) bgl.glClearColor(0.0, 0.0, 0.0, 0.0) bgl.glClear(bgl.GL_COLOR_BUFFER_BIT | bgl.GL_DEPTH_BUFFER_BIT) bgl.glBegin(bgl.GL_TRIANGLES) bgl.glColor3f(1.0, 1.0, 1.0) bgl.glTexCoord2f(0.0, 0.0) bgl.glVertex2f(-1.0, -1.0) bgl.glTexCoord2f(1.0, 0.0) bgl.glVertex2f(1.0, -1.0) bgl.glTexCoord2f(1.0, 1.0) bgl.glVertex2f(1.0, 1.0) bgl.glColor3f(1.0, 1.0, 1.0) bgl.glTexCoord2f(0.0, 0.0) bgl.glVertex2f(-1.0, -1.0) bgl.glTexCoord2f(1.0, 1.0) bgl.glVertex2f(1.0, 1.0) bgl.glTexCoord2f(0.0, 1.0) bgl.glVertex2f(-1.0, 1.0) bgl.glEnd() bgl.glCopyTexImage2D( bgl.GL_TEXTURE_2D, #target 0, #level bgl.GL_RGBA, #internalformat 0, #x 0, #y tr, tr, 0 #border ) #glFlush glFinish or none here? bgl.glFinish() bgl.glReadPixels(0, 0, tr, tr, bgl.GL_RGBA, bgl.GL_FLOAT, self.rdData[self.name]["buffer"]) self.rdData[self.name]["image"].pixels = self.rdData[ self.name]["buffer"][:] #write the image channel npImage = np.asarray(self.rdData[self.name]["image"].pixels, dtype="float") self.rdData[self.name]["npArray"] = npImage.reshape( tr, tr, self.rdData[self.name]["image"].channels) self.outputs[0].setPackedImageFromChannels( self.rdData[self.name]["npArray"][:, :, 0], j, flatten=True) self.outputs[1].setPackedImageFromChannels( self.rdData[self.name]["npArray"][:, :, 1], j, flatten=True) self.outputs[2].setPackedImageFromPixels( self.rdData[self.name]["npArray"]) self.inputs[0].setPackedImageFromChannels( self.rdData[self.name]["npArray"][:, :, 0], j, flatten=True) self.inputs[1].setPackedImageFromChannels( self.rdData[self.name]["npArray"][:, :, 1], j, flatten=True) # ================================= Test bed # self.outputs[0].getTexture().image.copy() # self.outputs[1].getTexture().image.copy() # self.outputs[2].getTexture().image.copy() # nparr = np.asarray(self.outputs[0].getTexture().image.pixels, dtype="float") # nparr = nparr.reshape(tr, tr, 4) # print(nparr) self.rdData[self.name]["image"].gl_free() #restore the state so blender wont break bgl.glEnable(bgl.GL_SCISSOR_TEST) bgl.glUseProgram(self.rdData[self.name]["prev_program"][0]) bgl.glActiveTexture(bgl.GL_TEXTURE0)
def bindUniformf(self, name): self.findProgram() bgl.glUseProgram(self.program) pname = self.__class__.__name__ + '_' + name bgl.glUniform1f(bgl.glGetUniformLocation(self.program, pname), getattr(self, name))