def make_vao(self): self.vao = glGenVertexArrays(1) glBindVertexArray(self.vao) self.shader_program = OurShaderProgram(vert='texture.vert', frag='texture.frag') self.shader_program.use() self.vbo = make_buffer_object(data=self.vertices.tostring(), usage='GL_STATIC_DRAW', target='GL_ARRAY_BUFFER') glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 7*sizeof(GLfloat), self.vbo) glEnableVertexAttribArray(0) glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 7*sizeof(GLfloat), self.vbo+2*sizeof(GLfloat)) glEnableVertexAttribArray(1) glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 7*sizeof(GLfloat), self.vbo+5*sizeof(GLfloat)) glEnableVertexAttribArray(2) self.ebo = make_buffer_object(data=self.indices.tostring(), usage='GL_STATIC_DRAW', target='GL_ELEMENT_ARRAY_BUFFER') self.vbo.unbind() glBindVertexArray(0)
def start_slicing_stl(stl_filename, layer_thickness, slice_save_path): glfw.init() glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) if platform.system() == 'Darwin': # for Mac OS glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, GL_TRUE) window = glfw.create_window(SCR_WIDTH, SCR_HEIGHT, 'STL Slicer', None, None) glfw.make_context_current(window) glfw.set_framebuffer_size_callback(window, framebuffer_size_callback) glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_NORMAL) loadMesh(stl_filename) glBindVertexArray(params.maskVAO) sliceShader = OurShaderProgram('shaders/slice.vert', 'shaders/slice.frag') prepareSlice() i, height = 0, 0. while not glfw.window_should_close(window): processInput(window) if height >= params.total_thickness - EPSILON: break else: height += layer_thickness i += 1 draw(sliceShader, height - EPSILON) renderSlice( sliceShader, height - EPSILON, os.path.join(slice_save_path, 'out{:04d}.png'.format(i - 1))) glfw.swap_buffers(window) glfw.poll_events() glfw.terminate()
def main(): init_glfw() window = prepare_window() vertices = np.array( [[-0.5, -0.5, -0.5, 0.0, 0.0], [0.5, -0.5, -0.5, 1.0, 0.0], [0.5, 0.5, -0.5, 1.0, 1.0], [0.5, 0.5, -0.5, 1.0, 1.0], [-0.5, 0.5, -0.5, 0.0, 1.0], [-0.5, -0.5, -0.5, 0.0, 0.0], [-0.5, -0.5, 0.5, 0.0, 0.0], [0.5, -0.5, 0.5, 1.0, 0.0], [0.5, 0.5, 0.5, 1.0, 1.0], [0.5, 0.5, 0.5, 1.0, 1.0], [-0.5, 0.5, 0.5, 0.0, 1.0], [-0.5, -0.5, 0.5, 0.0, 0.0], [-0.5, 0.5, 0.5, 1.0, 0.0], [-0.5, 0.5, -0.5, 1.0, 1.0], [-0.5, -0.5, -0.5, 0.0, 1.0], [-0.5, -0.5, -0.5, 0.0, 1.0], [-0.5, -0.5, 0.5, 0.0, 0.0], [-0.5, 0.5, 0.5, 1.0, 0.0], [0.5, 0.5, 0.5, 1.0, 0.0], [0.5, 0.5, -0.5, 1.0, 1.0], [0.5, -0.5, -0.5, 0.0, 1.0], [0.5, -0.5, -0.5, 0.0, 1.0], [0.5, -0.5, 0.5, 0.0, 0.0], [0.5, 0.5, 0.5, 1.0, 0.0], [-0.5, -0.5, -0.5, 0.0, 1.0], [0.5, -0.5, -0.5, 1.0, 1.0], [0.5, -0.5, 0.5, 1.0, 0.0], [0.5, -0.5, 0.5, 1.0, 0.0], [-0.5, -0.5, 0.5, 0.0, 0.0], [-0.5, -0.5, -0.5, 0.0, 1.0], [-0.5, 0.5, -0.5, 0.0, 1.0], [0.5, 0.5, -0.5, 1.0, 1.0], [0.5, 0.5, 0.5, 1.0, 0.0], [0.5, 0.5, 0.5, 1.0, 0.0], [-0.5, 0.5, 0.5, 0.0, 0.0], [-0.5, 0.5, -0.5, 0.0, 1.0]], dtype=GLfloat) cubePositions = np.array( [[0.0, 0.0, 0.0], [2.0, 5.0, -15.0], [-1.5, -2.2, -2.5], [-3.8, -2.0, -12.3], [2.4, -0.4, -3.5], [-1.7, 3.0, -7.5], [1.3, -2.0, -2.5], [1.5, 2.0, -2.5], [1.5, 0.2, -1.5], [-1.3, 1.0, -1.5]], dtype=GLfloat) VAO = glGenVertexArrays(1) glBindVertexArray(VAO) VBO = vbo.VBO(data=vertices.tostring(), usage='GL_STATIC_DRAW', target='GL_ARRAY_BUFFER') VBO.bind() VBO.copy_data() ourShader = OurShaderProgram('camera.vert', 'camera.frag') # position attribute glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), VBO) glEnableVertexAttribArray(0) # texture coord attribute glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), VBO + 3 * sizeof(GLfloat)) glEnableVertexAttribArray(1) texture1 = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texture1) # set the texture wrapping parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) # set texture filtering parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) # load image, create texture and generate mipmaps im = Image.open('../images/container.jpg') w, h = im.size try: im_bytes = im.tobytes("raw", "RGBA", 0, -1) except ValueError: im_bytes = im.tobytes("raw", "RGBX", 0, -1) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, im_bytes) glGenerateMipmap(GL_TEXTURE_2D) texture2 = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texture2) # set the texture wrapping parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) # set texture filtering parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) # load image, create texture and generate mipmaps im = Image.open('../images/awesomeface.png') w, h = im.size try: im_bytes = im.tobytes("raw", "RGBA", 0, -1) except ValueError: im_bytes = im.tobytes("raw", "RGBX", 0, -1) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, im_bytes) glGenerateMipmap(GL_TEXTURE_2D) # tell opengl for each sampler to which texture unit it belongs # to (only has to be done once) ourShader.use() ourShader.setInt('texture1', 0) ourShader.setInt('texture2', 1) # render loop global deltaTime global lastFrame while not glfw.window_should_close(window): # pre-frame time logic currentFrame = glfw.get_time() deltaTime = currentFrame - lastFrame lastFrame = currentFrame # input processInput(window) # render glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # bind textures on corresponding texture units glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, texture1) glActiveTexture(GL_TEXTURE1) glBindTexture(GL_TEXTURE_2D, texture2) # camera/view transformation projection = glm_mt.perspective(np.radians(camera.Zoom), SCR_WIDTH / SCR_HEIGHT, 0.1, 100.0) ourShader.setMat4('projection', np.array(projection, dtype=GLfloat)) # render boxes view = camera.GetViewMatrix() ourShader.setMat4('view', np.array(view, dtype=GLfloat)) for i in range(10): # calculate the model matrix for each object and pass # it to shader before drawing model = glm_mt.translate(glm.mat4(), cubePositions[i]) angle = 20.0 * i model = glm_mt.rotate(model, np.radians(angle), np.array([1, 0.3, 0.5])) ourShader.setMat4('model', np.array(model, dtype=GLfloat)) glDrawArrays(GL_TRIANGLES, 0, 36) # glfw: swap buffers and poll IO events (keys pressed/released, # mouse moved etc.) glfw.swap_buffers(window) glfw.poll_events() # optional: de-allocate all resources once they've outlived their purpose: ourShader.unuse() glBindVertexArray(0) ourShader.delete() glDeleteVertexArrays(1, (VAO, )) glDeleteBuffers(1, (VBO, )) # glfw: terminate, clearing all previously allocated GLFW resources. glfw.terminate()
class Triangle: def __init__(self): self.window = prepare_window() # Positions # Colors # Texture Coords self.vertices = np.array( [ [-0.5, 0.5, 1.0, 0.0, 0.0, 0.0, 1.0], # Top left [0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 1.0], # Top right [0.5, -0.5, 0.0, 0.0, 1.0, 1.0, 0.0], # Bottom right [-0.5, -0.5, 1.0, 1.0, 1.0, 0.0, 0.0] ], # Bottom left dtype=GLfloat) self.indices = np.array([[0, 1, 2], [2, 3, 0]], dtype=GLuint) self.make_vao() self.texture1 = self.make_texture('../../images/sample.png', unit=GL_TEXTURE0) self.shader_program.setInt('ourTexture1', 0) self.texture2 = self.make_texture('../../images/sample2.png', unit=GL_TEXTURE1) self.shader_program.setInt('ourTexture2', 1) self.shader_program.unuse() def __enter__(self): glBindVertexArray(self.vao) self.shader_program.use() return self def __exit__(self, type, value, traceback): self.shader_program.unuse() glBindVertexArray(0) self.shader_program.delete() glDeleteVertexArrays(1, (self.vao, )) glDeleteBuffers(1, (self.ebo, )) glDeleteBuffers(1, (self.vbo, )) def make_vao(self): self.vao = glGenVertexArrays(1) glBindVertexArray(self.vao) self.shader_program = OurShaderProgram(vert='texture.vert', frag='texture.frag') self.shader_program.use() self.vbo = make_buffer_object(data=self.vertices.tostring(), usage='GL_STATIC_DRAW', target='GL_ARRAY_BUFFER') glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), self.vbo) glEnableVertexAttribArray(0) glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), self.vbo + 2 * sizeof(GLfloat)) glEnableVertexAttribArray(1) glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), self.vbo + 5 * sizeof(GLfloat)) glEnableVertexAttribArray(2) self.ebo = make_buffer_object(data=self.indices.tostring(), usage='GL_STATIC_DRAW', target='GL_ELEMENT_ARRAY_BUFFER') self.vbo.unbind() glBindVertexArray(0) def make_texture(self, image, unit=GL_TEXTURE0): im = Image.open(image) w, h = im.size try: im_bytes = im.tobytes("raw", "RGBA", 0, -1) except ValueError: im_bytes = im.tobytes("raw", "RGBX", 0, -1) texture = glGenTextures(1) with bindTexture(unit, GL_TEXTURE_2D, texture): glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, im_bytes) glGenerateMipmap(GL_TEXTURE_2D) return texture def draw(self): glClear(GL_COLOR_BUFFER_BIT) glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, None) glfw.swap_buffers(self.window)
class Triangle: def __init__(self): self.window = prepare_window() # Positions # Colors # Texture Coords self.vertices = np.array([[-0.5, -0.5, -0.5, 1.0, 1.0, 1.0, 0.0, 0.0], [0.5, -0.5, -0.5, 1.0, 1.0, 1.0, 1.0, 0.0], [0.5, 0.5, -0.5, 1.0, 1.0, 1.0, 1.0, 1.0], [0.5, 0.5, -0.5, 1.0, 1.0, 1.0, 1.0, 1.0], [-0.5, 0.5, -0.5, 1.0, 1.0, 1.0, 0.0, 1.0], [-0.5, -0.5, -0.5, 1.0, 1.0, 1.0, 0.0, 0.0], [-0.5, -0.5, 0.5, 1.0, 1.0, 1.0, 0.0, 0.0], [0.5, -0.5, 0.5, 1.0, 1.0, 1.0, 1.0, 0.0], [0.5, 0.5, 0.5, 1.0, 1.0, 1.0, 1.0, 1.0], [0.5, 0.5, 0.5, 1.0, 1.0, 1.0, 1.0, 1.0], [-0.5, 0.5, 0.5, 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.0, 0.0, 1.0], [-1.0, -1.0, -0.5, 0.0, 0.0, 0.0, 0.0, 0.0], [1.0, -1.0, -0.5, 0.0, 0.0, 0.0, 1.0, 0.0], [1.0, 1.0, -0.5, 0.0, 0.0, 0.0, 1.0, 1.0], [1.0, 1.0, -0.5, 0.0, 0.0, 0.0, 1.0, 1.0], [-1.0, 1.0, -0.5, 0.0, 0.0, 0.0, 0.0, 1.0], [-1.0, -1.0, -0.5, 0.0, 0.0, 0.0, 0.0, 0.0]], dtype=GLfloat) self.make_vao() self.texture1 = self.make_texture('../images/sample.png', unit=GL_TEXTURE0) self.shader_program.setInt('texKitten', 0) self.texture2 = self.make_texture('../images/sample2.png', unit=GL_TEXTURE1) self.shader_program.setInt('texPuppy', 1) self.shader_program.unuse() def __enter__(self): glBindVertexArray(self.vao) self.shader_program.use() return self def __exit__(self, type, value, traceback): self.shader_program.unuse() glBindVertexArray(0) self.shader_program.delete() glDeleteVertexArrays(1, (self.vao, )) glDeleteBuffers(1, (self.vbo, )) def make_vao(self): self.vao = glGenVertexArrays(1) glBindVertexArray(self.vao) self.shader_program = OurShaderProgram(vert='texture.vert', frag='texture.frag') self.shader_program.use() self.vbo = make_buffer_object(data=self.vertices.tostring(), usage='GL_STATIC_DRAW', target='GL_ARRAY_BUFFER') glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), self.vbo) glEnableVertexAttribArray(0) glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), self.vbo + 3 * sizeof(GLfloat)) glEnableVertexAttribArray(1) glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), self.vbo + 6 * sizeof(GLfloat)) glEnableVertexAttribArray(2) self.vbo.unbind() glBindVertexArray(0) def make_texture(self, image, unit=GL_TEXTURE0): im = Image.open(image) w, h = im.size try: im_bytes = im.tobytes("raw", "RGBA", 0, -1) except ValueError: im_bytes = im.tobytes("raw", "RGBX", 0, -1) texture = glGenTextures(1) with bindTexture(unit, GL_TEXTURE_2D, texture): glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, im_bytes) glGenerateMipmap(GL_TEXTURE_2D) return texture def draw(self): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glDrawArrays(GL_TRIANGLES, 0, 36) glEnable(GL_STENCIL_TEST) # Draw floor glStencilFunc(GL_ALWAYS, 1, 0xFF) glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE) glStencilMask(0xFF) glDepthMask(GL_FALSE) glClear(GL_STENCIL_BUFFER_BIT) glDrawArrays(GL_TRIANGLES, 36, 6) # Draw cube reflection glStencilFunc(GL_EQUAL, 1, 0xFF) glStencilMask(0x00) glDepthMask(GL_TRUE)