Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'From Pharo3.0 of 18 March 2013 [Latest update: #30204] on 28 June 2013 at 3:08:01 pm'!
- GLViewportMorph subclass: #GLTutorial1
- instanceVariableNames: 'gl theProgram positionBufferObject vertexPositions vertexShader fragmentShader vao'
- classVariableNames: ''
- poolDictionaries: ''
- category: 'GLTutorial'!
- !GLTutorial1 commentStamp: '<historical>' prior: 0!
- First tutorial showing basic functionality of NBOpengl
- 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
- !
- !GLTutorial1 methodsFor: 'as yet unclassified' stamp: 'kilon 6/28/2013 14:56'!
- checkShaderError: aShader
- "check shader for any opengl compiler errors"
- "|statusAdress|
- statusAdress := NativeBoost allocate: NBOpenGLTypes GLint.
- gl getShaderiv_shader: shader pname: GL_INFO_LOG_LENGTH params: statusAdress .
- statusAdress = GL_FALSE ifTrue: [ ]"
- | len status statusPtr lenPtr logPtr statusPtrValue glValue gltrue|
- len := GL_INFO_LOG_LENGTH.
- status:= GL_COMPILE_STATUS.
- statusPtr := NativeBoost allocate: (NBExternalType sizeOf: 'int').
- "statusPtr nbUInt64AtOffset: 0 put: 0."
- gltrue := GL_TRUE.
- self halt.
- gl getShaderiv_shader: aShader pname: status params: statusPtr.
- statusPtrValue := statusPtr value.
- glValue := GL_FALSE.
- self halt.
- (statusPtrValue = GL_FALSE) ifTrue: [
- lenPtr := NativeBoost allocate: (NBExternalType sizeOf: 'GLint').
- gl getShaderiv_shader: aShader pname: len params: lenPtr.
- logPtr := NativeBoost allocate: (NBExternalType sizeOf: 'char') * (lenPtr+1).
- gl getShaderInfoLog_shader: aShader bufSize:lenPtr length: 0 infoLog: logPtr.
- Transcript show: logPtr.
- statusPtr free.
- lenPtr free.
- logPtr free.
- ] ifFalse: [Transcript show: 'Compile OK'; cr].
- ! !
- !GLTutorial1 methodsFor: 'as yet unclassified' stamp: 'kilon 6/23/2013 17:40'!
- glVersion
- "returns the version of Opengl used by NBOpenGL"
- ^ (gl getString: GL_VERSION) readString .! !
- !GLTutorial1 methodsFor: 'as yet unclassified' stamp: 'kilon 6/26/2013 11:56'!
- glerror
- "comment stating purpose of message"
- ^ gl getError ! !
- !GLTutorial1 methodsFor: 'Program' stamp: 'kilon 6/23/2013 17:41'!
- createProgram: shaderList
- "links shaders to the program"
- | program |
- program := gl createProgram .
- shaderList do: [ :each| gl attachShader_program: program shader: each ].
- gl linkProgram: program.
- shaderList do: [:each| gl detachShader_program: program shader: each ].
- ^ program
- ! !
- !GLTutorial1 methodsFor: 'shaders' stamp: 'kilon 6/27/2013 21:57'!
- createShader: shaderType string: shaderString
- " create shader using its source and compile it , return shader"
- | sources sizeOfPointer externalArrayOfPointers sourcesOnHeap shader |
- shader := gl createShader: shaderType .
- sources := shaderString .
- sizeOfPointer := NBExternalType sizeOf: 'void*'.
- "allocate buffer for array of pointers"
- externalArrayOfPointers := NativeBoost allocate: sizeOfPointer * sources size.
- "allocate strings and copy them to external heap"
- sourcesOnHeap := sources collect: [:each | NBExternalAddress fromString: each ].
- "copy string pointers to external array"
- sourcesOnHeap withIndexDo: [:ptr :i |
- "using nbUInt32AtOffset because we know pointer is 4 bytes :) "
- externalArrayOfPointers nbUInt32AtOffset: (i-1)*4 put: ptr value
- ].
- gl shaderSource_shader: shader count: sources size string:
- externalArrayOfPointers
- length: (NBExternalAddress null) .
- gl compileShader: shader.
- self checkShaderError: shader.
- "but after, don't forget to free the memory:"
- sourcesOnHeap do: [:each | each free].
- externalArrayOfPointers free.
- ^ shader! !
- !GLTutorial1 methodsFor: 'shaders' stamp: 'kilon 6/27/2013 21:36'!
- fragmentShader
- "fragment shader source"
- ^#('#version 110\n'
- 'out vec4 outputColor;\n'
- 'void main()\n'
- '{ \n'
- ' outputColor = vec4(1.0f, 1.0f ,1.0f , 1.0f);\n'
- '}\n')! !
- !GLTutorial1 methodsFor: 'shaders' stamp: 'kilon 6/28/2013 14:03'!
- vertexShader
- "vertex shader source code"
- ^# ('#version 110\n'
- 'layout(location = 0) in vec4 position;\n'
- 'void main()\n'
- '{\n'
- 'gl_Position = position and blah blah blah\n'
- '}\n')! !
- !GLTutorial1 methodsFor: 'instance variables' stamp: 'kilon 6/23/2013 17:44'!
- display
- "returns opengl context"
- ^ display! !
- !GLTutorial1 methodsFor: 'instance variables' stamp: 'kilon 6/9/2013 17:09'!
- gl
- ^display gl! !
- !GLTutorial1 methodsFor: 'instance variables' stamp: 'kilon 6/23/2013 17:42'!
- vao
- "vertex array"
- ^ 0! !
- !GLTutorial1 methodsFor: 'instance variables' stamp: 'kilon 6/23/2013 17:42'!
- vertexPositions
- "the positions of vertices to be passed to opengl"
- ^ #( 0.75 0.75 0.0 1.0 0.75 -0.75 0.0 1.0 -0.75 -0.75 0.0 1.0 )! !
- !GLTutorial1 methodsFor: 'initialisation' stamp: 'scorpion81 6/27/2013 18:32'!
- initialiseVertexBuffer
- "initialise Buffers"
- "| externalInt |"
- |externalInt vpos vptrsize |
- positionBufferObject := (NBExternalType sizeOf: 'float') * 12.
- vptrsize := positionBufferObject.
- externalInt := NativeBoost allocate: (NBExternalType sizeOf: 'int').
- externalInt nbInt32AtOffset: 0 put: positionBufferObject .
- gl genBuffers_n: 1 buffers: externalInt .
- gl bindBuffer_target: GL_ARRAY_BUFFER buffer: positionBufferObject .
- vpos := NativeBoost allocate: vptrsize.
- vertexPositions withIndexDo: [:ptr :i |
- "using nbUInt32AtOffset because we know pointer is 4 bytes :) "
- vpos nbFloat32AtOffset: (i-1)*(NBExternalType sizeOf: 'float') put: ptr value
- ].
- gl bufferData_target: GL_ARRAY_BUFFER size: positionBufferObject data: vpos usage: GL_STATIC_DRAW.
- gl bindBuffer_target: GL_ARRAY_BUFFER buffer: 0.
- externalInt free.
- vpos free.
- ! !
- !GLTutorial1 methodsFor: 'initialisation' stamp: 'scorpion81 6/27/2013 18:11'!
- initialize
- super initialize.
- gl := self gl.
- vertexShader := self vertexShader .
- fragmentShader := self fragmentShader .
- vao := self vao.
- vertexPositions := self vertexPositions .
- self initializeProgram .
- self initialiseVertexBuffer.
- "gl genVertexArrays_n: 1 arrays: vao.
- gl bindVertexArray: vao."
- ! !
- !GLTutorial1 methodsFor: 'initialisation' stamp: 'kilon 6/27/2013 21:42'!
- initializeProgram
- "create each shader and program"
- | shaderList strVertexShader strFragmentShader |
- strVertexShader := self createShader: GL_VERTEX_SHADER string: ( self vertexShader ).
- strFragmentShader := self createShader: GL_FRAGMENT_SHADER string: (self fragmentShader).
- shaderList := {strVertexShader. strFragmentShader}.
- theProgram := self createProgram: shaderList .
- shaderList do: [ :each | gl deleteShader: each ].
- ! !
- !GLTutorial1 methodsFor: 'display' stamp: 'kilon 6/27/2013 21:30'!
- render
- "handles the rendering of the context"
- | externalInt |
- gl clearColor_red: 0.0 green: 0.7 blue: 1.0 alpha: 1.0 .
- gl clear: GL_COLOR_BUFFER_BIT.
- gl enable: GL_MULTISAMPLE_ARB.
- gl useProgram: theProgram .
- gl bindBuffer_target: GL_ARRAY_BUFFER buffer: positionBufferObject .
- gl enableVertexAttribArray: 0.
- "externalInt := NativeBoost allocate: (NBExternalType sizeOf: 'int')."
- "externalInt nbInt32AtOffset: 0 put: 0."
- externalInt := (NBExternalAddress null).
- gl vertexAttribPointer_index: 0 size: 4 type: GL_FLOAT normalized: GL_FALSE stride: 0 pointer: 0.
- "externalInt free."
- gl drawArrays_mode: GL_TRIANGLES first: 0 count: 3.
- gl disableVertexAttribArray: 0.
- gl useProgram: 0.! !
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement