The program ends with an error: Invalid operation in glVertexAttribPointer
Log:
OpenGL Version: b'3.3.0 NVIDIA 391.35'
Traceback (most recent call last):
File "E:\ProgramFiles\Python\Python38\lib\site-packages\OpenGL\latebind.py", line 43, in __call__
return self._finalCall( *args, **named )
TypeError: 'NoneType' object is not callable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:\ProgramFiles\Python\Python38\lib\site-packages\OpenGL\latebind.py", line 43, in __call__
return self._finalCall( *args, **named )
TypeError: 'NoneType' object is not callable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "main.py", line 106, in <module>
main()
File "main.py", line 91, in main
glVertexAttribPointer(aPositionLocation, 2, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0))
File "E:\ProgramFiles\Python\Python38\lib\site-packages\OpenGL\latebind.py", line 47, in __call__
return self._finalCall( *args, **named )
File "E:\ProgramFiles\Python\Python38\lib\site-packages\OpenGL\latebind.py", line 63, in __call__
return self.wrapperFunction( self.baseFunction, *args, **named )
File "E:\ProgramFiles\Python\Python38\lib\site-packages\OpenGL\GL\VERSION\GL_2_0.py", line 470, in glVertexAttribPointer
return baseOperation(
File "E:\ProgramFiles\Python\Python38\lib\site-packages\OpenGL\latebind.py", line 47, in __call__
return self._finalCall( *args, **named )
File "E:\ProgramFiles\Python\Python38\lib\site-packages\OpenGL\wrapper.py", line 700, in wrapperCall
raise err
File "E:\ProgramFiles\Python\Python38\lib\site-packages\OpenGL\wrapper.py", line 693, in wrapperCall
result = wrappedOperation( *cArguments )
File "E:\ProgramFiles\Python\Python38\lib\site-packages\OpenGL\error.py", line 230, in glCheckError
raise self._errorClass(
OpenGL.error.GLError: GLError(
err = 1282,
description = b'invalid operation',
baseOperation = glVertexAttribPointer,
pyArgs = (
0,
2,
GL_FLOAT,
GL_FALSE,
0,
c_void_p(None),
),
cArgs = (
0,
2,
GL_FLOAT,
GL_FALSE,
0,
c_void_p(None),
),
cArguments = (
0,
2,
GL_FLOAT,
GL_FALSE,
0,
c_void_p(None),
)
)
main.py
import ctypes
import numpy as np
import pyrr
from OpenGL.GL import *
from OpenGL.GL.shaders import *
from sdl2 import *
window = None
maxFPS = 20
vertexShaderSource = """
#version 330 core\n
in vec2 aPosition;\n
void main() {\n
gl_Position = vec4(aPosition, 0.0, 1.0);\n
}\n
"""
fragmentShaderSource = """
#version 330 core\n
out vec4 fragColor;\n
void main() {\n
fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n
}\n
"""
def fatalError(message):
print(message)
if window:
SDL_DestroyWindow(window)
SDL_Quit()
exit(-1)
def main():
if SDL_Init(SDL_INIT_VIDEO) < 0:
fatalError(SDL_GetError())
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
SDL_GL_CONTEXT_PROFILE_CORE)
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)
winW, winH = 250, 250
window = SDL_CreateWindow(
b"OpenGL1, SDL2, Python",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
winW, winH,
SDL_WINDOW_OPENGL)
if not window:
fatalError(SDL_GetError())
context = SDL_GL_CreateContext(window)
if not context:
fatalError(SDL_GetError())
glClearColor(0.5, 0.5, 0, 1) # QColor(128, 128, 0)
glViewport(0, 0, winW, winH)
program = compileProgram(
compileShader(vertexShaderSource, GL_VERTEX_SHADER),
compileShader(fragmentShaderSource, GL_FRAGMENT_SHADER))
glUseProgram(program)
vertPositions = np.array([
-0.5, -0.5,
0.5, -0.5,
0.0, 0.5
], dtype=np.float32)
vertPosBuffer = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, vertPosBuffer)
glBufferData(GL_ARRAY_BUFFER, len(vertPositions) * 4,
vertPositions, GL_STATIC_DRAW)
aPositionLocation = glGetAttribLocation(program, "aPosition")
print("OpenGL Version:", glGetString(GL_VERSION))
event = SDL_Event()
running = True
while running:
while SDL_PollEvent(ctypes.byref(event)) != 0:
if event.type == SDL_QUIT:
running = False
startTicks = SDL_GetTicks()
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program)
glBindBuffer(GL_ARRAY_BUFFER, vertPosBuffer)
glVertexAttribPointer(aPositionLocation, 2, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0))
glEnableVertexAttribArray(aPositionLocation)
glDrawArrays(GL_TRIANGLES, 0, 3)
SDL_GL_SwapWindow(window)
# Limit the FPS to the max FPS
frameTicks = SDL_GetTicks() - startTicks
if 1000 / maxFPS > frameTicks:
SDL_Delay(int(1000 / maxFPS - frameTicks))
SDL_GL_DeleteContext(context)
SDL_DestroyWindow(window)
SDL_Quit()
main()