使用OpenGL在pygame窗口中绘制3D对象

问题描述 投票:0回答:1

我想知道如何编写代码以在

PyGame
窗口上显示 3D 对象。我使用的是 3.3 版本的 OpenGL。我决定首先添加
glEnable(GL_DEPTH_TEST)
并将立方体对象编写为 8x6 矩阵:

cube = (0.0,0.0,0.0,1.0,1.0,1.0,
        0.5,0.0,0.0,1.0,1.0,1.0,
        0.0,0.5,0.0,1.0,1.0,1.0,
        0.0,0.0,0.5,1.0,1.0,1.0,
        0.5,0.5,0.0,1.0,1.0,1.0,
        0.5,0.0,0.5,1.0,1.0,1.0,
        0.0,0.5,0.5,1.0,1.0,1.0,
        0.5,0.5,0.5,1.0,1.0,1.0



        )

然后打电话给

glDrawArrays(GL_IMAGE_CUBE,0,8)
glDrawArrays(GL_SAMPLER_CUBE)
但是没有用。我想出了一些想法:

我怀疑当我初始化

PyGame
窗口时,我需要将第二个参数更改为:

pg.display.set_mode(display,DOUBLEBUF|OPENGL)
或者我需要为片段深度编写一个着色器。

但是在网上搜索后我发现

glDrawArrays()
在$z时设置了自己的片段着色器 e 0$。我该怎么办?

python opengl graphics pygame
1个回答
0
投票
import pygame as pg
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GL.shaders import compileProgram, compileShader

# Define vertices of the cube
vertices = (
    (0.0, 0.0, 0.0),
    (0.5, 0.0, 0.0),
    (0.0, 0.5, 0.0),
    (0.0, 0.0, 0.5),
    (0.5, 0.5, 0.0),
    (0.5, 0.0, 0.5),
    (0.0, 0.5, 0.5),
    (0.5, 0.5, 0.5),
)

# Define the vertices that compose each of the 6 faces of the cube
indices = (
    (0, 1, 2, 3),
    (3, 2, 7, 6),
    (6, 7, 5, 4),
    (4, 5, 1, 0),
    (1, 5, 7, 2),
    (4, 0, 3, 6)
)

def main():
    # Initialize Pygame
    pg.init()
    display = (800, 600)
    pg.display.set_mode(display, DOUBLEBUF | OPENGL)

    # Initialize OpenGL
    glEnable(GL_DEPTH_TEST)

    # Perspective projection
    gluPerspective(45, (display[0] / display[1]), 0.1, 50.0)
    glTranslatef(0.0, 0.0, -5)

    # Compile shaders
    shader = compileProgram(
        compileShader("""
        #version 330
        layout(location = 0) in vec3 position;
        void main()
        {
            gl_Position = gl_ModelViewProjectionMatrix * vec4(position, 1.0);
        }
        """, GL_VERTEX_SHADER),
        compileShader("""
        #version 330
        out vec4 fragColor;
        void main()
        {
            fragColor = vec4(1, 1, 1, 1);
        }
        """, GL_FRAGMENT_SHADER)
    )

    # Create VAO and VBO
    VAO = glGenVertexArrays(1)
    glBindVertexArray(VAO)

    VBO = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, VBO)
    glBufferData(GL_ARRAY_BUFFER, 48, vertices, GL_STATIC_DRAW)

    # Vertex position attribute
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None)
    glEnableVertexAttribArray(0)

    # Unbind VBO and VAO
    glBindBuffer(GL_ARRAY_BUFFER, 0)
    glBindVertexArray(0)

    # Event loop
    running = True
    while running:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                running = False

        # Clear screen and depth buffer
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        # Draw the cube
        glUseProgram(shader)
        glBindVertexArray(VAO)
        for surface in indices:
            glBegin(GL_QUADS)
            for vertex in surface:
                glVertex3fv(vertices[vertex])
            glEnd()
© www.soinside.com 2019 - 2024. All rights reserved.