Untitled

mail@pastecode.io avatar
unknown
plain_text
16 days ago
1.6 kB
0
Indexable
Never
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GL.shaders import compileProgram, compileShader
import numpy as np

# Initialize Pygame and OpenGL
pygame.init()
screen = pygame.display.set_mode((800, 600), DOUBLEBUF | OPENGL)

# Define Shaders
vertex_shader = """
#version 330
in vec4 position;
void main()
{
    gl_Position = position;
}
"""

fragment_shader = """
#version 330
out vec4 outColor;
void main()
{
    outColor = vec4(1.0, 0.0, 0.0, 1.0); // Red color
}
"""

# Compile and Link Shaders
shader_program = compileProgram(
    compileShader(vertex_shader, GL_VERTEX_SHADER),
    compileShader(fragment_shader, GL_FRAGMENT_SHADER)
)

# Define a Simple Shape (Triangle)
vertices = np.array([
    [-0.5, -0.5, 0.0],
    [0.5, -0.5, 0.0],
    [0.0,  0.5, 0.0]
], dtype=np.float32)

# Set Up VBO and VAO
VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW)

VAO = glGenVertexArrays(1)
glBindVertexArray(VAO)
position = glGetAttribLocation(shader_program, 'position')
glEnableVertexAttribArray(position)
glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 0, None)

# Main Loop
running = True
while running:
    for event in pygame.event.get():
        if event.type == QUIT:
            running = False

    glClear(GL_COLOR_BUFFER_BIT)

    glUseProgram(shader_program)
    glBindVertexArray(VAO)
    glDrawArrays(GL_TRIANGLES, 0, 3)

    pygame.display.flip()
    pygame.time.wait(10)

pygame.quit()
Leave a Comment