def __init__(self, side): self.side = side # load shaders self.program = glutils.loadShaders(strVS, strFS) glUseProgram(self.program) s = side / 2.0 vertices = [ -s, s, -s, -s, -s, -s, s, s, -s, s, -s, -s, s, s, -s, -s, -s, -s, -s, s, s, -s, -s, s, s, s, s, s, -s, s, s, s, s, -s, -s, s, -s, -s, s, -s, -s, -s, s, -s, s, s, -s, -s, s, -s, s, -s, -s, -s, -s, s, s, -s, s, -s, s, s, s, s, s, -s, s, s, s, -s, s, -s, -s, -s, s, -s, -s, -s, -s, s, s, -s, s, -s, -s, s, s, -s, -s, -s, s, -s, s, s, -s, -s, s, s, s, s, s, -s, s, s, s, s, -s, -s ] # set up vertex array object (VAO) self.vao = glGenVertexArrays(1) glBindVertexArray(self.vao) # set up VBOs vertexData = numpy.array(vertices, numpy.float32) self.vertexBuffer = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer) glBufferData(GL_ARRAY_BUFFER, 4 * len(vertexData), vertexData, GL_STATIC_DRAW) #enable arrays self.vertIndex = glGetAttribLocation(self.program, "aVert") glEnableVertexAttribArray(self.vertIndex) # set buffers glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer) glVertexAttribPointer(self.vertIndex, 3, GL_FLOAT, GL_FALSE, 0, None) # unbind VAO glBindVertexArray(0)
def __init__(self, strVS, strFS): self.program = glutils.loadShaders(strVS=strVS, strFS=strFS) glUseProgram(self.program) self.pMatrixUniform = glGetUniformLocation(self.program, b'uPMatrix') self.mvMatrixUniform = glGetUniformLocation(self.program, b'uMVMatrix') self.tex2D = glGetUniformLocation(self.program, b'tex2D') # 2D texture # define triangle strip vertices vertex_data = numpy.array([-0.5, -0.5, 0.0, 0.5, -0.5, 0.0, -0.5, 0.5, 0.0, 0.5, 0.5, 0.0], numpy.float32) # set up vertex array object (VAO) self.vao = glGenVertexArrays(1) glBindVertexArray(self.vao) # vertices self.vertex_buffer = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, self.vertex_buffer) glBufferData(GL_ARRAY_BUFFER, 4 * len(vertex_data), vertex_data, GL_STATIC_DRAW) # set buffer data glEnableVertexAttribArray(0) # enable vertex array glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None) # set buffer data pointer glBindVertexArray(0) self.t = 0 self.tex_id = glutils.loadTexture('star.png') self.showCircle = False # show circle flag
def __init__(self, axes_len): # create shader self.program = glutils.loadShaders(strVS, strFS) glUseProgram(self.program) self.pMatrixUniform = glGetUniformLocation(self.program, b'uPMatrix') self.mvMatrixUniform = glGetUniformLocation(self.program, b'uMVMatrix') # axis length self.axes_len = axes_len # create axes geometry (vertices, colours) = self.create_axes(15) #print(vertices.shape) #print(colours.shape) # set up vertex array object (VAO) self.vao = glGenVertexArrays(1) glBindVertexArray(self.vao) # vertices self.vertexBuffer = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer) # set buffer data glBufferData(GL_ARRAY_BUFFER, 4 * len(vertices), vertices, GL_STATIC_DRAW) # colors self.colorBuffer = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, self.colorBuffer) # set buffer data glBufferData(GL_ARRAY_BUFFER, 4 * len(colours), colours, GL_STATIC_DRAW) # get locations self.vertLoc = glGetAttribLocation(self.program, b"aVert") self.colLoc = glGetAttribLocation(self.program, b"aCol") # enable vertex arrays glEnableVertexAttribArray(self.vertLoc) # bind glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer) # set buffer data pointer glVertexAttribPointer(self.vertLoc, 3, GL_FLOAT, GL_FALSE, 0, None) # enable vertex arrays glEnableVertexAttribArray(self.colLoc) # bind glBindBuffer(GL_ARRAY_BUFFER, self.colorBuffer) # set buffer data pointer glVertexAttribPointer(self.colLoc, 3, GL_FLOAT, GL_FALSE, 0, None) # unbind VAO glBindVertexArray(0)
def __init__(self): # create shader self.program = glutils.loadShaders(strVS, strFS) glUseProgram(self.program) self.pMatrixUniform = glGetUniformLocation(self.program, 'uPMatrix') self.mvMatrixUniform = glGetUniformLocation(self.program, "uMVMatrix") self.colorU = glGetUniformLocation(self.program, "uColor") # color self.col0 = [1.0, 0.0, 0.0, 1.0] # texture self.tex2D = glGetUniformLocation(self.program, "tex2D") # define quad vertices quadV = [ -0.5, -0.5, 0.0, 0.5, -0.5, 0.0, -0.5, 0.5, 0.0, 0.5, 0.5, 0.0 ] # set up vertex array object (VAO) self.vao = glGenVertexArrays(1) glBindVertexArray(self.vao) # vertices self.vertexBuffer = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer) vertexData = numpy.array(quadV, numpy.float32) glBufferData(GL_ARRAY_BUFFER, 4*len(vertexData), vertexData, GL_STATIC_DRAW) # enable vertex array glEnableVertexAttribArray(0) # set buffer data glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None) # unbind VAO glBindVertexArray(0) # time self.t = 0 # texture self.texId = glutils.loadTexture('test.png') # show circle? self.showCircle = False
def __init__(self, width, height, volume): """SliceRender constructor""" self.width = width self.height = height self.aspect = width/float(height) # slice mode self.mode = SliceRender.ZSLICE # create shader self.program = glutils.loadShaders(strVS, strFS) glUseProgram(self.program) self.pMatrixUniform = glGetUniformLocation(self.program, b'uPMatrix') self.mvMatrixUniform = glGetUniformLocation(self.program, b"uMVMatrix") # attributes self.vertIndex = glGetAttribLocation(self.program, b"aVert") # set up vertex array object (VAO) self.vao = glGenVertexArrays(1) glBindVertexArray(self.vao) # define quad vertices vertexData = numpy.array([ 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0], numpy.float32) # vertex buffer self.vertexBuffer = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer) glBufferData(GL_ARRAY_BUFFER, 4*len(vertexData), vertexData, GL_STATIC_DRAW) # enable arrays glEnableVertexAttribArray(self.vertIndex) # set buffers glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer) glVertexAttribPointer(self.vertIndex, 3, GL_FLOAT, GL_FALSE, 0, None) # unbind VAO glBindVertexArray(0) # load texture self.texture, self.Nx, self.Ny, self.Nz = volume # current slice index self.currSliceIndex = int(self.Nz/2); self.currSliceMax = self.Nz;
def __init__(self): # Compile and load the shaders. self.program = glutils.loadShaders( str_vector_shader, str_fragment_shader) glUseProgram(self.program) # Connect the variables in the Python code with those in the shaders # Returns the location of a uniform variable self.p_matrix_uniform = glGetUniformLocation(self.program, 'uPMatrix') self.mv_matrix_uniform = glGetUniformLocation( self.program, 'uMVMatrix') # Texture self.tex2D = glGetUniformLocation(self.program, 'tex2D') # Define triangle strip vertices vertex_data = numpy.array([ -0.5, -0.5, 0.0, 0.5, -0.5, 0.0, -0.5, 0.5, 0.0, 0.5, 0.5, 0.0, ], numpy.float32) # Set up vertex array object(VAO) self.vao = glGenVertexArrays(1) glBindVertexArray(self.vao) # Vertices self.vertex_buffer = glGenBuffers(1) # Create VBO # VBO is a memory buffer in the high speed memory of your video card # designed to hold information about vertices glBindBuffer(GL_ARRAY_BUFFER, self.vertex_buffer) # Set buffer data glBufferData(GL_ARRAY_BUFFER, 4*len(vertex_data), vertex_data, GL_STATIC_DRAW) # Enable vertex array glEnableVertexAttribArray(0) # Set buffer data pointer glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None) # Unbind VAO glBindVertexArray(0) # Loads the image as an OpenGL texture self.tex_id = glutils.loadTexture('star.png') self.t = 0 # time self.show_circle = False
def __init__(self, width, height, volume): """RayCastRender constr""" # create RayCube object self.raycube = raycube.RayCube(width, height) # set dims self.width = width self.height = height self.aspect = width / float(height) # create shader self.program = glutils.loadShaders(strVS, strFS) # texture self.texVolume, self.Nx, self.Ny, self.Nz = volume # initialize camera self.camera = Camera()
def __init__(self, width, height, volume): """RayCastRender constr""" # create RayCube object self.raycube = raycube.RayCube(width, height) # set dims self.width = width self.height = height self.aspect = width/float(height) # create shader self.program = glutils.loadShaders(strVS, strFS) # texture self.texVolume, self.Nx, self.Ny, self.Nz = volume # initialize camera self.camera = Camera()
def __init__(self, numP): # no. of particles self. numP = numP # time variable self.t = 0.0 self.lifeTime = 5.0 self.startPos = numpy.array([0.0, 0.0, 0.5]) # load texture self.texid = glutils.loadTexture('star.png') # create shader self.program = glutils.loadShaders(strVS, strFS) glUseProgram(self.program) # set sampler texLoc = glGetUniformLocation(self.program, b"uTex") glUniform1i(texLoc, 0) # uniforms self.timeU = glGetUniformLocation(self.program, b"uTime") self.lifeTimeU = glGetUniformLocation(self.program, b"uLifeTime") self.pMatrixUniform = glGetUniformLocation(self.program, b'uPMatrix') self.mvMatrixUniform = glGetUniformLocation(self.program, b"uMVMatrix") self.bMatrixU = glGetUniformLocation(self.program, b"bMatrix") self.colorU = glGetUniformLocation(self.program, b"uColor") self.samplerU = glGetUniformLocation(self.program, b"uSampler") self.posU = glGetUniformLocation(self.program, b"uPos") # attributes self.vertIndex = glGetAttribLocation(self.program, b"aVert") self.texIndex = glGetAttribLocation(self.program, b"aTexCoord") self.time0Index = glGetAttribLocation(self.program, b"aTime0") self.velIndex = glGetAttribLocation(self.program, b"aVel") # render flags self.enableBillboard = True self.disableDepthMask = True self.enableBlend = True # which texture to use self.useStarTexture = True # restart - first time self.restart(numP)
def __init__(self, numP): # no. of particles self.numP = numP # time variable self.t = 0.0 self.lifeTime = 5.0 self.startPos = numpy.array([0.0, 0.0, 0.5]) # load texture self.texid = glutils.loadTexture('star.png') # create shader self.program = glutils.loadShaders(strVS, strFS) glUseProgram(self.program) # set sampler texLoc = glGetUniformLocation(self.program, b"uTex") glUniform1i(texLoc, 0) # uniforms self.timeU = glGetUniformLocation(self.program, b"uTime") self.lifeTimeU = glGetUniformLocation(self.program, b"uLifeTime") self.pMatrixUniform = glGetUniformLocation(self.program, b'uPMatrix') self.mvMatrixUniform = glGetUniformLocation(self.program, b"uMVMatrix") self.bMatrixU = glGetUniformLocation(self.program, b"bMatrix") self.colorU = glGetUniformLocation(self.program, b"uColor") self.samplerU = glGetUniformLocation(self.program, b"uSampler") self.posU = glGetUniformLocation(self.program, b"uPos") # attributes self.vertIndex = glGetAttribLocation(self.program, b"aVert") self.texIndex = glGetAttribLocation(self.program, b"aTexCoord") self.time0Index = glGetAttribLocation(self.program, b"aTime0") self.velIndex = glGetAttribLocation(self.program, b"aVel") # render flags self.enableBillboard = True self.disableDepthMask = True self.enableBlend = True # which texture to use self.useStarTexture = True # restart - first time self.restart(numP)
def __init__(self, R, r, NX, NY): global strVS, strFS # create shader self.program = glutils.loadShaders(strVS, strFS) glProvokingVertex(GL_FIRST_VERTEX_CONVENTION) self.pMatrixUniform = glGetUniformLocation(self.program, b'uPMatrix') self.mvMatrixUniform = glGetUniformLocation(self.program, b'uMVMatrix') # torus geometry self.R = R self.r = r # grid size self.NX = NX self.NY = NY # no of points self.N = self.NX self.M = self.NY # time self.t = 0 # compute parameters for glMultiDrawArrays M1 = 2 * self.M + 2 self.first_indices = [2 * M1 * i for i in range(self.N)] self.counts = [2 * M1 for i in range(self.N)] # colors: {(i, j) : (r, g, b)} # with NX * NY entries self.colors_dict = self.init_colors(self.NX, self.NY) # create an empty array to hold colors self.colors = np.zeros((3 * self.N * (2 * self.M + 2), ), np.float32) # get vertices, normals, indices vertices, normals = self.compute_vertices() self.compute_colors() # set up vertex buffer objects self.setup_vao(vertices, normals, self.colors)
def __init__(self,vertices,tex_ma,tex_id,a,b,c,scale,r): # load shaders self.program = glutils.loadShaders(strVS, strFS) glUseProgram(self.program) # attributes self.vertIndex=glGetAttribLocation(self.program, b"position") self.texIndex=glGetAttribLocation(self.program, b"inTexcoord") self.vertices=vertices self.tex_ma=tex_ma self.tex_id=tex_id self.a=a self.b=b self.c=c self.scale=scale self.r=r # set up vertex array object (VAO) self.vao = glGenVertexArrays(1) glBindVertexArray(self.vao) # set up VBOs vertexData = np.array(self.vertices, np.float32) self.vertexBuffer = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer) glBufferData(GL_ARRAY_BUFFER, 4*len(vertexData), vertexData, GL_STATIC_DRAW) tcData = np.array(self.tex_ma, np.float32) self.tcBuffer = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, self.tcBuffer) glBufferData(GL_ARRAY_BUFFER, 4*len(tcData), tcData,GL_STATIC_DRAW) # enable arrays glEnableVertexAttribArray(self.vertIndex) glEnableVertexAttribArray(self.texIndex) # Position attribute glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer) glVertexAttribPointer(self.vertIndex, 3, GL_FLOAT, GL_FALSE, 0,None) # TexCoord attribute glBindBuffer(GL_ARRAY_BUFFER, self.tcBuffer) glVertexAttribPointer(self.texIndex, 2, GL_FLOAT, GL_FALSE, 0,None) # unbind VAO glBindVertexArray(0) glBindBuffer(GL_ARRAY_BUFFER,0)
def __init__(self): #从字符串调用着色器,编译并链接成一个OpenGL程序对象 self.program = glutils.loadShaders(strVS, strFS) #设置代码使用特定的“程序对象”(一个项目可能有多个程序) glUseProgram(self.program) #从着色器中取到pMatrixUniform,mvMatrixUniform,tex2D参数的值 self.pMatrixUniform = glGetUniformLocation(self.program, b"uPMatrix") self.mvMatrixUniform = glGetUniformLocation(self.program, b"uMVMatrix") self.tex2D = glGetUniformLocation(self.program, b"tex2D") #定义三角形带的顶点数组,用于绘制正方形 vertexData = numpy.array( [-0.5, -0.5, 0.0, 0.5, -0.5, 0.0, -0.5, 0.5, 0.0, 0.5, 0.5, 0.0], numpy.float32) #创建一个VAO,并绑定到该VAO,接下来的所有调用将绑定到它 self.vao = glGenVertexArrays(1) glBindVertexArray(self.vao) #创建一个VBO用来管理顶点数据和渲染 self.vertexBuffer = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer) #根据已定义的顶点,设置缓冲区数据 glBufferData(GL_ARRAY_BUFFER, 4 * len(vertexData), vertexData, GL_STATIC_DRAW) #着色区可以访问这些数据 glEnableVertexAttribArray(0) #设置顶点属性数组的位置和数据格式,属性的下标是0,组件个数是3,顶点的数据类型是GL_FLOAT glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None) #取消VAO绑定 glBindVertexArray(0) self.t = 0 #将图像加载为OpenGL的纹理,返回的纹理ID用于渲染 self.texId = glutils.loadTexture('test.png') self.showCircle = False
def generate_binary_mask(faces, vertices_2d, width, height): if not glfw.Init(): print('glfw not initialized') sys.exit() version = 3, 3 glfw.WindowHint(glfw.CONTEXT_VERSION_MAJOR, version[0]) glfw.WindowHint(glfw.CONTEXT_VERSION_MINOR, version[1]) glfw.WindowHint(glfw.OPENGL_FORWARD_COMPAT, 1) glfw.WindowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) glfw.WindowHint(glfw.VISIBLE, 1) window = glfw.CreateWindow(width, height, 'Quad') if not window: print('glfw window not created') glfw.Terminate() sys.exit() glfw.MakeContextCurrent(window) strVS = """ #version 330 layout(location = 0) in vec2 aPosition; void main() { gl_Position = vec4(vec3(aPosition, 0), 1.0); } """ strFS = """ #version 330 out vec3 fragColor; void main() { fragColor = vec3(0, 1, 0); } """ program = glutils.loadShaders(strVS, strFS) glUseProgram(program) element_array = np.reshape(faces, -1) elementData = np.array(element_array, np.uint32) vertex_array = np.reshape(vertices_2d, -1) vertexData = np.array(vertex_array, np.float32) vao = glGenVertexArrays(1) glBindVertexArray(vao) ebo = glGenBuffers(1) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo) glBufferData(GL_ELEMENT_ARRAY_BUFFER, 4 * len(elementData), elementData, GL_STATIC_DRAW) vbo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, vbo) glBufferData(GL_ARRAY_BUFFER, 4 * len(vertexData), vertexData, GL_STATIC_DRAW) glEnableVertexAttribArray(0) glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, None) glBindVertexArray(0) GL.glClearColor(0, 0, 0, 1.0) GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) glUseProgram(program) glBindVertexArray(vao) glDrawElements(GL_TRIANGLES, len(element_array), GL_UNSIGNED_INT, None) glBindVertexArray(0) glPixelStorei(GL_PACK_ALIGNMENT, 1) pixel_data = glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, outputType=None) im = np.array(pixel_data) mask = Image.frombuffer('RGB', (width, height), im, 'raw', 'RGB') mask = np.array(mask) glfw.Terminate() return mask
def main(): # init GLFW glfw.init() glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, GL_TRUE) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) window_width, window_height = 1280, 720 window_aspect = window_width / float(window_height) window = glfw.create_window(window_width, window_height, 'Triangle', None, None) glfw.make_context_current(window) # init GL glViewport(0, 0, window_width, window_height) glEnable(GL_DEPTH_TEST) glClearColor(0.5, 0.5, 0.5, 1) program = glutils.loadShaders(strVS, strFS) vertexData = numpy.array([-0.5, -0.5, 0.0, 0.5, -0.5, 0.0, 0.0, 0.5, 0.0,], numpy.float32) vao = glGenVertexArrays(1) glBindVertexArray(vao) vertexBuffer = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer) glBufferData(GL_ARRAY_BUFFER, 4*len(vertexData), vertexData, GL_STATIC_DRAW) glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None) glBindVertexArray(0) glfw.set_time(0) win_t = 0.0 while not glfw.window_should_close(window): currT = glfw.get_time() if currT - win_t > 0.1: # rotation angle # print(win_t) win_t = currT glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # render scene glUseProgram(program) glBindVertexArray(vao) glDrawArrays(GL_TRIANGLE_FAN, 0, 4) glBindVertexArray(0) glfw.swap_buffers(window) glfw.poll_events() glfw.terminate()
def __init__(self): # create shader vShader = "" with open("shaders/spinny.v.glsl", 'r') as src: vShader = src.read() fShader = "" with open("shaders/spinny.f.glsl", 'r') as src: fShader = src.read() self.program = glutils.loadShaders(vShader, fShader) self.bkpgm = glutils.loadShaders( *[open("shaders/bkgnd.%s.glsl" % i, "r").read() for i in "vf"]) self.backTex = glGetUniformLocation(self.bkpgm, b'tex2D') self.bkId = glutils.loadTexture('star.png') _, frame = cap.read() frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) frame = cv2.flip(frame, 0) height, width, _ = frame.shape glBindTexture(GL_TEXTURE_2D, self.bkId) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, frame) glBindTexture(GL_TEXTURE_2D, 0) glUseProgram(self.program) self.pMatrixUniform = glGetUniformLocation(self.program, b'uPMatrix') self.mvMatrixUniform = glGetUniformLocation(self.program, b'uMVMatrix') # texture self.tex2D = glGetUniformLocation(self.program, b'tex2D') # define triange strip vertices vertexData = np.array([ -0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5, -0.5, -0.5, -0.5, 0.5, -0.5, -0.5, -0.5, 0.5, -0.5, -0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5 ], np.float32) # set up vertex array object (VAO) self.vao = glGenVertexArrays(1) glBindVertexArray(self.vao) # vertices self.vertexBuffer = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer) # set buffer data glBufferData(GL_ARRAY_BUFFER, 14 * len(vertexData), vertexData, GL_STATIC_DRAW) # enable vertex array glEnableVertexAttribArray(0) # set buffer data pointer glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None) # unbind VAO glBindVertexArray(0) # time self.t = 0 # texture self.texId = glutils.loadTexture('star.png') _, frame = cap.read() frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) height, width, _ = frame.shape glBindTexture(GL_TEXTURE_2D, self.texId) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, frame) glBindTexture(GL_TEXTURE_2D, 0) # show circle? self.showCircle = False ## opencv stuff self.sift = cv2.xfeatures2d.SIFT_create() ref_img = cv2.imread(REFERENCE_IMG, 0) self.ref_kp, self.ref_des = self.sift.detectAndCompute(ref_img, None) FLANN_INDEX_KDTREE = 0 index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5) search_params = dict(checks=50) self.flann = cv2.FlannBasedMatcher(index_params, search_params)
def __init__(self, side): self.side = side # load shaders self.program = glutils.loadShaders(strVS, strFS) glUseProgram(self.program) s = side/2.0 vertices = [ -s, s, -s, -s, -s, -s, s, s, -s, s, -s, -s, s, s, -s, -s, -s, -s, -s, s, s, -s, -s, s, s, s, s, s, -s, s, s, s, s, -s, -s, s, -s, -s, s, -s, -s, -s, s, -s, s, s, -s, -s, s, -s, s, -s, -s, -s, -s, s, s, -s, s, -s, s, s, s, s, s, -s, s, s, s, -s, s, -s, -s, -s, s, -s, -s, -s, -s, s, s, -s, s, -s, -s, s, s, -s, -s, -s, s, -s, s, s, -s,-s, s, s, s, s, s, -s, s, s, s, s, -s,-s ] # set up vertex array object (VAO) self.vao = glGenVertexArrays(1) glBindVertexArray(self.vao) # set up VBOs vertexData = numpy.array(vertices, numpy.float32) self.vertexBuffer = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer) glBufferData(GL_ARRAY_BUFFER, 4*len(vertexData), vertexData, GL_STATIC_DRAW) #enable arrays self.vertIndex = glGetAttribLocation(self.program, "aVert") glEnableVertexAttribArray(self.vertIndex) # set buffers glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer) glVertexAttribPointer(self.vertIndex, 3, GL_FLOAT, GL_FALSE, 0, None) # unbind VAO glBindVertexArray(0)
def main(): if not glfw.init(): print('glfw.init error') return window = glfw.create_window(640, 480, 'Cube', None, None) if not window: glfw.terminate() print('glfw.create_window error') return glfw.make_context_current(window) print(glGetString(GL_VERSION)) # init OpenGL glEnable(GL_DEPTH_TEST) glClearColor(0.1, 0.1, 0.1, 1) # glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ) # shader program = glutils.loadShaders(strVS, strFS) glUseProgram(program) # vertex buffer # position # normal positions = numpy.array([ -1.0, 1.0, -1.0, 0.0, 1.0, 0.0, 1.0, 1.0, -1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, -1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, -1.0, 1.0, 0.0, 0.0, 1.0, -1.0, -1.0, 1.0, 0.0, 0.0, 1.0, -1.0, 1.0, 1.0, 0.0, 0.0, 1.0, -1.0, 1.0, 0.0, -1.0, 0.0, -1.0, -1.0, 1.0, 0.0, -1.0, 0.0, -1.0, -1.0, -1.0, 0.0, -1.0, 0.0, 1.0, -1.0, -1.0, 0.0, -1.0, 0.0, -1.0, -1.0, 1.0, -1.0, 0.0, 0.0, -1.0, 1.0, 1.0, -1.0, 0.0, 0.0, -1.0, 1.0, -1.0, -1.0, 0.0, 0.0, -1.0, -1.0, -1.0, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, -1.0, 1.0, 0.0, 0.0, 1.0, -1.0, -1.0, 1.0, 0.0, 0.0, 1.0, -1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, -1.0, 0.0, 0.0, -1.0, -1.0, 1.0, -1.0, 0.0, 0.0, -1.0, -1.0, -1.0, -1.0, 0.0, 0.0, -1.0, 1.0, -1.0, -1.0, 0.0, 0.0, -1.0, ], numpy.float32) buffer = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, buffer) glBufferData(GL_ARRAY_BUFFER, positions.itemsize * len(positions), positions, GL_STATIC_DRAW) glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, positions.itemsize * 6, None) glEnableVertexAttribArray(1) glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, positions.itemsize * 3, None) # Projections pMatrix = glutils.perspective(45, 640 / 480.0, 0.1, 100.0) mvMatrix = glutils.lookAt([4.0, 4.0, 3.0], [0.0, 0.0, 0.0], [0.0, 0.0, 1]) pMatrixUniform = glGetUniformLocation(program, b'uPMatrix') mvMatrixUniform = glGetUniformLocation(program, b'uMVMatrix') glUniformMatrix4fv(pMatrixUniform, 1, GL_FALSE, pMatrix) glUniformMatrix4fv(mvMatrixUniform, 1, GL_FALSE, mvMatrix) # Lighting lightColorUniform = glGetUniformLocation(program, b'lightColor') glUniform3f(lightColorUniform, 0.1, 0.8, 0.1) objectColorUniform = glGetUniformLocation(program, b'objectColor') glUniform3f(objectColorUniform, 0.7, 0.7, 0.7) lightPosUniform = glGetUniformLocation(program, b'lightPos') glUniform3f(lightPosUniform, 2.0, 4, -3.0) t = 0 while not glfw.window_should_close(window): time.sleep(1 / 45) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # glBegin(GL_TRIANGLES) # glVertex2f(-0.5, -0.5) # glVertex2f( 0.0 , 0.5) # glVertex2f( 0.5, -0.5) # glEnd() # rotation animation t = (t + 1) % 360 # set shader angle in radians glUniform1f(glGetUniformLocation(program, 'uTheta'), math.radians(t)) # draw call glDrawArrays(GL_QUADS, 0, 24) glfw.swap_buffers(window) glfw.poll_events()
def __init__(self, width, height): """RayCube constructor""" # set dims self.width, self.height = width, height # create shader self.program = glutils.loadShaders(strVS, strFS) # cube vertices vertices = numpy.array([ 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0 ], numpy.float32) # cube colors colors = numpy.array([ 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0 ], numpy.float32) # individual triangles indices = numpy.array([ 4, 5, 7, 7, 5, 6, 5, 1, 6, 6, 1, 2, 1, 0, 2, 2, 0, 3, 0, 4, 3, 3, 4, 7, 6, 2, 7, 7, 2, 3, 4, 0, 5, 5, 0, 1 ], numpy.int16) self.nIndices = indices.size # set up vertex array object (VAO) self.vao = glGenVertexArrays(1) glBindVertexArray(self.vao) #vertex buffer self.vertexBuffer = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer) glBufferData(GL_ARRAY_BUFFER, 4*len(vertices), vertices, GL_STATIC_DRAW) # vertex buffer - color self.colorBuffer = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, self.colorBuffer) glBufferData(GL_ARRAY_BUFFER, 4*len(colors), colors, GL_STATIC_DRAW); # index buffer self.indexBuffer = glGenBuffers(1) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.indexBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, 2*len(indices), indices, GL_STATIC_DRAW) # enable attrs using the layout indices in shader aPosLoc = 1 aColorLoc = 2 # bind buffers: glEnableVertexAttribArray(1) glEnableVertexAttribArray(2) # vertex glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer) glVertexAttribPointer(aPosLoc, 3, GL_FLOAT, GL_FALSE, 0, None) # color glBindBuffer(GL_ARRAY_BUFFER, self.colorBuffer) glVertexAttribPointer(aColorLoc, 3, GL_FLOAT, GL_FALSE, 0, None) # index glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.indexBuffer) # unbind VAO glBindVertexArray(0) # FBO self.initFBO()