Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.65 KB | None | 0 0
  1. import glfw
  2. from OpenGL.GL import *
  3. import math
  4.  
  5. # global variables for window info, camera info
  6. resx = 1000   # resolution of window horizontal direction
  7. resy = 600   # resoltuion vertical direction
  8. cam_x = 0.0 # left edge x-value
  9. cam_y = 0.0
  10. cam_height = 1.0  # lower edge y-value
  11. cam_width = cam_height*resx/float(resy) # right edge x-value
  12.  
  13. prevx = 0.0
  14. prevy = 0.0
  15.  
  16. def main():
  17.  
  18.  
  19.     itermax = 30
  20.  
  21.  
  22.     # Initialize the library
  23.     if not glfw.init():
  24.         return
  25.     # Create a windowed mode window and its OpenGL context
  26.     window = glfw.create_window(resx, resy, "Hello World", None, None)
  27.     if not window:
  28.         glfw.terminate()
  29.         return
  30.  
  31.     # Make the window's context current
  32.     glfw.make_context_current(window)
  33.  
  34.     # Tell gLFW which callback function to use on input
  35.     glfw.set_key_callback(window, key_callback)
  36.     glfw.set_mouse_button_callback(window, mousebutton_callback)
  37.     glfw.set_cursor_pos_callback(window, mouseposition_callback)
  38.     glfw.set_scroll_callback(window, mousewheel_callback)
  39.     glfw.set_window_size_callback(window, windowsize_callback)
  40.  
  41.     glClearColor(1.0, 1.0, 1.0, 1.0)
  42.     glOrtho(cam_x, cam_x+cam_width, cam_y, cam_y+cam_height, -1.0, 1.0) # set "camera" to screen coordinates
  43.  
  44.  
  45.     # Loop until the user closes the window
  46.     while not glfw.window_should_close(window):
  47.         # Render here, e.g. using pyOpenGL
  48.        
  49.         glClear(GL_COLOR_BUFFER_BIT)
  50.         glLoadIdentity();
  51.         glOrtho(cam_x, cam_x+cam_width, cam_y, cam_y+cam_height, -1.0, 1.0) # set "camera" to screen coordinates
  52.  
  53.         glBegin(GL_QUADS);
  54.         glColor3f(1.0, 0.0, 0.0);
  55.         glVertex2f(0.0, 0.0);
  56.         glVertex2f(1.0, 0.0);
  57.         glVertex2f(1.0, 1.0);
  58.         glVertex2f(0.0, 1.0);
  59.         glEnd()
  60.  
  61.  
  62.         glBegin(GL_TRIANGLES);
  63.         glColor3f(0.0, 1.0, 0.0);
  64.         glVertex2f(1.5, 0.5);
  65.         glVertex2f(2.5, 0.5);
  66.         glVertex2f(1.5, 1.0);
  67.        
  68.         glEnd()
  69.  
  70.         # Swap front and back buffers
  71.         glfw.swap_buffers(window)
  72.  
  73.         # Poll for and process events, call relevant callback function
  74.         glfw.poll_events()
  75.  
  76.     glfw.terminate()
  77.  
  78.  
  79.  
  80. # Callback function called every time the window size change
  81. # Adjusts the camera width and heigh so that the scale stays the same
  82. # Resets projection matrix
  83. def windowsize_callback(window, width, height):
  84.     global resx, resy, cam_width, cam_height
  85.     distance_per_pixel = cam_height/resy; # assuming cam_height/resy == cam_width/resx
  86.  
  87.     resx = width;
  88.     resy = height;
  89.     cam_width = distance_per_pixel*resx;
  90.     cam_height = distance_per_pixel*resy;
  91.  
  92.     glLoadIdentity();
  93.     glViewport(0, 0, resx, resy);
  94.     glOrtho(cam_x, cam_x + cam_width, cam_y, cam_y + cam_height, -1, 1);
  95.  
  96.  
  97. # Callback function called every time a keyboard key is pressed, released or held down
  98. def key_callback(window, key, scancode, action, mods):
  99.     print("Key: key = {}, scancode = {}, action = {}, mods = {}".format(key, scancode, action, mods))
  100.  
  101. # Callback function called every time a mouse button pressed or released
  102. def mousebutton_callback(window, button, action, mods):
  103.     global prevx, prevy
  104.  
  105.     # get current cursor position, used in mouseposition_callback
  106.     # to know how far the mouse moved
  107.     prevx, prevy = glfw.get_cursor_pos(window)
  108.  
  109.     print("Mousebutton: button = {}, action = {}, mods = {}, prevx = {}, prevy = {}".format(button, action, mods, prevx, prevy))
  110.  
  111. # Callback function called every time a the mouse is moved
  112. def mouseposition_callback(window, xpos, ypos):
  113.     global cam_x, cam_y, prevx, prevy
  114.  
  115.     # move the camera if the first mouse button is held down
  116.     # the cursor will stay on at the same location relative to world coordinates after movement
  117.  
  118.     if  glfw.get_mouse_button(window, 0):
  119.         # left button pressed
  120.         cam_x -= (xpos-prevx)/resx*cam_width
  121.         cam_y += (ypos-prevy)/resy*cam_height
  122.  
  123.         prevx = xpos
  124.         prevy = ypos
  125.  
  126.     print("Mousepos: xpos = {}, ypos = {}".format(xpos, ypos))
  127.  
  128. # Callback function called every time a the mouse scroll wheel is moved
  129. # yoffset = up-down
  130. # xoffset = left-right
  131. def mousewheel_callback(window, xoffset, yoffset):
  132.     global cam_x, cam_y, cam_height, cam_width
  133.     zoomFactor = math.pow(0.95,yoffset);
  134.  
  135.     # zoom the camera by keeping the center constant and move the edges accordingly
  136.     cam_x = cam_x + cam_width*(1.0 - zoomFactor)/2.0;
  137.     cam_y = cam_y + cam_height*(1.0 - zoomFactor)/2.0;
  138.     cam_width *= zoomFactor;
  139.     cam_height *= zoomFactor;
  140.     print("Mousewheel: xoffset = {}, yoffset = {}".format(xoffset, yoffset))
  141.  
  142. if __name__ == "__main__":
  143.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement