简体   繁体   中英

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:

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. Do I need to recompile a new shader?

The solution was achieved by using a uniform as user BDL suggested in his comment. It is also possible to set attributes of vertices, as suggested here:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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