This commit is contained in:
wittmannzl103469 committed 2026-11-05 15:08:42 +01:00
1 parent de41a02749
commit 9af09cebd3
764 files changed
+54218 -78859

No files matched your search

@@ -1,5 +1,5 @@
#!/usr/bin/env python
"""pygame.examples.glcube
""" pygame.examples.glcube
Draw a cube on the screen.
@@ -23,11 +23,10 @@ Keyboard Controls
* f key to toggle fullscreen.
"""
import ctypes
import math
import ctypes
import pygame
import pygame as pg
try:
import OpenGL.GL as GL
@@ -37,11 +36,12 @@ except ImportError:
raise SystemExit
try:
from numpy import array, dot, eye, float32, uint32, zeros
from numpy import array, dot, eye, zeros, float32, uint32
except ImportError:
print("numpy missing. The GLCUBE example requires: pyopengl numpy")
raise SystemExit
# do we want to use the 'modern' OpenGL API or the old one?
# This example shows you how to do both.
USE_MODERN_GL = True
@@ -259,16 +259,16 @@ def init_gl_modern(display_size):
uniform mat4 view;
uniform mat4 projection;
uniform vec4 color_mul;
uniform vec4 color_add;
uniform vec4 colour_mul;
uniform vec4 colour_add;
in vec4 vertex_color; // vertex color in
in vec4 vertex_colour; // vertex colour in
in vec3 vertex_position;
out vec4 vertex_color_out; // vertex color out
out vec4 vertex_color_out; // vertex colour out
void main()
{
vertex_color_out = (color_mul * vertex_color) + color_add;
vertex_color_out = (colour_mul * vertex_colour) + colour_add;
gl_Position = projection * view * model * vec4(vertex_position, 1.0);
}
@@ -276,7 +276,7 @@ def init_gl_modern(display_size):
fragment_code = """
#version 150
in vec4 vertex_color_out; // vertex color from vertex shader
in vec4 vertex_color_out; // vertex colour from vertex shader
out vec4 fragColor;
void main()
{
@@ -320,7 +320,9 @@ def init_gl_modern(display_size):
# ------------------------------------------
# Cube Data
vertices = zeros(8, [("vertex_position", float32, 3), ("vertex_color", float32, 4)])
vertices = zeros(
8, [("vertex_position", float32, 3), ("vertex_colour", float32, 4)]
)
vertices["vertex_position"] = [
[1, 1, 1],
@@ -333,7 +335,7 @@ def init_gl_modern(display_size):
[-1, -1, -1],
]
vertices["vertex_color"] = [
vertices["vertex_colour"] = [
[0, 1, 1, 1],
[0, 0, 1, 1],
[0, 0, 0, 1],
@@ -408,7 +410,7 @@ def init_gl_modern(display_size):
offset = ctypes.c_void_p(vertices.dtype["vertex_position"].itemsize)
loc = GL.glGetAttribLocation(program, "vertex_color")
loc = GL.glGetAttribLocation(program, "vertex_colour")
GL.glEnableVertexAttribArray(loc)
GL.glVertexAttribPointer(loc, 4, GL.GL_FLOAT, False, stride, offset)
@@ -442,19 +444,19 @@ def init_gl_modern(display_size):
)
GL.glUniformMatrix4fv(shader_data["constants"]["projection"], 1, False, eye(4))
# This color is multiplied with the base vertex color in producing
# This colour is multiplied with the base vertex colour in producing
# the final output
shader_data["constants"]["color_mul"] = GL.glGetUniformLocation(
program, "color_mul"
shader_data["constants"]["colour_mul"] = GL.glGetUniformLocation(
program, "colour_mul"
)
GL.glUniform4f(shader_data["constants"]["color_mul"], 1, 1, 1, 1)
GL.glUniform4f(shader_data["constants"]["colour_mul"], 1, 1, 1, 1)
# This color is added on to the base vertex color in producing
# This colour is added on to the base vertex colour in producing
# the final output
shader_data["constants"]["color_add"] = GL.glGetUniformLocation(
program, "color_add"
shader_data["constants"]["colour_add"] = GL.glGetUniformLocation(
program, "colour_add"
)
GL.glUniform4f(shader_data["constants"]["color_add"], 0, 0, 0, 0)
GL.glUniform4f(shader_data["constants"]["colour_add"], 0, 0, 0, 0)
# Set GL drawing data
# -------------------
@@ -489,8 +491,8 @@ def draw_cube_modern(shader_data, filled_cube_indices, outline_cube_indices, rot
GL.glDisable(GL.GL_BLEND)
GL.glEnable(GL.GL_DEPTH_TEST)
GL.glEnable(GL.GL_POLYGON_OFFSET_FILL)
GL.glUniform4f(shader_data["constants"]["color_mul"], 1, 1, 1, 1)
GL.glUniform4f(shader_data["constants"]["color_add"], 0, 0, 0, 0.0)
GL.glUniform4f(shader_data["constants"]["colour_mul"], 1, 1, 1, 1)
GL.glUniform4f(shader_data["constants"]["colour_add"], 0, 0, 0, 0.0)
GL.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, shader_data["buffer"]["filled"])
GL.glDrawElements(
GL.GL_TRIANGLES, len(filled_cube_indices), GL.GL_UNSIGNED_INT, None
@@ -499,14 +501,14 @@ def draw_cube_modern(shader_data, filled_cube_indices, outline_cube_indices, rot
# Outlined cube
GL.glDisable(GL.GL_POLYGON_OFFSET_FILL)
GL.glEnable(GL.GL_BLEND)
GL.glUniform4f(shader_data["constants"]["color_mul"], 0, 0, 0, 0.0)
GL.glUniform4f(shader_data["constants"]["color_add"], 1, 1, 1, 1.0)
GL.glUniform4f(shader_data["constants"]["colour_mul"], 0, 0, 0, 0.0)
GL.glUniform4f(shader_data["constants"]["colour_add"], 1, 1, 1, 1.0)
GL.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, shader_data["buffer"]["outline"])
GL.glDrawElements(GL.GL_LINES, len(outline_cube_indices), GL.GL_UNSIGNED_INT, None)
# Rotate cube
# rotation.theta += 1.0 # degrees
# rotation.phi += 1.0 # degrees
rotation.phi += 1.0 # degrees
# rotation.psi += 1.0 # degrees
model = eye(4, dtype=float32)
# rotate(model, rotation.theta, 0, 0, 1)
@@ -518,10 +520,8 @@ def draw_cube_modern(shader_data, filled_cube_indices, outline_cube_indices, rot
def main():
"""run the demo"""
# initialize pygame-ce and setup an opengl display
pygame.init()
clock = pygame.time.Clock()
# initialize pygame and setup an opengl display
pg.init()
gl_version = (3, 0) # GL Version number (Major, Minor)
if USE_MODERN_GL:
@@ -529,22 +529,16 @@ def main():
# By setting these attributes we can choose which Open GL Profile
# to use, profiles greater than 3.2 use a different rendering path
pygame.display.gl_set_attribute(pygame.GL_CONTEXT_MAJOR_VERSION, gl_version[0])
pygame.display.gl_set_attribute(pygame.GL_CONTEXT_MINOR_VERSION, gl_version[1])
pygame.display.gl_set_attribute(
pygame.GL_CONTEXT_PROFILE_MASK, pygame.GL_CONTEXT_PROFILE_CORE
pg.display.gl_set_attribute(pg.GL_CONTEXT_MAJOR_VERSION, gl_version[0])
pg.display.gl_set_attribute(pg.GL_CONTEXT_MINOR_VERSION, gl_version[1])
pg.display.gl_set_attribute(
pg.GL_CONTEXT_PROFILE_MASK, pg.GL_CONTEXT_PROFILE_CORE
)
# start in windowed mode
fullscreen = False # start in windowed mode
display_size = (640, 480)
try:
pygame.display.set_mode(
display_size, pygame.OPENGL | pygame.DOUBLEBUF | pygame.RESIZABLE, vsync=1
)
except:
pygame.display.set_mode(
display_size, pygame.OPENGL | pygame.DOUBLEBUF | pygame.RESIZABLE
)
pg.display.set_mode(display_size, pg.OPENGL | pg.DOUBLEBUF | pg.RESIZABLE)
if USE_MODERN_GL:
gpu, f_indices, o_indices = init_gl_modern(display_size)
@@ -552,55 +546,45 @@ def main():
else:
init_gl_stuff_old()
delta_time = 0
going = True
while going:
# check for quit'n events
events = pygame.event.get()
events = pg.event.get()
for event in events:
if event.type == pygame.QUIT or (
event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE
if event.type == pg.QUIT or (
event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE
):
going = False
elif event.type == pygame.KEYDOWN and event.key == pygame.K_f:
if not pygame.display.is_fullscreen():
elif event.type == pg.KEYDOWN and event.key == pg.K_f:
if not fullscreen:
print("Changing to FULLSCREEN")
pygame.display.set_mode(
display_size,
pygame.OPENGL | pygame.DOUBLEBUF | pygame.FULLSCREEN,
pg.display.set_mode(
(640, 480), pg.OPENGL | pg.DOUBLEBUF | pg.FULLSCREEN
)
else:
print("Changing to windowed mode")
pygame.display.set_mode(
display_size, pygame.OPENGL | pygame.DOUBLEBUF
)
pg.display.set_mode((640, 480), pg.OPENGL | pg.DOUBLEBUF)
fullscreen = not fullscreen
if gl_version[0] >= 4 or (gl_version[0] == 3 and gl_version[1] >= 2):
gpu, f_indices, o_indices = init_gl_modern(display_size)
rotation = Rotation()
else:
init_gl_stuff_old()
# orbit camera around by 60 degrees per second
angle = (delta_time / 1000) * 60
if USE_MODERN_GL:
rotation.phi += angle
draw_cube_modern(gpu, f_indices, o_indices, rotation)
else:
# clear screen and move camera
GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
# rotate camera to angle
GL.glRotatef(angle, 0, 1, 0)
# orbit camera around by 1 degree
GL.glRotatef(1, 0, 1, 0)
drawcube_old()
pygame.display.flip()
if pygame.display.is_vsync():
delta_time = clock.tick()
else:
delta_time = clock.tick(60)
pg.display.flip()
pg.time.wait(10)
pygame.quit()
pg.quit()
if __name__ == "__main__":