我正在使用带有GLES2和EGL的PyOpenGL库,以在带有Mali400 GPU的NanoPi M1 Plus上运行程序。 glVertexAttribPointer函数给我一个错误,说:NameError:未定义名称'ArrayDatatype'。但是,如果我使用OpenGL.GL.glVertexAttribPointer,它不会给我任何错误,但是,其余代码都是使用GLES2编写的。我不确定我是否可以使用OpenGL.GL进行此调用,还是我传递的参数错误?
代码行引发问题:
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(ctypes.c_float), ctypes.c_void_p(0*sizeof(ctypes.c_float)) )
缓冲数据代码:
vbo = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER,vbo)
glBufferData(GL_ARRAY_BUFFER, sizeof(ctypes.c_float) * len(vertices), (ctypes.c_float * len(vertices))(*vertices), GL_STATIC_DRAW)
glEnableVertexAttribArray(0)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
错误:
Traceback (most recent call last):
File "ferb_gpu_1.py", line 219, in <module>
vec_add_gpu(A, B, C, 1000)
File "ferb_gpu_1.py", line 206, in vec_add_gpu
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(ctypes.c_float), ctypes.c_void_p(0*sizeof(ctypes.c_float)) )
File "/home/fa/berryconda3/lib/python3.6/site-packages/OpenGL/latebind.py", line 61, in __call__
return self.wrapperFunction( self.baseFunction, *args, **named )
File "/home/fa/berryconda3/lib/python3.6/site-packages/OpenGL/GLES2/VERSION/GLES2_2_0.py", line 400, in glVertexAttribPointer
array = ArrayDatatype.asArray( pointer, type )
NameError: name 'ArrayDatatype' is not defined
如果需要,请输入完整代码:
import numpy as np
import ctypes
import os
os.environ['DISPLAY'] = ':0.0'
if not os.environ.get( 'PYOPENGL_PLATFORM' ):
os.environ['PYOPENGL_PLATFORM'] = 'egl'
import OpenGL
from OpenGL.GL import ArrayDatatype
from OpenGL.EGL import *
from OpenGL.GLES2 import *
from OpenGL import arrays
if os.environ.get( 'TEST_NO_ACCELERATE' ):
OpenGL.USE_ACCELERATE = False
def LoadShader(stype, shaderSrc):
shader = glCreateShader(stype)
if(shader == 0):
print("Error returned 0")
if(glGetError()!=GL_NO_ERROR):
print('GLerror')
return 0
glShaderSource(shader, shaderSrc)
glCompileShader(shader)
if glGetShaderiv(shader, GL_COMPILE_STATUS) != GL_TRUE:
raise RuntimeError(glGetShaderInfoLog(shader))
return shader
display, context, surface = None, None, None
configAttribs =[
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
EGL_DEPTH_SIZE, 8,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_NONE
]
pbufferAttribs=[
EGL_WIDTH,
1000,
EGL_HEIGHT,
2,
EGL_NONE
]
contextAttribs=[
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
]
vertexShaderCode = '''
attribute vec4 vPosition;
void main() {
gl_Position = vPosition;
}
'''
fragmentShaderCode = {}
fragmentShaderCode[0] = '''
precision highp float;
uniform float Aarr[1000];
uniform float Barr[1000];
vec4 EncodeRangeV4(float value, float minVal, float maxVal) {
value = clamp( (value-minVal) / (maxVal-minVal), 0.0, 1.0 );
value *= 0.99999994039;
vec4 encode = fract( value * vec4(1.0, 256.0, 65536.0, 16777216.0) );
return vec4( encode.xyz - encode.yzw / 256.0, encode.w );
}
void main() {
int my_index = int(gl_FragCoord[0]);
float result = Aarr[my_index] + Barr[my_index];
gl_FragColor = EncodeRangeV4(result, -512.0, 512.0) - (1.0/1300.0);
}
'''
vertices=(
-1, -1, 1,
-1, 1, 1,
1, 1, 1,
-1, -1, 1,
1, 1, 1,
1, -1, 1)
prog_array = {}
display = eglGetDisplay(EGL_DEFAULT_DISPLAY)
if(display == EGL_NO_DISPLAY):
print("Failed to get EGL display! Error: %s", eglGetError())
exit()
print("Egl error after display ",eglGetError())
major,minor = ctypes.c_long(), ctypes.c_long()
if not eglInitialize( display, major, minor):
print("Unable to initialize")
exit()
print("Egl error after initalize ",eglGetError())
configAttribs = arrays.GLintArray.asArray(configAttribs)
num_configs = ctypes.c_long()
config = (EGLConfig*1)()
eglChooseConfig(display, configAttribs, config, 1, num_configs)
print("Egl error after config ",eglGetError())
eglBindAPI(EGL_OPENGL_ES_API)
pbufferAttribs_list = pbufferAttribs[:]
pbufferAttribs_list = arrays.GLintArray.asArray(pbufferAttribs_list)
surface = eglCreatePbufferSurface(display, config[0], pbufferAttribs_list)
print("Egl error after surface ",eglGetError())
contextAttribs = arrays.GLintArray.asArray(contextAttribs)
context = eglCreateContext(display, config[0], EGL_NO_CONTEXT, contextAttribs)
print("Egl error after context ",eglGetError())
eglMakeCurrent(display, surface, surface, context)
print("Egl error after make current ",eglGetError())
desiredWidth, desiredHeight = pbufferAttribs[1], pbufferAttribs[3]
glViewport(0, 0, desiredWidth, desiredHeight)
#viewport = glGetIntegerv(GL_VIEWPORT)
print("Egl get error",eglGetError())
if(glGetError()!=GL_NO_ERROR):
print('GLerror')
glClearColor(0.0, 0.0, 0.0, 1.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
if(glGetError()!=GL_NO_ERROR):
print('GLerror')
prog_array[0] = glCreateProgram()
if(glGetError()!=GL_NO_ERROR):
print('GLerror')
vert = LoadShader(GL_VERTEX_SHADER, vertexShaderCode)
frag = LoadShader(GL_FRAGMENT_SHADER, fragmentShaderCode[0])
glAttachShader(prog_array[0], vert)
glAttachShader(prog_array[0], frag)
glLinkProgram(prog_array[0])
glUseProgram(prog_array[0])
vec_A = glGetUniformLocation(prog_array[0], 'Aarr')
vec_B = glGetUniformLocation(prog_array[0], 'Barr')
print("vec a ",vec_A, "type: ", type(vec_A))
print("vec b ",vec_B, "type: ", type(vec_B))
vbo = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER,vbo)
glBufferData(GL_ARRAY_BUFFER, sizeof(ctypes.c_float) * len(vertices), (ctypes.c_float * len(vertices))(*vertices), GL_STATIC_DRAW)
glEnableVertexAttribArray(0)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glUseProgram(prog_array[0])
width, height = pbufferAttribs[1], pbufferAttribs[3]
vec_A = glGetUniformLocation(prog_array[0], 'Aarr')
vec_B = glGetUniformLocation(prog_array[0], 'Barr')
print("vec a ",vec_A, "type: ", type(vec_A))
print("vec b ",vec_B, "type: ", type(vec_B))
glUniform1fv(vec_A, n, A)
glUniform1fv(vec_B, n, B)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(ctypes.c_float), ctypes.c_void_p(0*sizeof(ctypes.c_float)) )
glDrawArrays(GL_TRIANGLES, 0, 18)
eglSwapBuffers(display, surface)
buffer = glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE)
print("Buffer: ", buffer[0:10], "Len: ",len(buffer))
parse_output_buffer_vec_add(buffer, n, C)
查看了GitHub上的代码并收到我收到的错误后,我意识到GitHub上的代码已经解决了该问题。我对该软件包进行了conda更新,但仍然收到此错误。但是,当使用通过pip安装的pyopengl时,相同的代码不会产生此错误。