# Thread: why Polygon() don't use the brush?

1. ## why Polygon() don't use the brush?

i change the brush:
Code:
```Dim oldbrush As Long
oldbrush = GetDCBrushColor(Me.hDC)
SetDCBrushColor Me.hDC, RGB(255, 0, 0)```
i call the Polygon() function by double a double vectors(draw 1 line at a time)...
Code:
```Private Sub DrawLine(Origin As Position3D, Destiny As Position3D, Rotation As Angle3D, WorldSize As Size3D)
Dim Points(2) As POINTAPI
Dim NewPosition3D(2) As Position3D
Dim RotatedPosition As Position3D

With Player1.Position
FillPosition3D RotatedPosition, .x, .Y, .Z  'rotate using camera Position
End With

'Line:
'Calculate Vector1:
FillPosition3D NewPosition3D(0), Origin.x, Origin.Y, Origin.Z
NewPosition3D(0) = Rotate(NewPosition3D(0), Rotation, RotatedPosition)

'Calculate Vector2:
FillPosition3D NewPosition3D(1), Destiny.x, Destiny.Y, Destiny.Z
NewPosition3D(1) = Rotate(NewPosition3D(1), Rotation, RotatedPosition)

'Testing If Vector0 and Vector1 are inside of camera:

If (IsOnCamera(NewPosition3D(0), camera1.Position, camera1.size) = False And _
IsOnCamera(NewPosition3D(1), camera1.Position, camera1.size) = False) Then
Exit Sub
End If

If (IsOnCamera(NewPosition3D(0), camera1.Position, camera1.size) = False) Then
NewPosition3D(0) = GetInCamVector(NewPosition3D(0), NewPosition3D(1))
End If

If (IsOnCamera(NewPosition3D(1), camera1.Position, camera1.size) = False) Then
NewPosition3D(1) = GetInCamVector(NewPosition3D(1), NewPosition3D(0))
End If

'Convert the Vector1 to 2D:
Points(0) = ConvertPositon3DTo2D(NewPosition3D(0), camera1.size)

'Convert the Vector2 to 2D:
Points(1) = ConvertPositon3DTo2D(NewPosition3D(1), camera1.size)
Polygon Me.hDC, Points(0), 2

End Sub```
Code:
```DrawLine position1, position2, Rotation, WorldSize
SetDCBrushColor Me.hDC, oldbrush```
the line is drawed red.. on a pen way...
but why the brush isn't used?
yes if i call the Polygon() for draw all the 4 lines at once, the brush is used... but why not on these way? i selected the brush  Reply With Quote

2. ## Re: why Polygon() don't use the brush?

When you call polygon, it has to be a full polygon, in other words, the function will draw a line from the last point to the first point to close the polygon.

Since you only pass two points to the polygon method, you are drawing a polygon that goes from point 1, to point 2, and then back to point 1 to close it. So the polygon has zero area, and looks like a line.

I don't know if the Windows API has a method that allows building a polygon in pieces. APIs that allow that usually have to have separate call to either start the polygon, and the successive calls to the function collect the points until a End Polygon function is called to have the polygon rendered.

Doing it the way you're doing it, how would the polygon function know that you are done drawing the polygon, so it can use the brush and fill it?

If you want this functionality, then perhaps create your own function or sub that you call, which will build up the points in an array with each call, and then when the condition is met to be done with the polygon, have the method call the polygon function once, passing the collected points to it, to draw the complete polygon.  Reply With Quote

3. ## Re: why Polygon() don't use the brush?

