pyOpenGL:创建方向光线时出现问题

2024-06-08 04:43:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python编写一个程序,用户可以单击屏幕,它将根据窗口坐标发送光线。我写了一个光线方向函数,但是即使鼠标移动,方向向量中的值也不会有太大的变化,我看到的最大变化是x,y和z值的第14个小数点。我很好奇我的代码是否有问题。你知道吗

我的代码如下图所示:

from PyQt5 import QtCore, QtGui, QtOpenGL, QtWidgets
from OpenGL.GL import *
from OpenGL.GLU import *

def generate_ray(mouse_x, mouse_y):

    projection_matrix = glGetDoublev(GL_PROJECTION_MATRIX)
    model_matrix = glGetDoublev(GL_MODELVIEW_MATRIX)
    view_matrix = glGetIntegerv(GL_VIEWPORT)

    corrected_y = view_matrix[3] - mouse_y

    near_x, near_y, near_z = gluUnProject(mouse_x,corrected_y,0,model_matrix,projection_matrix,view_matrix)
    far_x, far_y, far_z = gluUnProject(mouse_x,corrected_y,1,model_matrix,projection_matrix,view_matrix)

    direction = [far_x - near_x]
    direction.append(far_y - near_y)
    direction.append(far_z - near_z)

    print("ray direction: x: {} y: {} z: {}".format(direction[0], direction[1], direction[2]))
    return direction

如果这有帮助,我的射线三角形相交测试如下。光线方向、原点和垂直列表都是numpy数组。你知道吗

import numpy

def crossProduct(x1, x2):

    """array[0] is the x-coord, array[1] is the y coord, and array[2] is the z coord """   
    COORDS_PER_VERTEX = 3
    cross_product_array = numpy.empty([COORDS_PER_VERTEX])
    #print(x1[0])
    #print(x2[2])
    cross_product_array[0] = x1[1] * x2[2] - x1[2] * x2[1]
    cross_product_array[1] = x1[2] * x2[0] - x1[0] * x2[2]
    cross_product_array[2] = x1[0] * x2[1] - x1[1] * x2[0]  
    return cross_product_array

def dotProduct(x1, x2):
    """array[0] is the x-coord, array[1] is the y coord, and array[2] is the z coord """
    result = x1[0] * x2[0] + x1[1]  * x2[1] + x1[2] * x2[2]
    return result

def ray_triangle_intersection_test(ray_origin, ray_direction, vertice_list):
    EPSILON = 0.0000001 
    NEG_EPSILON = -0.0000001 

    edge1 = vertice_list[1] - vertice_list[0]
    #print(edge1)
    edge2 = vertice_list[2] - vertice_list[0]
    #print(edge2)
    pvec = crossProduct(ray_direction,edge2)
    print("pvec: %s" %pvec)
    det = dotProduct(edge1,pvec)
    print("det: %s" %det)
    if det > NEG_EPSILON and det < EPSILON:
        print("parallel")
        return # This ray is parallel to this triangle.
    invDet = 1.0/det
    print("invDet: %s" %invDet)
    svec = ray_origin - vertice_list[0]
    u = invDet * (dotProduct(svec,pvec))
    print("u: %s" %u)
    """
    if u < 0.0 or u > 1.0:
        print("false1")
        return 
    """
    qvec = crossProduct(svec, edge1)
    v = invDet * dotProduct(ray_direction, qvec)
    """
    if v < 0.0 or u + v > 1.0:
        print("false2")
        return
    """
    # At this stage we can compute t to find out where the intersection point is on the line.
    t = invDet * dotProduct(edge2, qvec)
    print("t: %s" %t)
    if t > EPSILON: # ray intersection
        outIntersectionPoint = numpy.multiply((ray_origin + ray_direction),t) 
        return outIntersectionPoint 
    else: # This means that there is det line intersection but not det ray intersection.
        print("false3")
        return

Tags: thereturnisarraymatrixprintx1x2

热门问题