在opengl中有没有循环旋转2-D线的方法

63lcw9qa  于 2022-12-23  发布在  其他
关注(0)|答案(1)|浏览(92)

我想使这些二维线循环旋转,这样当鼠标滚动时,线向上或向下移动。例如:-如果鼠标滚轮向上滚动,顶部的线移动到底部,所有的线向上移动。我可以通过改变数据来做到这一点,但这是一个缓慢的过程。有没有办法在OpenGl中使用着色器来做到这一点?
代码:-

import OpenGL.GL as gl
import OpenGL.arrays.vbo as glvbo
from PyQt5.Qt import *
import numpy as np
import sys
import copy

VS1 = '''
#version 450

layout(location = 0) in vec2 position;

uniform float right;
uniform float bottom;
uniform float left;
uniform float top;

void main() {
    const float far = 1.0;
    const float near = -1.0;

    mat4 testmat = mat4(
            vec4(2.0 / (right - left), 0, 0, 0),
            vec4(0, 2.0 / (top - bottom), 0, 0),
            vec4(0, 0, -2.0 / (far - near), 0),
            vec4(-(right + left) / (right - left), -(top + bottom) / (top - bottom), -(far + near) / (far - near), 1)
    );

    gl_Position = testmat * vec4(position.x, position.y, 0., 1.);

}
'''

FS1 = '''
#version 450
// Output variable of the fragment shader, which is a 4D vector containing the
// RGBA components of the pixel color.

uniform vec3 triangleColor;
out vec4 outColor;

void main()
{   

    outColor = vec4(triangleColor, 1.0);

}

'''

def compile_vertex_shader(source):
    """Compile a vertex shader from source."""
    vertex_shader = gl.glCreateShader(gl.GL_VERTEX_SHADER)
    gl.glShaderSource(vertex_shader, source)
    gl.glCompileShader(vertex_shader)
    # check compilation error
    result = gl.glGetShaderiv(vertex_shader, gl.GL_COMPILE_STATUS)
    if not (result):
        raise RuntimeError(gl.glGetShaderInfoLog(vertex_shader))
    return vertex_shader

def compile_fragment_shader(source):
    """Compile a fragment shader from source."""
    fragment_shader = gl.glCreateShader(gl.GL_FRAGMENT_SHADER)
    gl.glShaderSource(fragment_shader, source)
    gl.glCompileShader(fragment_shader)

    result = gl.glGetShaderiv(fragment_shader, gl.GL_COMPILE_STATUS)
    if not (result):
        raise RuntimeError(gl.glGetShaderInfoLog(fragment_shader))
    return fragment_shader

def link_shader_program(vertex_shader, fragment_shader):
    """Create a shader program with from compiled shaders."""
    program = gl.glCreateProgram()
    gl.glAttachShader(program, vertex_shader)
    gl.glAttachShader(program, fragment_shader)
    gl.glLinkProgram(program)

    result = gl.glGetProgramiv(program, gl.GL_LINK_STATUS)
    if not (result):
        raise RuntimeError(gl.glGetProgramInfoLog(program))
    return program

