def __init__(self, window_width, window_height, samples, window_title): self.window_title = window_title assert glfw.Init(), 'Glfw Init failed!' glfw.WindowHint(glfw.SAMPLES, samples) self.windowID = glfw.CreateWindow(window_width, window_height, self.window_title, None) assert self.windowID, 'Could not create Window!' glfw.MakeContextCurrent(self.windowID) assert glInitBindlessTextureNV(), 'Bindless Textures not supported!' self.framebuf_width, self.framebuf_height = glfw.GetFramebufferSize( self.windowID) def framebuffer_size_callback(window, w, h): self.framebuf_width, self.framebuf_height = w, h glfw.SetFramebufferSizeCallback(self.windowID, framebuffer_size_callback) def key_callback(window, key, scancode, action, mode): if action == glfw.PRESS: if key == glfw.KEY_ESCAPE: glfw.SetWindowShouldClose(window, True) glfw.SetKeyCallback(self.windowID, key_callback) self.previous_second = glfw.GetTime() self.frame_count = 0.0 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE) glDisable(GL_STENCIL_TEST) glDisable(GL_BLEND) glDisable(GL_CULL_FACE) glClearColor(0.0, 0.0, 0.0, 1.0) glEnable(GL_LINE_SMOOTH) glEnable(GL_POLYGON_SMOOTH)
def display(window): glClearColor(0.0, 0.0, 0.0, 0.0) glClear(GL_COLOR_BUFFER_BIT) glUseProgram(theProgram) # unif = glGetUniformLocation(theProgram, 'myPos') # glUniform1f(unif, anim) # glUniform1f(unif, anim) # unif = glGetUniformLocation(theProgram, 'heartCenter') # glUniform2f(unif, x / 2, y / 2) # anim += 0.1 * dirc # if anim > 1 or anim < 0: # dirc *= -1 unif = glGetUniformLocation(theProgram, 'iTime') glUniform1f(unif, time() - start_time) unif = glGetUniformLocation(theProgram, 'iResolution') x, y = glfw.GetFramebufferSize(window) glUniform2f(unif, x, y) unif = glGetUniformLocation(theProgram, 'iMouse') glUniform2f(unif, 10, 10) unif = glGetUniformLocation(theProgram, 'MVP') global angle cosA = cos(angle) sinA = sin(angle) # angle += 0.05 glUniformMatrix3fv( unif, 1, GL_TRUE, np.array([cosA * 2, sinA, 0, -sinA, cosA * 2, 0, 0, 0, 1], dtype='float32')) # uniform float iTime; # uniform vec2 iResolution; # uniform vec2 iMouse; # anim = 1 glBindVertexArray(vao) glEnableVertexAttribArray(0) glDrawArrays(GL_TRIANGLES, 0, nVertices) glDisableVertexAttribArray(0) glBindVertexArray(0) glUseProgram(0)
import rospy from sensor_msgs.msg import Image from cv_bridge import CvBridge, CvBridgeError import cv2 bridge = CvBridge() window_width, window_height = 640, 480 samples = 16 window_title = 'OpenCV+OpenGL' assert glfw.Init(), 'Glfw Init failed!' glfw.WindowHint(glfw.SAMPLES, samples) window = glfw.CreateWindow(window_width, window_height, window_title, None) assert window, 'Could not create Window!' glfw.MakeContextCurrent(window) assert glInitBindlessTextureNV(), 'Bindless Textures not supported!' framebuf_width, framebuf_height = glfw.GetFramebufferSize(window) def framebuffer_size_callback(window, w, h): global framebuf_width, framebuf_height framebuf_width, framebuf_height = w, h glfw.SetFramebufferSizeCallback(window, framebuffer_size_callback) def key_callback(window, key, scancode, action, mode): if action == glfw.PRESS: if key == glfw.KEY_ESCAPE: glfw.SetWindowShouldClose(window, True) glfw.SetKeyCallback(window, key_callback) previous_second = glfw.GetTime(); frame_count = 0.0 def update_fps_counter(window): global previous_second, frame_count current_second = glfw.GetTime() elapsed_seconds = current_second - previous_second
def __init__(self, window_width, window_height, samples=1, window_title="", monitor=1, show_at_center=True, offscreen=False): self.window_title = window_title assert glfw.Init(), "Glfw Init failed!" glfw.WindowHint(glfw.SAMPLES, samples) if offscreen: glfw.WindowHint(glfw.VISIBLE, False) mon = glfw.GetMonitors()[monitor] if monitor != None else None self.windowID = glfw.CreateWindow(window_width, window_height, self.window_title, mon) assert self.windowID, "Could not create Window!" glfw.MakeContextCurrent(self.windowID) if not glInitBindlessTextureNV(): raise RuntimeError("Bindless Textures not supported") self.framebuf_width, self.framebuf_height = glfw.GetFramebufferSize( self.windowID) self.framebuffer_size_callback = [] def framebuffer_size_callback(window, w, h): self.framebuf_width, self.framebuf_height = w, h for callback in self.framebuffer_size_callback: callback(w, h) glfw.SetFramebufferSizeCallback(self.windowID, framebuffer_size_callback) self.key_callback = [] def key_callback(window, key, scancode, action, mode): if action == glfw.PRESS: if key == glfw.KEY_ESCAPE: glfw.SetWindowShouldClose(window, True) for callback in self.key_callback: callback(key, scancode, action, mode) glfw.SetKeyCallback(self.windowID, key_callback) self.mouse_callback = [] def mouse_callback(window, xpos, ypos): for callback in self.mouse_callback: callback(xpos, ypos) glfw.SetCursorPosCallback(self.windowID, mouse_callback) self.mouse_button_callback = [] def mouse_button_callback(window, button, action, mods): for callback in self.mouse_button_callback: callback(button, action, mods) glfw.SetMouseButtonCallback(self.windowID, mouse_button_callback) self.scroll_callback = [] def scroll_callback(window, xoffset, yoffset): for callback in self.scroll_callback: callback(xoffset, yoffset) glfw.SetScrollCallback(self.windowID, scroll_callback) self.previous_second = glfw.GetTime() self.frame_count = 0.0 if show_at_center: monitors = glfw.GetMonitors() assert monitor >= 0 and monitor < len( monitors), "Invalid monitor selected." vidMode = glfw.GetVideoMode(monitors[monitor]) glfw.SetWindowPos( self.windowID, vidMode.width / 2 - self.framebuf_width / 2, vidMode.height / 2 - self.framebuf_height / 2, )
window = glfw.CreateWindow(W, H, "My Title", None, None) glfw.MakeContextCurrent(window) def center_pos(monitor_id): xpos = glfw.GetMonitorPos(mon[monitor_id])[0] + glfw.GetVideoMode( mon[monitor_id]).width / 2 - W / 2 ypos = glfw.GetMonitorPos(mon[monitor_id])[1] + glfw.GetVideoMode( mon[monitor_id]).height / 2 - H / 2 glfw.SetWindowPos(window, xpos, ypos) center_pos(monitor_id) w, h = glfw.GetFramebufferSize(window) def key_callback(window, key, scancode, action, mods): global monitor_id if key == glfw.KEY_E and action == glfw.PRESS: monitor_id = (monitor_id + 1) % len(mon) center_pos(monitor_id) if key == glfw.KEY_N and action == glfw.PRESS: glfw.SetInputMode(window, glfw.CURSOR, glfw.CURSOR_NORMAL) glfw.SetKeyCallback(window, key_callback) def character_callback(window, codepoint):
def window_size_callback(window, w, h): width, height = glfw.GetFramebufferSize(window) glViewport(0, 0, width, height)