Author Topic: ASCII Cube  (Read 3726 times)

0 Members and 1 Guest are viewing this topic.

Offline Hezad

  • Sponsor
  • Pentium
  • *******
  • Posts: 613
  • Karma: 44
  • I believe .. in Patrick.
    • View Profile
    • Hezad.com Web hosting
ASCII Cube
« on: December 27, 2008 »
Heya :) There were several posts about ascii these days so I wanted to play with it !

No lib needed, just run the code :D

Code: [Select]
Randomize timer


Const _255 = 1/255

Const MAX_Z = 50
ReDim shared as single ZBuffer(79*25)

#Define Clear_ZBuffer Redim as single ZBuffer(79*25)

screenres 320,240,8

Palette 0, 10,10,10
Palette 1, 50,38,30
Palette 2, 70,50,50
Palette 3, 90,80,70
Palette 4, 110,100,80
Palette 5, 130,120,90
Palette 6, 150,145,100
Palette 7, 170,160,120
Palette 8, 190,170,130
Palette 9, 210,200,150
Palette 10, 235,215,165
Palette 11, 255,255,255

Dim shared as integer Flash, CanFlash


Sub ASCII(x as integer, y as integer, gradval as integer)
   
    if flash then
        Color gradval,Gradval
    else
        Color gradval,0
    end if
   
    locate y,x : Print str(int(rnd*2))'"#"'Gradient(GradVal);
   
end sub

Sub Flash_It()
   
    static t as single
   
    if CanFlash = 1 then

        Flash = 1
        t = timer
        CanFlash = 0
       
    else
       
        if (timer-t)>1 then Flash = 0
       
    end if
   
    If rnd<.05 and Flash = 0 then CanFlash = 1
   
end sub
   
'' cube

type p3d
    x as single
    y as single
    z as single
end type

dim shared as p3d p(7),pos2d(7)

p(0) = type(-5,-5,-5)
p(1) = type(5,-5,-5)
p(2) = type(5,5,-5)
p(3) = type(-5,5,-5)
p(4) = type(-5,-5,5)
p(5) = type(5,-5,5)
p(6) = type(5,5,5)
p(7) = type(-5,5,5)


#Macro Z_Buffer_Line(x1,x2,y,_z1,_z2)

    static as single itx1,itx2
    static as single xDiv,Cur_Z,_zz1,_zz2,d_Z
   
    static as integer ptr izval,izbuf
   
    izbuf = Cast(integer ptr,@Zbuffer(0))
   
    if x1<x2 then
        _zz1 = _z1
        _zz2 = _z2
       
        itx1 = x1
        itx2 = x2
       
    else
        _zz1 = _z2
        _zz2 = _z1
       
        itx1 = x2
        itx2 = x1

    end if
   
    xDiv = 1/(itx2 - itx1)
   
    d_Z = xDiv * (_zz2 - _zz1)
   
    Cur_Z = _zz1
   
    static as integer it
    static as integer zc
           
    it = y*78 + itx1
   
    for i as integer = itx1 to itx2-1
       
        if i<78 and i>=0 and y>=0 and y<32 then
       
            izval = Cast(integer ptr,@Cur_Z)
           
            If *izval > izbuf[it] then
               
                ZBuffer(it) = Cur_Z
           
                zc = 800*(Cur_Z)-50
                if zc>255 then zc = 255
               
                Zc = Zc*10*_255
               
                If Zc>10 then Zc = 10
               
                ASCII i,y,zc
               
            end if
       
        end if
       
        it+=1
           
        Cur_Z += d_Z
           
    Next
   
#endmacro

WindowTitle "ASCII Cube"

Sub Render_Opt_Z_Buffer(ByVal p0 as p3d, ByVal p1 as p3d, ByVal p2 as p3d)
   
    '' Sorting Y values
    if p1.y < p0.y then
        swap p1,p0
    end if
    if p2.y < p0.y then
        swap p2,p0
    end if
    if p2.y < p1.y then
        swap p2,p1
    end if
   
    static as single dx1,dy1,dx2,dy2,dx3,dy3
    static as single Slope1,Slope2,Slope3, dYDiv
   
    static as single d_z1,d_z2,d_z3,_z1,_z2,_z3
    static as single _zSlope1,_zSlope2,_zSlope3
   
    _z1 = 1/p0.z
    _z2 = 1/p1.z
    _z3 = 1/p2.z
   
    '' interpolate 0 to 1
   
    d_z1 = _z2 - _z1
   
    dX1 = p1.x - p0.x
    dY1 = p1.y - p0.y
   
    if dY1 then
        dYDiv = 1/dY1
       
        _zSlope1 = d_Z1 * dYDiv
       
        Slope1 = dX1 * dYDiv
    else
        _zSlope1 = 0
       
        Slope1 = 0
    End if
   
   
    '' interpolate 1 to 2
   
    d_Z2 = _z3 - _z2
   
    dX2 = p2.x - p1.x
    dY2 = p2.y - p1.y

    if dY2 then
        dYDiv = 1/dY2
       
        _zSlope2 = d_Z2 * dYDiv
       
        Slope2 = dX2 * dYDiv
    else
        _zSlope2 = 0
       
        Slope2 = 0
    End if
   
   
    '' interpolate 0 to 2
   
    d_Z3 = _z1 - _z3
   
    dX3 = p0.x - p2.x
    dY3 = p0.y - p2.y
   

    if dY3 then
        dYDiv = 1/dY3
       
        _zSlope3 = d_Z3 * dYDiv
       
        Slope3 = dX3 * dYDiv       
    else
        _zSlope3 = 0
       
        Slope3 = 0
    End if
   
   
    '' drawing
   
    static as single CurX1, CurX2
   
    CurX1=p0.x
   
    CurX2=CurX1
   
    static as single Cur_Z1, Cur_Z2
   
    Cur_Z1 = _z1
    Cur_Z2 = _z1
   
    For y as integer = p0.y to p1.y-1
       
        Z_Buffer_Line(CurX1,CurX2,y,Cur_Z1,Cur_Z2)
       
        Cur_Z1 += _zSlope1
        Cur_Z2 += _zSlope3
       
        CurX1 += Slope1
        CurX2 += Slope3
       
    Next
   
    '' down
   
    Cur_Z1 = _z2
   
    CurX1 = p1.x

    For y as integer = p1.y to p2.y-1
       
        Z_Buffer_Line(CurX1,CurX2,y,Cur_Z1,Cur_Z2)
       
        Cur_Z1 += _zSlope2
        Cur_Z2 += _zSlope3
       
        CurX1 += Slope2
        CurX2 += Slope3
       
    Next

