Pyopengl中是否有任何Inbuild函数会在鼠标悬停在对象上时选择对象?

问题描述 投票:0回答:1

我想在pyopengl中将鼠标移到对象上时在Pyopengl中添加对象突出显示。我可以通过将鼠标位置与数据匹配来在python中完成此操作,但是在旋转数据时失败。在Pyopengl中有什么方法可以做同样的事情吗?代码:-

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

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);

}


'''

VS = '''
#version 450

attribute vec2 position;
attribute vec3 a_Color;


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

out vec3 g_color;

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.);
    g_color = a_Color;
}
'''

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

in vec3 g_color;
out vec4 outColor;

void main()
{

    outColor = vec4(g_color, 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[:, :, :])

        origshape = self.e.shape[:]
        origmin, origmax = self.e[0, :, 1].max(), self.e[-1, :, 1].min()
        newmin, newmax = origmin, origmax
        self.first = self.e[0, :, :].copy().reshape((1, *origshape[1:]))
        self.last = self.e[-1, :, :].copy().reshape((1, *origshape[1:]))
        self.first[:, :, 1] = newmin
        self.last[:, :, 1] = newmax
        self.e = np.concatenate((self.first, self.e, self.last))

        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.linerange = [(self.e[li, :, 1].min(), self.e[-li, :, 1].min()) for li in range(self.vbo.shape[0])]
        self.setMouseTracking(True)
        self.current_mouse_on_sensor = -1
        self.showMaximized()

    def initializeGL(self):
        vs = compile_vertex_shader(VS1)
        fs = compile_fragment_shader(FS1)
        self.shaders_program_plot = link_shader_program(vs, fs)
        self.greyscale_data()

    def greyscale_data(self):
        self.color = np.zeros((self.e.shape[1] * (self.e.shape[0]), 3), dtype=np.float32)
        i = 23
        a = self.e[i, :, 1].min()
        b = self.e[i, :, 1].max()
        c = np.interp(self.e[i, :, 1], (a, b), (0.15, 0.85))
        i = 0
        self.color[self.e.shape[1] * i:self.e.shape[1] * (i + 1), 0] = c
        self.color[self.e.shape[1] * i:self.e.shape[1] * (i + 1), 1] = c
        self.color[self.e.shape[1] * i:self.e.shape[1] * (i + 1), 2] = c

        for i in range(1, 25):
            a = self.e[i, :, 1].min()
            b = self.e[i, :, 1].max()
            c = np.interp(self.e[i, :, 1], (a, b), (0.15, 0.85))
            self.color[self.e.shape[1] * i:self.e.shape[1] * (i + 1), 0] = c
            self.color[self.e.shape[1] * i:self.e.shape[1] * (i + 1), 1] = c
            self.color[self.e.shape[1] * i:self.e.shape[1] * (i + 1), 2] = c
        i = 1
        a = self.e[i, :, 1].min()
        b = self.e[i, :, 1].max()
        c = np.interp(self.e[i, :, 1], (a, b), (0.15, 0.85))
        # d = np.array([c, c, c])
        i = 25
        self.color[self.e.shape[1] * i:self.e.shape[1] * (i + 1), 0] = c
        self.color[self.e.shape[1] * i:self.e.shape[1] * (i + 1), 1] = c
        self.color[self.e.shape[1] * i:self.e.shape[1] * (i + 1), 2] = c
        self.elems = []
        b = self.e.shape[1]  # number of points per line
        a = self.e.shape[0]  # total number of arms

        for i in range(0, a-1):
                for j in range(0, b-1):
                    self.elems += [j + b * i, j + b * i + 1, j + b * (i + 1)]
                    self.elems += [j + b * (i + 1), j + b * (i + 1) + 1, j + b * i + 1]

        self.elems = np.array(self.elems, dtype=np.int32)
        # print(self.elems[0:100])
        vs = compile_vertex_shader(VS)
        fs = compile_fragment_shader(FS)
        self.shaders_program = link_shader_program(vs, fs)

        self.vertexbuffer = gl.glGenBuffers(1)
        gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.vertexbuffer)
        gl.glBufferData(gl.GL_ARRAY_BUFFER, self.e, gl.GL_DYNAMIC_DRAW)

        self.elementbuffer = gl.glGenBuffers(1)
        gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, self.elementbuffer)
        gl.glBufferData(gl.GL_ELEMENT_ARRAY_BUFFER, self.elems, gl.GL_DYNAMIC_DRAW)

        self.colorbuffer = gl.glGenBuffers(1)
        gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.colorbuffer)
        gl.glBufferData(gl.GL_ARRAY_BUFFER, self.color, gl.GL_DYNAMIC_DRAW)

    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 greyscale(self):
        gl.glUseProgram(self.shaders_program)
        self.ortho_view(self.shaders_program)
        gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.vertexbuffer)

        stride = 0  # 3*self.e.itemsize
        offset = None  # ctypes.c_void_p(0)
        loc = gl.glGetAttribLocation(self.shaders_program, 'position')
        gl.glEnableVertexAttribArray(loc)
        gl.glVertexAttribPointer(loc, 2, gl.GL_FLOAT, False, stride, offset)

        gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, self.elementbuffer)

        loc = gl.glGetAttribLocation(self.shaders_program, 'a_Color')
        gl.glEnableVertexAttribArray(loc)
        gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.colorbuffer)
        gl.glVertexAttribPointer(loc, 3, gl.GL_FLOAT, False, stride, offset)

        loc_top1 = gl.glGetUniformLocation(self.shaders_program, "top")
        loc_bottom1 = gl.glGetUniformLocation(self.shaders_program, "bottom")
        for i in range(0, self.e.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_top1, top)
            gl.glUniform1f(loc_bottom1, bottom)
            c = int(self.elems.size/(self.e.shape[0]-1))
            f = ctypes.c_void_p(4*c*i)
            gl.glDrawElements(gl.GL_TRIANGLE_STRIP, c, gl.GL_UNSIGNED_INT, f)

    def paintGL(self):
        self.resizeGL(self.width, self.height)
        gl.glClearColor(0.5, 0.5, 0.5, 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(1, self.vbo.data.shape[0]-1):

            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)
            if i == self.current_mouse_on_sensor:
                gl.glUniform3f(uni_color, 1, 6, 220/255)
                gl.glLineWidth(2.5)
            else:
                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 resizeGL(self, width, height):
        self.width, self.height = width, height
        gl.glViewport(0, 0, width, height)

    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
        self.update()

    def find_nearest(self, array, value):
        idx = np.searchsorted(array, value, side="left")
        if idx > 0 and (idx == len(array) or math.fabs(value - array[idx - 1]) < math.fabs(value - array[idx])):
            return idx - 1
        else:
            return idx

    def mouseMoveEvent(self, event):
        highlight_pos = [0, 0]
        highlight_pos[0], highlight_pos[1] = event.pos().x(), event.pos().y()
        mouse_highlight_x = highlight_pos[0] / self.width
        mouse_highlight_y = highlight_pos[1] / self.height
        mouse_high_x = (mouse_highlight_x * (self.right - self.left)) + self.left
        mouse_high_y = (mouse_highlight_y * (self.bottom - self.top)) + self.top

        x_index = self.find_nearest(self.e[0, :, 0], mouse_high_x)
        temp_y = self.e[1:25, x_index, 1]
        y_data = min(temp_y, key=lambda x: abs(x - mouse_high_y))
        y_index = np.where(temp_y == y_data)[0][0]
        self.current_mouse_on_sensor = y_index
        self.update()


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

python python-3.x opengl pyopengl
1个回答
0
投票

[计算C0]时必须考虑滚动偏移(self.scroll)。将mouse_high_y限制为[mouse_high_yself.bottom]:

self.top
© www.soinside.com 2019 - 2024. All rights reserved.