Author Topic: Fake Phong Shading  (Read 4193 times)

0 Members and 1 Guest are viewing this topic.

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Fake Phong Shading
« on: September 26, 2007 »
This is a very fast fake phong shaded circle drawing routine that can handle ovals and even cones (move the highlight to a point outside of the circle to produce cones) use it freely

Code: [Select]
# FAKE PHONG SHADED CIRCLES
open window 640, 512
circles = 255

sub pcircle(a,b,c,d,g,h)
  doubpi = pi*2
  x = a+c/g
  y = b+d/h
  f = pi/30
  for e = f to doubpi step f
    gtriangle x,y to a+cos(e)*c,b+sin(e)*d to a+cos(e+f)*c,b+sin(e+f)*d
  next
end sub

label loop
  setdispbuf draw
  draw = 1 - draw
  setdrawbuf draw
  for c = 0 to circles
    centerx = ran(640)
    centery = ran(512)
    radiusx = ran(50) + 25
    radiusy = ran(50) + 25
    r = ran(255)
    g = ran(255)
    b = ran(255)
    l = ran(0.6)
    setrgb 1, r, g, b
    setrgb 2, l*r, l*g, l*b
    setrgb 3, l*r, l*g, l*b
    pcircle(centerx,centery, radiusx,radiusy, 3,-3)
  next
  goto loop

Here is the same routine but with an extra parameter that allows you to rotate the ovals
Code: [Select]
sub pcircle(a,b, c,d, g,h, r)
  doubpi = pi*2
  x = a + (c/g) * cos(r)
  y = b + (d/h) * sin(r)
  f = pi/30
  for e = f+r to doubpi+r step f
    gtriangle x,y to a+cos(e)*c,b+sin(e)*d to a+cos(e+f)*c,b+sin(e+f)*d
  next
end sub

Challenge Trophies Won:

Offline Clanky

  • Laser Guided Memories
  • Amiga 1200
  • ****
  • Posts: 340
  • Karma: 16
  • kiss that sound that pounds your senses
    • View Profile
Re: Fake Phong Shading
« Reply #1 on: September 28, 2007 »
Very nice Rain!
He tilts, and his eyes are focused on the ground far below.. Wind? Angels? Men..

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17412
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Fake Phong Shading
« Reply #2 on: September 29, 2007 »
Yep, that's an accepted technique that lots and lots of yabasic programmers have used to draw shaded circles, the first person to use them in this way (as far as I know) was Xalthorn.

I used them myself in lots of demos, would be nice to see you do some real effect out of them :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Fake Phong Shading
« Reply #3 on: September 29, 2007 »
Aha, found it.

http://members.iinet.net.au/~jimshaw/Yabasic/yabres/demos/rafryer/balldemo.txt


EDIT: OMG, I've just looked at that code, the lookuptables for sin and cos (and all the other arrays) are dimmed inside the sub at the end of the program but are used earlier in the main code!!! I remember yabasic having some crazy behaviour but that's going too far.
« Last Edit: September 29, 2007 by Stonemonkey »

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17412
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Fake Phong Shading
« Reply #4 on: September 29, 2007 »
Hehe, like the longer the listing the slower it ran...

It certainly had some strange quirks!
Shockwave ^ Codigos
Challenge Trophies Won: