pyopengl需要调试帮助,glUniform1f用于sampler2d类型错误

问题描述

import pygame
from PIL import Image
import math
import ctypes
import numpy as np
from pygame.locals import *
from OpenGL.GL import shaders
from OpenGL.GL import *
 

models = [-0.5,0.5,2.0,0.0,1.0,-0.5,]

         
vertex_shader="""
#version 430
in vec3 position;
in vec2 texturecoordinate;
out vec2 texcoordinate;
void main()
{
    gl_Position = vec4(position,1.0);
    texcoordinate = texturecoordinate;
    
}
"""
fragment_shader="""
#version 430
precision mediump float;
in vec2 texcoordinate;
uniform sampler2D texturergbadata;
out vec4 colour;

void main()
{
    colour = texture2D(texturergbadata,texcoordinate);
}
"""

shader = OpenGL.GL.shaders.compileProgram(OpenGL.GL.shaders.compileShader(vertex_shader,GL_VERTEX_SHADER),OpenGL.GL.shaders.compileShader(fragment_shader,GL_FRAGMENT_SHADER))

VBO=glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER,VBO)
glBufferData(GL_ARRAY_BUFFER,len(models)*4,None,GL_STATIC_DRAW)

position = glGetAttribLocation(shader,"position")
glVertexAttribPointer(position,3,GL_FLOAT,False,20,ctypes.c_void_p(0))
glEnabLevertexAttribArray(position)

image = Image.open('Box.png')
rgbadata = list(image.getdata())
width = 200
height = 151

texture = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D,texture)
glTexImage2D(GL_TEXTURE_2D,GL_RGBA,width,height,GL_UNSIGNED_BYTE,rgbadata)

glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_MIRRORED_REPEAT)
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST)

texturecoordinate = glGetAttribLocation(shader,"texturecoordinate")
glEnabLevertexAttribArray(texturecoordinate)
glVertexAttribPointer(texturecoordinate,2,ctypes.c_void_p(12))
glEnabLevertexAttribArray(texturecoordinate)

texture = glGetUniformlocation(shader,"texturergbadata")
glActiveTexture(GL_TEXTURE0)
gluniform1f(texture,ctypes.c_float(0))


glEnable(GL_DEPTH_TEST)

gluseProgram(shader)

这是我的代码,我被卡在这错误中,我已经调试了好几天了,我彻底陷入困境。 id喜欢在这上面有新的眼光。此代码的主要问题是此行:

gluniform1f(texture,ctypes.c_float(0))

此值进入我的着色器,但是无法在python中使用此函数的整数变体,因为python ctypes库将所有c类型整数认为c_long,这是我在论坛上的帖子:How to convert python integers into c integers?所以我使用的是float版本,但我仍然遇到此错误

    Traceback (most recent call last):
  File "C:C:\path to file(this isnt what the actual error shows)",line 233,in <module>
    main()
  File "C:\path to file(this isnt what the actual error shows)",line 184,in main
    gluniform1f(texture,ctypes.c_float(0))
  File "C:\Program Files (x86)\python38-32\lib\site-packages\OpenGL\platform\baseplatform.py",line 415,in __call__
    return self( *args,**named )
  File "C:\Program Files (x86)\python38-32\lib\site-packages\OpenGL\error.py",line 230,in glCheckerror
    raise self._errorClass(
OpenGL.error.GLError: GLError(
    err = 1282,description = b'invalid operation',baSEOperation = gluniform1f,cArguments = (0,c_float(0.0))
)

解决方法

不能将整数和浮点数,句号混合使用。即使是有符号和无符号整数也不能在OpenGL中混合使用。因此,要么找到一种调用glUniform1i的方法,要么使用layout(binding = 0)限定符。有关详细信息,请参见OpenGL Wiki