Home Game Development python – How to implement and understand Clipping

python – How to implement and understand Clipping

0
python – How to implement and understand Clipping

[ad_1]

I am writing a 3D renderer from scratch in Python using PyGame and Numpy. I have the basics implemented, which are working well. I have noticed that when you get too near to objects, the objects invert. I think this is because the projection or transformations matrices are inverting objects when renderering -z values. This is why I am trying to implement a clipping routine to clip any objects which go behind the camera view plane.

This seems to be kind of working. When set to clip in the Z plane, it cuts off objects as its meant to, but when moving the camera forward, the plane seems to move forward with half the speed of the camera (not sure why?). Then when the camera catches up with the plane, the shapes begin to try and invert to the edges of the screen. I’ve tried changing the FOV and Zoom of the camera and also how far forward the plane is in-front of the camera and they all work well, clipping the whole object without any inverting. However I want the clipping plane to clip only near to the camera.

I have also tried clipping in the X axis and the plane clips the object at the same speed as the camera moves to the right (seems to move at the same speed as the camera in the X axis).

I have also been following this video to try and understand clipping better: One Lone Coder – Clipping

GitHub link.

The code in question is shown below (in the projectionViewer Class):

def clipFaceAgainstPlane(self, pointOnPlane, planeNormal, face, wireframe):

        planeNormal = normaliseVector(planeNormal)
        distance1 = self.distanceOfPointToPlane(pointOnPlane, planeNormal, wireframe.nodes[face.vertices[0]])
        distance2 = self.distanceOfPointToPlane(pointOnPlane, planeNormal, wireframe.nodes[face.vertices[1]])
        distance3 = self.distanceOfPointToPlane(pointOnPlane, planeNormal, wireframe.nodes[face.vertices[2]])

        insidePoints = []
        outsidePoints = []
        outputPoints = []

        if distance1 > 0:
            insidePoints.append(face.vertices[0])
        else:
            outsidePoints.append(face.vertices[0])

        if distance2 > 0:
            insidePoints.append(face.vertices[1])
        else:
            outsidePoints.append(face.vertices[1])

        if distance3 > 0:
            insidePoints.append(face.vertices[2])
        else:
            outsidePoints.append(face.vertices[2])

        if insidePoints == 0:
            pass
        elif len(insidePoints) == 1:
            outputPoints.append(wireframe.perspectiveNodes[insidePoints[0]])
            outputPoints.append(self.checkLineOnPlane(pointOnPlane, planeNormal, wireframe.perspectiveNodes[insidePoints[0]], wireframe.perspectiveNodes[outsidePoints[0]]))
            outputPoints.append(self.checkLineOnPlane(pointOnPlane, planeNormal, wireframe.perspectiveNodes[insidePoints[0]], wireframe.perspectiveNodes[outsidePoints[1]]))
        elif len(insidePoints) == 2:
            outputPoints.append(wireframe.perspectiveNodes[insidePoints[0]])
            outputPoints.append(wireframe.perspectiveNodes[insidePoints[1]])
            outputPoints.append(self.checkLineOnPlane(pointOnPlane, planeNormal, wireframe.perspectiveNodes[insidePoints[0]], wireframe.perspectiveNodes[outsidePoints[0]]))

            outputPoints.append(wireframe.perspectiveNodes[insidePoints[1]])
            outputPoints.append(self.checkLineOnPlane(pointOnPlane, planeNormal, wireframe.perspectiveNodes[insidePoints[1]], wireframe.perspectiveNodes[outsidePoints[0]]))
            outputPoints.append(self.checkLineOnPlane(pointOnPlane, planeNormal, wireframe.perspectiveNodes[insidePoints[0]], wireframe.perspectiveNodes[outsidePoints[0]]))
        elif len(insidePoints) == 3:
            outputPoints.append(wireframe.perspectiveNodes[insidePoints[0]])
            outputPoints.append(wireframe.perspectiveNodes[insidePoints[1]])
            outputPoints.append(wireframe.perspectiveNodes[insidePoints[2]])

        return outputPoints

Also, another reason which is why this could be happening is because I am not clipping before applying the perspective to the points. Would I apply the clipping in the Z axis to the regular nodes?

[ad_2]