[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] pygame.surfarray.pixels2d does not work with OpenGL?



Hi everyone,

can anyone tell me what I am doing wrong here?
Please have a look at the attached example code.

When using pygame,image.tostring for getting the pixel values to
transform them into a texture, everything works well (see the
outcommented part with WORKS).

However, when I replace this part with pygame.surfarray.pixels2d, then
the resulting texture seems broken for some reason.

Does anyone have an idea what is going on here?

Thanks in advance,
Florian
import time

import pygame
import OpenGL.GL as ogl
import OpenGL.GLU as oglu

pygame.init()
screensize = (pygame.display.Info().current_w,
              pygame.display.Info().current_h)
screen = pygame.display.set_mode(screensize,
                                 pygame.DOUBLEBUF | pygame.OPENGL | pygame.FULLSCREEN)

surf = pygame.image.load("expyriment_logo.png").convert_alpha()

txtr = ogl.glGenTextures(1)

### WORKS ###
#textureData = pygame.image.tostring(surf, "RGBA", 1)
### /WORKS ###

### DOESN NOT WORK ###
textureData = pygame.surfarray.pixels2d(surf)
### /DOES NOT WORK ###

ogl.glEnable(ogl.GL_TEXTURE_2D)
ogl.glBindTexture(ogl.GL_TEXTURE_2D, txtr)
width, height = surf.get_size()
ogl.glTexImage2D(ogl.GL_TEXTURE_2D, 0, ogl.GL_RGBA, width, height, 0,
                 ogl.GL_RGBA, ogl.GL_UNSIGNED_BYTE, textureData)
ogl.glTexParameterf(ogl.GL_TEXTURE_2D,
                    ogl.GL_TEXTURE_MAG_FILTER,
                    ogl.GL_NEAREST)
ogl.glTexParameterf(ogl.GL_TEXTURE_2D,
                    ogl.GL_TEXTURE_MIN_FILTER,
                    ogl.GL_NEAREST)
ogl.glDisable(ogl.GL_TEXTURE_2D)

bottomleft = oglu.gluUnProject(0, height , 0)
bottomright = oglu.gluUnProject(width, height, 0)
topleft = oglu.gluUnProject(0, 0, 0)
topright = oglu.gluUnProject(width,0, 0)
dims = bottomleft, bottomright, topright, topleft

ogl.glEnable(ogl.GL_BLEND)
ogl.glBlendFunc(ogl.GL_SRC_ALPHA, ogl.GL_ONE_MINUS_SRC_ALPHA)
ogl.glEnable(ogl.GL_TEXTURE_2D)
ogl.glBindTexture(ogl.GL_TEXTURE_2D, txtr)
ogl.glTexEnvf(ogl.GL_TEXTURE_ENV, ogl.GL_TEXTURE_ENV_MODE,
              ogl.GL_REPLACE)
ogl.glTexParameterfv(ogl.GL_TEXTURE_2D, ogl.GL_TEXTURE_MIN_FILTER,
                   ogl.GL_LINEAR)
ogl.glBegin(ogl.GL_QUADS)
ogl.glTexCoord2f(0.0, 1.0)
ogl.glVertex3f(*dims[0])
ogl.glTexCoord2f(1.0, 1.0)
ogl.glVertex3f(*dims[1])
ogl.glTexCoord2f(1.0, 0.0)
ogl.glVertex3f(*dims[2])
ogl.glTexCoord2f(0.0, 0.0)
ogl.glVertex3f(*dims[3])
ogl.glEnd()
ogl.glDisable(ogl.GL_BLEND)
ogl.glDisable(ogl.GL_TEXTURE_2D)

pygame.display.flip()
time.sleep(3)

Attachment: expyriment_logo.png
Description: PNG image