Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import glfw
- from OpenGL.GL import *
- import math
- # global variables for window info, camera info
- resx = 1000 # resolution of window horizontal direction
- resy = 600 # resoltuion vertical direction
- cam_x = 0.0 # left edge x-value
- cam_y = 0.0
- cam_height = 1.0 # lower edge y-value
- cam_width = cam_height*resx/float(resy) # right edge x-value
- prevx = 0.0
- prevy = 0.0
- def main():
- itermax = 30
- # Initialize the library
- if not glfw.init():
- return
- # Create a windowed mode window and its OpenGL context
- window = glfw.create_window(resx, resy, "Hello World", None, None)
- if not window:
- glfw.terminate()
- return
- # Make the window's context current
- glfw.make_context_current(window)
- # Tell gLFW which callback function to use on input
- glfw.set_key_callback(window, key_callback)
- glfw.set_mouse_button_callback(window, mousebutton_callback)
- glfw.set_cursor_pos_callback(window, mouseposition_callback)
- glfw.set_scroll_callback(window, mousewheel_callback)
- glfw.set_window_size_callback(window, windowsize_callback)
- glClearColor(1.0, 1.0, 1.0, 1.0)
- glOrtho(cam_x, cam_x+cam_width, cam_y, cam_y+cam_height, -1.0, 1.0) # set "camera" to screen coordinates
- # Loop until the user closes the window
- while not glfw.window_should_close(window):
- # Render here, e.g. using pyOpenGL
- glClear(GL_COLOR_BUFFER_BIT)
- glLoadIdentity();
- glOrtho(cam_x, cam_x+cam_width, cam_y, cam_y+cam_height, -1.0, 1.0) # set "camera" to screen coordinates
- glBegin(GL_QUADS);
- glColor3f(1.0, 0.0, 0.0);
- glVertex2f(0.0, 0.0);
- glVertex2f(1.0, 0.0);
- glVertex2f(1.0, 1.0);
- glVertex2f(0.0, 1.0);
- glEnd()
- glBegin(GL_TRIANGLES);
- glColor3f(0.0, 1.0, 0.0);
- glVertex2f(1.5, 0.5);
- glVertex2f(2.5, 0.5);
- glVertex2f(1.5, 1.0);
- glEnd()
- # Swap front and back buffers
- glfw.swap_buffers(window)
- # Poll for and process events, call relevant callback function
- glfw.poll_events()
- glfw.terminate()
- # Callback function called every time the window size change
- # Adjusts the camera width and heigh so that the scale stays the same
- # Resets projection matrix
- def windowsize_callback(window, width, height):
- global resx, resy, cam_width, cam_height
- distance_per_pixel = cam_height/resy; # assuming cam_height/resy == cam_width/resx
- resx = width;
- resy = height;
- cam_width = distance_per_pixel*resx;
- cam_height = distance_per_pixel*resy;
- glLoadIdentity();
- glViewport(0, 0, resx, resy);
- glOrtho(cam_x, cam_x + cam_width, cam_y, cam_y + cam_height, -1, 1);
- # Callback function called every time a keyboard key is pressed, released or held down
- def key_callback(window, key, scancode, action, mods):
- print("Key: key = {}, scancode = {}, action = {}, mods = {}".format(key, scancode, action, mods))
- # Callback function called every time a mouse button pressed or released
- def mousebutton_callback(window, button, action, mods):
- global prevx, prevy
- # get current cursor position, used in mouseposition_callback
- # to know how far the mouse moved
- prevx, prevy = glfw.get_cursor_pos(window)
- print("Mousebutton: button = {}, action = {}, mods = {}, prevx = {}, prevy = {}".format(button, action, mods, prevx, prevy))
- # Callback function called every time a the mouse is moved
- def mouseposition_callback(window, xpos, ypos):
- global cam_x, cam_y, prevx, prevy
- # move the camera if the first mouse button is held down
- # the cursor will stay on at the same location relative to world coordinates after movement
- if glfw.get_mouse_button(window, 0):
- # left button pressed
- cam_x -= (xpos-prevx)/resx*cam_width
- cam_y += (ypos-prevy)/resy*cam_height
- prevx = xpos
- prevy = ypos
- print("Mousepos: xpos = {}, ypos = {}".format(xpos, ypos))
- # Callback function called every time a the mouse scroll wheel is moved
- # yoffset = up-down
- # xoffset = left-right
- def mousewheel_callback(window, xoffset, yoffset):
- global cam_x, cam_y, cam_height, cam_width
- zoomFactor = math.pow(0.95,yoffset);
- # zoom the camera by keeping the center constant and move the edges accordingly
- cam_x = cam_x + cam_width*(1.0 - zoomFactor)/2.0;
- cam_y = cam_y + cam_height*(1.0 - zoomFactor)/2.0;
- cam_width *= zoomFactor;
- cam_height *= zoomFactor;
- print("Mousewheel: xoffset = {}, yoffset = {}".format(xoffset, yoffset))
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement