Advertisement
Guest User

Untitled

a guest
Jul 30th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.70 KB | None | 0 0
  1. import glfw
  2. from OpenGL.GL import *
  3. import math
  4. import numpy
  5.  
  6. # global variables for window info, camera info
  7. resx = 1000   # resolution of window horizontal direction
  8. resy = 600   # resoltuion vertical direction
  9. prevx = 0.0
  10. prevy = 0.0
  11.  
  12. VertexArrayID = -1;
  13. vertexbuffer = -1;
  14.  
  15.  
  16. computeProgramID = -1
  17. computerRenderProgramID = -1;
  18. texture = -1
  19. texture3D = -1
  20.  
  21.  
  22. PI = numpy.pi
  23.  
  24. g_vertex_buffer_data = numpy.array([
  25.     -1.0, -1.0, 0.0,
  26.      1.0, -1.0, 0.0,
  27.     -1.0,  1.0, 0.0,
  28.      1.0,  1.0, 0.0,
  29. ], numpy.float32)
  30.  
  31. g_uv_buffer_data = numpy.array([
  32.     0.0, 0.0,
  33.     1.0, 0.0,
  34.     0.0, 1.0,
  35.     1.0, 1.0,
  36. ], numpy.float32)
  37.  
  38. vertexbuffer_quad = -1
  39. uvbuffer_quad = -1
  40.  
  41.  
  42. pos = numpy.array([-2.0, -2.0, 0.0], numpy.float32);
  43. f = numpy.array([0,1,0], numpy.float32)
  44. u = numpy.array([0,0,1], numpy.float32)
  45. r = numpy.array([1,0,0], numpy.float32)
  46. phi = numpy.pi/4;
  47. theta = numpy.pi/2;
  48. fov = 60.0;
  49.  
  50. Nx = 512
  51. Ny = 512
  52. Nz = 512
  53.  
  54. voxels = numpy.zeros(Nx*Ny*Nz, dtype=numpy.uint8)
  55.  
  56. window = -1
  57.  
  58. def main():
  59.     global VertexArrayID, texture, texture3D, window, PI, f,r, u, fov, theta, phi, pos
  60.     global vertexbuffer_quad, uvbuffer_quad, computeProgramID, computerRenderProgramID
  61.  
  62.     # DOWNLOAD THIS FILE FIRST: https://www.digitalrocksportal.org/projects/16/images/65564/download/
  63.     with(open("segmented_castle_512.ubc", "rb")) as f:
  64.         voxels = numpy.frombuffer(f.read(), dtype=numpy.uint8)
  65.     voxels = 255*(1 - voxels)
  66.    
  67.     print(len(voxels), type(voxels))
  68.     print(voxels[:100])
  69.  
  70.  
  71.     # Initialize the library
  72.     if not glfw.init():
  73.         return
  74.  
  75.     glfw.window_hint(glfw.SAMPLES, 4);
  76.     glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4);
  77.     glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3);
  78.     glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE);
  79.  
  80.     # Create a windowed mode window and its OpenGL context
  81.     window = glfw.create_window(resx, resy, "Hello World", None, None)
  82.     if not window:
  83.         glfw.terminate()
  84.         return
  85.  
  86.     # Make the window's context current
  87.     glfw.make_context_current(window)
  88.  
  89.     # Tell gLFW which callback function to use on input
  90.     glfw.set_key_callback(window, key_callback)
  91.     glfw.set_mouse_button_callback(window, mousebutton_callback)
  92.     glfw.set_cursor_pos_callback(window, mouseposition_callback)
  93.     glfw.set_scroll_callback(window, mousewheel_callback)
  94.     glfw.set_window_size_callback(window, windowsize_callback)
  95.  
  96.     glViewport(0, 0, resx, resy);
  97.     glClearColor(0.2, 0.2, 0.2, 1.0)
  98.     glfw.swap_interval(0);
  99.     VertexArrayID = glGenVertexArrays(1);
  100.     glBindVertexArray(VertexArrayID);
  101.  
  102.     computerRenderProgramID = loadShaders("vertex_shader_render.vs", "fragment_shader_render.fs")
  103.  
  104.     vertexbuffer_quad = glGenBuffers(1)
  105.     glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer_quad)
  106.     glBufferData(GL_ARRAY_BUFFER, g_vertex_buffer_data, GL_STATIC_DRAW)
  107.  
  108.     uvbuffer_quad = glGenBuffers(1)
  109.     glBindBuffer(GL_ARRAY_BUFFER, uvbuffer_quad)
  110.     glBufferData(GL_ARRAY_BUFFER, g_uv_buffer_data, GL_STATIC_DRAW)
  111.  
  112.  
  113.     texture = glGenTextures(1)
  114.     glActiveTexture(GL_TEXTURE0)
  115.     glBindTexture(GL_TEXTURE_2D, texture)
  116.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  117.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  118.  
  119.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, resx, resy, 0, GL_RED, GL_FLOAT, None);
  120.     glBindImageTexture(0, texture, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
  121.     computeProgramID = loadComputeShader("compute_shader.cs", texture);
  122.  
  123.  
  124.     texture3D = glGenTextures(1);
  125.     glActiveTexture(GL_TEXTURE1);
  126.     glBindTexture(GL_TEXTURE_3D, texture3D);
  127.     glTexImage3D(GL_TEXTURE_3D, 0, GL_RED, Nx, Ny, Nz, 0, GL_RED, GL_UNSIGNED_BYTE, voxels);
  128.     glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  129.     glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  130.  
  131.  
  132.  
  133.     # Loop until the user closes the window
  134.     while not glfw.window_should_close(window):
  135.         # Render here, e.g. using pyOpenGL
  136.        
  137.         cameraStuff()
  138.  
  139.         glClear(GL_COLOR_BUFFER_BIT)
  140.  
  141.  
  142.  
  143.         aspect = resx/resy;
  144.         FOV = fov*PI/180.0;
  145.         tan_psi = numpy.tan(FOV/2.0);
  146.         tan_phi = numpy.tan(FOV/2.0)*aspect;
  147.  
  148.  
  149.  
  150.         glUseProgram(computeProgramID);
  151.         loc = glGetUniformLocation(computeProgramID, "orig");
  152.         if (loc != -1): glUniform3f(loc, pos[0], pos[1], pos[2]);
  153.         loc = glGetUniformLocation(computeProgramID, "N");
  154.         if (loc != -1): glUniform3i(loc, Nx, Ny, Nz);
  155.         loc = glGetUniformLocation(computeProgramID, "chosenFace");
  156.         if (loc != -1): glUniform1i(loc, -1);
  157.  
  158.         loc = glGetUniformLocation(computeProgramID, "chosenVoxel");
  159.         if (loc != -1): glUniform3i(loc, 0, 0, 0);
  160.         loc = glGetUniformLocation(computeProgramID, "f");
  161.         if (loc != -1): glUniform3f(loc, f[0], f[1], f[2]);
  162.         loc = glGetUniformLocation(computeProgramID, "r");
  163.         if (loc != -1): glUniform3f(loc, r[0], r[1], r[2]);
  164.         loc = glGetUniformLocation(computeProgramID, "u");
  165.         if (loc != -1): glUniform3f(loc, u[0], u[1], u[2]);
  166.  
  167.         loc = glGetUniformLocation(computeProgramID, "tan_phi");
  168.         if (loc != -1): glUniform1f(loc, tan_phi);
  169.  
  170.         loc = glGetUniformLocation(computeProgramID, "tan_psi");
  171.         if (loc != -1): glUniform1f(loc, tan_psi);
  172.  
  173.  
  174.        
  175.  
  176.         glDispatchCompute(int(resx)//8, int(resy)//8, 1);
  177.  
  178.  
  179.  
  180.  
  181.  
  182.         glUseProgram(computerRenderProgramID);
  183.        
  184.         glUseProgram(computerRenderProgramID);
  185.  
  186.         glEnableVertexAttribArray(0);
  187.         glEnableVertexAttribArray(1);
  188.         glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer_quad);
  189.         glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,None);
  190.         glBindBuffer(GL_ARRAY_BUFFER, uvbuffer_quad);
  191.         glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,0,None);
  192.         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  193.  
  194.         glDisableVertexAttribArray(1);
  195.         glDisableVertexAttribArray(0);
  196.  
  197.  
  198.  
  199.         # Swap front and back buffers
  200.         glfw.swap_buffers(window)
  201.  
  202.         # Poll for and process events, call relevant callback function
  203.         glfw.poll_events()
  204.  
  205.     glfw.terminate()
  206.  
  207. t1 = glfw.get_time()
  208. dt = 1.0/60.0
  209.  
  210. def cameraStuff():
  211.     global t1, pos, f, u, r, window, dt
  212.     t2 = glfw.get_time()
  213.     speed = (t2-t1)*2;
  214.     alpha = 0.01
  215.     dt = alpha*(t2-t1) + (1-alpha)*dt
  216.     glfw.set_window_title(window, "fps = {:.3f}".format(1.0/dt))
  217.     t1 = t2
  218.  
  219.    
  220.     if (glfw.get_key(window, glfw.KEY_LEFT_CONTROL) == glfw.PRESS):
  221.         speed *= 10;
  222.    
  223.     if (glfw.get_key(window, glfw.KEY_LEFT_SHIFT) == glfw.PRESS):
  224.         speed *= 10;
  225.    
  226.  
  227.     deltaMoveForward = speed*(glfw.get_key(window, glfw.KEY_W) - glfw.get_key(window, glfw.KEY_S));
  228.     deltaMoveRight   = speed*(glfw.get_key(window, glfw.KEY_D) - glfw.get_key(window, glfw.KEY_A));
  229.     deltaMoveUp      = speed*(glfw.get_key(window, glfw.KEY_E) - glfw.get_key(window, glfw.KEY_Q));
  230.  
  231.  
  232.     sinp = numpy.sin(phi)  
  233.     cosp = numpy.cos(phi)
  234.     sint = numpy.sin(theta)
  235.     cost = numpy.cos(theta);
  236.     f = numpy.array((cosp*sint, sinp*sint, cost), numpy.float32)
  237.     r = numpy.array((sinp, -cosp, 0.0), numpy.float32)
  238.     u = numpy.array((-cosp*cost, -sinp*cost, sint), numpy.float32)
  239.  
  240.     pos = pos + deltaMoveForward*f + deltaMoveRight*r + deltaMoveUp*u;
  241.    
  242.  
  243. def loadComputeShader(computeShaderPath, texture):
  244.     ComputeShaderID   = glCreateShader(GL_COMPUTE_SHADER);
  245.  
  246.     with open(computeShaderPath, "r") as f:
  247.         shaderCode = f.read()
  248.  
  249.     glShaderSource(ComputeShaderID, shaderCode)
  250.     glCompileShader(ComputeShaderID)
  251.  
  252.     Result = glGetShaderiv(ComputeShaderID, GL_COMPILE_STATUS);
  253.  
  254.     if Result == GL_FALSE:
  255.         print(glGetShaderInfoLog(ComputeShaderID))
  256.  
  257.  
  258.     ProgramID = glCreateProgram()
  259.     glAttachShader(ProgramID, ComputeShaderID);
  260.     glLinkProgram(ProgramID);
  261.  
  262.     Result = glGetProgramiv(ProgramID, GL_LINK_STATUS);
  263.  
  264.     if Result == GL_FALSE:
  265.         print(glGetProgramInfoLog(ProgramID))
  266.  
  267.     glDeleteShader(ComputeShaderID);
  268.  
  269.  
  270.     glUseProgram(ProgramID);
  271.  
  272.     glUniform1i(glGetUniformLocation(ProgramID, "framebufferImage"), 0);
  273.     glUniform1i(glGetUniformLocation(ProgramID, "voxelTextureSampler"), 1);
  274.     glUniform1i(glGetUniformLocation(ProgramID, "voxelImage"), 3);
  275.     glUniform1i(glGetUniformLocation(ProgramID, "cubemapSampler"), 2);
  276.  
  277.  
  278.     return ProgramID
  279.  
  280.  
  281. def loadShaders(vertexShaderPath, fragmentShaderPath):
  282.     VertexShaderID   = glCreateShader(GL_VERTEX_SHADER);
  283.  
  284.     with open(vertexShaderPath, "r") as f:
  285.         shaderCode = f.read()
  286.  
  287.     glShaderSource(VertexShaderID, shaderCode)
  288.     glCompileShader(VertexShaderID)
  289.  
  290.     Result = glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS);
  291.  
  292.     if Result == GL_FALSE:
  293.         print(glGetShaderInfoLog(VertexShaderID))
  294.  
  295.  
  296.     FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
  297.  
  298.     with open(fragmentShaderPath, "r") as f:
  299.         shaderCode = f.read()
  300.  
  301.     glShaderSource(FragmentShaderID, shaderCode)
  302.     glCompileShader(FragmentShaderID)
  303.  
  304.     Result = glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS);
  305.  
  306.     if Result == GL_FALSE:
  307.         print(glGetShaderInfoLog(FragmentShaderID))
  308.  
  309.     ProgramID = glCreateProgram()
  310.     glAttachShader(ProgramID, VertexShaderID);
  311.     glAttachShader(ProgramID, FragmentShaderID);
  312.     glLinkProgram(ProgramID);
  313.  
  314.     Result = glGetProgramiv(ProgramID, GL_LINK_STATUS);
  315.  
  316.     if Result == GL_FALSE:
  317.         print(glGetProgramInfoLog(ProgramID))
  318.  
  319.     glDeleteShader(VertexShaderID);
  320.     glDeleteShader(FragmentShaderID);
  321.  
  322.     return ProgramID
  323.  
  324.  
  325.  
  326. # Callback function called every time the window size change
  327. # Adjusts the camera width and heigh so that the scale stays the same
  328. # Resets projection matrix
  329. def windowsize_callback(window, width, height):
  330.     global resx, resy
  331.  
  332.     resx = width;
  333.     resy = height;
  334.  
  335.     glViewport(0, 0, resx, resy);
  336.     glBindTexture(GL_TEXTURE_2D, texture);
  337.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, resx, resy, 0, GL_RED, GL_FLOAT, None);
  338.  
  339.  
  340.  
  341. # Callback function called every time a keyboard key is pressed, released or held down
  342. def key_callback(window, key, scancode, action, mods):
  343.     pass
  344.     #print("Key: key = {}, scancode = {}, action = {}, mods = {}".format(key, scancode, action, mods))
  345.  
  346.  
  347. # Callback function called every time a mouse button pressed or released
  348. def mousebutton_callback(window, button, action, mods):
  349.     global prevx, prevy
  350.  
  351.     # get current cursor position, used in mouseposition_callback
  352.     # to know how far the mouse moved
  353.     prevx, prevy = glfw.get_cursor_pos(window)
  354.  
  355.  
  356.  
  357.     #print("Mousebutton: button = {}, action = {}, mods = {}, prevx = {}, prevy = {}".format(button, action, mods, prevx, prevy))
  358.  
  359. # Callback function called every time a the mouse is moved
  360. def mouseposition_callback(window, xpos, ypos):
  361.     global prevx, prevy, phi, theta
  362.  
  363.     # move the camera if the first mouse button is held down
  364.     # the cursor will stay on at the same location relative to world coordinates after movement
  365.  
  366.     if  glfw.get_mouse_button(window, 0):
  367.         anglesPerPixel = 0.1 * PI / 180.0;
  368.         phi = numpy.fmod(phi - (xpos-prevx) * anglesPerPixel + 2*PI, 2*PI);
  369.         theta = max(1.0*PI/180.0, min(179.0*PI/180.0, theta + (ypos-prevy) * anglesPerPixel));
  370.  
  371.         prevx = xpos
  372.         prevy = ypos
  373.  
  374.     #print("Mousepos: xpos = {}, ypos = {}".format(xpos, ypos))
  375.  
  376. # Callback function called every time a the mouse scroll wheel is moved
  377. # yoffset = up-down
  378. # xoffset = left-right
  379. def mousewheel_callback(window, xoffset, yoffset):
  380.     global prevx, prevy, fov
  381.     zoomFactor = math.pow(0.95,yoffset);
  382.  
  383.     fov = max(15, min(120.0, fov*zoomFactor));
  384.  
  385. if __name__ == "__main__":
  386.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement