简体   繁体   English

使用 pyopengl glTexImage2D 时出现 OSError

[英]OSError when using pyopengl glTexImage2D

Traceback (most recent call last):
  File "./x.py", line 132, in <module>
    texid = glutils.loadTexture("wall.png")
  File "C:\Users\6e25h\Desktop\Mess\py-opengl-error\glutils.py", line 31, in loadTexture
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.size[0], img.size[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, imgData)
  File "src/latebind.pyx", line 39, in OpenGL_accelerate.latebind.LateBind.__call__
  File "src/wrapper.pyx", line 311, in OpenGL_accelerate.wrapper.Wrapper.__call__
  File "C:\Users\6e25h\AppData\Local\Programs\Python\Python37\lib\site-packages\OpenGL\platform\baseplatform.py", line 415, in __call__
    return self( *args, **named )
OSError: exception: access violation reading 0x0000020F6D752000

This error I can't solve... I think I can't describe more detailed My Code:这个错误我无法解决......我想我无法描述更详细的我的代码:

def loadTexture(filename):
    """load OpenGL 2D texture from given image file"""
    img = Image.open(filename) 
    imgData = numpy.array(list(img.getdata()), np.int8)
    texture = glGenTextures(1)
    glPixelStorei(GL_UNPACK_ALIGNMENT,1)
    glBindTexture(GL_TEXTURE_2D, texture)
    glPixelStorei(GL_UNPACK_ALIGNMENT,1)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.size[0], img.size[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, imgData)
    glBindTexture(GL_TEXTURE_2D, 0)
    return texture

How I can solve this problem?Help me,Thanks我该如何解决这个问题?帮帮我,谢谢

The images doesn't seem to have 4 color channels.图像似乎没有 4 个颜色通道。 Most likely the image has just 3 color channels.该图像很可能只有 3 个颜色通道。 You've to specify the format GL_RGB rather than GL_RGBA in glTexImage2D .您必须在glTexImage2D中指定格式GL_RGB而不是GL_RGBA For instance:例如:

format = GL_RGB if imgData.shape[1] == 3 else GL_RGBA
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.size[0], img.size[1], 0, 
    format, GL_UNSIGNED_BYTE, imgData)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM