Пример #1
0
class HeightMap(o.Base):

    def __init__(self, *args, **kwargs):

        npoints = 80
        X = np.linspace(-1, 1, npoints).astype(np.float32)
        Y = np.linspace(-1, 1, npoints).astype(np.float32)
        Z = np.zeros((npoints,npoints), dtype=np.float32)
        x, y = np.meshgrid(X, Y)
        mesh = np.vstack((x, y, Z)).reshape(3, -1).T.astype(np.float32)

        super(HeightMap, self).__init__(
            mesh,
            linetype=o.TriangleMesh(
                data=mesh,
                side_x=npoints,
                side_y=npoints,
            ),
        )

        self._shaders += t.shader_path("heightmap/heightmap.vsh")
        self._shaders += t.shader_path("heightmap/heightmap.fsh")

    def createShaders(self, parent):

        # create buffers
        self._vertices = VBO(VERTEX_BUFFER)
        self._index = VBO(INDEX_BUFFER)
        self._vao = VAO()

        self._vertices.create()
        self._index.create()
        self._vao.create()

        # allocate buffers
        self._vertices.bind()
        self._vertices.allocate(
            self._data,
            len(self._data) * 4
        )
        self._vertices.release()
        self._index.bind()
        self._index.allocate(
            self._plot_prop._ids,
            len(self._plot_prop._ids) * 4
        )
        self._index.release()

        self._textures = parent.textures << [
            (
                "heightmap/two.png",
                {
                    "parameters": {
                        "TEXTURE_MIN_FILTER": "LINEAR",
                        "TEXTURE_MAG_FILTER": "LINEAR",
                        "TEXTURE_WRAP_S": "CLAMP_TO_EDGE",
                        "TEXTURE_WRAP_T": "CLAMP_TO_EDGE",
                    }
                }
            )
        ]

        self._shaders.build()
        self._shaders.bindAttribLocation("position")

        self._shaders.link()

        # Initialization of the VAO
        self._vao.bind()

        self._vertices.bind()
        self._shaders.enableAttributeArray("position")
        self._shaders.setAttributeBuffer(
            "position",
            self._data,
        )

        self._index.bind()

        # self._vertices.release()
        # self._index.release()

        self._vao.release()

    def show(self, parent):

        GL.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE)

        self._shaders.bind()

        self._shaders.setUniformValue(
            "projection",
            parent._projection
        )
        self._shaders.setUniformValue(
            "modelview",
            parent._view * self._model
        )

        self._shaders.setUniformValue(
            "map",
            self._textures[0],
        )
        self._textures[0].activate()

        # self._vertices.bind()
        # self._shaders.enableAttributeArray("position")
        # self._shaders.setAttributeBuffer(
            # "position",
            # self._data,
        # )
        # self._vertices.release()

        self._vao.bind()
        # self._index.bind()

        GL.glDrawElements(
            GL.GL_TRIANGLES,
            len(self._plot_prop._ids),
            GL.GL_UNSIGNED_INT,
            None,
        )

        # self._index.release()

        # self._shaders.disableAttributeArray("position")

        # GL. glDrawArrays(GL.GL_TRIANGLES, 0, len(self._plot_prop._ids)) #self._data.shape[0]//3)

        self._vao.release()
        self._shaders.release()
        self._textures[0].release()
Пример #2
0
class Rippler(o.Base):

    def __init__(self, *args, **kwargs):

        npoints = 30
        X = np.linspace(-1, 1, npoints).astype(np.float32)
        Y = np.linspace(-1, 1, npoints).astype(np.float32)
        Z = np.zeros((npoints, npoints), dtype=np.float32)
        x, y = np.meshgrid(X, Y)
        mesh = np.vstack((x, y, Z)).reshape(3, -1).T.astype(np.float32)

        super(Rippler, self).__init__(mesh, linetype=o.TriangleMesh(data=mesh))

        self._shaders += t.shader_path("rippler/rippler.vsh")
        self._shaders += t.shader_path("rippler/rippler.fsh")

        self._time = datetime.datetime.now()

        self._widget = Application()
        self._widget.title.text = "Window title"
        button1 = Button()
        button2 = Button()
        button1.text.font_size = 20
        button2.text.font_size = 20
        button1.text = "Hello world !"
        button2.text = "Viva Sponge Bob !"
        self._widget.x = 300
        self._widget.y = 300
        button1.click.connect(self._echo)
        button2.click.connect(self._echo)
        button1.size_hint_x = None
        button2.size_hint_x = None
        button1.size_hint_y = 0.5
        button2.size_hint_y = 0.5
        self._widget.addWidget(button1)
        self._widget.addWidget(button2)

    def createShaders(self, parent):

        # create buffers
        self._vertices = VBO(VERTEX_BUFFER)
        self._index = VBO(INDEX_BUFFER)
        self._vao = VAO()

        self._vertices.create()
        self._index.create()
        self._vao.create()

        # allocate buffers
        self._vertices.bind()
        self._vertices.allocate(
            self._data,
            len(self._data) * 4
        )
        self._vertices.release()
        self._index.bind()
        self._index.allocate(
            self._plot_prop._ids,
            len(self._plot_prop._ids) * 4
        )
        self._index.release()

        self._shaders.build()
        self._shaders.bindAttribLocation("position")

        self._shaders.link()

        self._vao.bind()

        self._index.bind()
        self._vertices.bind()

        self._shaders.enableAttributeArray("position")
        self._shaders.setAttributeBuffer(
            "position",
            self._data,
        )

        self._vao.release()

        # create shaders for widget
        self._widget.createShaders()

    def createWidget(self):

        return self._widget

    def _echo(self):
        print("clicked")

    def show(self, parent):

        GL.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE)

        self._shaders.bind()

        self._shaders.setUniformValue(
            "projection",
            parent._projection
        )
        self._shaders.setUniformValue(
            "modelview",
            parent._view * self._model
        )
        dt = datetime.datetime.now() - self._time
        second = float((dt.seconds * 1000000 + dt.microseconds) * 0.000006)
        self._shaders.setUniformValue("time", Vector(second, dtype=np.float32))

        self._vao.bind()

        GL.glDrawElements(
            GL.GL_TRIANGLES,
            len(self._plot_prop._ids),
            GL.GL_UNSIGNED_INT,
            None,
        )

        self._vao.release()

        self._shaders.release()