end Sub

Sub ASCIICube()
   
    static as single zDiv,cx,cy,cz,sx,sy,sz,tx=0.03,ty=0.05,tz=-0.01'.007
    static as single tmpx,tmpy,tmpz
   
    cX = cos(Tx) : sX = sin(Tx)
    cY = cos(Ty) : sY = sin(Ty)
    cZ = cos(Tz) : sZ = sin(Tz)
   
    '' rotation
    for i as integer = 0 to 7
       
        TmpY = p(i).y * cx - p(i).z * sx
        TmpZ = p(i).z * cx + p(i).y * sx
        p(i).y = TmpY
        p(i).z = TmpZ
           
        TmpZ = p(i).z * cy - p(i).x * sy
        TmpX = p(i).x * cy + p(i).z * sy
        p(i).x = TmpX
           
        TmpX = p(i).x * cz - p(i).y * sz
        TmpY = p(i).y * cz + p(i).x * sz
           
        p(i).x = TmpX
        p(i).y = TmpY
        p(i).z = TmpZ
    next
   
    '' projection
   
    for i as integer = 0 to 7
       
        if p(i).z+10>1 and p(i).z+10<50 then
            zDiv = 1/(p(i).z+50)
           
            pos2d(i).x = 22+p(i).x * zDiv * 70
            pos2d(i).y = 15+p(i).y * zDiv * 70
            pos2d(i).z = p(i).z+10
        end if
    next
   
    '' rendering
   
    Render_Opt_Z_Buffer(pos2d(0), pos2d(2), pos2d(1))
    Render_Opt_Z_Buffer(pos2d(0), pos2d(2), pos2d(3))
   
    Render_Opt_Z_Buffer(pos2d(1), pos2d(4), pos2d(0))
    Render_Opt_Z_Buffer(pos2d(1), pos2d(4), pos2d(5))
   
    Render_Opt_Z_Buffer(pos2d(2), pos2d(5), pos2d(1))
    Render_Opt_Z_Buffer(pos2d(2), pos2d(5), pos2d(6))
   
    Render_Opt_Z_Buffer(pos2d(3), pos2d(4), pos2d(0))
    Render_Opt_Z_Buffer(pos2d(3), pos2d(4), pos2d(7))
   
    Render_Opt_Z_Buffer(pos2d(4), pos2d(6), pos2d(5))
    Render_Opt_Z_Buffer(pos2d(4), pos2d(6), pos2d(7))
   
    Render_Opt_Z_Buffer(pos2d(2), pos2d(7), pos2d(3))
    Render_Opt_Z_Buffer(pos2d(2), pos2d(7), pos2d(6))
   
   
end sub










'' ************** MAIN ****************

Do : cls
    Clear_ZBuffer
   
    Flash_It
   
    ASCIICube
   
    if flash then
        color ,1
    else
        color ,0
    end if
   
    sleep 50
Loop until multikey(&h01)



Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: ASCII Cube
« Reply #1 on: December 27, 2008 »
@Hezad:
Is it possible that you add an exe please? Would be cool.
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline bikerboy

  • Amiga 1200
  • ****
  • Posts: 349
  • Karma: 12
    • View Profile
Re: ASCII Cube
« Reply #2 on: December 27, 2008 »
nice one man  :D , is it meant to change from  1's & 0's to solid?


Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: ASCII Cube
« Reply #3 on: December 27, 2008 »
Quote
There were several posts about ascii these days so I wanted to play with it !
Nice one, Hezad!
But true ANSI would be limited to the default 16 colours in textmode.
It's possible to pick fore- & background-colour independently, though.
The trick is to select a character which is representative for the "gradient" at the current pixel.
The way you pick these characters strongly affects the style of your image.
That's why it's considered as art:

« Last Edit: December 27, 2008 by hellfire »
Challenge Trophies Won:

Offline bikerboy

  • Amiga 1200
  • ****
  • Posts: 349
  • Karma: 12
    • View Profile
Re: ASCII Cube
« Reply #4 on: December 27, 2008 »
messed the pallete a bit ::) :P

Code: [Select]
Palette 0, 10,10,10
Palette 1, 50,38,30
Palette 2, 90,90,90
Palette 3, 230,230,230
Palette 4, 110,100,80
Palette 5, 150,150,150
Palette 6, 110,110,110
Palette 7, 30,30,30
Palette 8, 140,140,140
Palette 9, 210,200,150
Palette 10, 235,215,165
Palette 11, 215,215,215



Offline Hezad

  • Sponsor
  • Pentium
  • *******
  • Posts: 613
  • Karma: 44
  • I believe .. in Patrick.
    • View Profile
    • Hezad.com Web hosting
Re: ASCII Cube
« Reply #5 on: December 27, 2008 »
Thanks  :D


Quote
is it meant to change from  1's & 0's to solid?

yep ;D

hellfire > ok ok, i faked a bit then ^^