pythonopenglpyglet

Prevent pyglet shapes from blending


I have the following code.

import pyglet

window = pyglet.window.Window(fullscreen=True)
batch = pyglet.graphics.Batch()

rectangle1 = pyglet.shapes.Rectangle(100, 100, 80, 80, color=(128, 0, 0, 128), batch=batch)
rectangle2 = pyglet.shapes.Rectangle(150, 150, 80, 80, color=(128, 0, 0, 128), batch=batch)

@window.event
def on_draw():
    window.clear()
    
    batch.draw()

pyglet.app.run()

Here is the result:

Rectangles

I want to prevent that the color of the two rectangles add. Instead, the whole area should have just one color.

I googled, found the pyglet.graphics module and the commands glEnable(GL_BLEND), glDisable(GL_BLEND), and glBlendFunc(GL_ONE, GL_ZERO). All of these do not have the desired effect or any effect at all. I suspect that they do not affect the classes from the shapes library. I also asked chatGPT, no good results.


Solution

  • Change the color to solid color without opacity then you will be able to achieve this color=(128, 0, 0)

    or you have to custom draw the requirment

    fbo1 = GLuint()
    glGenFramebuffers(1, fbo1)
    texture1 = GLuint()
    glGenTextures(1, texture1)
    glBindTexture(GL_TEXTURE_2D, texture1)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, window.width, window.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, None)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glBindFramebuffer(GL_FRAMEBUFFER, fbo1)
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture1, 0)
    

    then you can get output like

    output image