Exemplo n.º 1
0
    def _vertexBuffersSetup(
            self, mesh: MeshData) -> Optional[QOpenGLVertexArrayObject]:
        # See if the mesh has already been stored to the GPU:
        vao = cast(Optional[QOpenGLVertexArrayObject],
                   mesh.getCachedUserValue(self._shader.getReferenceKey()))
        if vao is not None:
            return vao

        # Initialize VAO (VertexArrayObject). On activation, this will wrap around the other vertex/index buffers.
        # That enables reusing them without much fuss.
        if not OpenGLContext.properties["supportsVertexArrayObjects"]:
            Logger.log(
                "e",
                "RenderBatch: This OpenGL doesn't support VAO? You will not go to R^3 today."
            )
            return None

        vao = QOpenGLVertexArrayObject()
        vao.create()
        if not vao.isCreated():
            Logger.log(
                "e",
                "RenderBatch: VAO not created. You will not go to R^3 today.")
            return None

        # Setup VAO:
        vao.bind()

        vertex_buffer = OpenGL.getInstance().createVertexBuffer(mesh)
        vertex_buffer.bind()

        index_buffer = OpenGL.getInstance().createIndexBuffer(mesh)
        if index_buffer is not None:
            index_buffer.bind()

        self._setMeshAttributes(mesh)

        # Cache and return:
        mesh.setCachedUserValue(self._shader.getReferenceKey(), vao)
        vao.release()
        return vao