Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

python - PyOpenGL or OpenGL picking question (Not color picking)

I have a small python program that uses PyOpenGL libraries to draw to spheres. The code has the below process

  • draw spheres

    if there is a hit, then uses the below

    SELECT_BUFFER_SIZE = 512
    x, y = event.x(), event.y()
    # required to call this to force PyQt to read from the correct, updated buffer
    viewport = glGetIntegerv(GL_VIEWPORT)
    # print viewport
    w = viewport[2] - viewport[0]
    h = viewport[3] - viewport[1]
    aspect_ratio = w / h
    
    
    glSelectBuffer(SELECT_BUFFER_SIZE)
    glRenderMode(GL_SELECT)
    
    glInitNames()
    glPushName(0)
    
    glMatrixMode(GL_PROJECTION)
    glPushMatrix()
    glLoadIdentity()
    gluPickMatrix(x, viewport[3] - y, 5, 5, viewport)
    
    glOrtho( *** setting the schene***) 
    
    **-draw spheres using glLoadName(for each)**
    
    glMatrixMode(GL_PROJECTION)
    glPopMatrix()
    glFlush()
    
    buffer = glRenderMode(GL_RENDER)
    
    # print buffer
    for hit_record in buffer:
        _, _, names = hit_record
        print(names)
    

But when I try to print the names, although I hit only one of them on the window, both names are printed. What should be the solution for that

In short, I am trying to draw three spheres(in white color) in a Pyqt5 window using PyOpenGL with the same colors, but I am trying to make them eligible for picking. I have also the capabilities of rotating, moving, and zoom in-out. So these capabilities should not break picking when they are done.(As it is asked in the comment, it is not homework. It is a side project that I work on for the last 2 years. So this problem is only a really small portion of my code.)

question from:https://stackoverflow.com/questions/65836295/pyopengl-or-opengl-picking-question-not-color-picking

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm not too sure at all that this will help
and I confess that I didn't read the actual code,
and I didn't quite understood if the spheres were colliding between them or with the border itself

I once had a tiny bit similar problem that I couldn't understand for too much time.
but basically a hit was printed twice because both of the objects were registering a hit,
so whenever they've hit each other two hit messages were printed.

I used their ID(or place in a list or something similar)
so only one of them (the one with the bigger ID) will print the message

I have no idea if that helps but it might just be it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...