Advertisement
kilon

GLTutorial1

Jun 28th, 2013
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'From Pharo3.0 of 18 March 2013 [Latest update: #30204] on 28 June 2013 at 3:08:01 pm'!
  2. GLViewportMorph subclass: #GLTutorial1
  3.     instanceVariableNames: 'gl theProgram positionBufferObject vertexPositions vertexShader fragmentShader vao'
  4.     classVariableNames: ''
  5.     poolDictionaries: ''
  6.     category: 'GLTutorial'!
  7. !GLTutorial1 commentStamp: '<historical>' prior: 0!
  8. First tutorial showing basic functionality of NBOpengl
  9.  
  10. In this tutorial we show how to draw a triangle . The tutorial follows the tutorial here -> http://www.arcsynthesis.org/gltut/Basics/Tut01%20Dissecting%20Display.html
  11. !
  12.  
  13.  
  14. !GLTutorial1 methodsFor: 'as yet unclassified' stamp: 'kilon 6/28/2013 14:56'!
  15. checkShaderError: aShader
  16.     "check shader for any opengl compiler errors"
  17.     "|statusAdress|
  18.     statusAdress := NativeBoost allocate:  NBOpenGLTypes GLint.
  19.     gl getShaderiv_shader: shader pname: GL_INFO_LOG_LENGTH params: statusAdress .
  20.     statusAdress  = GL_FALSE ifTrue: [  ]"
  21.  
  22. | len status statusPtr lenPtr logPtr  statusPtrValue glValue gltrue|
  23.     len := GL_INFO_LOG_LENGTH.
  24.     status:= GL_COMPILE_STATUS.
  25.     statusPtr := NativeBoost allocate: (NBExternalType sizeOf: 'int').
  26.     "statusPtr nbUInt64AtOffset: 0 put: 0."
  27.     gltrue  := GL_TRUE.
  28.     self halt.
  29.     gl getShaderiv_shader: aShader pname: status params: statusPtr.
  30.     statusPtrValue := statusPtr value.
  31.     glValue := GL_FALSE.
  32.     self halt.
  33.     (statusPtrValue = GL_FALSE) ifTrue: [
  34.         lenPtr := NativeBoost allocate: (NBExternalType sizeOf: 'GLint').
  35.         gl getShaderiv_shader: aShader pname: len  params: lenPtr.
  36.        
  37.         logPtr := NativeBoost allocate: (NBExternalType sizeOf: 'char') * (lenPtr+1).
  38.         gl getShaderInfoLog_shader: aShader bufSize:lenPtr length: 0 infoLog: logPtr.
  39.        
  40.         Transcript show: logPtr.
  41.        
  42.         statusPtr free.
  43.         lenPtr free.
  44.         logPtr free.
  45.     ]  ifFalse: [Transcript show: 'Compile OK'; cr].
  46.  
  47.    
  48.    
  49.    
  50.    
  51.     ! !
  52.  
  53. !GLTutorial1 methodsFor: 'as yet unclassified' stamp: 'kilon 6/23/2013 17:40'!
  54. glVersion
  55. "returns the version of Opengl used by NBOpenGL"
  56.     ^ (gl getString: GL_VERSION) readString .! !
  57.  
  58. !GLTutorial1 methodsFor: 'as yet unclassified' stamp: 'kilon 6/26/2013 11:56'!
  59. glerror
  60.     "comment stating purpose of message"
  61.  
  62.     ^ gl getError ! !
  63.  
  64.  
  65. !GLTutorial1 methodsFor: 'Program' stamp: 'kilon 6/23/2013 17:41'!
  66. createProgram: shaderList
  67.     "links shaders to the program"
  68.  
  69.     | program |
  70.     program := gl createProgram .
  71.    
  72.     shaderList do: [ :each| gl attachShader_program: program shader: each  ].
  73.     gl linkProgram:  program.
  74.    
  75.     shaderList do: [:each| gl detachShader_program: program shader: each ].
  76.    
  77.     ^ program
  78.      
  79.  
  80.     ! !
  81.  
  82.  
  83. !GLTutorial1 methodsFor: 'shaders' stamp: 'kilon 6/27/2013 21:57'!
  84. createShader: shaderType string: shaderString
  85.    
  86.     " create shader using its source and compile it , return shader"
  87.    
  88.     | sources sizeOfPointer externalArrayOfPointers sourcesOnHeap shader |
  89.    
  90.     shader := gl createShader: shaderType .
  91.    
  92.     sources := shaderString .
  93.      
  94.     sizeOfPointer := NBExternalType sizeOf: 'void*'.
  95.  
  96.     "allocate buffer for array of pointers"
  97.     externalArrayOfPointers := NativeBoost allocate: sizeOfPointer * sources size.
  98.  
  99.     "allocate strings and copy them to external heap"
  100.     sourcesOnHeap := sources collect: [:each | NBExternalAddress fromString: each ].
  101.  
  102.     "copy string pointers to external array"
  103.     sourcesOnHeap withIndexDo: [:ptr :i |
  104.        "using nbUInt32AtOffset because we know pointer is 4 bytes :) "
  105.             externalArrayOfPointers nbUInt32AtOffset: (i-1)*4 put: ptr value
  106.     ].
  107.  
  108.     gl shaderSource_shader: shader count:  sources size  string:
  109.     externalArrayOfPointers
  110.      length: (NBExternalAddress null) .
  111.  
  112.    
  113.  
  114.     gl compileShader: shader.
  115.    
  116.     self checkShaderError: shader.
  117.    
  118.     "but after, don't forget to free the memory:"
  119.     sourcesOnHeap do: [:each | each free].
  120.     externalArrayOfPointers free.
  121.    
  122.     ^ shader! !
  123.  
  124. !GLTutorial1 methodsFor: 'shaders' stamp: 'kilon 6/27/2013 21:36'!
  125. fragmentShader
  126.     "fragment shader source"
  127. ^#('#version 110\n'
  128.     'out vec4 outputColor;\n'
  129.     'void main()\n'
  130.     '{ \n'
  131.     '  outputColor = vec4(1.0f, 1.0f ,1.0f , 1.0f);\n'
  132.     '}\n')! !
  133.  
  134. !GLTutorial1 methodsFor: 'shaders' stamp: 'kilon 6/28/2013 14:03'!
  135. vertexShader
  136.     "vertex shader source code"
  137. ^# ('#version 110\n'
  138.     'layout(location = 0) in vec4 position;\n'
  139.     'void main()\n'
  140.     '{\n'
  141.       'gl_Position = position and blah blah blah\n'
  142.     '}\n')! !
  143.  
  144.  
  145. !GLTutorial1 methodsFor: 'instance variables' stamp: 'kilon 6/23/2013 17:44'!
  146. display
  147. "returns opengl context"
  148.     ^ display! !
  149.  
  150. !GLTutorial1 methodsFor: 'instance variables' stamp: 'kilon 6/9/2013 17:09'!
  151. gl
  152.     ^display gl! !
  153.  
  154. !GLTutorial1 methodsFor: 'instance variables' stamp: 'kilon 6/23/2013 17:42'!
  155. vao
  156.     "vertex array"
  157.  
  158.     ^ 0! !
  159.  
  160. !GLTutorial1 methodsFor: 'instance variables' stamp: 'kilon 6/23/2013 17:42'!
  161. vertexPositions
  162. "the positions of vertices to be passed to opengl"
  163.  
  164.     ^ #( 0.75  0.75 0.0 1.0 0.75  -0.75 0.0 1.0 -0.75 -0.75 0.0 1.0 )! !
  165.  
  166.  
  167. !GLTutorial1 methodsFor: 'initialisation' stamp: 'scorpion81 6/27/2013 18:32'!
  168. initialiseVertexBuffer
  169. "initialise Buffers"
  170.    
  171.     "| externalInt |"
  172.     |externalInt vpos vptrsize |
  173.    
  174.     positionBufferObject := (NBExternalType sizeOf: 'float') * 12.
  175.     vptrsize := positionBufferObject.
  176.     externalInt := NativeBoost allocate: (NBExternalType sizeOf: 'int').
  177.     externalInt nbInt32AtOffset: 0 put: positionBufferObject .
  178.    
  179.     gl genBuffers_n:  1 buffers: externalInt .
  180.     gl bindBuffer_target: GL_ARRAY_BUFFER buffer: positionBufferObject .
  181.    
  182.     vpos := NativeBoost allocate: vptrsize.
  183.     vertexPositions withIndexDo: [:ptr :i |
  184.        "using nbUInt32AtOffset because we know pointer is 4 bytes :) "
  185.             vpos nbFloat32AtOffset: (i-1)*(NBExternalType sizeOf: 'float') put: ptr value
  186.     ].
  187.    
  188.    
  189.     gl bufferData_target: GL_ARRAY_BUFFER size: positionBufferObject data: vpos usage: GL_STATIC_DRAW.
  190.     gl bindBuffer_target: GL_ARRAY_BUFFER buffer:  0.
  191.    
  192.     externalInt free.
  193.     vpos free.
  194.    
  195. ! !
  196.  
  197. !GLTutorial1 methodsFor: 'initialisation' stamp: 'scorpion81 6/27/2013 18:11'!
  198. initialize
  199.  
  200.     super initialize.
  201.    
  202.     gl := self gl.
  203.     vertexShader := self vertexShader .
  204.     fragmentShader := self fragmentShader .
  205.     vao := self vao.
  206.     vertexPositions := self vertexPositions .
  207.  
  208.     self initializeProgram .
  209.     self initialiseVertexBuffer.
  210.    
  211.     "gl genVertexArrays_n: 1 arrays: vao.
  212.     gl bindVertexArray: vao."
  213.    
  214.    
  215.    
  216.     ! !
  217.  
  218. !GLTutorial1 methodsFor: 'initialisation' stamp: 'kilon 6/27/2013 21:42'!
  219. initializeProgram
  220.     "create each shader and program"
  221.  
  222.     | shaderList strVertexShader strFragmentShader |
  223.    
  224.     strVertexShader  := self createShader: GL_VERTEX_SHADER string:  ( self vertexShader ).
  225.     strFragmentShader  := self createShader:  GL_FRAGMENT_SHADER string: (self fragmentShader).  
  226.     shaderList := {strVertexShader. strFragmentShader}.
  227.     theProgram := self createProgram:  shaderList .
  228.     shaderList do: [ :each | gl deleteShader: each  ].
  229. ! !
  230.  
  231.  
  232. !GLTutorial1 methodsFor: 'display' stamp: 'kilon 6/27/2013 21:30'!
  233. render
  234. "handles the rendering of the context"
  235.  
  236.     | externalInt |
  237.    
  238.     gl clearColor_red: 0.0 green: 0.7  blue: 1.0  alpha: 1.0  .
  239.     gl clear: GL_COLOR_BUFFER_BIT.
  240.     gl enable: GL_MULTISAMPLE_ARB.
  241.    
  242.     gl useProgram: theProgram .
  243.    
  244.     gl bindBuffer_target: GL_ARRAY_BUFFER buffer:  positionBufferObject .
  245.     gl enableVertexAttribArray: 0.
  246.    
  247.     "externalInt := NativeBoost allocate: (NBExternalType sizeOf: 'int')."
  248.     "externalInt nbInt32AtOffset: 0 put: 0."
  249.     externalInt := (NBExternalAddress null).
  250.      
  251.     gl vertexAttribPointer_index: 0 size: 4  type:  GL_FLOAT normalized: GL_FALSE stride: 0  pointer: 0.
  252.    
  253.     "externalInt free."
  254.    
  255.     gl drawArrays_mode: GL_TRIANGLES first: 0  count: 3.
  256.    
  257.     gl disableVertexAttribArray:  0.
  258.     gl useProgram: 0.! !
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement