def main(): if not glfw.init(): raise Exception("glfw nt initialized") window=glfw.create_window(480,480, "Midpoint Circle Algorithm",None,None) if not window: glfw.terminate() raise Exception("glfw window not created") w,h = glfw.get_framebuffer_size(window) print("width: {}, height:{}".format(w,h)) glfw.set_window_pos(window, 400,200) glfw.make_context_current(window) glfw.set_key_callback(window, key_callback) glfw.set_window_size_callback(window, reshape_callback) gluOrtho2D(-200.0, 200.0,-200.0,200.0) setpixel(0,0,[1,0,1]) while not glfw.window_should_close(window): glClear(GL_COLOR_BUFFER_BIT) #glClearColor(0.0,0.76,0.56,1.0) glClearColor(1,1,1,1.0) setpixel((0), (0), [1,0,0]) BresenhamCircle(0,0,150, [1,0,1]) glfw.swap_buffers(window) glfw.poll_events() glfw.terminate()
def _glfw_init(width=0, height=0): window_name = 'pliky' if not glfw.init(): print("Could not initialize OpenGL context") exit(1) mode = glfw.get_video_mode(glfw.get_primary_monitor()) if width == 0 or height == 0: width = mode.size.width / 2 if width == 0 else width height = mode.size.height / 2 if height == 0 else height # OS X supports only forward-compatible core profiles from 3.2 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) glfw.window_hint(glfw.DECORATED, False) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, gl.GL_TRUE) print(f'Window width: {width}, height: {height}') # Create a windowed mode window and its OpenGL context window = glfw.create_window(int(width), int(height), window_name, None, None) glfw.make_context_current(window) if window: glfw.set_window_pos(window, int((mode.size.width - width) / 2), int((mode.size.height - height) / 2)) else: glfw.terminate() print("Could not initialize Window") exit(1) return window
def on_cursor_pos(self, window, x, y): p = ivec2(x, y) delta = p - self._prevpos self._prevpos = p if self._isdrag: win_pos = ivec2(*glfw.get_window_pos(window)) glfw.set_window_pos(window, *(win_pos + delta))
def render(self, mode: RenderMode = RenderMode(), render_step: int = 1): if self._curr_step % render_step == 0: # Call base class super().render(mode) # Print to console if mode.text: print( f"step: {self._curr_step:4d} | r_t: {self._curr_rew: 1.3f} | a_t: {self._curr_act} | s_t+1: {self.state}" ) # Forward to MuJoCo viewer if mode.video: if self.viewer is None: # Create viewer if not existent (see 'human' mode of OpenAI Gym's MujocoEnv) self.viewer = mujoco_py.MjViewer(self.sim) # Adjust window size and position to custom values import glfw glfw.make_context_current(self.viewer.window) glfw.set_window_size(self.viewer.window, 1280, 720) glfw.set_window_pos(self.viewer.window, 50, 50) self.configure_viewer() self.viewer.render()
def init_window(self): window_width, window_height = glfw.get_window_size(self.window) _, _, center_x, center_y = glfw.get_monitor_workarea( glfw.get_primary_monitor()) window_x = (center_x // 2) - (window_width // 2) window_y = (center_y // 2) - (window_height // 2) glfw.set_window_pos(self.window, window_x, window_y)
def on_cursor_pos(self, window, x, y): pos = ivec2(x, y) delta = pos - self.prev_cursor self.prev_cursor = pos if self.is_drag_win: new_pos = ivec2(glfw.get_window_pos(window)) + delta glfw.set_window_pos(window, *new_pos)
def __init__(self, width: int, height: int, title: str, figure='', operations=False): # Initializing glfw library if not glfw.init(): raise Exception('Glfw can not be initialized!') # Creating the window self._win = glfw.create_window(width, height, title, None, None) # Check if window was created if not self._win: glfw.terminate() raise Exception('Glfw window could not be created!') # Set window position glfw.set_window_pos(self._win, 400, 200) # Make the context current glfw.make_context_current(self._win) glClearColor(34 / 255, 34 / 255, 34 / 255, 1) # 222222 if figure == 'triangle': # Setup for drawing a triangle self._triangle = self._draw_triangle() else: self._triangle = False self._operations = operations
def main(): if not glfw.init(): print ('GLFW initialization failed') sys.exit(-1) width = 800 height = 600 window = glfw.create_window(width, height, 'COW COASTER', None, None) if not window: glfw.terminate() sys.exit(-1) glfw.make_context_current(window) glfw.set_key_callback(window, onKeyPress) glfw.set_mouse_button_callback(window, onMouseButton) glfw.set_cursor_pos_callback(window, onMouseDrag) glfw.swap_interval(1) mac_moved = False initialize(window) while not glfw.window_should_close(window): glfw.poll_events() display() glfw.swap_buffers(window) # TO FIX ERROR IN MAC # Screen does not render properly until move/resizing its window if not mac_moved: x_win, y_win = glfw.get_window_pos(window) x_win += 1 glfw.set_window_pos(window, x_win, y_win) mac_moved = True glfw.terminate()
def __init__(self, WIDTH=1280, HEIGHT=720, framerate=25): self.width = WIDTH self.height = HEIGHT # initializing glfw library if not glfw.init(): raise Exception("glfw can not be initialized!") self.window = glfw.create_window(self.width, self.height, "Display", None, None) # check if window was created if not self.window: glfw.terminate() raise Exception("glfw window can not be created!") # make the context current glfw.make_context_current(self.window) # set window's position glfw.set_window_pos(self.window, 400, 200) # set the callback function for window resize glfw.set_window_size_callback(self.window, self.window_resize) self.scenes = [] self.viewports = [] self.cameras = [] self.update_calls = [] self.components = [] self.frame_rate = framerate
def __create_window(self): window = glfw.create_window(self.widht, self.height, self.window_title, None, None) glfw.set_window_pos(window, 400, 200) glfw.make_context_current(window) return window
def __init__(self, width, height, FPS_CAP, title="Our LBS Window BITCH!"): try: print("creating window...") glfw.init() glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3) # TODO: you must be wondering why I chose version 3.3 even though our pyopengl is 3.1. # it's based on this comment from another project: # "If we are planning to use anything above 2.1 we must at least # request a 3.3 core context to make this work across platforms" # anyway, it works! self.window = glfw.create_window(width, height, title, monitor=None, share=None) glfw.set_window_pos(self.window, 400, 200) glfw.make_context_current(self.window) except Exception as e: print(e) print("Couldn't create window!") raise SystemExit # gl.GL11.glViewport(0, 0, width, height) print("window created")
def initWindow(self, width, height): self.width = width self.height = height # initializing glfw library if not glfw.init(): raise Exception("glfw can not be initialized!") # Configure the OpenGL context mac osx. # If we are planning to use anything above 2.1 we must at least # request a 3.3 core context to make this work across platforms. #glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4) #glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3) #glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) #glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True) # 4 MSAA is a good default with wide support #glfw.window_hint(glfw.SAMPLES, 4) # creating the window window = glfw.create_window(self.width, self.height, "Mein Grafikfenster", None, None) # check if window was created if not window: glfw.terminate() raise Exception("glfw window can not be created!") # set window's position glfw.set_window_pos(window, XPOS, YPOS) # make the context current glfw.make_context_current(window) return window
def __init__ (self, Module, Title, Width, Height): global Pyron Pyron = Module self.Module = Module self.Title = Title self.Width = Width self.Height = Height # Change the FPS later idk FPS = 100 self._NextUpdateTime = 0 self._OneFrameTime = 1 / FPS self._Update = None self._Draw = None Monitor = glfw.get_primary_monitor () DisplayWidth, DisplayHeight = glfw.get_video_mode (Monitor)[0] self._Window = glfw.create_window (_Width, _Height, _Title, None, None) if not self._Window: glfw.terminate () glfw.set_window_pos ( self._Window, (DisplayWidth - _Width) // 2, (DisplayHeight - _Height) // 2 )
def main(): if not glfw.init(): raise Exception("glfw nt initialized") window = glfw.create_window(700, 700, "B117049- Quartic Function", None, None) if not window: glfw.terminate() raise Exception("glfw window not created") w, h = glfw.get_framebuffer_size(window) print("width: {}, height:{}".format(w, h)) glfw.set_window_pos(window, 400, 45) glfw.make_context_current(window) glfw.set_key_callback(window, key_callback) glfw.set_window_size_callback(window, reshape_callback) gluOrtho2D(-80.0, 80, -400000.0, 40000000.0) while not glfw.window_should_close(window): glClear(GL_COLOR_BUFFER_BIT) #glClearColor(0.0,0.76,0.56,1.0) glClearColor(1, 1, 1, 1.0) drawAxes() for i in range(-79, 79, 1): y = quartic(i) setpixel(i, y, (1, 0, 0)) glfw.swap_buffers(window) glfw.poll_events() glfw.terminate()
def on_cursor_pos(self, window, x, y): p = (x, y) dx, dy = p[0] - self._prevpos[0], p[1] - self._prevpos[1] self._prevpos = p self.uniform("u_mousepos", (x, y)) # rotate camera if self._isdrag_camera: ru = rotate(mat4(1.0), -dx * 0.0005, UP) rr = rotate(mat4(1.0), dy * 0.0005, cross(self._campos, UP)) p = (ru * rr) * vec4(self._campos, 1.0) self._campos = p.xyz if self._isdrag_light: ru = rotate(mat4(1.0), dx * 0.0005, UP) rr = rotate(mat4(1.0), -dy * 0.0005, cross(self._lightpos, UP)) p = (ru * rr) * vec4(self._lightpos, 1.0) self._lightpos = p.xyz # zoom if self._isdrag_right: forward = normalize(-self._campos) next_pos = self._campos + forward * dx * 0.005 self._campos = self._campos if length(next_pos) < 4.0 else next_pos # move window if self._isdrag_mid: wx, wy = glfw.get_window_pos(window) glfw.set_window_pos(window, int(wx + dx), int(wy + dy))
def main(): # initializing glfw library if not glfw.init(): raise Exception("glfw can not be initialized!") # Configure the OpenGL context. # If we are planning to use anything above 2.1 we must at least # request a 3.3 core context to make this work across platforms. if "Windows" in pltf.platform(): glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 6) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True) else: glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 1) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True) # 4 MSAA is a good default with wide support glfw.window_hint(glfw.SAMPLES, 4) # creating the window window = glfw.create_window(1280, 720, "My OpenGL window", None, None) # check if window was created if not window: glfw.terminate() raise Exception("glfw window can not be created!") # Query the actual framebuffer size so we can set the right viewport later # -> glViewport(0, 0, framebuffer_size[0], framebuffer_size[1]) framebuffer_size = glfw.get_framebuffer_size(window) # set window's position glfw.set_window_pos(window, 100, 100) # make the context current glfw.make_context_current(window) # glClearColor(0.5, 0.5, 0.5, 1.0) print("GL_RENDERER = ", glGetString(GL_RENDERER).decode("utf8")) print("GL_VERSION = ", glGetString(GL_VERSION).decode("utf8")) # the main application loop while not glfw.window_should_close(window): glfw.poll_events() # glClear(GL_COLOR_BUFFER_BIT) COLOR = glm.array( glm.vec4([ glm.sin(glfw.get_time()) * 0.5 + 0.5, glm.cos(glfw.get_time()) * 0.5 + 0.5, 0.0, 1.0 ])) glClearBufferfv(GL_COLOR, 0, COLOR.ptr) glfw.swap_buffers(window) # terminate glfw, free up allocated resources glfw.terminate()
def on_cursor_pos(self, window, x, y): pos = ivec2(x, y) delta = pos - self.last_pos self.last_pos = pos if self.is_drag: win_pos = ivec2(*glfw.get_window_pos(window)) glfw.set_window_pos(window, *(win_pos + delta))
def on_cursor_pos(self, window, x, y): pos = ivec2(x, y) d = pos - self.prev_pos self.prev_pos = pos if self.is_drag: win_pos = ivec2(*glfw.get_window_pos(window)) glfw.set_window_pos(window, *(win_pos + d))
def center_pos(window, monitor_id, W, H): # W, H: window dimensions mon = glfw.get_monitors() xpos = glfw.get_monitor_pos(mon[monitor_id])[0] + \ glfw.get_video_mode(mon[monitor_id]).size[0]/2-W/2 ypos = glfw.get_monitor_pos(mon[monitor_id])[1] + \ glfw.get_video_mode(mon[monitor_id]).size[1]/2-H/2 glfw.set_window_pos(window, int(xpos), int(ypos))
def on_cursor_pos(self, window, x, y): pos = ivec2(x, y) d = pos - self.prev_pos self.prev_pos = pos if self.is_drag: new_pos = ivec2(glfw.get_window_pos(window)) + d glfw.set_window_pos(window, *new_pos)
def main(): # initializing glfw library if not glfw.init(): raise Exception("glfw can not be initialized!") # creating the window window = glfw.create_window(1280, 720, "My OpenGL window", None, None) # check if window was created if not window: glfw.terminate() raise Exception("glfw window can not be created!") # set window's position glfw.set_window_pos(window, 100, 100) # make the context current glfw.make_context_current(window) vertices = [-0.5, -0.5, 0.0, 0.5, -0.5, 0.0, 0.0, 0.5, 0.0] colors = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0] vertices = np.array(vertices, dtype=np.float32) colors = np.array(colors, dtype=np.float32) glEnableClientState(GL_VERTEX_ARRAY) glVertexPointer(3, GL_FLOAT, 0, vertices) glEnableClientState(GL_COLOR_ARRAY) glColorPointer(3, GL_FLOAT, 0, colors) glClearColor(0, 0.1, 0.1, 1) # the main application loop while not glfw.window_should_close(window): glfw.poll_events() glClear(GL_COLOR_BUFFER_BIT) ct = glfw.get_time() # returns the elapsed time, since init was called glLoadIdentity() glScale(abs(sin(ct)), abs(sin(ct)), 1) glRotatef(sin(ct) * 45, 0, 0, 1) glTranslatef(sin(ct), cos(ct), 0) glDrawArrays(GL_TRIANGLES, 0, 3) glfw.swap_buffers(window) # terminate glfw, free up allocated resources glfw.terminate()
def on_cursor_pos(self, window, x, y): dx, dy = self.precursor.x - x, self.precursor.y - y if self.is_drag_window: wx, wy = glfw.get_window_pos(window) wx, wy = wx - dx, wy - dy glfw.set_window_pos(window, int(wx), int(wy)) self.precursor.x, self.precursor.y = x, y
def __init__(self, bodybg_path, texture_path, ans_path="ans_render.png"): self.mixValue = 0.3 if not glfw.init(): raise Exception("glfw can not be initialized!") # creating the window self.window = glfw.create_window(1024, 1024, "OpenGL Window", None, None) if not self.window: glfw.terminate() raise Exception("glfw window can not be created!") # set window's position glfw.set_window_pos(self.window, 100, 100) # set the callback function for window resize glfw.set_window_size_callback(self.window, self.window_resize) # Install a key handler glfw.set_key_callback(self.window, self.on_key) # make the context current glfw.make_context_current(self.window) self.shader = compileProgram( compileShader(vertex_src, GL_VERTEX_SHADER), compileShader(fragment_src, GL_FRAGMENT_SHADER)) glUseProgram(self.shader) self.vertices = np.array(vertices, dtype=np.float32) self.indices = np.array(indices, dtype=np.uint32) # Step2: 创建并绑定VBO 对象 传送数据 VBO = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, VBO) glBufferData(GL_ARRAY_BUFFER, self.vertices.nbytes, self.vertices, GL_STATIC_DRAW) # Step3: 创建并绑定EBO 对象 传送数据 EBO = glGenBuffers(1) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO) glBufferData(GL_ELEMENT_ARRAY_BUFFER, self.indices.nbytes, self.indices, GL_STATIC_DRAW) # Step4: 指定解析方式 并启用顶点属性 # 顶点位置属性 glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, self.vertices.itemsize * 8, ctypes.c_void_p(0)) # 顶点颜色属性 glEnableVertexAttribArray(1) glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, self.vertices.itemsize * 8, ctypes.c_void_p(12)) # 顶点纹理属性 glEnableVertexAttribArray(2) glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, self.vertices.itemsize * 8, ctypes.c_void_p(24)) texture = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texture) self.texid1 = loadTexture(bodybg_path) self.texid2 = loadTexture(texture_path) self.path = ans_path
def on_cursor_pos(self, w, x, y): if not self.is_drag: return wx, wy = glfw.get_window_pos(self.window) glfw.set_window_pos( self.window, int(wx + x - self.drag_offset.x), int(wy + y - self.drag_offset.y), )
def __init__(self, width: int, height: int, title: str): if not glfw.init(): raise Exception("Cannot initialize glfw") self.win = glfw.create_window(width, height, title, None, None) if not self.win: glfw.terminate() raise Exception("glfw window cannot be created") glfw.set_window_pos(self.win, 500, 80) glfw.make_context_current(self.win)
def main(): # Initialize the library if not glfw.init(): return # Set some window hints glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3); glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3); glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, GL.GL_TRUE); glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE); glfw.window_hint(glfw.SAMPLES, 16) # This works as expected glfw.window_hint(glfw.RESIZABLE, 0) # These should work, but don't :( # could control focus w/ http://crunchbang.org/forums/viewtopic.php?id=22226 # ended up using xdotool, see run.py glfw.window_hint(glfw.FOCUSED, 0) # I've set 'shader-*' to raise in openbox-rc as workaround # Looking at the code and confirming version 3.1.2 and it should work glfw.window_hint(glfw.FLOATING, 1) # Create a windowed mode window and its OpenGL context window = glfw.create_window(300, 300, "shader-test", None, None) if not window: glfw.terminate() return # Move Window glfw.set_window_pos(window, 1600, 50) # Make the window's context current glfw.make_context_current(window) # vsync glfw.swap_interval(1) # Setup GL shaders, data, etc. initialize() # Loop until the user closes the window while not glfw.window_should_close(window): # Render here, e.g. using pyOpenGL render() # Swap front and back buffers glfw.swap_buffers(window) # Poll for and process events glfw.poll_events() glfw.terminate()
def set_pos(self, x=None, y=None): '设置窗口位置' if self.window is None: return if x is None or x is None: ws = glfw.get_window_size(self.window) ma = glfw.get_monitor_workarea(glfw.get_primary_monitor()) if x is None: x = (ma[2] - ws[0]) // 2 if y is None: y = (ma[3] - ws[1]) // 2 glfw.set_window_pos(self.window, x, y)
def move_window_to_monitor(window, monitor_name): monitors = glfw.get_monitors() monitor = None for m in monitors: if glfw.get_monitor_name(m) == monitor_name: monitor = m if monitor is not None: glfw.set_window_pos(window, *glfw.get_monitor_pos(monitor)) return True else: return False
def main(): if not glfw.init(): raise Exception("glfw nt initialized") window = glfw.create_window(640, 480, "B117049 - Random Points, Colours and Sizes", None, None) if not window: glfw.terminate() raise Exception("glfw window not created") w, h = glfw.get_framebuffer_size(window) print("width: {}, height:{}".format(w, h)) glfw.set_window_pos(window, 400, 200) glfw.make_context_current(window) glfw.set_key_callback(window, key_callback) glfw.set_window_size_callback(window, reshape_callback) gluOrtho2D(-200.0, 200.0, -200.0, 200.0) x = [] y = [] r = [] g = [] b = [] size = [] for i in range(0, 10, 1): x.append(random.randint(-200, 200)) y.append(random.randint(-200, 200)) r.append(random.randint(0, 256) / 256) g.append(random.randint(0, 256) / 256) b.append(random.randint(0, 256) / 256) size.append(random.randint(3, 20)) print("x= ", x[i], " y= ", y[i], "colour=(", r[i], ",", g[i], ",", b[i], ") ", "size=", size[i]) while not glfw.window_should_close(window): glClear(GL_COLOR_BUFFER_BIT) glClearColor(1, 1, 1, 1.0) for i in range(0, 10, 1): setpixel(x[i], y[i], (r[i], g[i], b[i]), size[i]) glfw.swap_buffers(window) glfw.poll_events() glfw.terminate()
def on_scroll(self, window, x, y): self._isresizing = True w, h = glfw.get_window_size(window) a = 1.1 if y > 0.0 else 0.9 w, h = w * a, h * a w, h = int(clamp(w, 128, 4096)), int(clamp(h, 128, 4096)) old_w, old_h = glfw.get_window_size(window) glfw.set_window_size(window, w, h) x, y = glfw.get_window_pos(window) glfw.set_window_pos(window, x - (w - old_w) // 2, y - (h - old_h) // 2) self._isresizing = False
def init(self): ''' initialization that must be done in rendering thread ''' # creating the opengl context self.handle = glfw.create_window(self.width, self.height, self.title, None, None); if self.handle == None: glfw.terminate() msg = "GLFW cannot create the window: width={width}, height={height}, title={title}".format( width=self.width, height=self.height, title=self.title) raise RuntimeError(msg) glfw.set_window_pos(self.handle, *self.DF_POSITION) glfw.make_context_current(self.handle) glfw.set_input_mode(self.handle, glfw.STICKY_KEYS, 1) glfw.swap_interval(self.DF_INTERVAL) glClearColor(*self.background_color)
def setup(self): # get glfw started glfw.init() self.window = glfw.create_window(self.width, self.height, "Python NanoVG Demo", None, None) glfw.set_window_pos(self.window, 0, 0) # Register callbacks window glfw.set_window_size_callback(self.window, self.on_resize) glfw.set_window_close_callback(self.window, self.on_close) glfw.set_key_callback(self.window, self.on_key) glfw.set_mouse_button_callback(self.window, self.on_button) self.basic_gl_setup() # glfwSwapInterval(0) glfw.make_context_current(self.window)
def run(self): # Initialize the library if not glfw.init(): return # Set some window hints glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3); glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3); glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, GL.GL_TRUE); glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE); glfw.window_hint(glfw.SAMPLES, 16) # This works as expected # glfw.window_hint(glfw.RESIZABLE, 0) # These should work, but don't :( # could control focus w/ http://crunchbang.org/forums/viewtopic.php?id=22226 # ended up using xdotool, see run.py glfw.window_hint(glfw.FOCUSED, 0) # I've set 'shader-*' to raise in openbox-rc as workaround # Looking at the code and confirming version 3.1.2 and it should work glfw.window_hint(glfw.FLOATING, 1) # Create a windowed mode window and its OpenGL context self.w = glfw.create_window(500, 500, "shader-test", None, None) if not self.w: glfw.terminate() return # Move Window glfw.set_window_pos(self.w, 1400, 50) # Callbacks glfw.set_key_callback(self.w, self.on_key) glfw.set_framebuffer_size_callback(self.w, self.on_resize); # Make the window's context current glfw.make_context_current(self.w) # vsync glfw.swap_interval(1) # Setup GL shaders, data, etc. self.initialize() # Loop until the user closes the window while not glfw.window_should_close(self.w): # print difference in time since start # this should always be 16.6-16.7 @ 60fps # print('%0.1fms' % (1000 * (time.time()-self.lastframe))) # start of the frame self.lastframe = time.time() # Render here, e.g. using pyOpenGL self.render() # Swap front and back buffers glfw.swap_buffers(self.w) # Poll for and process events glfw.poll_events() glfw.terminate()
def __init__(self, win, *args, **kwargs): """Set up the backend window according the params of the PsychoPy win Before PsychoPy 1.90.0 this code was executed in Window._setupPygame() Parameters ---------- win : psychopy.visual.Window instance PsychoPy Window (usually not fully created yet). share : psychopy.visual.Window instance PsychoPy Window to share a context with bpc : array_like Bits per color (R, G, B). refreshHz : int Refresh rate in Hertz. depthBits : int, Framebuffer (back buffer) depth bits. swapInterval : int Swap interval for the current OpenGL context. stencilBits : int Framebuffer (back buffer) stencil bits. winTitle : str Optional window title string. *args Additional position arguments. **kwargs Additional keyword arguments. """ BaseBackend.__init__(self, win) # window to share a context with shareWin = kwargs.get('share', None) if shareWin is not None: if shareWin.winType == 'glfw': shareContext = shareWin.winHandle else: logging.warning( 'Cannot share a context with a non-GLFW window. Disabling.') shareContext = None else: shareContext = None if sys.platform=='darwin' and not win.useRetina and pyglet.version >= "1.3": raise ValueError("As of PsychoPy 1.85.3 OSX windows should all be " "set to useRetina=True (or remove the argument). " "Pyglet 1.3 appears to be forcing " "us to use retina on any retina-capable screen " "so setting to False has no effect.") # window framebuffer configuration win.bpc = kwargs.get('bpc', (8, 8, 8)) # nearly all displays use 8 bpc win.refreshHz = int(kwargs.get('refreshHz', 60)) win.depthBits = int(kwargs.get('depthBits', 8)) win.stencilBits = int(kwargs.get('stencilBits', 8)) # win.swapInterval = int(kwargs.get('swapInterval', 1)) # vsync ON if 1 # get monitors, with GLFW the primary display is ALWAYS at index 0 allScrs = glfw.get_monitors() if len(allScrs) < int(win.screen) + 1: logging.warn("Requested an unavailable screen number - " "using first available.") win.screen = 0 thisScreen = allScrs[win.screen] if win.autoLog: logging.info('configured GLFW screen %i' % win.screen) # find a matching video mode (can we even support this configuration?) isVidmodeSupported = False for vidmode in glfw.get_video_modes(thisScreen): size, bpc, hz = vidmode if win._isFullScr: # size and refresh rate are ignored if windowed hasSize = size == tuple(win.size) hasHz = hz == win.refreshHz else: hasSize = hasHz = True hasBpc = bpc == tuple(win.bpc) if hasSize and hasBpc and hasHz: isVidmodeSupported = True break nativeVidmode = glfw.get_video_mode(thisScreen) if not isVidmodeSupported: # the requested video mode is not supported, use current logging.warning( ("The specified video mode is not supported by this display, " "using native mode ...")) logging.warning( ("Overriding user video settings: size {} -> {}, bpc {} -> " "{}, refreshHz {} -> {}".format( tuple(win.size), nativeVidmode[0], tuple(win.bpc), nativeVidmode[1], win.refreshHz, nativeVidmode[2]))) # change the window settings win.size, win.bpc, win.refreshHz = nativeVidmode if win._isFullScr: useDisplay = thisScreen else: useDisplay = None # configure stereo useStereo = 0 if win.stereo: # provide warning if stereo buffers are requested but unavailable if not glfw.extension_supported('GL_STEREO'): logging.warning( 'A stereo window was requested but the graphics ' 'card does not appear to support GL_STEREO') win.stereo = False else: useStereo = 1 # setup multisampling # This enables multisampling on the window backbuffer, not on other # framebuffers. msaaSamples = 0 if win.multiSample: maxSamples = (GL.GLint)() GL.glGetIntegerv(GL.GL_MAX_SAMPLES, maxSamples) if (win.numSamples & (win.numSamples - 1)) != 0: # power of two? logging.warning( 'Invalid number of MSAA samples provided, must be ' 'power of two. Disabling.') elif 0 > win.numSamples > maxSamples.value: # check if within range logging.warning( 'Invalid number of MSAA samples provided, outside of valid ' 'range. Disabling.') else: msaaSamples = win.numSamples win.multiSample = msaaSamples > 0 # disable stencil buffer if not win.allowStencil: win.stencilBits = 0 # set buffer configuration hints glfw.window_hint(glfw.RED_BITS, win.bpc[0]) glfw.window_hint(glfw.GREEN_BITS, win.bpc[1]) glfw.window_hint(glfw.BLUE_BITS, win.bpc[2]) glfw.window_hint(glfw.REFRESH_RATE, win.refreshHz) glfw.window_hint(glfw.STEREO, useStereo) glfw.window_hint(glfw.SAMPLES, msaaSamples) glfw.window_hint(glfw.STENCIL_BITS, win.stencilBits) glfw.window_hint(glfw.DEPTH_BITS, win.depthBits) glfw.window_hint(glfw.AUTO_ICONIFY, 0) # window appearance and behaviour hints if not win.allowGUI: glfw.window_hint(glfw.DECORATED, 0) # create the window self.winHandle = glfw.create_window( width=win.size[0], height=win.size[1], title=str(kwargs.get('winTitle', "PsychoPy (GLFW)")), monitor=useDisplay, share=shareContext) # The window's user pointer maps the Python Window object to its GLFW # representation. glfw.set_window_user_pointer(self.winHandle, win) glfw.make_context_current(self.winHandle) # ready to use # set the position of the window if not fullscreen if not win._isFullScr: # if no window position is specified, centre it on-screen if win.pos is None: size, bpc, hz = nativeVidmode win.pos = [(size[0] - win.size[0]) / 2.0, (size[1] - win.size[1]) / 2.0] # get the virtual position of the monitor, apply offset to the # window position px, py = glfw.get_monitor_pos(thisScreen) glfw.set_window_pos(self.winHandle, int(win.pos[0] + px), int(win.pos[1] + py)) elif win._isFullScr and win.pos is not None: logging.warn("Ignoring window 'pos' in fullscreen mode.") # set the window icon glfw.set_window_icon(self.winHandle, 1, _WINDOW_ICON_) # set the window size to the framebuffer size win.size = np.array(glfw.get_framebuffer_size(self.winHandle)) if win.useFBO: # check for necessary extensions if not glfw.extension_supported('GL_EXT_framebuffer_object'): msg = ("Trying to use a framebuffer object but " "GL_EXT_framebuffer_object is not supported. Disabled") logging.warn(msg) win.useFBO = False if not glfw.extension_supported('GL_ARB_texture_float'): msg = ("Trying to use a framebuffer object but " "GL_ARB_texture_float is not supported. Disabling") logging.warn(msg) win.useFBO = False # Assign event callbacks, these are dispatched when 'poll_events' is # called. glfw.set_mouse_button_callback(self.winHandle, event._onGLFWMouseButton) glfw.set_scroll_callback(self.winHandle, event._onGLFWMouseScroll) glfw.set_key_callback(self.winHandle, event._onGLFWKey) glfw.set_char_mods_callback(self.winHandle, event._onGLFWText) # set swap interval to manual setting, independent of waitBlanking self.setSwapInterval(int(kwargs.get('swapInterval', 1))) # give the window class GLFW specific methods win.setMouseType = self.setMouseType if not win.allowGUI: self.setMouseVisibility(False)
config_file = open('config.txt', 'r') ip, port, = open('config.txt', 'r').readline().split() player_id = registerMe('OpenGL') t1 = threading.Thread(target=asking, daemon=True).start() t2 = threading.Thread(target=sending, daemon=True).start() if not glfw.init(): exit(0) glfw.window_hint(glfw.RESIZABLE, GL_FALSE) glfw.window_hint(glfw.SAMPLES, 8) window = glfw.create_window(WINDOW_WIDTH, WINDOW_HEIGHT, 'agar.io', None, None) w, h = glfw.get_video_mode(glfw.get_primary_monitor())[0] glfw.set_window_pos(window, (w - WINDOW_WIDTH) // 2, (h - WINDOW_HEIGHT) // 2) glfw.set_cursor_pos_callback(window, on_motion) if not window: glfw.terminate() exit(0) glfw.make_context_current(window) glfw.swap_interval(1) glClearColor(1., 1., 1., 1.) glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(0, WINDOW_WIDTH, WINDOW_HEIGHT, 0) glMatrixMode(GL_MODELVIEW)
print( "Desktop video mode:\n%s\n" % str(vm) ) print( "GLFW Version: %d.%d.%d" % glfw.get_version() ) w = glfw.create_window(800, 600, 'test', None, None) #print("OpenGL version: %d.%d.%d\n" % glfw.get_gl_version()) #glfw.ext.set_icons([(icon_data, icon_width, icon_height)]) glfw.set_window_title(w, "pyglfw test") #glfw.disable(w, glfw.AUTO_POLL_EVENTS) #glfw.enable(w, glfw.KEY_REPEAT) center_x = int(vm[0][0] / 2 - glfw.get_window_size(w)[0] / 2) center_y = int(vm[0][1] / 2 - glfw.get_window_size(w)[1] / 2) print( "new window position: {!s}, {!s}".format(center_x, center_y) ) glfw.set_window_pos(w, center_x, center_y) glfw.set_window_size_callback(w, on_resize) glfw.set_window_close_callback(w, on_close) glfw.set_window_refresh_callback(w, on_refresh) glfw.set_key_callback(w, on_key) glfw.set_char_callback(w, on_char) glfw.set_mouse_button_callback(w, on_button) glfw.set_cursor_pos_callback(w, on_pos) glfw.set_scroll_callback(w, on_scroll) while not glfw.window_should_close(w): glfw.poll_events() if glfw.get_key(w, glfw.KEY_E) == glfw.PRESS: break