Python and OpenGL do not blend squares well.

Asked 2 years ago, Updated 2 years ago, 93 views

I want to display a square above the image with PyOpenGL(3.1.1a1)+Python3.7, but the behavior is strange.

A PNG image is textured and a translucent black square is displayed thereon.
The rectangle is one size smaller.

The square can be displayed normally, but left-clicking the mouse will cause the display to go wrong.

Left-click before

Before Left Click

Left-click

left-click

Here's the source code

import sys
from OpenGL.GL import*
from OpenGL.GLU import*
from OpenGL.GLUT import*
from win32 api import GetSystemMetrics
from PIL import Image

defload_texture():
    img=Image.open("PIL_capture.png")
    w,h = img.size
    glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, img.tobytes())

def ScreenShort():
    glPushMatrix()
    glClearColor (1.0, 1.0, 0.0, 0.0)# Background Color ##gl Initialization
    glClear(GL_COLOR_BUFFER_BIT)

    
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
    glEnable(GL_BLEND)##gl initialization#Is blending enabled?

    glEnable(GL_TEXTURE_2D)## Enable gl initialization## Texture

    glBegin(GL_QUADS)
    glTexCoord 2d (0.0, 1.0)
    glVertex3d (-1.0, -1.0, 0.0)
    glTexCoord 2d (1.0, 1.0)
    glVertex3d (1.0, -1.0, 0.0)
    glTexCoord 2d (1.0, 0.0)
    glVertex 3d (1.0, 1.0, 0.0)
    glTexCoord 2d (0.0, 0.0)
    glVertex3d (-1.0, 1.0, 0.0)
    glEnd()
    glDisable(GL_TEXTURE_2D)
    glDisable(GL_BLEND)
    glFlush()
    glPopMatrix()

def sikakukei():    
     ## square
    glutSwapBuffers() 
    glPushMatrix()
    glColor4f (0.0, 0.0, 0.0, 0.4)# Texture Color Specification ##Black ##Transparency ##Adjustment
    glEnable(GL_BLEND)## Blend Enabled
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
    glBegin(GL_POLYGON)
    glVertex2d (-0.9, -0.9)## Texture vertex specification
    glVertex2d (0.9, -0.9)
    glVertex2d (0.9,0.9)
    glVertex2d (-0.9,0.9)
    glEnd()
    glPopMatrix()
    glFlush()##Forced Run
# def haikei():
#     glClearColor (1.0, 1.0, 1.0, 0.0)## Background Color ## White
    ## Buffer Deleted
def display():#Drawing a figure
    #glClear(GL_COLOR_BUFFER_BIT)
    glPushMatrix()
    ScreenShort()
    glPopMatrix()
    Save to glPushMatrix()## conversion matrix l Do not move when redrawing event occurs.Combine with glPopMatrix().
    # glRotated (1.0, 1.0, 1.0, 1.0)
    sikakukei()
    glPopMatrix()#
defmain():## GLUT configuration   
    glutInit(sys.argv)##GLUT Initialization
    glutInitDisplayMode(GLUT_RGBA)## Specify the mode to use when drawing OpenGL
    w = GetSystemMetrics(0)# Get Horizontal Pixels
    h = GetSystemMetrics(1)# Get vertical pixels
    glutGameModeString(str(w)+"x"+str(h)+":32@60")## Screen Settings in Game Mode
#     glutGameModeString ("1920x1080:32@60")## Screen Settings in Game Mode
    glutEnterGameMode()##glutEnterGameMode
    ##glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
    glutSwapBuffers()    
    load_texture()
    glutDisplayFunc(display)#Write before drawing process glutMainLoop()
    
     ## Specify how to zoom in and out of textures*/
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    
    ###glutIdleFunc (glutPostRedisplay()) 
    glutKeyboardFunc (keyboard)
    
    glutMainLoop()
        
## Statement to prevent programs from moving when imported
## http://blog.pyq.jp/entry/Python_kaiketsu_180207
if__name__=='__main__':
    main()

python python3 anaconda opengl

2022-09-29 21:27

1 Answers

We did not reproduce the problem, but how about the following corrections?


2022-09-29 21:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.