Mojo2 Polygons from Mojo1?


Well-known member
3rd Party Module Dev
Tutorial Author
Jan 2, 2020
90% of Mojo2 has a one-by-on mapping when you go form Mojo1. The one big difference from Mojo1 to Mojo2 for me seems to be how polygon commands seem to be wildly changed.

Mojo1 has e.g. DrawPoly(points) etc but I dont' understand how to translate Mojo1 Polygons into Mojo2 using the documentation alone.
Can anyone shed some light on polygons for Mojo2? Maybe a few simple examples in both Mojo1 and Mojo2?
There is a big difference indeed. mojo1 uses a polygon function that is highly problematic in terms of performance so it was ditched in GLES2 and therefor mojo2. Please have a look at the example for triangle fans because this is what DrawPoly is using under the hood at the moment.
examples\mojo2\drawtrianglefan_test. If you change the commenting from material to image usage you can easily see the tiling functionality.
But for instance, how woul I go about to convert this Mojo1 demo into Mojo2? It uses no other special commands except a single Drawpoly, so it ought to be a good example to see how Mojo2 works.

Import mojo

Function Main:Int()
    New Game
    Return 1
End Function

Class Game Extends App
    Field vortex:Vortex
    Method OnCreate:Int()
        vortex = New Vortex()
        SetUpdateRate 0
        Return 1
    End Method
    Method OnUpdate:Int()
        Return 1
    End Method
    Method OnRender:Int()
        Return 1
    End Method
End Class
Class Point
    Field x:Float
    Field y:Float
    Field z:float
End Class

Class Vortex

    Field cyl:Point[][]
    Field slicex:Int[][]
    Field slicey:Int[][]
    Field mnv:Float
    Field mn:Int
    Field gadd:Float
    Field rings:Int
    Const CAMERAWIDTH:Int = 800
    Const CAMERAHEIGHT:Int = 600

    Method New()
        rings = 26
        cyl = New Point[ 17][]
        slicex = New Int[17][]
        slicey = New Int[17][]
        For Local i:Int = 0 Until 17
            cyl[i] = New Point[rings]
            slicex[i] = New Int[rings]
            slicey[i] = New Int[rings]
        Local d1:Float[17], d2:Float[17]
        For Local i:Int = 0 Until 17
            d1[i] = Cos((360.0/16.0)*i)
            d2[i] = Sin((360.0/16.0)*i)
        For Local i:Int =0 To 16
             cyl[i][0] = New Point
             cyl[i][0].x = d1[i] * 2500
             cyl[i][0].y = d2[i] * 2500
        Local ps:Float = rings
        For Local M:Int = 0 Until rings
            For Local L:Int=0 To 16
                If cyl[L][M] = Null cyl[L][M] = New Point
                cyl[L][M].z = ps
                If M>0 Then   
                    cyl[L][M].x = cyl[L][0].x
                    cyl[L][M].y = cyl[L][0].y
            ps = ps - 1
    End Method
    Method Update:Void()
        gadd=gadd + 0.15
        Local YO:Float,XO:Float
        Local hw:Int = CAMERAWIDTH/2
        Local hh:Int = CAMERAHEIGHT/2
        For Local k:Int=0 To rings-1
            XO=CAMERAWIDTH *(Cos((((k+gadd)-mnv)*90)/9))
            For Local j:Int=0 To 16
                slicex[j][k] = (cyl[j][k].x+XO) / (cyl[j][k].z+mnv) + hw
                slicey[j][k] = (cyl[j][k].y+YO) / (cyl[j][k].z+mnv) + hh           
        mnv=mnv - 0.01
        If mnv<0.0 Then mnv=mnv+4.0
    End Method
    Method Render:Void()
        Local MD:Int = 0
        For Local k:Int= 1 To (rings -2)
            For Local j:Int= 0 To 15 Step 2
                If MD=0 Then
                    DrawQuad(slicex[j  ][k], slicey[j  ][k], slicex[j  ][k+1], slicey[j  ][k+1], slicex[j+1][k+1], slicey[j+1][k+1], slicex[j+1][k  ], slicey[j+1][k  ], k*1.0, k, k*12.0)
'                    DrawQuad(slicex[j+1][k], slicey[j+1][k], slicex[j+1][k+1], slicey[j+1][k+1], slicex[j+2][k+1], slicey[j+2][k+1], slicex[j+2][k  ], slicey[j+2][k  ], k*8.0, k, k     )
'                    DrawQuad(slicex[j  ][k], slicey[j  ][k], slicex[j+1][  k], slicey[j+1][k  ], slicex[j+1][k+1], slicey[j+1][k+1], slicex[j  ][k+1], slicey[j  ][k+1], k,k,k*8.0)                   
                    DrawQuad(slicex[j+1][k], slicey[j+1][k], slicex[j+2][  k], slicey[j+2][k  ], slicex[j+2][k+1], slicey[j+2][k+1], slicex[j+1][k+1], slicey[j+1][k+1], k*12.0,k,k)
                End If
    End Method
    Method DrawQuad:Void(x1:Int, y1:Int, x2:Int, y2:Int, x3:Int, y3:Int,x4:Int,y4:Int, r:Int, g:Int, b:Int)
        Local p:Float[8]
        p[0] = x1-80
        p[1] = y1-80
        p[2] = x2-80
        p[3] = y2-80
        p[4] = x3-80
        p[5] = y3-80
        p[6] = x4-80
        p[7] = y4-80
        mojo.SetColor r,g,b
        DrawPoly p
    End Method
End Class
In your example DrawPoly is an overkill for just dawing Quads. I did a short test with my defaultcanvas module and ... voila I found a bug with it. :oops: It doesn't draw the color of the outer quads properly. But otherwise it works fine as far as I see it no matter if I use mojo2 DrawPoly or DrawQuad.
Top Bottom