BrightScript 3D test - Roku (4)

My initial attempt to port over an old Actionscript program, here it goes in main.brs.

Library "v30/bslDefender.brs"

'''''''''''''''''''''''''''''''''''''''''''''''''''
'
function Main() as void

    screen = CreateObject("roScreen")
    port = CreateObject("roMessagePort")
    screen.SetMessagePort(port)
    screen.SetAlphaEnable(true)

    m.PI = 3.14159265358979323846264338327958209749445923078164
    m.origin = { x:1280/2, y:720/2, z:0 }
    m.focal_length = 400
    m.scene = { x:m.origin.x, y:m.origin.y }
    m.axis_rotation = { x:0, y:0, z:0 }
    m.camera = { x:0, y:0, z:0 }

    LINE_COLOR = &h00ee00ff
    VERTEX_COLOR = &h00dd00ff

    m.len = 80
    m.points = [
        '
        vertex3d(-m.len, -m.len, -m.len),
        vertex3d(m.len, -m.len, -m.len),
        vertex3d(m.len, -m.len, m.len),
        vertex3d(-m.len, -m.len, m.len),
        '
        vertex3d(-m.len, m.len, -m.len),
        vertex3d(m.len, m.len, -m.len),
        vertex3d(m.len, m.len, m.len),
        vertex3d(-m.len, m.len, m.len),
                ]

    ' Initialize drawing points, skip
'    screen.Clear(&h000000ff)
'    for i=0 to m.points.count()-1 step 1
'        screen.DrawPoint(m.points[i].x, m.points[i].y, 4, &h00ff00ff)
'    end for
'    screen.finish()

    timer = CreateObject("roTimespan")
    start_time = timer.TotalMilliseconds()

    while(true)
        current_time = timer.TotalMilliseconds()
        if (current_time - start_time > 1000/30)
            screen.Clear(&h000000ff)
            ' Rotation
            m.axis_rotation.x = m.axis_rotation.x + 0.001
            m.axis_rotation.y = m.axis_rotation.y + 0.001
            m.axis_rotation.z = m.axis_rotation.z + 0.001
            ' 3D translation simulated
            projected = project_pts(m.points)

            ' Top face
            screen.drawLine(projected[0].x, projected[0].y, projected[1].x, projected[1].y, line_color)
            screen.drawLine(projected[1].x, projected[1].y, projected[2].x, projected[2].y, line_color)
            screen.drawLine(projected[2].x, projected[2].y, projected[3].x, projected[3].y, line_color)
            screen.drawLine(projected[3].x, projected[3].y, projected[0].x, projected[0].y, line_color)
            ' Bottom face
            screen.drawLine(projected[4].x, projected[4].y, projected[5].x, projected[5].y, line_color)
            screen.drawLine(projected[5].x, projected[5].y, projected[6].x, projected[6].y, line_color)
            screen.drawLine(projected[6].x, projected[6].y, projected[7].x, projected[7].y, line_color)
            screen.drawLine(projected[7].x, projected[7].y, projected[4].x, projected[4].y, line_color)
            ' Vertices
            screen.drawLine(projected[0].x, projected[0].y, projected[4].x, projected[4].y, line_color)
            screen.drawLine(projected[1].x, projected[1].y, projected[5].x, projected[5].y, line_color)
            screen.drawLine(projected[2].x, projected[2].y, projected[6].x, projected[6].y, line_color)
            screen.drawLine(projected[3].x, projected[3].y, projected[7].x, projected[7].y, line_color)
            ' Vertex
            for i=0 to projected.count()-1 step 1
                screen.DrawPoint(projected[i].x, projected[i].y, 4, vertex_color)
            end for

            screen.finish()
        end if
    end while

End Function

'''''''''''''''''''''''''''''''''''''''''''''''''''
' 3D translation simulated
function project_pts(points as Object) as Object

    projected = []

    sin_x = sin(m.axis_rotation.x)
    cos_x = cos(m.axis_rotation.x)
    sin_y = sin(m.axis_rotation.y)
    cos_y = cos(m.axis_rotation.y)
    sin_z = sin(m.axis_rotation.z)
    cos_z = cos(m.axis_rotation.z)

    for i=0 to m.points.count()-1 step 1
        x = m.points[i].x
        y = m.points[i].y
        z = m.points[i].z

        ' rotate about x axis
        xy = cos_x*y - sin_x*z
        xz = sin_x*y + cos_x*z
        ' rotate about y axis
        yz = cos_y*xz - sin_y*x
        yx = sin_y*xz + cos_y*x
        ' rotate about z axis
        zx = cos_z*yx - sin_z*xy
        zy = sin_z*yx + cos_z*xy

        scale = m.focal_length/(m.focal_length+yz-m.camera.z)
        x = zx*scale - m.camera.x         'get x position in the view of camera
        y = zy*scale - m.camera.y         'get y position in the view of camera

        projected[i] = vertex3d(x+m.scene.x, y+m.scene.y, yz, scale)
    end for

    return projected

end function

'''''''''''''''''''''''''''''''''''''''''''''''''''
' Return a vector point
function vertex3d(x as Integer, y as Integer, z as Integer, scale = 1 as Integer) as Object

    return { x:x, y:y, z:z, scale:scale }

end function

And a screen shot:

-- Regards

posted on 2016-07-16 04:28  yangzhou1030  阅读(377)  评论(0编辑  收藏  举报

导航