now works fine:
Code:
```Private Sub DrawPlane(position As position3D, size As Size3D, Rotation As Angle3D, WorldSize As Size3D, color As ColorConstants)
Dim RotatedPosition As position3D
With Player1.position
FillPosition3D RotatedPosition, .x, .Y, .Z  'Camera Position
End With

If (IsCollision3D(camera1.position, camera1.size, position, size) = False) Then Exit Sub

Dim position3D(8) As position3D
Dim Position2D(8) As POINTAPI
Dim position3DtoDraw(8) As POINTAPI
Dim PositionsCount As Long
PositionsCount = 0
'Draw the Line:
FillStyle = vbFSSolid
ForeColor = vbRed
Dim oldbrush As Long
oldbrush = GetDCBrushColor(Me.hDC)
SetDCBrushColor Me.hDC, RGB(255, 0, 0)
'Line top:
FillPosition3D position3D(0), position.x, position.Y, position.Z
position3D(0) = Rotate(position3D(0), Rotation, RotatedPosition)
FillPosition3D position3D(1), position.x + size.Width, position.Y, position.Z + size.ZDepth
position3D(1) = Rotate(position3D(1), Rotation, RotatedPosition)

If (IsOnCamera(position3D(0), camera1.position, camera1.size) = False) Then
position3D(0) = GetInCamVector(position3D(0), position3D(1))
End If
If (IsOnCamera(position3D(1), camera1.position, camera1.size) = False) Then
position3D(1) = GetInCamVector(position3D(1), position3D(0))
End If

Position2D(0) = ConvertPositon3DTo2D(position3D(0), camera1.size)
Position2D(1) = ConvertPositon3DTo2D(position3D(1), camera1.size)

If (IsOnCamera(position3D(0), camera1.position, camera1.size) = True And _
IsOnCamera(position3D(1), camera1.position, camera1.size) = True) Then
PositionsCount = PositionsCount + 2
position3DtoDraw(0) = Position2D(0)
position3DtoDraw(1) = Position2D(1)
End If

'Line right:
FillPosition3D position3D(2), position.x + size.Width, position.Y, position.Z + size.ZDepth
position3D(2) = Rotate(position3D(2), Rotation, RotatedPosition)

FillPosition3D position3D(3), position.x + size.Width, position.Y + size.Height, position.Z + size.ZDepth
position3D(3) = Rotate(position3D(3), Rotation, RotatedPosition)
If (IsOnCamera(position3D(2), camera1.position, camera1.size) = False) Then
position3D(2) = GetInCamVector(position3D(2), position3D(3))
End If

If (IsOnCamera(position3D(3), camera1.position, camera1.size) = False) Then
position3D(3) = GetInCamVector(position3D(3), position3D(2))
End If
Position2D(2) = ConvertPositon3DTo2D(position3D(2), camera1.size)
Position2D(3) = ConvertPositon3DTo2D(position3D(3), camera1.size)

If (IsOnCamera(position3D(2), camera1.position, camera1.size) = True And _
IsOnCamera(position3D(3), camera1.position, camera1.size) = True) Then
PositionsCount = PositionsCount + 2
position3DtoDraw(2) = Position2D(2)
position3DtoDraw(3) = Position2D(3)
End If

'Line bottom:
FillPosition3D position3D(4), position.x + size.Width, position.Y + size.Height, position.Z + size.ZDepth
position3D(4) = Rotate(position3D(4), Rotation, RotatedPosition)

FillPosition3D position3D(5), position.x, position.Y + size.Height, position.Z
position3D(5) = Rotate(position3D(5), Rotation, RotatedPosition)
If (IsOnCamera(position3D(4), camera1.position, camera1.size) = False) Then
position3D(4) = GetInCamVector(position3D(4), position3D(5))
End If
If (IsOnCamera(position3D(4), camera1.position, camera1.size) = False) Then
position3D(5) = GetInCamVector(position3D(5), position3D(4))
End If
Position2D(4) = ConvertPositon3DTo2D(position3D(4), camera1.size)
Position2D(5) = ConvertPositon3DTo2D(position3D(5), camera1.size)

If (IsOnCamera(position3D(4), camera1.position, camera1.size) = True And _
IsOnCamera(position3D(5), camera1.position, camera1.size) = True) Then
PositionsCount = PositionsCount + 2
position3DtoDraw(4) = Position2D(4)
position3DtoDraw(5) = Position2D(5)
End If

'Line left:
FillPosition3D position3D(6), position.x, position.Y + size.Height, position.Z
position3D(6) = Rotate(position3D(6), Rotation, RotatedPosition)
FillPosition3D position3D(7), position.x, position.Y, position.Z
position3D(7) = Rotate(position3D(7), Rotation, RotatedPosition)
If (IsOnCamera(position3D(6), camera1.position, camera1.size) = False) Then
position3D(6) = GetInCamVector(position3D(6), position3D(7))
End If
If (IsOnCamera(position3D(7), camera1.position, camera1.size) = False) Then
position3D(7) = GetInCamVector(position3D(7), position3D(6))
End If
Position2D(6) = ConvertPositon3DTo2D(position3D(6), camera1.size)
Position2D(7) = ConvertPositon3DTo2D(position3D(7), camera1.size)

If (IsOnCamera(position3D(6), camera1.position, camera1.size) = True And _
IsOnCamera(position3D(7), camera1.position, camera1.size) = True) Then
PositionsCount = PositionsCount + 2
position3DtoDraw(6) = Position2D(6)
position3DtoDraw(7) = Position2D(7)
End If

Polygon Me.hDC, position3DtoDraw(0), PositionsCount
SetDCBrushColor Me.hDC, oldbrush
End Sub```
but i must review the code for draw only what is on camera... but it's working now.. tomorrow i will do it.
thanks for all  Reply With Quote

4. ## Re: why Polygon() don't use the brush?

if i lose the left line(for exemple), because it's outside the camera, the plane isn't drawed.... how can i fix these?
i must draw all lines   Reply With Quote

5. ## Re: why Polygon() don't use the brush?

Which line is the "left line"?

If you're saying the left line is the vertical line at the "front" of a polygon in 3D, and that vertical line is completely behind the camera, then the lines that connect to the vertical line (the top and bottom lines), should have been shortened, and the vertical left line, would now be between the shortened end points of the top and bottom lines, so that the left line is essentially a new line that is at a greater Z depth, and it doesn't go behind the camera.

The line can be off the screen, and still be in front of the camera position, so drawing should work even if the lines are off screen. It is only when the coordinates go behind the camera that the math will cause artifacts if you try to draw using those coordinates.

You can't treat all the lines the same. Lines that are going into or out of the screen need to be tested for going behind the camera, and if only one of the coordinates of the line is behind the camera, that line has to be shortened. Since the vertical line should be using the same coordinates for its top and bottom as the end coordinates of the top and bottom lines, it shouldn't need to be calculated. When you shorten the top and bottom line, the end points of the vertical line should already have been modified as part of that so you don't have to do anything for the vertical lines if you're using the same coordinates.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•