class GLPlotWidget(QGLWidget):

    def __init__(self, *args):
        super(GLPlotWidget, self).__init__()
        self.width, self.height = 100, 100
        self.we = np.load('two.npy', mmap_mode='r')
        self.e = copy.deepcopy(self.we[:, :, :])
        self.w = copy.deepcopy(self.we[:, :, :])
        for i in range(0, 24):
            self.w[i, :, 1] = self.e[i, :, 1] - np.array(9999 * i)
        # self.e[:, :, 1] = np.interp(self.e[:, :, 1], (self.e[:, :, 1].min(), self.e[:, :, 1].max()),
        #                                                                           (-1, 1))
        #
        # self.e[:, :, 0] = np.interp(self.e[:, :, 0], (self.e[:, :, 0].min(), self.e[:, :, 0].max()),
        #                                                                           (-1, +1))
        self.right, self.left, self.top, self.bottom = self.e[0, -1, 0], self.e[
            0, 0, 0], self.e[0, :, 1].max(), self.e[-1, :, 1].min()
        self.vbo = glvbo.VBO(self.e)
        self.count = self.vbo.shape[1]
        self.scroll = 0
        self.number_of_arm = 24
        self.sensor_list_const = np.array(range(0, self.number_of_arm))
        self.sensor_list_roll = copy.deepcopy(self.sensor_list_const)
        self.showMaximized()

    def initializeGL(self):

        vs = compile_vertex_shader(VS1)
        fs = compile_fragment_shader(FS1)
        self.shaders_program_plot = link_shader_program(vs, fs)

    def ortho_view(self, i):

        right = gl.glGetUniformLocation(i, "right")
        gl.glUniform1f(right, self.right)

        left = gl.glGetUniformLocation(i, "left")
        gl.glUniform1f(left, self.left)

        top = gl.glGetUniformLocation(i, "top")
        gl.glUniform1f(top, self.top)

        bottom = gl.glGetUniformLocation(i, "bottom")
        gl.glUniform1f(bottom, self.bottom)

    def paintGL(self):
        self.resizeGL(self.width, self.height)
        gl.glClearColor(0.75, 0.75, 0.75, 0)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
        gl.glEnable(gl.GL_DEPTH_TEST)

        self.vbo.bind()
        gl.glEnableVertexAttribArray(0)
        gl.glVertexAttribPointer(0, 2, gl.GL_FLOAT, gl.GL_FALSE, 0, None)
        gl.glUseProgram(self.shaders_program_plot)
        self.ortho_view(self.shaders_program_plot)
        uni_color = gl.glGetUniformLocation(self.shaders_program_plot, "triangleColor")

        for i in range(0, self.vbo.data.shape[0]):
            gl.glUniform3f(uni_color, 0, 0, 0)
            gl.glLineWidth(1)
            gl.glDrawArrays(gl.GL_LINE_STRIP, i * self.count, self.count)
        self.vbo.unbind()
        # self.greyscale()
        # gl.glUseProgram(0)

    def reset_vbo(self):
        self.wex = copy.deepcopy(self.w)

        for i, j in zip(self.sensor_list_roll, self.sensor_list_const):
            self.wex[j, :, 1] = self.w[j, :, 1] + np.array(9999 * (self.number_of_arm - i))

        self.vbo.set_array(self.wex)
        self.right, self.left, self.top, self.bottom = self.wex[0, -1, 0], self.wex[
            0, 0, 0], self.wex[:, :, 1].max(), self.wex[:, :, 1].min()
        self.update()

    def resizeGL(self, width, height):
        self.width, self.height = width, height
        gl.glViewport(0, 0, width, height)

    def wheelEvent(self, *args, **kwargs):
        event = args[0]
        # print(event.angleDelta().y())
        if event.angleDelta().y() > 0:

            self.scroll = self.scroll - 1

        else:
            self.scroll = self.scroll + 1

        if self.scroll > self.number_of_arm - 1 or self.scroll < -(self.number_of_arm - 1):
            self.scroll = 0
        self.sensor_list_roll = np.roll(self.sensor_list_const, self.scroll)
        # self.patch_move(event)
        self.reset_vbo()

def main():
    app = QApplication(sys.argv)
    editor = GLPlotWidget()
    editor.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

数据文件:-https://drive.google.com/file/d/1y6w35kuMguR1YczK7yMJpXU86T6qtGSv/view?usp=sharing

aor9mmx1

aor9mmx11#

您可以使用制服topbottom滚动视图。您必须为每一行单独设置。
根据投影的y标度计算滚动因子self.scroll

class GLPlotWidget(QGLWidget):
    # [...]

    def wheelEvent(self, *args, **kwargs):
        event = args[0]

        scroll_scale = 0.01
        size = self.top - self.bottom
        if event.angleDelta().y() > 0:
            self.scroll = self.scroll - size * scroll_scale
            if self.scroll < 0:
                self.scroll += size
        else:
            self.scroll = self.scroll + size * scroll_scale
            if self.scroll > size:
                self.scroll -= size

此外,你还必须知道每一行的y范围,计算每一行的最小值和最大值,并将其存储到一个列表(self.linerange)中:

class GLPlotWidget(QGLWidget):

    def __init__(self, *args):
        # [...]

        self.linerange = [(self.e[li, :, 1].max(), self.e[-li, :, 1].min()) for li in range(self.vbo.shape[0])]

将每行的self.topself.bottom移动self.scroll

top, bottom = self.top+self.scroll, self.bottom+self.scroll

如果线条从窗口底部“掉出”,则将其提升到视图顶部:

if self.linerange[i][0]-self.scroll < self.bottom:
    top, bottom = top-size, bottom-size

完整的paintGL方法:

class GLPlotWidget(QGLWidget):
    # [...]

    def paintGL(self):
        self.resizeGL(self.width, self.height)
        gl.glClearColor(0.75, 0.75, 0.75, 0)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
        gl.glEnable(gl.GL_DEPTH_TEST)

        self.vbo.bind()
        gl.glEnableVertexAttribArray(0)
        gl.glVertexAttribPointer(0, 2, gl.GL_FLOAT, gl.GL_FALSE, 0, None)
        gl.glUseProgram(self.shaders_program_plot)
        self.ortho_view(self.shaders_program_plot)
        uni_color  = gl.glGetUniformLocation(self.shaders_program_plot, "triangleColor")
        loc_top    = gl.glGetUniformLocation(self.shaders_program_plot, "top")
        loc_bottom = gl.glGetUniformLocation(self.shaders_program_plot, "bottom")

        for i in range(0, self.vbo.data.shape[0]):

            size = self.top - self.bottom
            top, bottom = self.top+self.scroll, self.bottom+self.scroll
            if self.linerange[i][0]-self.scroll < self.bottom:
                top, bottom = top-size, bottom-size
            gl.glUniform1f(loc_top, top)
            gl.glUniform1f(loc_bottom, bottom)

            gl.glUniform3f(uni_color, 0, 0, 0)
            gl.glLineWidth(1)
            gl.glDrawArrays(gl.GL_LINE_STRIP, i * self.count, self.count)
        self.vbo.unbind()

相关问题