PyOpenGL代码与vbos一起用于渲染基于图块的游戏,创建空白屏幕

kxe2p93d  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(88)

我一直在用pygame和pyopengl(哦,显然是Python),试图制作一个基于瓦片的2D小游戏,但我在使用VBO和glMultiDrawArray()渲染时遇到了麻烦。
程序运行时没有错误,但我没有看到任何几何图形,所以它只是一个空白屏幕。
我试过使用glTranslate来查看是否正在绘制几何图形,但我看不到,也试过在GluPerspective()和glOrthro2D()之间切换。没有成功。我仔细研究了代码,看看哪里不起作用,但我不知道哪里出了问题。我仍然在努力理解OpenGL和VBO。
下面是我的代码的相关部分:
Chunk类。每个块都有自己的顶点和纹理VBO(纹理当前未使用)

class Chunk():

def __init__(self, position):
    self.Position = position

    self.VertexVBOId = _get_chunk_id()
    self.VertexVBO = glGenBuffers(self.VertexVBOId)
    self.TextureVBOId = _get_chunk_id()
    self.TextureVBO = glGenBuffers(self.TextureVBOId)

    Chunks[str(position)] = self

    self.__updateVertexArray()
   # glBindBuffer (GL_ARRAY_BUFFER, self.VertexVBO)
    #self.__updateVertexArray()

def __getvertices(self):
    vertices = []

    for x in range(self.Position.x, self.Position.x + 16):
        for y in range(self.Position.y, self.Position.y + 16):
            pos = Vector2(x, y)
            tile = GetTile(pos)
            if tile != "air":
                vertices.append(x+1)
                vertices.append(y)

                vertices.append(x+1)
                vertices.append(y+1)

                vertices.append(x)
                vertices.append(y+1)

                vertices.append(x)
                vertices.append(y)

    return vertices

def __updateVertexArray(self): #This will be called when a change is made the the chunk, as well as once initially
    print("UPDATING VERTEX ARRAY")
    vertices = self.__getvertices()
    glBindBuffer (GL_ARRAY_BUFFER, self.VertexVBOId)
    glBufferData (GL_ARRAY_BUFFER, len(vertices)*4, (c_float*len(vertices))(*vertices), GL_DYNAMIC_DRAW)

下面是渲染循环:

def main():
    print("Started")
    pygame.init()
    global displaySize
    global SCREENSIZE
    global PIXELS_PER_TILE

    pygame.display.set_mode(displaySize, DOUBLEBUF|OPENGL)
    #gluOrtho2D(-SCREENSIZE[0]/2, SCREENSIZE[0]/2, -SCREENSIZE[1]/2, SCREENSIZE[1]/2)
    gluPerspective(180, 2, 0.1, 100)

    ... some other stuff ...

    while True:
        #Drawing 
        glClearColor(0.7, 0.7, 1, 0)
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

       cameraTranslateX = (camera.Position.x % 1) * PIXELS_PER_TILE
        cameraTranslateY = (camera.Position.y % 1) * PIXELS_PER_TILE

        #Figure out which chunks to render
        botLeft = camera.Position - Vector2(SCREENSIZE[0]/2, SCREENSIZE[1]/2) + Vector2(cameraTranslateX, cameraTranslateY)
        topRight = camera.Position + Vector2(SCREENSIZE[0]/2, SCREENSIZE[1]/2) + Vector2(cameraTranslateX, cameraTranslateY)

        FirstChunkPos = (botLeft/16).floor()

        TotalChunksX = (topRight/16).ceil().x - FirstChunkPos.x
        TotalChunksY = (topRight/16).ceil().y - FirstChunkPos.y

        for x in range(TotalChunksX):
            for y in range(TotalChunksY):
                pos = Vector2(x + FirstChunkPos.x, y + FirstChunkPos.y)
                chunk = Chunks.get(str(pos))
                if not chunk:
                    chunk = Chunk(pos)

                VertexVBO = chunk.VertexVBOId

                glBindBuffer (GL_ARRAY_BUFFER, VertexVBO)
                glVertexPointer (2, GL_FLOAT, 0, None)

                TextureVBO = chunk.TextureVBOId

                glMultiDrawArrays (GL_POLYGON, vertexArrayThingy1, vertexArrayThingy2, 255)

               # glUnmapBuffer(GL_ARRAY_BUFFER,VertexVBO)

        pygame.display.flip ()
owfi6suc

owfi6suc1#

glMultiDrawArrays的第二个和第三个参数的类型为const GLint*const GLsizei*。此函数无法从不同的缓冲区绘制。没有glDraw*命令可以使用多个顶点缓冲区进行绘制。所有vertecx属性必须在同一个缓冲区中。glMultiDrawArrays应该从缓冲区绘制不同的范围。假设您要绘制以下3个属性范围[3:6]、[18:27]、[30:36]:

first = [3, 18, 30]
count = [3, 9, 6]
glMultiDrawArrays(GL_TRIANGLES, first, count, 3)

如果要绘制多个索引列表,则必须使用glMultiDrawElements,并且必须创建指向索引数组的指针数组:
第一个

相关问题