Guest User

Untitled

a guest
Jan 5th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. # OpenGL example using SDL2
  2.  
  3. import sdl2
  4. import opengl/opengl
  5. import opengl/glu
  6.  
  7. # Initialize SDL
  8. discard SDL_Init(INIT_EVERYTHING)
  9.  
  10. var
  11. window = CreateWindow("SDL Skeleton", 100, 100, 640, 480, SDL_WINDOW_OPENGL or SDL_WINDOW_RESIZABLE)
  12. context = window.GL_CreateContext()
  13.  
  14. # Initialize OpenGL
  15. proc Init_OpenGL() =
  16. loadExtensions()
  17. glClearColor(0.0, 0.0, 0.0, 1.0) # Set background color to black and opaque
  18. glClearDepth(1.0) # Set background depth to farthest
  19. glEnable(GL_DEPTH_TEST) # Enable depth testing for z-culling
  20. glDepthFunc(GL_LEQUAL) # Set the type of depth-test
  21. glShadeModel(GL_SMOOTH) # Enable smooth shading
  22. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) # Nice perspective corrections
  23.  
  24. proc reshape(pos_x :float, pos_y :float) =
  25. # TODO: use real width and height instead of constant values
  26. glViewport(0, 0, 640, 480) # Set the viewport to cover the new window
  27. glMatrixMode(GL_PROJECTION) # To operate on the Projection matrix
  28. glLoadIdentity() # Reset
  29. # gluPerspective(40.0, 640 / 480, 0.1, 100.0) # Enable perspective projection with fovy, aspect, zNear and zFar
  30. gluPerspective(pos_y, 640 / 480, 0.1, 100.0)
  31.  
  32. proc render() =
  33. glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT) # Clear color and depth buffers
  34. glMatrixMode(GL_MODELVIEW) # To operate on model-view matrix
  35. glLoadIdentity() # Reset the model-view matrix
  36. glTranslatef(1.5, 0.0, -7.0) # Move right and into the screen
  37.  
  38. # Render a cube consisting of 6 quads
  39. # Each quad consists of 2 triangles
  40. # Each triangle consists of 3 vertices
  41.  
  42. glBegin(GL_TRIANGLES) # Begin drawing of triangles
  43.  
  44. # Top face (y = 1.0f)
  45. glColor3f(0.0, 1.0, 0.0) # Green
  46. glVertex3f( 1.0, 1.0, -1.0)
  47. glVertex3f(-1.0, 1.0, -1.0)
  48. glVertex3f(-1.0, 1.0, 1.0)
  49. glVertex3f( 1.0, 1.0, 1.0)
  50. glVertex3f( 1.0, 1.0, -1.0)
  51. glVertex3f(-1.0, 1.0, 1.0)
  52.  
  53. # Bottom face (y = -1.0f)
  54. glColor3f(1.0, 0.5, 0.0) # Orange
  55. glVertex3f( 1.0, -1.0, 1.0)
  56. glVertex3f(-1.0, -1.0, 1.0)
  57. glVertex3f(-1.0, -1.0, -1.0)
  58. glVertex3f( 1.0, -1.0, -1.0)
  59. glVertex3f( 1.0, -1.0, 1.0)
  60. glVertex3f(-1.0, -1.0, -1.0)
  61.  
  62. # Front face (z = 1.0f)
  63. glColor3f(1.0, 0.0, 0.0) # Red
  64. glVertex3f( 1.0, 1.0, 1.0)
  65. glVertex3f(-1.0, 1.0, 1.0)
  66. glVertex3f(-1.0, -1.0, 1.0)
  67. glVertex3f( 1.0, -1.0, 1.0)
  68. glVertex3f( 1.0, 1.0, 1.0)
  69. glVertex3f(-1.0, -1.0, 1.0)
  70.  
  71. # Back face (z = -1.0f)
  72. glColor3f(1.0, 1.0, 0.0) # Yellow
  73. glVertex3f( 1.0, -1.0, -1.0)
  74. glVertex3f(-1.0, -1.0, -1.0)
  75. glVertex3f(-1.0, 1.0, -1.0)
  76. glVertex3f( 1.0, 1.0, -1.0)
  77. glVertex3f( 1.0, -1.0, -1.0)
  78. glVertex3f(-1.0, 1.0, -1.0)
  79.  
  80. # Left face (x = -1.0f)
  81. glColor3f(0.0, 0.0, 1.0) # Blue
  82. glVertex3f(-1.0, 1.0, 1.0)
  83. glVertex3f(-1.0, 1.0, -1.0)
  84. glVertex3f(-1.0, -1.0, -1.0)
  85. glVertex3f(-1.0, -1.0, 1.0)
  86. glVertex3f(-1.0, 1.0, 1.0)
  87. glVertex3f(-1.0, -1.0, -1.0)
  88.  
  89. # Right face (x = 1.0f)
  90. glColor3f(1.0, 0.0, 1.0) # Magenta
  91. glVertex3f(1.0, 1.0, -1.0)
  92. glVertex3f(1.0, 1.0, 1.0)
  93. glVertex3f(1.0, -1.0, 1.0)
  94. glVertex3f(1.0, -1.0, -1.0)
  95. glVertex3f(1.0, 1.0, -1.0)
  96. glVertex3f(1.0, -1.0, 1.0)
  97.  
  98. glEnd() # End of drawing
  99.  
  100. window.GL_SwapWindow() # Swap the front and back frame buffers (double buffering)
  101.  
  102.  
  103.  
  104.  
  105. const
  106. RELEASED = 0
  107. PRESSED = 1
  108.  
  109. let
  110. MAXFRAMERATE: uint32 = 20 # milli seconds
  111.  
  112. var
  113. frametime :uint32
  114. evt :TEvent
  115. runGame :bool = true
  116. position :float = 20
  117. key_state :ptr array[0 .. SDL_NUM_SCANCODES.int, uint8]
  118. tmp :cstring
  119.  
  120. # Framerate limiter
  121. proc limitFramerate() =
  122. var now = GetTicks()
  123. if frametime > now:
  124. Delay(frametime - now)
  125. frametime = frametime + MAXFRAMERATE
  126.  
  127. #Initialize OpenGL
  128. Init_OpenGL()
  129.  
  130. #### ERROR
  131. tmp = cast[cstring](glGetString(GL_SHADING_LANGUAGE_VERSION))
  132. echo(tmp)
  133.  
  134. # Main loop
  135. while runGame:
  136. while PollEvent(evt):
  137. if evt.kind == QuitEvent:
  138. runGame = false
  139. break
  140. if evt.kind == WindowEvent:
  141. reshape(pos_x=0.0, pos_y=position)
  142.  
  143. #Checking the keyboard state
  144. key_state = GetKeyboardState()
  145. if key_state[SDL_SCANCODE_UP.ord] == PRESSED:
  146. reshape(pos_x=0.0, pos_y=position)
  147. if position < 100:
  148. position += 1
  149. elif key_state[SDL_SCANCODE_DOWN.ord] == PRESSED:
  150. reshape(pos_x=0.0, pos_y=position)
  151. if position > 10:
  152. position -= 1
  153. elif key_state[SDL_SCANCODE_ESCAPE.ord] == PRESSED:
  154. rungame = false
  155. render()
  156. limitFramerate()
  157.  
  158. destroy window
Advertisement
Add Comment
Please, Sign In to add comment