使用OpenGL在pygame窗口中绘制3D对象[重复]

cwdobuhd  于 2023-11-18  发布在  其他
关注(0)|答案(1)|浏览(100)

此问题在此处已有答案

Texture arrays in OpenGL(1个答案)
Pygame OpenGL 3D Cube Lag(1个答案)
glDrawElements to draw a cube in PyOpenGL(1个答案)
8天前关闭。
我想知道如何编写代码来在PyGame窗口上显示3D对象。我使用OpenGL 3.3版本。我决定从添加glEnable(GL_DEPTH_TEST)开始,并将立方体对象编写为8x 6矩阵:

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 \ne为$时设置了自己的片段着色器。我该怎么办?

sc4hvdpw

sc4hvdpw1#

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()

字符串

相关问题