PyGameExamplesAndAnswers

StackOverflow            reply.it reply.it


PyGame and OpenGL 4

Context

Related Stack Overflow questions:

import pygame 

pygame.init()

pygame.display.gl_set_attribute(pygame.GL_CONTEXT_MAJOR_VERSION, 3)
pygame.display.gl_set_attribute(pygame.GL_CONTEXT_MINOR_VERSION, 3)
pygame.display.gl_set_attribute(pygame.GL_CONTEXT_PROFILE_MASK, pygame.GL_CONTEXT_PROFILE_CORE)
pygame.display.gl_set_attribute(pygame.GL_CONTEXT_FORWARD_COMPATIBLE_FLAG, True)
DISPLAYSURf = pygame.display.set_mode((500,475), pygame.OPENGL | pygame.DOUBLEBUF)

Vertex buffer and vertex attributes

Related Stack Overflow questions:

If a named buffer object is bound, then the 5th parameter of glVertexAttribPointer is treated as a byte offset into the buffer object’s data store. However, the type of the parameter is still a pointer (c_void_p).

Hence the offset must be cast with c_void_p. If the offset is 0, the 5th parameter can either be None or c_void_p(0):

loc_col = glGetAttribLocation(shader_program, "color")
glVertexAttribPointer(loc_col, 3, GL_FLOAT, False, 6*4, ctypes.c_void_p(3*4))

Mesh

Related Stack Overflow questions:

Clipping and Stencil

Related Stack Overflow questions:

Matrix and vertex transformation

Related Stack Overflow questions:

Pyrr’s Matrix44 behaves like and is implemented using numpy.array.

For Numpy arrays the * operator means element-wise multiplication, while the @ operator means matrix multiplication. See array.

The expression

rot = rot_x * rot_y

performs a component-wise multiplication of the elements of the matrix.

The correct operator for the matrix multiplication is:

rot = rot_x @ rot_y

Shader

Related Stack Overflow questions:

Texture

Related Stack Overflow questions:

It is like wrapping a checkered paper around the sphere. The 3D vertex coordinates form the sphere. The checkered paper is the 2D texture, with the 2D texture coordinates arranged in a grid. Each texture coordinate has to be associated to a point on the sphere (a vertex coordinate).