1
0
mirror of https://github.com/lxsang/Diya-API.git synced 2024-12-27 03:48:21 +01:00

update openGL 1.10 binding

This commit is contained in:
Dany LE 2022-02-13 17:15:23 +01:00
parent 00c350805d
commit 94d3a6266b
9 changed files with 362 additions and 23 deletions

View File

@ -126,6 +126,20 @@ DiyaBoot >> render [
].
]
{ #category : #events }
DiyaBoot >> render:sr [
|event|
event := SDL_Event new.
[ running ] whileTrue: [
[ (SDL2 pollEvent: event) > 0 ] whileTrue: [
self processEvent: event
].
sr render.
SDL2 glSwapWindow: window.
SDL2 delay: 50.
].
]
{ #category : #events }
DiyaBoot >> run [
self init.
@ -199,12 +213,16 @@ DiyaBoot >> showSystemInfo [
{ #category : #events }
DiyaBoot >> startx [
|sr|
display ifNil: [ ^self error: 'Please run #init before this method' ].
self createWindow.
self createGLContext.
self createRenderer.
self showSystemInfo.
self render.
sr := SimpleDiyaRenderer new.
sr setup.
self render:sr.
sr destroy.
context delete.
renderer destroy.
window destroy.

View File

@ -0,0 +1,24 @@
Class {
#name : #DiyaRenderer,
#superclass : #DiyaBaseObject,
#pools : [
'OpenGLConstants',
'OpenGLTypes'
],
#category : #'Diya-Graphics'
}
{ #category : #deleting }
DiyaRenderer >> destroy [
^ self subclassResponsibility
]
{ #category : #deleting }
DiyaRenderer >> render [
^ self subclassResponsibility
]
{ #category : #deleting }
DiyaRenderer >> setup [
^ self subclassResponsibility
]

View File

@ -0,0 +1,25 @@
Class {
#name : #GLSimpleShader,
#superclass : #OpenGLSL,
#category : #'Diya-Shaders'
}
{ #category : #accessing }
GLSimpleShader class >> fragmentShader [
^ '
void main()
{
gl_FragColor = vec4(0.4,0.4,0.8,1.0);
}
'
]
{ #category : #accessing }
GLSimpleShader class >> vertextShader [
^ '
void main()
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
'
]

View File

@ -33,6 +33,11 @@ OpenGL class >> color3fR: red G: green B: blue [
^self ffiCall: #(void glColor3f(GLfloat red,GLfloat green,GLfloat blue))
]
{ #category : #'as yet unclassified' }
OpenGL class >> drawArrays: mode first: idx count:n [
^self ffiCall: #(void glDrawArrays(GLenum mode,GLint idx,GLsizei n))
]
{ #category : #accessing }
OpenGL class >> end [
^ self ffiCall: #(void glEnd( void ))

View File

@ -7,15 +7,24 @@ Class {
'GL_ACTIVE_ATTRIBUTE_MAX_LENGTH',
'GL_ACTIVE_UNIFORMS',
'GL_ACTIVE_UNIFORM_MAX_LENGTH',
'GL_ARRAY_BUFFER',
'GL_ATTACHED_SHADERS',
'GL_BYTE',
'GL_COLOR_BUFFER_BIT',
'GL_COMPILE_STATUS',
'GL_COMPUTE_SHADER',
'GL_DELETE_STATUS',
'GL_DEPTH_BUFFER_BIT',
'GL_DOUBLE',
'GL_FALSE',
'GL_FIXED',
'GL_FLOAT',
'GL_FRAGMENT_SHADER',
'GL_GEOMETRY_SHADER',
'GL_HALF_FLOAT',
'GL_INFO_LOG_LENGTH',
'GL_INT',
'GL_INT_2_10_10_10_REV',
'GL_LINES',
'GL_LINE_LOOP',
'GL_LINE_STRIP',
@ -26,18 +35,51 @@ Class {
'GL_QUAD_STRIP',
'GL_SHADER_SOURCE_LENGTH',
'GL_SHADER_TYPE',
'GL_SHORT',
'GL_STATIC_DRAW',
'GL_STENCIL_BUFFER_BIT',
'GL_TESS_CONTROL_SHADER',
'GL_TESS_EVALUATION_SHADER',
'GL_TEXTURE_BUFFER',
'GL_TRIANGLES',
'GL_TRIANGLE_FAN',
'GL_TRIANGLE_STRIP',
'GL_TRUE',
'GL_UNIFORM_BUFFER',
'GL_UNSIGNED_BYTE',
'GL_UNSIGNED_INT',
'GL_UNSIGNED_INT_10F_11F_11F_REV',
'GL_UNSIGNED_INT_2_10_10_10_REV',
'GL_UNSIGNED_SHORT',
'GL_VALIDATE_STATUS',
'GL_VERTEX_SHADER'
],
#category : #'Diya-OpenGL'
}
{ #category : #'class initialization' }
OpenGLConstants class >> initCommonConstants [
GL_BYTE := 16r1400.
GL_UNSIGNED_BYTE := 16r1401.
GL_SHORT := 16r1402.
GL_UNSIGNED_SHORT := 16r1403.
GL_INT := 16r1404.
GL_UNSIGNED_INT := 16r1405.
GL_HALF_FLOAT := 16r140B.
GL_FLOAT := 16r1406.
GL_DOUBLE := 16r140A.
GL_FIXED := 16r140C.
GL_INT_2_10_10_10_REV := 16r8D9F.
GL_UNSIGNED_INT_2_10_10_10_REV := 16r8368.
GL_UNSIGNED_INT_10F_11F_11F_REV := 16r8C3B.
GL_ARRAY_BUFFER := 16r8892.
GL_TEXTURE_BUFFER := 16r8C2A.
GL_UNIFORM_BUFFER := 16r8A11.
GL_STATIC_DRAW := 16r88E4.
GL_FALSE := 0.
GL_TRUE := 1
]
{ #category : #'class initialization' }
OpenGLConstants class >> initCommonMask [
GL_COLOR_BUFFER_BIT := 16r00004000.

View File

@ -94,7 +94,7 @@ OpenGLSL class >> reset [
{ #category : #'as yet unclassified' }
OpenGLSL class >> setShaderSourceFor: shader count: n string: s length: l [
^ self ffiCall: #(void glShaderSource( GLuint shader,GLsizei n,const char ** s,const GLint *l))
^ self ffiCall: #(void glShaderSource( GLuint shader,GLsizei n,const void* s,const GLint *l))
]
{ #category : #'instance creation' }
@ -103,6 +103,11 @@ OpenGLSL class >> uniqueInstance [
^ singleton
]
{ #category : #'as yet unclassified' }
OpenGLSL class >> useProgram:program [
^self ffiCall:#(void glUseProgram(GLuint program))
]
{ #category : #accessing }
OpenGLSL class >> vertextShader [
^ self subclassResponsibility
@ -111,10 +116,10 @@ OpenGLSL class >> vertextShader [
{ #category : #compiling }
OpenGLSL >> checkStatus:status of: id [
|infoLength buffer|
infoLength := FFIExternalArray externalNewType: #GLint size: 1.
infoLength := FFIExternalArray externalNewType: GLint size: 1.
infoLength autoRelease.
OpenGLSL getShaderiv: id parameterName: status params: nil.
OpenGLSL getShaderiv: id parameterName: GL_INFO_LOG_LENGTH params: infoLength.
OpenGLSL getShaderiv: id parameterName: GL_INFO_LOG_LENGTH params: infoLength getHandle.
(infoLength at:1) > 0 ifTrue: [
"report the error"
buffer := ByteArray new:(infoLength at: 1).
@ -143,7 +148,8 @@ OpenGLSL >> compile [
self checkStatus: GL_LINK_STATUS of: programID.
OpenGLSL detachShaderFrom: programID shader: vertexShaderID.
OpenGLSL detachShaderFrom: programID shader: fragmentShaderID.
OpenGLSL deleteShader: vertexShaderID.
OpenGLSL deleteShader: fragmentShaderID
]
{ #category : #compiling }
@ -167,28 +173,16 @@ OpenGLSL >> delete [
OpenGLSL deleteProgram: programID
]
{ #category : #'library path' }
OpenGLSL >> ffiLibraryName [
^self class ffiLibraryName
]
{ #category : #compiling }
OpenGLSL >> getSourcePtr: string for: shaderId [
|cstr i ptr |
cstr := FFIExternalArray externalNewType: #uint8 size: string size + 1.
cstr autoRelease.
i := 1.
(string asByteArray copyWith: 0) do: [ :e|
cstr at: i put: e.
i := i+1.
].
ptr := FFIExternalArray externalNewType: #GLintptr size: 1.
ptr autoRelease.
ptr at: 1 put: cstr pointer value.
OpenGLSL setShaderSourceFor: shaderId count: 1 string: ptr length: nil.
|xarray|
xarray := FFIExternalArray externalNewType: 'char*' size: 1.
xarray at:1 put: (ExternalAddress fromString: string).
xarray autoRelease.
OpenGLSL setShaderSourceFor: shaderId count: 1 string: xarray getHandle length: nil.
]
{ #category : #'submorphs-add/remove' }
OpenGLSL >> use [
^self ffiCall: #(void glUseProgram( GLuint program))
^OpenGLSL useProgram: programID
]

View File

@ -0,0 +1,102 @@
Class {
#name : #OpenGLVertexArray,
#superclass : #DiyaBaseObject,
#instVars : [
'vertexArrayID'
],
#pools : [
'OpenGLConstants',
'OpenGLTypes'
],
#category : #'Diya-OpenGL'
}
{ #category : #'as yet unclassified' }
OpenGLVertexArray class >> bind:array [
^self ffiCall:#(void glBindVertexArray( GLuint array))
]
{ #category : #'as yet unclassified' }
OpenGLVertexArray class >> deteleVertexArraysSize:n arrays: arrays [
^self ffiCall:#(void glDeleteVertexArrays( GLsizei n,const GLuint *arrays))
]
{ #category : #'as yet unclassified' }
OpenGLVertexArray class >> disableArrayAttribute: vaobj index: index [
^self ffiCall: #(void glDisableVertexArrayAttrib( GLuint vaobj,GLuint index))
]
{ #category : #'as yet unclassified' }
OpenGLVertexArray class >> disableAttributeArray: index [
^self ffiCall: #(void glDisableVertexAttribArray( GLuint index))
]
{ #category : #'as yet unclassified' }
OpenGLVertexArray class >> enableArrayAttribute: vaobj index: index [
^self ffiCall: #(void glEnableVertexArrayAttrib( GLuint vaobj,GLuint index))
]
{ #category : #'as yet unclassified' }
OpenGLVertexArray class >> enableAttributeArray: index [
^self ffiCall: #(void glEnableVertexAttribArray( GLuint index))
]
{ #category : #'library path' }
OpenGLVertexArray class >> ffiLibraryName [
^ OpenGL ffiLibraryName
]
{ #category : #'as yet unclassified' }
OpenGLVertexArray class >> genVertexArraysSize: n arrays: arrays [
^self ffiCall:#(void glGenVertexArrays( GLsizei n,GLuint *arrays))
]
{ #category : #'as yet unclassified' }
OpenGLVertexArray class >> vertexAttributeLPointerIndex: index size: size type: type stride: stride pointer: pointer [
^self ffiCall: #(void glVertexAttribLPointer( GLuint index,GLint size,GLenum type,GLsizei stride,const void * pointer))
]
{ #category : #'as yet unclassified' }
OpenGLVertexArray class >> vertexAttributePointerIndex: index size: size type: type normalized: normalized stride: stride pointer: pointer [
^self ffiCall: #(void glVertexAttribPointer( GLuint index,GLint size,GLenum type,GLboolean normalized,GLsizei stride,const void * pointer))
]
{ #category : #'as yet unclassified' }
OpenGLVertexArray class >> vertexAttributelPointerIndex: index size: size type: type stride: stride pointer: pointer [
^self ffiCall: #(void glVertexAttribIPointer( GLuint index,GLint size,GLenum type,GLsizei stride,const void * pointer))
]
{ #category : #accessing }
OpenGLVertexArray >> bind [
OpenGLVertexArray bind: self vertexArrayID
]
{ #category : #accessing }
OpenGLVertexArray >> delete [
OpenGLVertexArray deteleVertexArraysSize:1 arrays: vertexArrayID.
vertexArrayID at: 1 put: -1
]
{ #category : #'as yet unclassified' }
OpenGLVertexArray >> disableAttribute: index [
^OpenGLVertexArray disableAttributeArray: index
]
{ #category : #'as yet unclassified' }
OpenGLVertexArray >> enableAttribute: index [
^OpenGLVertexArray enableAttributeArray: index
]
{ #category : #initialization }
OpenGLVertexArray >> initialize [
vertexArrayID := FFIExternalArray externalNewType: GLint size:1.
vertexArrayID autoRelease.
vertexArrayID at:1 put: -1.
OpenGLVertexArray genVertexArraysSize: 1 arrays: vertexArrayID.
]
{ #category : #accessing }
OpenGLVertexArray >> vertexArrayID [
^vertexArrayID at: 1
]

View File

@ -0,0 +1,70 @@
Class {
#name : #OpenGLVertexBuffer,
#superclass : #DiyaBaseObject,
#instVars : [
'vertexBufferID'
],
#pools : [
'OpenGLConstants',
'OpenGLTypes'
],
#category : #'Diya-OpenGL'
}
{ #category : #'as yet unclassified' }
OpenGLVertexBuffer class >> bind: target buffer: buffer [
^ self ffiCall: #(void glBindBuffer( GLenum target,GLuint buffer))
]
{ #category : #'as yet unclassified' }
OpenGLVertexBuffer class >> bufferData:target size: size data: data usage: usage [
^self ffiCall: #(void glBufferData( GLenum target,GLsizeiptr size,const void * data,GLenum usage))
]
{ #category : #'as yet unclassified' }
OpenGLVertexBuffer class >> deleteBuffersSize:n buffers: buffers [
^self ffiCall: #(void glDeleteBuffers( GLsizei n,const GLuint * buffers))
]
{ #category : #'as yet unclassified' }
OpenGLVertexBuffer class >> ffiLibraryName [
^ OpenGL ffiLibraryName
]
{ #category : #'as yet unclassified' }
OpenGLVertexBuffer class >> genVertexBuffersSize:n buffers: buffers [
^self ffiCall: #(void glGenBuffers( GLsizei n,GLuint * buffers))
]
{ #category : #'as yet unclassified' }
OpenGLVertexBuffer class >> namedBufferData:buffer size: size data: data usage: usage [
^self ffiCall: #(void glNamedBufferData(GLuint buffer,GLsizeiptr size,const void *data,GLenum usage))
]
{ #category : #'as yet unclassified' }
OpenGLVertexBuffer >> bind: target [
^OpenGLVertexBuffer bind:target buffer: self vertexBufferID
]
{ #category : #'as yet unclassified' }
OpenGLVertexBuffer >> data:target data: data usage: usage [
^OpenGLVertexBuffer bufferData: target size: data size data:data getHandle usage: usage
]
{ #category : #'as yet unclassified' }
OpenGLVertexBuffer >> delete [
^OpenGLVertexBuffer deleteBuffersSize: 1 buffers: vertexBufferID
]
{ #category : #initialization }
OpenGLVertexBuffer >> initialize [
vertexBufferID := FFIExternalArray externalNewType: GLint size:1.
vertexBufferID autoRelease.
vertexBufferID at:1 put: -1.
OpenGLVertexBuffer genVertexBuffersSize: 1 buffers: vertexBufferID
]
{ #category : #initialization }
OpenGLVertexBuffer >> vertexBufferID [
^ vertexBufferID at: 1
]

View File

@ -0,0 +1,59 @@
Class {
#name : #SimpleDiyaRenderer,
#superclass : #DiyaRenderer,
#instVars : [
'vertexBuffer',
'arrayBuffer',
'bufferData',
'shader'
],
#category : #'Diya-Graphics'
}
{ #category : #deleting }
SimpleDiyaRenderer >> destroy [
vertexBuffer delete.
arrayBuffer delete.
shader delete.
GLSimpleShader reset.
]
{ #category : #initialization }
SimpleDiyaRenderer >> initialize [
vertexBuffer := OpenGLVertexBuffer new.
arrayBuffer := OpenGLVertexArray new.
bufferData := FFIExternalArray externalNewType: GLfloat size: 9.
bufferData autoRelease.
shader := GLSimpleShader uniqueInstance.
]
{ #category : #deleting }
SimpleDiyaRenderer >> render [
OpenGL clearColorR: 1.0 G: 0 B: 1.0 A:0.
OpenGL clear: GL_COLOR_BUFFER_BIT.
shader use.
arrayBuffer enableAttribute: 0.
vertexBuffer bind: GL_ARRAY_BUFFER.
OpenGLVertexArray vertexAttributePointerIndex: 0 size:3 type: GL_FLOAT normalized: GL_FALSE stride: 0 pointer: nil .
OpenGL drawArrays: GL_TRIANGLES first:0 count: 3.
arrayBuffer disableAttribute: 0.
]
{ #category : #deleting }
SimpleDiyaRenderer >> setup [
bufferData
at: 1 put: -1.0;
at: 2 put: -1.0;
at: 3 put: 0;
at: 4 put: 1.0;
at: 5 put: -1.0;
at: 6 put: 0;
at: 7 put: 0;
at: 8 put: 1.0;
at: 9 put: 0.
arrayBuffer bind.
vertexBuffer bind: GL_ARRAY_BUFFER.
vertexBuffer data: GL_ARRAY_BUFFER data:bufferData usage: GL_STATIC_DRAW.
shader compile.
]