Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. """
  2. Navigation paradigm in PyOpenGL
  3.  
  4. Needs to have PyOpenGl e.g., pip install pyopengl.
  5. It draws a scene for navigation paradigm to test
  6. if it is possible to transfer it to MALTAB with
  7. Psychtoolbox installed.
  8.  
  9. Michael Tesar 2017
  10. Ceske Budejovice
  11. """
  12. from OpenGL.GLUT import *
  13. from OpenGL.GLU import *
  14. from OpenGL.GL import *
  15. import sys
  16.  
  17. name = 'Navigation paradigm'
  18.  
  19.  
  20. def main():
  21. """
  22. Main function which handles scene and also
  23. OpenGL (PyOpenGl) basic functions as drawing
  24. of screen, enabling shader and light etc.
  25. :return: void
  26. """
  27. glutInit(sys.argv)
  28. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
  29. glutInitWindowSize(800, 800)
  30. glutInitWindowPosition(350, 200)
  31. glutCreateWindow(name)
  32.  
  33. glClearColor(0., 0., 0., 1.)
  34. glShadeModel(GL_SMOOTH)
  35. glEnable(GL_CULL_FACE)
  36. glEnable(GL_DEPTH_TEST)
  37. glEnable(GL_LIGHTING)
  38. lightZeroPosition = [10., 4., 10., 1.]
  39. lightZeroColor = [0.8, 1.0, 0.8, 1.0]
  40. glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition)
  41. glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor)
  42. glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1)
  43. glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05)
  44. glEnable(GL_LIGHT0)
  45. glutDisplayFunc(displayscene)
  46. glMatrixMode(GL_PROJECTION)
  47. gluPerspective(40., 1., 1., 40.)
  48. glMatrixMode(GL_MODELVIEW)
  49. gluLookAt(0, 0, 10,
  50. 0, 0, 0,
  51. 0, 1, 0)
  52. glPushMatrix()
  53. glutMainLoop()
  54. return
  55.  
  56.  
  57. def displayscene():
  58. """
  59. Draws two spheres and arena with
  60. basic texture on it.
  61. :return: void
  62. """
  63. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
  64. glPushMatrix()
  65.  
  66. # Left sphere
  67. color = [1.0, 0.0, 0.0, 1.0]
  68. glMaterialfv(GL_FRONT, GL_DIFFUSE, color)
  69. glTranslatef(-2, 0, 0)
  70. glutSolidSphere(1, 100, 20)
  71.  
  72. # Right sphere
  73. color = [0.0, 1.0, 0.0, 1.0]
  74. glMaterialfv(GL_FRONT, GL_DIFFUSE, color)
  75. glTranslatef(4, 0, 0)
  76. glutSolidSphere(1, 100, 20)
  77.  
  78. glPopMatrix()
  79. glutSwapBuffers()
  80. return
  81.  
  82.  
  83. def loadtexture():
  84. """
  85. Defines a global texture for applying
  86. in any time as fucntion
  87. :return:
  88. """
  89. image = open("brick.jpg")
  90.  
  91. ix = image.size[0]
  92. iy = image.size[1]
  93. image = image.tostring("raw", "RGBX", 0, -1)
  94.  
  95. glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
  96. glTexImage2D(GL_TEXTURE_2D, 0, 3, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image)
  97. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
  98. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
  99. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
  100. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
  101. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
  102. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
  103. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
  104. return image
  105.  
  106. if __name__ == '__main__':
  107. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement