Forum Replies Created
-
AuthorPosts
-
The catch here is when you want to use a library written in C through a module, you might use pointers to call the C API (for example the OpenGL API), personally I would no find something odd about this approach.
Perhaps using a wrapper around the C standard library might make lot sense, I find no other better.
http://www.cplusplus.com/reference/cstdlib/rand/
Now I have created a 3D cube example.
Update 29 Nov 2016: I have fixed some odd problems with the cube and cleaned up the example.
The only problem is that I get weird draw Z order in the faces of the cube.This problem is fixed, I have declared the order of vertices in a proper manner.
The vertex declaration is from a C++ glm example, the matrix math is from OpenTK library.I switched into using the Monogame math since it’s much lean and clean implementation.
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290#Import "<mojo>"Using std..Using mojo..Using gles20..Function glLoadProgram:GLuint(vertexSource:String, fragmentSource:String)Local vs := mojo.graphics.glutil.glCompile(GL_VERTEX_SHADER, vertexSource)Local fs := mojo.graphics.glutil. glCompile(GL_FRAGMENT_SHADER, fragmentSource)Local program := glCreateProgram()glAttachShader(program, vs)glAttachShader(program, fs)mojo.graphics.glutil.glLink(program)glDeleteShader(vs)glDeleteShader(fs)Return programEndFunction LoadShader:GLuint()Local vertex:String = "uniform mat4 Matrix;attribute vec3 Position;varying vec3 OutColor;void main(){gl_Position = Matrix * vec4(Position, 1);// Higher vertices can be yellow and lower can be red.OutColor = (Position.y < 0.0) ? vec3(1, 0, 0) : vec3(1, 1, 0);}"Local fragment:String = "varying vec3 OutColor;void main(){gl_FragColor = vec4(OutColor, 1);}"Local program := glLoadProgram(vertex, fragment)glBindAttribLocation(program, 0, "Position")Return programEndClass 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 resultEndEndClass MyWindow Extends GLWindowField vbo:GLuintField ebo:GLuintField shader:GLuintField elementsLength:IntField matrixView:MatrixField matrixProjection:MatrixField matrixViewProjection:MatrixField matrixUniform:IntMethod _LoadShader()EndMethod _LoadVertexBuffer()EndMethod New()' enable OpenGL statesglClearColor(0.1, 0.1, 0.5, 1.0)glEnable(GL_DEPTH_TEST)glEnable(GL_CULL_FACE)' define the shape for a cube'' 7_________6' / / Top vertices' 4________5'' 3_________2' / / Bottom Vertices' 0________1Local vertices := New Float[](-1, -1, -1,1, -1, -1,1, -1, 1,-1, -1, 1,-1, 1, -1,1, 1, -1,1, 1, 1,-1, 1, 1)Local elements := New Int[](4, 7, 6, 6, 5, 4, ' Top0, 1, 2, 2, 3, 0, ' Bottom0, 3, 7, 7, 4, 0, ' Left1, 5, 6, 6, 2, 1, ' Right3, 2, 6, 6, 7, 3, ' Front0, 4, 5, 5, 1, 0) ' BackelementsLength = elements.Length' load shadershader = LoadShader()Print("Program ID: " + shader)Print("Attribute Location: " + glGetAttribLocation(shader, "Position"))' 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", 10, 10)EndMethod OnRenderGL() Override' update the view projection matrixmatrixView = Matrix.RotateY(App.Millisecs * -0.0005) * Matrix.RotateX(Sin(App.Millisecs*0.0001))matrixView *= Matrix.Translation(0, 0, -5)matrixViewProjection = matrixView * matrixProjection' use the shader and update the matrix uniformglUseProgram(shader)glUniformMatrix4fv(matrixUniform, 1, False, matrixViewProjection.ToArray.Data)' bind the vertex buffer object and enable the vertex attributesglBindBuffer(GL_ARRAY_BUFFER, vbo)glEnableVertexAttribArray(0)glVertexAttribPointer(0, 3, GL_FLOAT, False, 0, Cast<Void Ptr>(0))' 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()EndI see that you are very good developer based on what you posted on this forum. Perhaps you can use this power to create an awesome game and donate the 10% of your earnings. It’s more challenging (because you will complete the project that way) and fair (win-2-win) in my opinion.
Very nice stuff, currently I have in the works to create a cool 3D cube, but I have stucked a bit. I might get some ideas by your code because things get stuck.
Here I have started a thread that it will based mostly on OpenGL snippets.
http://monkey2.monkey-x.com/forums/topic/monkey2-opengl-experiments/Very good stuff, the battleship and cards games did well, indeed.
Good tips, thanks to all.
Now trying some rain effect.
Virtually, any sort of cool effects can be created here.
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475#Import "<mojo>"Using mojo..Using std.collectionsUsing std.geomUsing std.graphicsUsing std.randomClass RainEffectField rain:Vec2f[]Method Generate(width:Int, height:Int)rain = New Vec2f[100]For Local i := 0 Until rain.Lengthrain[i] = New Vec2f(Rnd(0, width), Rnd(0, height))NextEndMethod Update(width:Int, height:Int)For Local i := 0 Until rain.Lengthrain[i].Y += 10If rain[i].Y > height + 50rain[i].Y = -50rain[i].X = Rnd(0, width)EndNextEndMethod Draw(canvas:Canvas)For Local i := Eachin raincanvas.DrawRect(i.X, i.Y - 25, 2, 50)NextEndEndClass MyWindow Extends WindowField imgCanvas:CanvasField imgBuffer:ImageField rain:RainEffectMethod New()imgBuffer = New Image(Width, Height, TextureFlags.Dynamic)imgCanvas = New Canvas(imgBuffer)imgCanvas.Color = Color.BlackimgCanvas.DrawRect(0, 0, Width, Height)rain = New RainEffectrain.Generate(Width, Height)EndMethod OnRender(canvas:Canvas) OverrideimgCanvas.Color = New Color(0.1, 0.1, 0.1, 0.1)imgCanvas.DrawRect(0, 0, Width, Height)imgCanvas.Color = Color.Whiterain.Update(Width, Height)rain.Draw(imgCanvas)imgCanvas.Flush()canvas.DrawImage(imgBuffer, 0, 0)App.RequestRender()EndEndFunction Main()For Local i := 0 To 1000Rnd(1, 1000)NextNew AppInstanceNew MyWindowApp.Run()EndIs it like? SeedRnd(Millisecs())
However I don’t know if you type it on Main function the application elapsed milliseconds would be 0, perhaps on somewhere else like loading a new scene or level initialization might make sense better, right?
By design it’s impossible to do that, however there are clever workarounds. Wine is such clever workaround. I don’t know if there’s other alternative.
The most standard one is going for the “automated builds” (you would write a Python script to execute shell commands). Once you run various virtual machines and let them do daily builds you might suffer less from manual effort.
It will be a good idea, won’t hurt to post it. Can you make a huge image (i.e. 1500×1500) that explains it?
Generally I use “gihub for windows” but I consider switching to sourcetree, lots of people like this one.
Doing some tests…
Monkey123456789101112131415161718192021222324252627282930#Import "<std>"Using std..Struct TestPrivateField value:FloatEndStruct Test Extension' Mutator methods are allowed.Method SetValue(v:Float)value = vEnd' Accessor methods are allowed.Method GetValue:Float()Return valueEnd' Properties are not allowed.'Property GetSomething:String()' Return "Hello"'EndEndFunction Main()Local a := New Testa.SetValue(100)Print(a.GetValue())EndNow that you mention it, a snippets sub forum (not projects) might be a good idea.
Awesome stuff, this could be actually very interesting for general purpose usage.
Very interesting, how about trying some old school effects?
I would not mind if there’s serious pixel pushing.
-
AuthorPosts