This problem was solved. Thank you very much!
But for shader OpenGL 2.1, calling this function SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, b"2")
has no effect:
You must replace it with this line:
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4)
And it’s important to call this function before SDL_CreateWindow
:
pip install numpy PyOpenGL PySDL2 pysdl2-dll
main.py
import ctypes
import numpy as np
from OpenGL.GL import *
from OpenGL.GL.shaders import *
from sdl2 import *
window = None
maxFPS = 5
vertexShaderSource = """
attribute vec2 aPosition;
void main()
{
gl_Position = vec4(aPosition, 0.0, 1.0);
}
"""
fragmentShaderSource = """
void main()
{
gl_FragColor = vec4(0.5, 0.8, 0.7, 1.0);
}
"""
def fatalError(message):
print(message)
if window:
SDL_DestroyWindow(window)
SDL_Quit()
exit(-1)
if __name__ == "__main__":
if SDL_Init(SDL_INIT_VIDEO) < 0:
fatalError(SDL_GetError())
# SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, b"2")
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4)
winW, winH = 350, 350
window = SDL_CreateWindow(b"OpenGL21, 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.2, 0.2, 0.2, 1)
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")
glVertexAttribPointer(aPositionLocation, 2, GL_FLOAT, GL_FALSE,
0, ctypes.c_void_p(0))
glEnableVertexAttribArray(aPositionLocation)
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)
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()