Drawing (?) a triangle

Share your advanced PureBasic knowledge/code with the community.
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Drawing (?) a triangle

Post by Dr. Dri »

Code updated For 5.20+

After a talk on french forums i wrote this

Code: Select all

;     Procedure IsDrawing()
;       !extrn _PB_2DDrawing_CurrentDC
;       !MOV eax, dword [_PB_2DDrawing_CurrentDC]
;       ProcedureReturn
;     EndProcedure

Procedure Triangle(hdc,x1.l, y1.l, x2.l, y2.l, x3.l, y3.l)
  ProcedureReturn Polygon_(hdc, @x1, 3)
EndProcedure

OpenWindow(0, 0, 0, 320, 240, "Triangle", #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
Repeat
  hdc = StartDrawing( WindowOutput(0) )
  If hdc
    FrontColor(RGB(255, 0, 0))
    Triangle(hdc,10, 10, 10, 100, 100, 10)
    ;     Polygon_(hdc, @x1, 3)
    
    StopDrawing()
  EndIf
Until WaitWindowEvent() = #PB_Event_CloseWindow
Dri ;)
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Post by Dr. Dri »

it's fast, isnt it ? :lol: :lol:

Code: Select all

OpenWindow(0, 0, 0, 320, 240, #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered, "Triangle")
Repeat
  If StartDrawing( WindowOutput() )
    FrontColor(Random(255), Random(255), Random(255))
    Triangle(Random(320), Random(240), Random(320), Random(240), Random(320), Random(240))
    StopDrawing()
  EndIf
Until WindowEvent() = #PB_Event_CloseWindow
Dri ;)
Blade
Enthusiast
Enthusiast
Posts: 362
Joined: Wed Aug 06, 2003 2:49 pm
Location: Venice - Italy, Japan when possible.
Contact:

Post by Blade »

Simple and smart. Thanks :)
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

nice done.
bye,
Daniel
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Thanx :D
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Post by Dr. Dri »

enjoy :)

Code: Select all

Procedure IsDrawing()
  !extrn _PB_2DDrawing_CurrentDC
  !MOV eax, dword [_PB_2DDrawing_CurrentDC]
  ProcedureReturn
EndProcedure

Procedure Triangle(x1.l, y1.l, x2.l, y2.l, x3.l, y3.l)
  ProcedureReturn Polygon_(IsDrawing(), @x1, 3)
EndProcedure

Structure Point3D
  x.f
  y.f
  z.f
EndStructure

Structure Face3D
  points.Long[3]
  color.l
EndStructure

Structure Object3D
  ;position
  x.f
  y.f
  z.f
  ;orientation
  ax.f
  ay.f
  az.f
  ;proportion
  sx.f
  sy.f
  sz.f
  nPoints.l
  *points.Point3D
  nFaces.l
  *faces.Face3D
EndStructure

DataSection
  cube_points:
  Data.f -0.5, -0.5, -0.5
  Data.f -0.5, -0.5,  0.5
  Data.f -0.5,  0.5, -0.5
  Data.f -0.5,  0.5,  0.5
  Data.f  0.5, -0.5, -0.5
  Data.f  0.5, -0.5,  0.5
  Data.f  0.5,  0.5, -0.5
  Data.f  0.5,  0.5,  0.5
  cube_faces:
  Data.l 1, 3, 7, $0000FF
  Data.l 1, 7, 5, $0000FF
  Data.l 0, 4, 6, $00FF00
  Data.l 0, 6, 2, $00FF00
  Data.l 2, 6, 7, $FF0000
  Data.l 2, 7, 3, $FF0000
  Data.l 0, 1, 5, $00FFFF
  Data.l 0, 5, 4, $00FFFF
  Data.l 0, 2, 3, $FF00FF
  Data.l 0, 3, 1, $FF00FF
  Data.l 4, 5, 7, $FFFF00
  Data.l 4, 7, 6, $FFFF00
EndDataSection

Procedure Face3D_Render(*o.Object3D, face.l, cax.f, sax.f, cay.f, say.f, caz.f, saz.f)
  Protected x1.l, y1.l, x2.l, y2.l, x3.l, y3.l, *p.Point, i.l, color.l, p.l, *p3D.Point3D
  Protected x.f, y.f, z.f, tx.f, ty.f, tz.f, l.l, h.l, distance.f, visible.l, *f3D.Face3D
  
  l = DesktopWidth (0) / 2
  h = DesktopHeight(0) / 2
  distance = l * 1.15470052
  
  *p   = @x1
  *f3D = *o\faces + face * SizeOf(Face3D)
  While i < 3
    p    = *f3D\points[i]\l
    *p3D = *o\points + p * SizeOf(Point3D)
    
    x = *p3D\x * *o\sx
    y = *p3D\y * *o\sy
    z = *p3D\z * *o\sz
    
    ;rotation autour de l'axe x
    ty = y : tz = z
    y = cax * ty - sax * tz
    z = sax * ty + cax * tz
    
    ;rotation autour de l'axe y
    tx = x : tz = z
    x = say * tz + cay * tx
    z = cay * tz - say * tx
    
    ;rotation autour de l'axe z
    tx = x : ty = y
    x = caz * tx - saz * ty
    y = saz * tx + caz * ty
    
    x + *o\x
    y + *o\y
    z + *o\z
    
    *p\x = l + distance * x / z
    *p\y = h - distance * y / z
    
    i  + 1
    *p + SizeOf(Point)
  Wend
  
  visible = (x1 * y2) - (y1 * x2) + (x2 * y3) - (y2 * x3) + (x3 * y1) - (y3 * x1)
  If visible < 0
    color = *f3D\color
    FrontColor(Red(color), Green(color), Blue(color))
    Triangle(x1, y1, x2, y2, x3, y3)
    FrontColor(0, 0, 0)
  EndIf
EndProcedure

Procedure Object3D_Render(*o.Object3D)
  Protected ax.f, ay.f, az.f, i.l
  Protected cax.f, sax.f, cay.f, say.f, caz.f, saz.f
  
  ax = *o\ax * 0.01745329
  ay = *o\ay * 0.01745329
  az = *o\az * 0.01745329
  
  cax = Cos(ax) : sax = Sin(ax)
  cay = Cos(ay) : say = Sin(ay)
  caz = Cos(az) : saz = Sin(az)
  
  While i < *o\nFaces
    Face3D_Render(*o, i, cax, sax, cay, say, caz, saz)
    i + 1
  Wend
EndProcedure

Rectangle.Object3D
Rectangle\z       = 50.0
Rectangle\sx      = 20.0
Rectangle\sy      = 10.0
Rectangle\sz      =  5.0
Rectangle\nPoints =  8
Rectangle\points  = ?cube_points
Rectangle\nFaces  = 12
Rectangle\faces   = ?cube_faces

InitSprite()
InitKeyboard()

ExamineDesktops()
OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), 0, "")
OpenScreen(DesktopWidth(0), DesktopHeight(0), DesktopDepth(0), "")

Repeat
  ClearScreen(0, 0, 0)
  ExamineKeyboard()
  
  If StartDrawing( ScreenOutput() )
    Object3D_Render(Rectangle)
    StopDrawing()
  EndIf
  
  Rectangle\ax + 1
  Rectangle\ay + 2
  Rectangle\az + 3
  If Rectangle\ax > 360 : Rectangle\ax - 360 : EndIf
  If Rectangle\ay > 360 : Rectangle\ay - 360 : EndIf
  If Rectangle\az > 360 : Rectangle\az - 360 : EndIf
  
  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
Dri ;)
benny
Enthusiast
Enthusiast
Posts: 465
Joined: Fri Apr 25, 2003 7:44 pm
Location: end of www
Contact:

Post by benny »

:shock: coooool :!:
regards,
benny!
-
pe0ple ar3 str4nge!!!
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Fabulous, Dr. Dri! Thanks! :D
BERESHEIT
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Post by va!n »

@Dr. Dri
Wow!!!! Thats very impressive!!! Very cool OldSkool feeling! 8)
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Post by Dr. Dri »

you like it ?
i'll try to make a screensaver with sevral objects if i have time enough ^_^

Dri ;)
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

Pretty cool! 8)
Good programmers don't comment their code. It was hard to write, should be hard to read.
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post by Froggerprogger »

Atm I used to draw polygons line by line!
This one makes it at least 70 trillions times faster!
Thanx! :D

This would be a powerful native-PB-command!
%1>>1+1*1/1-1!1|1&1<<$1=1
Post Reply