Flat Shading with OpenB3d

Started by Xaron, March 18, 2022, 08:30:38

Previous topic - Next topic

Xaron

Hi there, is there a way to enable flat shading with OpenB3d?

As far as I see the classic Minib3d has a function to unweld triangles to do that:

Method UnWeldTriangles:Int(inSurf:TSurface=Null, updatenorms:Bool=true)

Local list:List<TSurface> = surf_list
If inSurf
list = New List<TSurface>
list.AddLast(inSurf)
Endif

Local newlist:List<TSurface> = New List<TSurface>

For Local surf:TSurface = Eachin list

Local newSurf:TSurface = surf.Copy()
newSurf.ClearSurface(True,True)

Local data0:Vertex, data1:Vertex, data2:Vertex, v0:Int,v1:Int,v2:Int, norm:Vector = New Vector

For Local i:Int = 0 To surf.no_tris-1
Local a0%=surf.TriangleVertex(i,0)
Local a1%=surf.TriangleVertex(i,1)
Local a2%=surf.TriangleVertex(i,2)
v0 = newSurf.AddVertex(0.0,1.0,0.0)
v1 = newSurf.AddVertex(0.0,0.0,1.0)
v2 = newSurf.AddVertex(0.0,1.0,1.0)

data0 = surf.GetVertex(a0)
data1 = surf.GetVertex(a1)
data2 = surf.GetVertex(a2)

If updatenorms
Local ax#=data1.x-data0.x
Local ay#=data1.y-data0.y
Local az#=-data1.z+data0.z

Local bx#=data2.x-data1.x
Local by#=data2.y-data1.y
Local bz#=-data2.z+data1.z

Local nx#=(ay*bz)-(az*by)
Local ny#=(az*bx)-(ax*bz)
Local nz#=-(ax*by)+(ay*bx)

norm.Update(nx,ny,nz)
norm = norm.Normalize()
data0.nx = norm.x
data0.ny = norm.y
data0.nz = norm.z
data1.nx = norm.x
data1.ny = norm.y
data1.nz = norm.z
data2.nx = norm.x
data2.ny = norm.y
data2.nz = norm.z

Endif

newSurf.SetVertex(v0,data0)
newSurf.SetVertex(v1,data1)
newSurf.SetVertex(v2,data2)

newSurf.AddTriangle(v0,v1,v2)

Next

newSurf.CropSurfaceBuffers()
newlist.AddLast(newSurf)

surf.ClearSurface(True,True)


Next

surf_list.Clear()
surf_list = newlist


Return 1

End


Is there an easy way to port that over or is there another way of doing it with OpenB3d?

Thanks!

angros47

You can always use EntityFX with flag 4 to enable flat shading, you don't need to transform the normals.

Anyway, if, for any reason, you want to use the code you provided, you can either translate it to C++ (since OpenB3D is written in C++), or you can replace the OOP code with a more Blitz3D-like code (instead of lists, for example, you use the vertex index with commands like VertexX, VertexNX, CountVertices and so on.

Xaron

I'm so stupid.  :-\

Thank you angros! It's that easy, oh well. Forgot about that EntityFX command. :)