About Monkey 2 › Forums › Monkey 2 Programming Help › help with opengl
This topic contains 2 replies, has 2 voices, and was last updated by
AdamStrange
1 year, 11 months ago.
-
AuthorPosts
-
May 17, 2017 at 7:02 am #8191
OK, I’ve a bit of code to do some basic opengl + glsl:
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187#Import "<mojo>"Using std..Using mojo..Using gles20..#import "matrix"Function glLoadProgram:GLuint(vertexSource:String, fragmentSource:String)Local vs := glCompile( GL_VERTEX_SHADER, vertexSource )Local fs := glCompile( GL_FRAGMENT_SHADER, fragmentSource )Local program := glCreateProgram()glAttachShader( program, vs )glAttachShader( program, fs )glLink( program )glDeleteShader( vs )glDeleteShader( fs )Return programEndFunction LoadSimpleShader:GLuint()Local vertex:String = "uniform mat4 Matrix;attribute vec3 mPosition;attribute vec3 mColor;attribute vec3 BANG;varying vec3 OutColor;void main() {// gl_Position = vec4( mPosition, 1);gl_Position = Matrix * vec4( mPosition, 1);OutColor = mColor;}"Local fragment:String = "varying vec3 OutColor;void main() {gl_FragColor = vec4(OutColor, 0);}"local program:int = glLoadProgram(vertex, fragment)Return programEndClass MyWindow Extends GLWindowField vbo:GLuintField ebo:GLuintField shader:GLuintField elementsLength:IntField matrixView:MatrixField matrixProjection:MatrixField matrixViewProjection:MatrixField matrixUniform:Intfield sColor:intfield sPosition:intfield sNormal:intfield sUV:intfield sData:intMethod New()glClearColor(0.1, 0.1, 0.5, 1.0)glEnable(GL_DEPTH_TEST)glEnable(GL_CULL_FACE)'color, position, normal, uv, dataLocal vertices:Float[] = New Float[](1, 0, 0, -1,1,-1, 0, 1, 0, 0, 0, 0,0, 1, 0, 1,1,-1, 0, 0.5, 0, 0, 0, 0,0, 0, 1, 1,1,1, 0, 1, 0, 0, 0, 0,1, 0.5, 0, -1,1,1, 0, 1, 0, 0, 0, 0,1, 0, 0, -1,-1,-1, 0, 1, 0, 0, 0, 1,0, 1, 0, 1,-1,-1, 0, 0.5, 0, 0, 0, 1,0, 0, 1, 1,-1,1, 0, 1, 0, 0, 0, 1,1, 0.5, 0, -1, -1,1, 0, 1, 0, 0, 0, 1)'NOTE anticlockwise winding!!!!!!Local elements:int[] = New Int[](' 0, 2, 1) 'single triangle top' 0, 2, 1, 0, 3, 2) 'top0, 2, 1, 0, 3, 2,4, 5, 6, 4, 6, 7,0, 4, 3, 4, 7, 3) 'top bottom and sideelementsLength = elements.Lengthlocal max_attribs:intglGetIntegerv( GL_MAX_VERTEX_ATTRIBS, Varptr max_attribs )Print "max attributes= "+max_attribs' Load Shadershader = LoadSimpleShader()sColor = glGetAttribLocation( shader, "mColor" )sPosition = glGetAttribLocation( shader, "mPosition" )sNormal = glGetAttribLocation( shader, "mNormal" )sUV = glGetAttribLocation( shader, "mUV" )sData = glGetAttribLocation( shader, "mData" )Print "Program ID: " + shaderPrint "Color Location: " + sColorPrint "Position Location: " + sPositionPrint "Normal Location: " + sNormalPrint "UV Location: " + sUVPrint "Data Location: " + sData' Create vertex bufferglGenBuffers(1, Varptr vbo)glBindBuffer(GL_ARRAY_BUFFER, vbo)glBufferData(GL_ARRAY_BUFFER, vertices.Length * 4, vertices.Data, GL_STATIC_DRAW)' create element bufferglGenBuffers(1, Varptr ebo)glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo)glBufferData(GL_ELEMENT_ARRAY_BUFFER, elements.Length * 4, elements.Data, GL_STATIC_DRAW)' create matrix stuffmatrixProjection = Matrix.Perspective(Pi / 4.0, Cast<Float>(Width)/Height, 0.5, 100)matrixViewProjection = New MatrixmatrixUniform = glGetUniformLocation(shader, "Matrix")EndMethod OnRender(canvas:mojo.graphics.Canvas) OverrideApp.RequestRender()Super.OnRender(canvas)canvas.DrawText("Monkey2 OpenGL Test ", 10, 10)canvas.Color = Color.Whitecanvas.DrawLine( 0, Height*0.5, Width, Height * 0.5 )canvas.DrawLine( Width*0.5, 0, Width*0.5, Height )EndMethod OnRenderGL() Override' update the view projection matrix' matrixView = Matrix.RotateY(App.Millisecs * -0.0005) * Matrix.RotateX(Sin(App.Millisecs*0.0001))matrixView = Matrix.RotateY( App.Millisecs * -0.0005 ) * Matrix.RotateX(1)matrixView *= Matrix.Translation( 0.5, 0, -5 )matrixViewProjection = matrixView * matrixProjection' use the shader and update the matrix uniformglUseProgram( shader )glUniformMatrix4fv( matrixUniform, 1, False, matrixViewProjection.ToArray.Data )glBindBuffer(GL_ARRAY_BUFFER, vbo)glEnableVertexAttribArray( sColor )glEnableVertexAttribArray( sPosition )glEnableVertexAttribArray( sNormal )glEnableVertexAttribArray( sUV )glEnableVertexAttribArray( sData )glVertexAttribPointer( sColor, 3, GL_FLOAT, False, 48, Cast<Void Ptr>(0) )glVertexAttribPointer( sPosition, 3, GL_FLOAT, true, 48, Cast<Void Ptr>(12) )glVertexAttribPointer( sNormal, 3, GL_FLOAT, False, 48, Cast<Void Ptr>(24) )glVertexAttribPointer( sUV, 2, GL_FLOAT, False, 48, Cast<Void Ptr>(36) )glVertexAttribPointer( sData, 1, GL_FLOAT, False, 48, Cast<Void Ptr>(44) )' bind the element buffer objectglBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ebo )' finally do the drawingglClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )glDrawElements( GL_TRIANGLES, elementsLength, GL_UNSIGNED_INT, Cast<Int Ptr>(0) )EndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndIt should be pretty obvious what is does:
It draws the top and side of a cube that is rotating, with vertex colors. so far so good.
What is not working is the ability to defines and reference the normal, uv and data parameters I am wanting to feed into the shader.
checking the glGetAttributelocation return -1 for these 3.
I’ve tried bindings them, but just can’t get it to work.
Anyone have any suggestions what I’m doing wrong here?
Here’s the matrix.monkey2 code to go with it…
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138Class Matrix' Matrix class is based on Monogame math.PrivateField data:Float[] = New Float[16]PublicField M11:Float, M12:Float, M13:Float, M14:FloatField M21:Float, M22:Float, M23:Float, M24:FloatField M31:Float, M32:Float, M33:Float, M34:FloatField M41:Float, M42:Float, M43:Float, M44:FloatProperty ToArray:Float[]()data[0] = M11data[1] = M12data[2] = M13data[3] = M14data[4] = M21data[5] = M22data[6] = M23data[7] = M24data[8] = M31data[9] = M32data[10] = M33data[11] = M34data[12] = M41data[13] = M42data[14] = M43data[15] = M44Return dataEndOperator*:Matrix(matrix:Matrix)Local result := New Matrixresult.M11 = (Self.M11 * matrix.M11) + (Self.M12 * matrix.M21) + (Self.M13 * matrix.M31) + (Self.M14 * matrix.M41)result.M12 = (Self.M11 * matrix.M12) + (Self.M12 * matrix.M22) + (Self.M13 * matrix.M32) + (Self.M14 * matrix.M42)result.M13 = (Self.M11 * matrix.M13) + (Self.M12 * matrix.M23) + (Self.M13 * matrix.M33) + (Self.M14 * matrix.M43)result.M14 = (Self.M11 * matrix.M14) + (Self.M12 * matrix.M24) + (Self.M13 * matrix.M34) + (Self.M14 * matrix.M44)result.M21 = (Self.M21 * matrix.M11) + (Self.M22 * matrix.M21) + (Self.M23 * matrix.M31) + (Self.M24 * matrix.M41)result.M22 = (Self.M21 * matrix.M12) + (Self.M22 * matrix.M22) + (Self.M23 * matrix.M32) + (Self.M24 * matrix.M42)result.M23 = (Self.M21 * matrix.M13) + (Self.M22 * matrix.M23) + (Self.M23 * matrix.M33) + (Self.M24 * matrix.M43)result.M24 = (Self.M21 * matrix.M14) + (Self.M22 * matrix.M24) + (Self.M23 * matrix.M34) + (Self.M24 * matrix.M44)result.M31 = (Self.M31 * matrix.M11) + (Self.M32 * matrix.M21) + (Self.M33 * matrix.M31) + (Self.M34 * matrix.M41)result.M32 = (Self.M31 * matrix.M12) + (Self.M32 * matrix.M22) + (Self.M33 * matrix.M32) + (Self.M34 * matrix.M42)result.M33 = (Self.M31 * matrix.M13) + (Self.M32 * matrix.M23) + (Self.M33 * matrix.M33) + (Self.M34 * matrix.M43)result.M34 = (Self.M31 * matrix.M14) + (Self.M32 * matrix.M24) + (Self.M33 * matrix.M34) + (Self.M34 * matrix.M44)result.M41 = (Self.M41 * matrix.M11) + (Self.M42 * matrix.M21) + (Self.M43 * matrix.M31) + (Self.M44 * matrix.M41)result.M42 = (Self.M41 * matrix.M12) + (Self.M42 * matrix.M22) + (Self.M43 * matrix.M32) + (Self.M44 * matrix.M42)result.M43 = (Self.M41 * matrix.M13) + (Self.M42 * matrix.M23) + (Self.M43 * matrix.M33) + (Self.M44 * matrix.M43)result.M44 = (Self.M41 * matrix.M14) + (Self.M42 * matrix.M24) + (Self.M43 * matrix.M34) + (Self.M44 * matrix.M44)Return resultEndFunction Identity:Matrix()Local result := New Matrixresult.M11 = 1.0result.M22 = 1.0result.M33 = 1.0result.M44 = 1.0Return resultEndFunction Translation:Matrix(x:Float, y:Float, z:Float)Local result := Identity()result.M41 = xresult.M42 = yresult.M43 = zReturn resultEndFunction RotateX:Matrix(angle:Float)Local cos:Float = Cast<Float>(Cos(angle))Local sin:Float = Cast<Float>(Sin(angle))Local result := Identity()result.M22 = cosresult.M23 = sinresult.M32 = -sinresult.M33 = cosReturn resultEndFunction RotateY:Matrix(angle:Float)Local cos:Float = Cast<Float>(Cos(angle))Local sin:Float = Cast<Float>(Sin(angle))Local result := Identity()result.M11 = cosresult.M13 = -sinresult.M31 = sinresult.M33 = cosReturn resultEndFunction Perspective:Matrix(fovy:Float, aspect:Float, near:Float, far:float)Local yMax:Float = near * Tan(0.5 * fovy)Local yMin:Float = -yMaxLocal xMin:Float = yMin * aspectLocal xMax:Float = yMax * aspectReturn Frustum(xMin, xMax, yMin, yMax, near, far)EndFunction Frustum:Matrix(left:Float, right:Float, bottom:Float, top:Float, near:Float, far:Float)Local invRL:Float = 1.0 / (right - left)Local invTB:Float = 1.0 / (top - bottom)Local invFN:Float = 1.0 / (far - near)Local result := New Matrix()result.M11 = 2.0 * near * invRLresult.M12 = 0.0result.M13 = 0.0result.M14 = 0.0result.M21 = 0.0result.M22 = 2.0 * near * invTBresult.M23 = 0.0result.M24 = 0.0result.M31 = (right + left) * invRLresult.M32 = (top + bottom) * invTBresult.M33 = -(far + near) * invFNresult.M34 = -1.0result.M41 = 0.0result.M42 = 0.0result.M43 = -2.0 * far * near * invFNresult.M44 = 0.0Return resultEndEndMay 18, 2017 at 3:40 am #8198The shader compiler may be optimizing out normal, color etc if they’re not actually used by the shader which they don’t appear to be.
Another way to deal with attribs is to use glBindAttribLocation instead (after compiling but *before* linking) to ‘force’ attribs to go into fixed locations, position=0, normal=1, color=2 etc.
May 18, 2017 at 5:30 am #8199thanks mark, thats good information
-
AuthorPosts
You must be logged in to reply to this topic.