简体   繁体   English

PyOpenGL-使用VBO绘制具有不同颜色的多个三角形

[英]PyOpenGL - Draw multiple triangles with different colors with VBO

My goal is to render a scene with many triangles, with different colors. 我的目标是渲染具有许多不同颜色的三角形的场景。 I want to render this same scene many times, as quickly as possible from many different camera positions and different view angles. 我想从许多不同的相机位置和不同的视角尽可能快地多次渲染同一场景。

I managed to combine several samples and get a code that draws a single triangle using VBO: 我设法合并了几个样本,并获得了使用VBO绘制单个三角形的代码:

import cv2
import numpy as np
import glfw
from OpenGL.GL import shaders
from OpenGL.GLU import *
from OpenGL.arrays import vbo
from OpenGL.GL import *
from OpenGL.raw.GL.ARB.vertex_array_object import glGenVertexArrays, glBindVertexArray

def draw_model():
    vertices = np.array([[0,1,0],[-1,-1,0],[1,-1,0]], dtype=np.float32)
    vertexPositions = vbo.VBO(vertices)
    indices = np.array([[0,2,1]], dtype=np.int32)
    indexPositions = vbo.VBO(indices, target=GL_ELEMENT_ARRAY_BUFFER)
    VERTEX_SHADER = shaders.compileShader("""
    #version 330
    in vec4 position;
    void main()
    {
        gl_Position = position;
    }
    """, GL_VERTEX_SHADER)

    FRAGMENT_SHADER = shaders.compileShader("""
    #version 330
    out vec4 outputColor;
    void main()
    {
        outputColor = vec4(0.0f, 1.0f, 0.9f, 1.0f);
    }
    """, GL_FRAGMENT_SHADER)

    shader = shaders.compileProgram(VERTEX_SHADER, FRAGMENT_SHADER)
    glUseProgram(shader)
    indexPositions.bind()
    vertexPositions.bind()
    glEnableVertexAttribArray(0)
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None)
    glDrawArrays(GL_TRIANGLES, 0, 3)

# Reads the pixels to NP
def get_display_pixels(rendered_image_width, rendered_image_height):
    data = glReadPixels(0, 0, rendered_image_width, rendered_image_height, OpenGL.GL.GL_RGB, OpenGL.GL.GL_UNSIGNED_BYTE)
    return np.frombuffer(data, dtype=np.uint8).reshape(rendered_image_height, rendered_image_width, 3)[::-1]

DISPLAY_WIDTH = 900
DISPLAY_HEIGHT = 900

glfw.init()
glfw.window_hint(glfw.VISIBLE, False)
window = glfw.create_window(DISPLAY_WIDTH, DISPLAY_HEIGHT, "some window", None, None)
glfw.make_context_current(window)

gluPerspective(90, (DISPLAY_WIDTH / DISPLAY_HEIGHT), 0.01, 30)
glEnable(GL_TEXTURE_2D)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LEQUAL)

# Get cube 1
glPushMatrix()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
draw_model()
cube1 = get_display_pixels(DISPLAY_WIDTH, DISPLAY_HEIGHT)
glPopMatrix()

cv2.imwrite(r"C:\temp\image1.png", cube1)

glfw.destroy_window(window)
glfw.terminate()

I have several problems with this code: 这段代码有几个问题:

It seems strange that I have to compile a shader for drawing a simple shape. 我必须编译着色器以绘制简单形状,这似乎很奇怪。 And it seems more strange to recompile a shader for each color I want to use. 为我要使用的每种颜色重新编译着色器似乎更奇怪。

I don't understand how I can combine this code with gluLookAt for my scene. 我不明白如何将这段代码与gluLookAt结合起来用于我的场景。 Do I need to recompile a new shader? 我需要重新编译一个新的着色器吗?

The solution was achieved by using a uniform as user BDL suggested in his comment. 该解决方案是通过使用BDL用户在其评论中建议的制服来实现的。 It is also possible to set attributes of vertices, as suggested here: 也可以设置顶点的属性,如下所示:

http://www.labri.fr/perso/nrougier/python-opengl/ http://www.labri.fr/perso/nrougier/python-opengl/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM