Author Topic: Algorithm bug (ez)[BMAX]  (Read 420 times)

0 Members and 1 Guest are viewing this topic.

Offline Roly

  • Amiga 1200
  • ****
  • Posts: 390
  • Karma: 7
    • View Profile
Algorithm bug (ez)[BMAX]
« on: May 17, 2006 »


Original post from zparticle, taken from the ezboard forum


I'm try to adapt this code to bmax from java. I shows a bar twisting back and forth. I've almost got it but the polygons in the back ground look like they are getting blended with the polygons in the foreground and I can't figure out why. any help you can give would be nice.
Code: [Select]
[quote]' this code adapted from one of ETROMIC's (Olof HÃ¥nell) effects
Strict

Framework BRL.GlMax2D

Import BRL.System
Import BRL.Basic
Import BRL.pngloader
Import BRL.Retro

?Win32
SetGraphicsDriver(GLMax2DDriver())
?
Graphics 640,480,0'32,60

Const NUM_DEGREES:Int = 20
Const NUM_SECTIONS:Int = 45
Const NUM_POINTS:Int = 5
Const ROTATION_SCALE:Int = 2
'Global baseColor:Int = 2895943
Global baseColor:Int = 13412915
Const bgColor:Int = 2895943
Global cylinderWidth:Int = 150
Global XZ_SCALE:Int = cylinderWidth Shr 1
Global circle_y_step:Int = GraphicsHeight()/NUM_SECTIONS
Global variation_speed:Float = 0.00021
Global cycledifference:Float = 0.022
Global fm:Float = 0.0
Global CP:CirclePlane[] = New CirclePlane[NUM_SECTIONS]
setupSections()

Global frames:Long = 0
Global fps:Long = 0
Global lastTime:Long = MilliSecs()

SetClsColor(44,48,71)

While Not KeyHit(KEY_ESCAPE)
Cls ' clear the screen
update()
DoFPS()
Flip() ' flip the buffers
FlushMem() ' garbage collection
Wend

Type Point
Field x:Float
Field y:Float
'Field z:Float

Method Set(x1:Float,y1:Float)',z1:Float)
x=x1
y=y1
'z=z1
End Method
End Type

Type Polygon
Field points:Point[] = New Point[4]
Field c:Color = New Color
Field array:Float[] = New Float[6]

Function Create:Polygon()
Local p:Polygon= New Polygon
For Local c:Int = 0 To 3
p.points[c] = New Point
Next
Return p
End Function

Method firstPoints()
array[0] = points[0] .x
array[1] = points[0] .y
array[2] = points[1].x
array[3] = points[1].y
array[4] = points[2].x
array[5] = points[2].y
End Method

Method secondPoints()
array[0] = points[2].x
array[1] = points[2].y
array[2] = points[3].x
array[3] = points[3].y
array[4] = points[0] .x
array[5] = points[0] .y
End Method

End Type

Type Color
Field r:Int=255
Field g:Int=255
Field b:Int=255

Method Set(r1:Float,g1:Float,b1:Float)
r=r1
g=g1
b=b1
End Method
End Type

Function rotPoints()
Local diff:Float = 0
For Local i:Int=0 Until NUM_SECTIONS
'CP[i].rotateBy(Float(Float(ROTATION_SCALE)*Sin((fm+diff)*0.0174532925)))
CP[i].rotateBy(Float(Float(ROTATION_SCALE)*Sin(fm+diff)))
diff:+cycledifference
fm:+variation_speed
Next
End Function

Function update()
rotPoints()

Local yStep:Float = Float(GraphicsHeight()) / Float(NUM_SECTIONS-1)
Local yp:Float = 0
Local xPush:Int = (GraphicsWidth() Shr 1) - (XZ_SCALE Shr 1)

Local pts:Polygon[] = New Polygon[(NUM_SECTIONS-1) * (NUM_POINTS)]
Local ptsZ:Int[] = New Int[pts.length]
Local colors:Color[] = New Color[pts.length]
Local order:Int[] = New Int[pts.length]

Local n:Int = 0;
Local p:Polygon;
For Local i:Int=0 Until NUM_SECTIONS-1
For Local j:Int=0 Until NUM_POINTS
p = Polygon.Create();
Local p0:Float[] = CP[i].GetPoint(j)
Local p1:Float[] = CP[i].GetPoint(j+1)
Local p2:Float[] = CP[i+1].GetPoint(j+1)
Local p3:Float[] = CP[i+1].GetPoint(j)
p.points[0] .Set(Int(p0[0] + xPush +.5), Int(yp+.5))
p.points[1].Set(Int(p1[0] + xPush +.5), Int(yp+.5))
p.points[2].Set(Int(p2[0] + xPush +.5), Int(yp+yStep+.5))
p.points[3].Set(Int(p3[0] + xPush +.5), Int(yp+yStep+.5))

pts[n] = p;
ptsZ[n] = Int(p0[1] + p1[1] + p2[1] + p3[1]) Shr 2

Local c:Int = Int( ptsZ[n] - XZ_SCALE )
Local base_r:Int = ((baseColor Shr 16) & 255) + c
Local base_g:Int = ((baseColor Shr 8) & 255) + c
Local base_b:Int = ((baseColor ) & 255) + c

If base_r>255 Then base_r = 255
If base_g>255 Then base_g = 255
If base_b>255 Then base_b = 255

colors[n] = New Color
colors[n].Set(base_r,base_g,base_b)
n:+1
Next
yp:+yStep
Next
order = zSort(ptsZ)

SetBlend(SOLIDBLEND)
SetColor(255,255,255)
SetAlpha(1)

Local nu:Int = order.length
For Local i:Int=0 Until nu

SetColor(colors[order[i]].r,colors[order[i]].g,colors[order[i]].b);

pts[order[i]].firstPoints()
DrawPoly(pts[order[i]].array);

pts[order[i]].secondPoints()
DrawPoly(pts[order[i]].array);
Next
End Function

Function zSort:Int[](ptsZ:Int[])
Local sorted:Byte = False
Local num:Int = ptsZ.length - 1
Local back:Int[] = New Int[ptsZ.length]
For Local i:Int=0 Until back.length
back[i] = i
Next
' Rem
While sorted=False
sorted = True
For Local i:Int=0 Until num
If ptsZ[i] > ptsZ[i+1] Then
Local backTemp:Int = back[i]
Local tmp:Int = ptsZ[i]
back[i] = back[i+1]
ptsZ[i] = ptsZ[i+1]
back[i+1] = backTemp
ptsZ[i+1] = tmp
sorted = False
EndIf
Next
Wend
'EndRem
Return back
End Function

Function DoFPS()
SetBlend(SOLIDBLEND)
SetColor(255,255,255)
SetAlpha(1)
DrawText "FPS: "+fps,0,0
frames:+1
If MilliSecs()-lastTime>=1000 Then
fps=frames
frames=0
lastTime=MilliSecs()
EndIf
End Function

Function setupSections()
Local sAngle:Int = 0
Local aStep:Int = Int(Float(NUM_DEGREES) / Float(NUM_SECTIONS))
For Local i:Int=0 Until NUM_SECTIONS
CP[i] = CirclePlane.Create(NUM_POINTS, XZ_SCALE, sAngle)
sAngle:+aStep
Next
End Function

Type CirclePlane
Field numPoints:Int = 0
Field rotation:Float = 0
Field degreeStep:D ouble = 0
Field ps:Float[,]
Field psb:Float[,]
Field scale:Int = 0
Const X:Int = 0
Const Z:Int = 1

Function Create:CirclePlane(pointCount:Int, scale:Int, sRotation:Int)
Local cp:CirclePlane = New CirclePlane
cp.numPoints = pointCount
cp.rotation = Float(sRotation)
cp.scale = scale
cp.degreeStep = Double(360) / Double(pointCount)
cp.ps = New Float[pointCount,2]
cp.rot()
Return cp
End Function

Method rotateBy(deg:Float)
rotation :+ deg
rot()
End Method

Method GetPoint:Float[](which:Int)
Local back:Float[] = New Float[2]
back[0] = ps[which Mod numPoints,0]
back[1] = ps[which Mod numPoints,1]
Return back
End Method

Method rot()
Local ang:D ouble = 0
For Local i:Int=0 Until numPoints
ps[i,X] = Int(Float(scale) * Cos(rotation + ang) + .5)
ps[i,Z] = Int(Float(scale) * Cos(rotation + ang - (3.1416/2.0)) + Float(scale)+.5)
ang:+degreeStep
Next
End Method
End Type[/quote]

Edited by: zparticle at: 17/10/05 22:03
zawran
Zawran / TTD

Posts: 2114
(18/10/05 5:34)
Reply | Edit | Del
   New Post Re: Algorithm bug I will take a look at it tonight when I get home from work. Do you have a link to the original java source ?

zac.interactive | Blog | Tutorials | Demos | Tools
zparticle
AMSTRAD CPC

Posts: 77
(18/10/05 15:46)
Reply | Edit | Del    New Post Re: Algorithm bug www.scottshaver2000.com/b...tromic.bmx (my version)

www.scottshaver2000.com/b...ect02.java (java version)

www.scottshaver2000.com/b...cialfx.jar ( java program using it, java -jar specialfx.jar)

Edited by: zparticle at: 18/10/05 15:48
zawran
Zawran / TTD

Posts: 2115
(18/10/05 16:18)
Reply | Edit | Del
   New Post Re: Algorithm bug Not really sure whats wrong here. It must be the setBlend command that doesn't work right as the polys gets blended. Or the setAlpha. Perhaps someone else have a similar problem. I will do a seach on the official forum and see if something comes up.

Does that guy have a website with these effects shown?

zac.interactive | Blog | Tutorials | Demos | Tools
zparticle
AMSTRAD CPC

Posts: 78
(18/10/05 16:23)
Reply | Edit | Del    New Post Re: Algorithm bug it's on my site he added some fx to a program I wrote: www.scottshaver2000.com/t...=specialfx


here is what it originally looked like

www.scottshaver2000.com/b...omic02.png

zawran
Zawran / TTD

Posts: 2116
(18/10/05 16:31)
Reply | Edit | Del
   New Post Re: Algorithm bug Ah yes, now I know what the effect is supposed to look like :) I did one of these, just textured a while back. Pure pixel pushing stuff though, not polygons.

I don't have much experience using the bmax 2d commands as I have so far kept to pure openGL stuff. I will experiment a bit and see if I can't figure out whats going wrong.

zac.interactive | Blog | Tutorials | Demos | Tools
zparticle
AMSTRAD CPC

Posts: 79
(18/10/05 16:48)
Reply | Edit | Del    New Post Re: Algorithm bug I'm wondering if it loooks odd because I have to use 2 trianlge for each section where he was able to use a 4 sided polygon for each one. Also I'm thinking I might have the z sort screwed up. I can't find anything wrong with the blend mode. very frustrating. :)

zparticle
AMSTRAD CPC

Posts: 80
(18/10/05 16:54)
Reply | Edit | Del    New Post Re: Algorithm bug another possibility is that I've got it building the polygons around more than once, more the 360 degrees so that they overlap. I don't see it though.

zparticle
AMSTRAD CPC

Posts: 81
(18/10/05 17:09)
Reply | Edit | Del    New Post Re: Algorithm bug definately not a blending problem I can draw other polygon behind it and not see them.

zawran
Zawran / TTD

Posts: 2117
(18/10/05 17:14)
Reply | Edit | Del
   New Post Re: Algorithm bug Could be something with the z ordering as you were suggesting. Looks like its drawing the polys that is supposed to be hidden.

zac.interactive | Blog | Tutorials | Demos | Tools
zparticle
AMSTRAD CPC

Posts: 82
(18/10/05 17:16)
Reply | Edit | Del    New Post Re: Algorithm bug I'm over in the chat room if you would care to join.

zawran
Zawran / TTD

Posts: 2118
(18/10/05 17:28)
Reply | Edit | Del
   New Post Re: Algorithm bug Its a java based chat client, so it won't work on this computer. I don't have any java vm installed or anything. My internet connection is payed by my employer, and they don't want us to install stuff like that onto computer connected through their equipment. On the plus side, I get the 2mbit connection for free.

zac.interactive | Blog | Tutorials | Demos | Tools
zparticle
AMSTRAD CPC

Posts: 83
(18/10/05 17:34)
Reply | Edit | Del    New Post Re: Algorithm bug cool , I looked a bit closer. removed the sorting and then watching it run I think I may be missing a side. That would cause the strange visual that is happening

zawran
Zawran / TTD

Posts: 2119
(18/10/05 17:44)
Reply | Edit | Del
   New Post Re: Algorithm bug For what its doing, the code looks a bit overly complex I think. I am almost certain that it could be done much simpler. At least, I am having problems figuring out what different parts of the code does.

I might try and re-create the effect in a different way if I get time during the week.

A bit off topic, I saw that you are working on a RPG game. I have almost always wanted to make one, but just never got anywhere as its a huge thing to create. How are progress on it? Looks like its based on the D20 game rules.

zac.interactive | Blog | Tutorials | Demos | Tools
zparticle
AMSTRAD CPC

Posts: 84
(18/10/05 17:53)
Reply | Edit | Del    New Post Re: Algorithm bug I haven't done anything on it in a week or two. I've got all of the character creation, equipment/inventory, classes, races, help system, money system, dice system (D20) done. I'm going to use my SAS Map Editor for the maps, so that is done. I've been playing with LUA for the scripting engine. I'm hoping I'll get motivated to get a basic module up and running so you can enter it and walk around soon. You'll be able to create you own modules when it is finished.

Edited by: zparticle at: 18/10/05 17:54
zparticle
AMSTRAD CPC

Posts: 85
(18/10/05 20:51)
Reply | Edit | Del    New Post Re: Algorithm bug Well I thought I had it figured out, for a minute I thought maybe the winding of the polygon points was causing problems since bmax uses ogl. But alas it doesn't seem to make a difference.

zawran
Zawran / TTD

Posts: 2120
(19/10/05 19:20)
Reply | Edit | Del
   New Post Re: Algorithm bug I have just made this alternative way of making the effect based on the 3D code from one of my tutorials and a custom cylinder function. Its coded with BlitzPlus, but should be easy enough to convert to bmax if you don't have BlitzPlus or Blitz3D.

 
Quote
Graphics 640, 480, 32, 2

; setup vector variables
Global maxpoints = 10000
Global screenXcenter = 320
Global screenYcenter = 240
Global distance# = 20 ; distance of viewing
Dim points#( maxpoints, 2 ) ; original points
Dim tpoints#( maxpoints, 2 ) ; transformed points
Global maxquads = 1000
Dim quads( maxquads, 3 )
Dim quadRGB( maxquads )
Global pointCount = 0
Global quadCount = 0

; quad fill dims
Dim xval(20)
Dim yval(20)

SetBuffer BackBuffer()
While Not KeyHit( 1 )

pointCount = 0
quadCount = 0
createCylinder( 15, 5, 6, 40, 3+Sin( ang1# ) * 3 )

Cls

rotate3Dpoints( 0,ang1#,0 )
draw3Dquads()
ang1# = ang1# + 0.7
ang2# = ang2# + 1.3
ang3# = ang3# + 0.4

Color 255,255,255
Text 10,10,pointCount
Text 10,30,quadCount

Flip

Wend
End

Function createCylinder( HEIGHT, RADIUS#, SIDES, SECTIONS, TWIST# )
        Local yOff# = Float ( HEIGHT / 2 ) - HEIGHT
        Local yAdd# = Float HEIGHT / SECTIONS
        Local stepsize# = Float 360 / SIDES
        Local twisted# = 0
        Local RED, GREEN, BLUE

        i = 0
        For b = 0 To SECTIONS
                For a = 0 To SIDES-1
                        addPoint( Float Cos(a*stepsize#+twisted#)* RADIUS#, yOff#, Float Sin(a*stepsize#+twisted#) * RADIUS#  )
                        i = i + 1
                Next
                twisted# = twisted# + TWIST#
                yOff# = yOff# + yAdd#
        Next

        i = 0
        For a=0 To SECTIONS-1
                For b=0 To SIDES-2
                        If i = 0 Then
                                RED = 255
                                GREEN = 255
                                BLUE = 255
                        Else
                                RED = 0
                                GREEN = 0
                                BLUE = 255
                        End If
                        i = 1 - i
                        addQuad( b+a*SIDES, b+a*SIDES+1, b+(a+1)*SIDES+1, b+(a+1)*SIDES, RED, GREEN, BLUE )
                Next
                addQuad( b+a*SIDES, a*SIDES, (a+1)*SIDES, b+(a+1)*SIDES, RED, GREEN, BLUE )
        Next
End Function

Function addPoint( x#, y#, z# )
        points#( pointCount, 0 ) = x#
        points#( pointCount, 1 ) = y#
        points#( pointCount, 2 ) = z#
        pointCount = pointCount + 1
End Function

Function addQuad( p1, p2, p3, p4, R, G, B )
        quads( quadCount, 0 ) = p1
        quads( quadCount, 1 ) = p2
        quads( quadCount, 2 ) = p3
        quads( quadCount, 3 ) = p4
        quadRGB( quadCount ) = R Shl 16 + G Shl 8 + B
        quadCount = quadCount + 1
End Function

Function draw3Dquads()
For i = 0 To quadCount-1
normal1 = ( tpoints#( quads( i, 1 ), 0 ) - tpoints#( quads( i, 0 ), 0 ) ) * ( tpoints#( quads( i, 0 ), 1 ) - tpoints#( quads( i, 2 ), 1 ) )
normal2 = ( tpoints#( quads( i, 1 ), 1 ) - tpoints#( quads( i, 0 ), 1 ) ) * ( tpoints#( quads( i, 0 ), 0 ) - tpoints#( quads( i, 2 ), 0 ) )
normalcheck = normal1 - normal2
If normalcheck < 0 Then
Quad( tpoints#( quads( i, 0 ), 0 ), tpoints#( quads( i, 0 ), 1 ), tpoints#( quads( i, 1 ), 0 ), tpoints#( quads( i, 1 ), 1 ), tpoints#( quads( i, 2 ), 0 ), tpoints#( quads( i, 2 ), 1 ), tpoints#( quads( i, 3 ), 0 ), tpoints#( quads( i, 3 ), 1 ), quadRGB( i ) )
End If
Next
End Function

Function rotate3Dpoints( XROT#, YROT#, ZROT# )
sx# = Sin( XROT# )
cx# = Cos( XROT# )
sy# = Sin( YROT# )
cy# = Cos( YROT# )
sz# = Sin( ZROT# )
cz# = Cos( ZROT# )
For i = 0 To pointCount-1
x# = points#( i, 0 )
y# = points#( i, 1 )
z# = points#( i, 2 )
; rotation around x
xy# = cx# * y# - sx# * z#
xz# = sx# * y# + cx# * z#
; rotation around y
yz# = cy# * xz# - sy# * x#
yx# = sy# * xz# + cy# * x#
; rotation around z
zx# = cz# * yx# - sz# * xy#
zy# = sz# * yx# + cz# * xy#
; perspective correction
tpoints#( i, 0 ) = zx# / ( distance# + yz# ) * 512 + screenXcenter
tpoints#( i, 1 ) = zy# / ( distance# + yz# ) * 512 + screenYcenter
tpoints#( i, 2 ) = yz#
Next
End Function

; triangle and rect fill code from Skidracer and Swift
; can be found at: www.blitzbasic.com/codear...p?code=136

Function Triangle(x0,y0,x1,y1,x2,y2)
xval(0)=x0
yval(0)=y0
xval(1)=x1
yval(1)=y1
xval(2)=x2
yval(2)=y2
FastPoly(3, RGB)
End Function

Function Quad(x0,y0,x1,y1,x2,y2,x3,y3,RGB)
xval(0)=x0
yval(0)=y0
xval(1)=x1
yval(1)=y1
xval(2)=x2
yval(2)=y2
xval(3)=x3
yval(3)=y3
FastPoly(4,RGB)
End Function

Function FastPoly(vcount,RGBColor)

; get clipping region
width=GraphicsWidth()
height=GraphicsHeight()

; Lock the current drawing buffer.
LockBuffer()

; find top verticy
b=vcount-1
y=yval(0)
While c<>b
c=c+1
yy=yval(c)
If yy<y y=yy d=c
Wend
c=d
t=c

; draw top to bottom
While y<height

; get left gradient
If y=yval(c)
While y=yval(c)
x0=xval(c) Shl 16
c=c+1
If c>b c=a
If c=t Goto Finish
If y>yval(c) Goto Finish
Wend
h=yval(c)-y
g0=((xval(c) Shl 16)-x0)/h
EndIf

; get right gradient
If y=yval(d)
While y=yval(d)
x1=xval(d) Shl 16
d=d-1
If d<a d=b
If y>yval(d) Goto Finish
Wend
h=yval(d)-y
g1=((xval(d) Shl 16)-x1)/h
EndIf

; calc horizontal span
x=x1 Sar 16
w=((x0 Sar 16)-x)+1

; draw down to next vert
If (w > 0) And (y > -1) And (x < width) And ((x+w) > 0)

;crop left
If x < 0
w=w+x
x=0
EndIf

;crop right
If (x+w) > width
w=width-x
EndIf

; Draw scanline.
For Lx = x To (x+w)
WritePixelFast Lx, y, RGBColor
Next

EndIf

; next
x0=x0+g0
x1=x1+g1
y=y+1

Wend

.Finish

; Unlock the draw buffer.
UnlockBuffer()

End Function


zac.interactive | Blog | Tutorials | Demos | Tools
zparticle
AMSTRAD CPC

Posts: 86
(21/10/05 15:43)
Reply | Edit | Del    New Post Re: Algorithm bug Thanks, I got it fixed. I was do the rotation is radians and not degress so it wrapping around more than once.

The code is available at

www.scottshaver2000.com/b...tromic.bmx

zawran
Zawran / TTD

Posts: 2121
(21/10/05 15:50)
Reply | Edit | Del
   New Post Re: Algorithm bug Yes, looks like it works just fine now. :)

zac.interactive | Blog | Tutorials | Demos | Tools
zawran
Zawran / TTD

Posts: 2133
(25/10/05 16:30)
Reply | Edit | Del
   New Post Re: Algorithm bug Is this effect part of some kind of demo, or just something you are working on to play around with Bmax and figure things out?

I still haven't picked up where I left Bmax last. But I am getting there, its just been difficult finding the motivation to code again.

zac.interactive | Blog | Tutorials | Demos | Tools
zparticle
AMSTRAD CPC

Posts: 89
(26/10/05 16:29)
Reply | Edit | Del    New Post Re: Algorithm bug I was thinking about putting it into a demo, maybe the mirror thing. Mainly I wanted to see if I could make it work in BMax.
« Last Edit: July 21, 2007 by Shockwave »