Here is a code snippet from a small game I am in the process of coding. When you run the code you will notice that the flat areas are shaded grey. What I want is only complete squares shaded - you will notice the there are some individual triangles shaded (attached to squares and by themselves) - How do I stop those triangles from being shaded?
Please help.
; level test
Graphics3D 640,480,16,2
Global width = 32
Global seed = 3
Dim plasma#(width,width)
GeneratePlasma(width,width,1,seed)
SquarePlasma(width,0.005)
SetBuffer BackBuffer()
terr = CreateTerrainMesh(width,width)
b0 = LoadBrush("efx_sample_05.jpg",1+8)
b1 = LoadBrush("grass.bmp",1+8)
terr = GradePaintTerrainMesh(terr, 0.00001, 90, 90, 90, b0, b1, b2, b3, b4)
ScaleEntity terr,1,4,1
EntityFX terr,2
EntityColor terr,0,0,255
Global cam_camera = CreateCamera()
PositionEntity cam_camera,10,40,10
sun= CreateLight()
PositionEntity sun,10,40,10
RotateEntity sun,45,0,0
While Not KeyHit(1)
lev_docamera()
UpdateWorld
RenderWorld
Flip
Wend
End
Function LEV_docamera()
CamSpd# = .50
MoveEntity(CAM_Camera, Float(KeyDown(205) - KeyDown(203)) * CamSpd, 0, Float(KeyDown(200) - KeyDown(208)) * CamSpd)
If MouseDown(1) Then
WireFrame 1
Else
WireFrame 0
EndIf
If MouseDown(2)
TurnSpeed# = 0.8
TurnEntity(CAM_Camera, Float(MouseYSpeed()) * TurnSpeed#, 0, 0, False)
TurnEntity(CAM_Camera, 0, -Float(MouseXSpeed()) * TurnSpeed#, 0, True)
Else
MouseXSpeed() : MouseYSpeed()
EndIf
End Function
Function GeneratePlasma(width, height, grain#, seed)
SeedRnd(seed)
Dim plasma#(width, height)
For j = 0 To width - 1
For i = 0 To height - 1
plasma#(j, i) = -1.0
Next
Next
x_max = Ceil(Float(width - 1)/2.0) ; mid-point x
y_max = Ceil(Float(height - 1)/2.0) ; mid-point y
plasma#(0, 0) = Rnd(0.0, 1.0)
plasma#(x_max, 0) = Rnd(0.0, 1.0)
plasma#(0, y_max) = Rnd(0.0, 1.0)
plasma#(x_max, y_max) = Rnd(0.0, 1.0)
RecursePlasma(0, 0, x_max, y_max, grain#, 1.0)
For y = 0 To y_max
plasma#(width - 1, y) = plasma#(0, y)
Next
RecursePlasma(x_max, 0, width - 1, y_max, grain#, 1.0)
For x = 0 To width - 1
plasma#(x, height - 1) = plasma#(x, 0)
Next
RecursePlasma(x_max, y_max, width - 1, height - 1, grain#, 1.0)
For y = (y_max + 1) To height - 1
plasma#(0, y) = plasma#(width - 1, y)
Next
RecursePlasma(0, y_max, x_max, height - 1, grain#, 1.0)
End Function
;---------------------------------------------------------------------
Function MidPointDisplace#(x0, y0, x1, y1, x2, y2, grain#, level#)
r# = Rnd(-grain#, grain#)/level#
r# = (plasma#(x0, y0) + plasma#(x2, y2))/2.0 + r#
If r# < 0.0 Then r# = 0.0
If r# > 1.0 Then r# = 1.0
plasma#(x1, y1) = r#
Return r#
End Function
;---------------------------------------------------------------------
Function RecursePlasma(x0, y0, x2, y2, grain#, level#)
If (x2 - x0 < 2) And (y2 - y0 < 2) Then Return
level# = 2.0*level#
x1 = Ceil((x0 + x2)/2.0)
y1 = Ceil((y0 + y2)/2.0)
v# = plasma#(x1, y0)
If v# = -1.0 Then v# = MidPointDisplace#(x0, y0, x1, y0, x2, y0, grain#, level#)
i# = v#
v# = plasma#(x2, y1)
If v# = -1.0 Then v# = MidPointDisplace#(x2, y0, x2, y1, x2, y2, grain#, level#)
i# = i# + v#
v# = plasma#(x1, y2)
If v# = -1.0 Then v# = MidPointDisplace#(x0, y2, x1, y2, x2, y2, grain#, level#)
i# = i# + v#
v# = plasma#(x0, y1)
If v# = -1.0 Then v# = MidPointDisplace#(x0, y0, x0, y1, x0, y2, grain#, level#)
i# = i# + v#
If plasma#(x1, y1) = -1.0 Then plasma#(x1, y1) = i#/4.0
RecursePlasma(x0, y0, x1, y1, grain#, level#)
RecursePlasma(x1, y0, x2, y1, grain#, level#)
RecursePlasma(x1, y1, x2, y2, grain#, level#)
RecursePlasma(x0, y1, x1, y2, grain#, level#)
End Function
; ------------------------------------------------------------------------
Function SquarePlasma(width,value#)
yMax# = 0
tempx = 0
tempy = 0
For x = 0 To width - 1
For y = 0 To width -1
x1# = plasma(x,y)
x2# = plasma(x+1,y)
x3# = plasma(x,y+1)
x4# = plasma(x+1,y+1)
av# = (x1+x2+x3+x4) / Float(4)
If Abs(av-x1) <= value Then
plasma(x,y) = x1
plasma(x+1,y) = x1
plasma(x,y+1) = x1
plasma(x+1,y+1) = x1
EndIf
If x1 > yMax Then
yMax = x1
tempx = x
tempy = y
EndIf
Next
Next
plasma(tempx,tempy) = ymax
plasma(tempx+1,tempy) = ymax
plasma(tempx,tempy+1) = ymax
plasma(tempx+1,tempy+1) = ymax
End Function
;----------------------------------------------------------------------------
Function CreateTerrainMesh(x,y)
;x = how many horizontal vertices
;y = how many vertical vertices
mesh = CreateMesh()
surf = CreateSurface(mesh)
If x < 2 Or y < 2 Then Return mesh ;in case user gives a bad number simply return the mesh
For tempx = 1 To x
For tempy = 1 To y
AddVertex(surf,tempx*4,plasma#(tempx,tempy)*10,tempy*4)
Next
Next
For v0 = 0 To (x*y)-y-2
For Z = 1 To x*y
If v0 = z*y-1 Then v0 = v0 + 1
Next
v1 = V0 + 1
v2 = v1 + y
v3 = v0 + y
AddTriangle(surf,v0,v1,v2)
AddTriangle(surf,v0,v2,v3)
Next
UpdateNormals(mesh)
Return mesh
End Function
; ----------------------------------- thanks to mrsnidesmin
Function GradePaintTerrainMesh%(m%, g1#, g2#, g3#, g4#, bGrad0%, bGrad1%, bGrad2%, bGrad3%, bGrad4%)
m2% = CreateMesh()
s_g0% = CreateSurface(m2)
s_g1% = CreateSurface(m2)
For i% = 1 To CountSurfaces(m)
s% = GetSurface(m, i)
For it% = 0 To CountTriangles(s)-1
v0% = TriangleVertex(s, it, 0)
v1% = TriangleVertex(s, it, 1)
v2% = TriangleVertex(s, it, 2)
vt% = TriangleVertex(s,it+1,0) ; error here?
v0x# = VertexX(s, v0)
v0y# = VertexY(s, v0)
v0z# = VertexZ(s, v0)
v0u# = VertexU(s, v0)
v0v# = VertexV(s, v0)
v0w# = VertexW(s, v0)
v0nx# = VertexNX(s, v0)
v0ny# = VertexNY(s, v0)
v0nz# = VertexNZ(s, v0)
v0red# = VertexRed(s, v0)
v0green# = VertexRed(s, v0)
v0blue# = VertexRed(s, v0)
v0alpha# = VertexAlpha(s, v0)
v1x# = VertexX(s, v1)
v1y# = VertexY(s, v1)
v1z# = VertexZ(s, v1)
v1u# = VertexU(s, v1)
v1v# = VertexV(s, v1)
v1w# = VertexW(s, v1)
v1nx# = VertexNX(s, v1)
v1ny# = VertexNY(s, v1)
v1nz# = VertexNZ(s, v1)
v1red# = VertexRed(s, v1)
v1green# = VertexRed(s, v1)
v1blue# = VertexRed(s, v1)
v1alpha# = VertexAlpha(s, v1)
v2x# = VertexX(s, v2)
v2y# = VertexY(s, v2)
v2z# = VertexZ(s, v2)
v2u# = VertexU(s, v2)
v2v# = VertexV(s, v2)
v2w# = VertexW(s, v2)
v2nx# = VertexNX(s, v2)
v2ny# = VertexNY(s, v2)
v2nz# = VertexNZ(s, v2)
v2red# = VertexRed(s, v2)
v2green# = VertexRed(s, v2)
v2blue# = VertexRed(s, v2)
v2alpha# = VertexAlpha(s, v2)
If (v0y = v2y And v0y = v1y And v0y = VertexY(s,vt)) Then ; error here?
s2 = s_g0
Else
s2 = s_g1
EndIf
v0% = AddVertex(s2, v0x, v0y, v0z, v0u, v0v, v0w)
v1% = AddVertex(s2, v1x, v1y, v1z, v1u, v1v, v1w)
v2% = AddVertex(s2, v2x, v2y, v2z, v2u, v2v, v2w)
VertexNormal s2, v0, v0nx, v0ny, v0nz
VertexNormal s2, v1, v1nx, v1ny, v1nz
VertexNormal s2, v2, v2nx, v2ny, v2nz
VertexColor s2, v0, v0red, v0green, v0blue, v0alpha
VertexColor s2, v1, v1red, v1green, v1blue, v1alpha
VertexColor s2, v2, v2red, v2green, v2blue, v2alpha
AddTriangle s2, v0, v1, v2
Next
Next
PaintSurface s_g0, bGrad0
PaintSurface s_g1, bGrad1
FreeEntity m
Return m2
End Function
|