Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.86 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import sys
  4. import time
  5.  
  6. sys.path.append("./shared")
  7.  
  8. from sbmloader import SBMObject # location of sbm file format loader
  9. from ktxloader import KTXObject # location of ktx file format loader
  10.  
  11. from sbmath import m3dDegToRad, m3dRadToDeg, m3dTranslateMatrix44, m3dRotationMatrix44, m3dMultiply, m3dOrtho, m3dPerspective, rotation_matrix, translate, m3dScaleMatrix44
  12.  
  13. fullscreen = True
  14.  
  15. import numpy.matlib
  16. import numpy as np
  17.  
  18. try:
  19. from OpenGL.GLUT import *
  20. from OpenGL.GL import *
  21. from OpenGL.GLU import *
  22. from OpenGL.raw.GL.ARB.vertex_array_object import glGenVertexArrays, glBindVertexArray
  23. except:
  24. print ('''
  25. ERROR: PyOpenGL not installed properly.
  26. ''')
  27. sys.exit()
  28.  
  29. identityMatrix = [1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1]
  30.  
  31. clear_program = GLuint(0)
  32. append_program = GLuint(0)
  33. resolve_program = GLuint(0)
  34.  
  35. class textures:
  36. color = GLuint(0)
  37. normals = GLuint(0)
  38.  
  39. class uniforms_block:
  40. mv_matrix = (GLfloat * 16)(*identityMatrix)
  41. view_matrix = (GLfloat * 16)(*identityMatrix)
  42. proj_matrix = (GLfloat * 16)(*identityMatrix)
  43.  
  44. uniforms_buffer = GLuint(0)
  45.  
  46. class uniforms:
  47. mvp = GLuint(0)
  48.  
  49. fragment_buffer = GLuint(0)
  50. head_pointer_image = GLuint(0)
  51. atomic_counter_buffer = GLuint(0)
  52. dummy_vao = GLuint(0)
  53.  
  54. uniform = uniforms()
  55. myobject = SBMObject()
  56.  
  57. def link_from_shaders(shaders, shader_count, delete_shaders, check_errors=False):
  58.  
  59. program = GLuint(0)
  60.  
  61. program = glCreateProgram()
  62.  
  63. for i in range(0, shader_count):
  64. glAttachShader(program, shaders[i]);
  65.  
  66. glLinkProgram(program);
  67.  
  68. if (delete_shaders):
  69.  
  70. for i in range(0, shader_count):
  71. glDeleteShader(shaders[i]);
  72.  
  73. return program
  74.  
  75.  
  76. def shader_load(filename, shader_type):
  77.  
  78. result = GLuint(0)
  79.  
  80. with open ( filename, "rb") as data:
  81.  
  82. result = glCreateShader(shader_type)
  83.  
  84. glShaderSource(result, data.read() )
  85.  
  86. glCompileShader(result)
  87.  
  88. return result
  89.  
  90.  
  91. def load_shaders():
  92. global clear_program
  93. global append_program
  94. global resolve_program
  95.  
  96. global uniform
  97.  
  98. shaders = [GLuint(0), GLuint(0)]
  99.  
  100. shaders[0] = shader_load("fragmentlist_shaders/clear.vs.glsl", GL_VERTEX_SHADER);
  101.  
  102. shaders[1] = shader_load("fragmentlist_shaders/clear.fs.glsl", GL_FRAGMENT_SHADER);
  103.  
  104. if (clear_program):
  105. glDeleteProgram(clear_program);
  106.  
  107. clear_program = link_from_shaders(shaders, 2, True);
  108.  
  109. shaders[0] = shader_load("fragmentlist_shaders/append.vs.glsl", GL_VERTEX_SHADER);
  110. shaders[1] = shader_load("fragmentlist_shaders/append.fs.glsl", GL_FRAGMENT_SHADER);
  111.  
  112. if (append_program):
  113. glDeleteProgram(append_program);
  114.  
  115. append_program = link_from_shaders(shaders, 2, True);
  116.  
  117. uniform.mvp = glGetUniformLocation(append_program, "mvp");
  118.  
  119. shaders[0] = shader_load("fragmentlist_shaders/resolve.vs.glsl", GL_VERTEX_SHADER);
  120. shaders[1] = shader_load("fragmentlist_shaders/resolve.fs.glsl", GL_FRAGMENT_SHADER);
  121.  
  122. if (resolve_program):
  123. glDeleteProgram(resolve_program)
  124.  
  125. resolve_program = link_from_shaders(shaders, 2, True);
  126.  
  127.  
  128. class Scene:
  129.  
  130. def __init__(self, width, height):
  131.  
  132. global uniforms_buffer
  133. global fragment_buffer
  134. global atomic_counter_buffer
  135. global head_pointer_image
  136. global dummy_vao
  137. global myobject
  138.  
  139. self.width = width
  140. self.height = height
  141.  
  142. load_shaders()
  143.  
  144. glGenBuffers(1, uniforms_buffer)
  145. glBindBuffer(GL_UNIFORM_BUFFER, uniforms_buffer)
  146. glBufferData(GL_UNIFORM_BUFFER, sys.getsizeof(uniforms_block), None, GL_DYNAMIC_DRAW)
  147.  
  148. myobject.load("dragon.sbm")
  149.  
  150. glGenBuffers(1, fragment_buffer)
  151. glBindBuffer(GL_SHADER_STORAGE_BUFFER, fragment_buffer);
  152. glBufferData(GL_SHADER_STORAGE_BUFFER, 1024 * 1024 * 16, None, GL_DYNAMIC_COPY)
  153.  
  154. glGenBuffers(1, atomic_counter_buffer);
  155. glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, atomic_counter_buffer);
  156. glBufferData(GL_ATOMIC_COUNTER_BUFFER, 4, None, GL_DYNAMIC_COPY);
  157.  
  158. head_pointer_image = glGenTextures(1)
  159. glBindTexture(GL_TEXTURE_2D, head_pointer_image);
  160. glTexStorage2D(GL_TEXTURE_2D, 1, GL_R32UI, 1024, 1024);
  161.  
  162. glGenVertexArrays(1, dummy_vao);
  163. glBindVertexArray(dummy_vao);
  164.  
  165.  
  166. def display(self):
  167.  
  168. green = [ 0.0, 0.1, 0.0, 0.0 ]
  169. currentTime = time.time()
  170. f = currentTime
  171.  
  172. zeros = [ 0.0, 0.0, 0.0, 0.0 ]
  173. gray = [ 0.1, 0.1, 0.1, 0.0 ]
  174. ones = [ 1.0 ]
  175.  
  176. glViewport(0, 0, self.width , self.height);
  177.  
  178. glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_ATOMIC_COUNTER_BARRIER_BIT | GL_SHADER_STORAGE_BARRIER_BIT);
  179.  
  180. glUseProgram(clear_program);
  181. glBindVertexArray(dummy_vao);
  182. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  183.  
  184. glUseProgram(append_program)
  185.  
  186. model_matrix = (GLfloat * 16)(*identityMatrix)
  187. # vmath::mat4 model_matrix = vmath::scale(7.0f);
  188.  
  189. #view_position = (GLfloat * 3)(0.0,0.0,0.0)
  190. # vmath::vec3 view_position = vmath::vec3(cosf(f * 0.35f) * 120.0f, cosf(f * 0.4f) * 30.0f, sinf(f * 0.35f) * 120.0f);
  191.  
  192. view_matrix = (GLfloat * 16)(*identityMatrix)
  193. # vmath::mat4 view_matrix = vmath::lookat(view_position,
  194. # vmath::vec3(0.0f, 30.0f, 0.0f),
  195. # vmath::vec3(0.0f, 1.0f, 0.0f));
  196.  
  197.  
  198. # temp used translate, rotation just to display something - delete ######
  199. T = (GLfloat * 16)(*identityMatrix) #
  200. RX = (GLfloat * 16)(*identityMatrix) #
  201. RY = (GLfloat * 16)(*identityMatrix) #
  202. R = (GLfloat * 16)(*identityMatrix) #
  203. m3dTranslateMatrix44(T, 0, 0, -4) #
  204. m3dRotationMatrix44(RX, currentTime * m3dDegToRad(17.0), 1.0, 0.0, 0.0) #
  205. m3dRotationMatrix44(RY, currentTime * m3dDegToRad(13.0), 0.0, 1.0, 0.0) #
  206. R = m3dMultiply(RY, RX) #
  207. model_matrix = m3dMultiply(T, R) #
  208. # ////////////////////////////// temp code #
  209.  
  210.  
  211. mv_matrix = (GLfloat * 16)(*identityMatrix)
  212. mv_matrix = m3dMultiply(view_matrix , model_matrix)
  213.  
  214. proj_matrix = (GLfloat * 16)(*identityMatrix)
  215. proj_matrix = m3dPerspective(m3dDegToRad(50.0), float(self.width) / float(self.height), 0.1, 1000.0)
  216.  
  217. glUniformMatrix4fv(uniform.mvp, 1, GL_FALSE, m3dMultiply(proj_matrix , mv_matrix))
  218.  
  219. zero = 0;
  220. glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER, 0, atomic_counter_buffer)
  221.  
  222. # next line not working ????
  223. #glBufferSubData(GL_ATOMIC_COUNTER_BUFFER, 0, sys.getsizeof(zero), zero);
  224.  
  225. glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, fragment_buffer)
  226.  
  227. glBindImageTexture(0, head_pointer_image, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R32UI)
  228.  
  229. glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_ATOMIC_COUNTER_BARRIER_BIT | GL_SHADER_STORAGE_BARRIER_BIT)
  230.  
  231. myobject.render()
  232.  
  233. glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_ATOMIC_COUNTER_BARRIER_BIT | GL_SHADER_STORAGE_BARRIER_BIT)
  234.  
  235. glUseProgram(resolve_program)
  236.  
  237. glBindVertexArray(dummy_vao)
  238.  
  239. glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_ATOMIC_COUNTER_BARRIER_BIT | GL_SHADER_STORAGE_BARRIER_BIT)
  240.  
  241. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4)
  242.  
  243. glutSwapBuffers()
  244.  
  245. def reshape(self, width, height):
  246. self.width = width
  247. self.height = height
  248.  
  249. def keyboard(self, key, x, y ):
  250. global fullscreen
  251.  
  252. print ('key:' , key)
  253. if key == b'x1b': # ESC
  254. sys.exit()
  255.  
  256. elif key == b'f' or key == b'F': #fullscreen toggle
  257.  
  258. if (fullscreen == True):
  259. glutReshapeWindow(512, 512)
  260. glutPositionWindow(int((1360/2)-(512/2)), int((768/2)-(512/2)))
  261. fullscreen = False
  262. else:
  263. glutFullScreen()
  264. fullscreen = True
  265.  
  266. print('done')
  267.  
  268. def init(self):
  269. pass
  270.  
  271. def timer(self, blah):
  272.  
  273. glutPostRedisplay()
  274. glutTimerFunc( int(1/60), self.timer, 0)
  275. time.sleep(1/60.0)
  276.  
  277. if __name__ == '__main__':
  278. start = time.time()
  279.  
  280. glutInit()
  281. glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
  282.  
  283. glutInitWindowSize(512, 512)
  284.  
  285. w1 = glutCreateWindow('OpenGL SuperBible - Fragment List')
  286. glutInitWindowPosition(int((1360/2)-(512/2)), int((768/2)-(512/2)))
  287.  
  288. fullscreen = False
  289. many_cubes = False
  290. #glutFullScreen()
  291.  
  292. scene = Scene(512,512)
  293. glutReshapeFunc(scene.reshape)
  294. glutDisplayFunc(scene.display)
  295. glutKeyboardFunc(scene.keyboard)
  296.  
  297. glutIdleFunc(scene.display)
  298. #glutTimerFunc( int(1/60), scene.timer, 0)
  299.  
  300. scene.init()
  301.  
  302. glutMainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement