Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python OpenGL-从不同角度渲染同一场景,而无需重新绘制所有内容_Python_Python 3.x_Opengl_Scene_Pyopengl - Fatal编程技术网

Python OpenGL-从不同角度渲染同一场景,而无需重新绘制所有内容

Python OpenGL-从不同角度渲染同一场景,而无需重新绘制所有内容,python,python-3.x,opengl,scene,pyopengl,Python,Python 3.x,Opengl,Scene,Pyopengl,我有一个场景,我正试图在各个方向渲染(后来缝合在一起,在OpenGL之外) 当前用于创建每个帧-我将整个场景重绘4次(前、右、后、左)。我可以在不重画整个场景4次的情况下将4个立方体一起渲染吗 下面是我使用的示例代码(特别是我使用的PyOpenGL-但这并不重要): 有很多方法可以一次渲染四个不同的视图,但这仍然意味着几何体着色器阶段之后的所有操作都会在每层执行一次。这可以提高在顶点着色器阶段(或细分着色器)中完成大量工作时的速度,但在瓶颈是像素填充率时不会有太大变化 如果不进行任何重画,就无法

我有一个场景,我正试图在各个方向渲染(后来缝合在一起,在OpenGL之外)

当前用于创建每个帧-我将整个场景重绘4次(前、右、后、左)。我可以在不重画整个场景4次的情况下将4个立方体一起渲染吗

下面是我使用的示例代码(特别是我使用的PyOpenGL-但这并不重要):


有很多方法可以一次渲染四个不同的视图,但这仍然意味着几何体着色器阶段之后的所有操作都会在每层执行一次。这可以提高在顶点着色器阶段(或细分着色器)中完成大量工作时的速度,但在瓶颈是像素填充率时不会有太大变化

如果不进行任何重画,就无法从不同的场景渲染相同的场景。整个光栅化(和剪裁等)在NDC空间中运行,通常在应用视图和投影矩阵之后,因此每个视图至少必须执行一次这些阶段


我也不确定分层渲染是否与您正在使用的古老版本的OpenGL一起使用。固定功能管道十年来一直被弃用,应该尽可能避免使用。

解决方案是使用评论中“Rabbi76”的想法实现的


将所有顶点移动到VBO,然后同时绘制它们,可以提高所需的性能

瓶颈是从GPU读取渲染(像素数据)。如果“一次”渲染4个视图,则这不会改变,因为仍然需要读取数据。为了改进渲染,我建议先使用and,从不推荐的
glBegin
/
glEnd
序列切换到最先进的渲染方式。
import cv2
import numpy as np
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import glfw

# Draws some model (The real code draws a much more complicated model)
def draw_model():
    glBegin(GL_QUADS)
    glColor3f(1.0, 1.0, 1.0)
    glVertex(10, 10, 0); glVertex(10, 10, 3); glVertex(10, -10, 3); glVertex(10, -10, 0)

    glColor3f(1.0, 1.0, 0)
    glVertex(-10, 10, 0); glVertex(-10, 10, 3); glVertex(-10, -10, 3); glVertex(-10, -10, 0)

    glColor3f(1.0, 0, 1.0)
    glVertex(10, 10, 0); glVertex(10, 10, 3); glVertex(-10, 10, 3); glVertex(-10, 10, 0)

    glColor3f(0, 1.0, 0)
    glVertex(10, -10, 0); glVertex(10, -10, 3); glVertex(-10, -10, 3); glVertex(-10, -10, 0)
    glEnd()

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

position = (0, 3, 1)
# Get cube 1
glPushMatrix()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
gluLookAt(*position, 1, 4, 1, 0, 0, 1)
draw_model()
cube1 = get_display_pixels(DISPLAY_WIDTH, DISPLAY_HEIGHT)
glPopMatrix()

# Get cube 2
glPushMatrix()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
gluLookAt(*position, 1, 2, 1, 0, 0, 1)
draw_model()
cube2 = get_display_pixels(DISPLAY_WIDTH, DISPLAY_HEIGHT)
glPopMatrix()

# Get cube 3
glPushMatrix()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
gluLookAt(*position, -1, 2, 1, 0, 0, 1)
draw_model()
cube3 = get_display_pixels(DISPLAY_WIDTH, DISPLAY_HEIGHT)
glPopMatrix()

# Get cube 4
glPushMatrix()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
gluLookAt(*position, -1, 4, 1, 0, 0, 1)
draw_model()
cube4 = get_display_pixels(DISPLAY_WIDTH, DISPLAY_HEIGHT)
glPopMatrix()

cv2.imwrite(r"C:\temp\image1.png", cube1)
cv2.imwrite(r"C:\temp\image2.png", cube2)
cv2.imwrite(r"C:\temp\image3.png", cube3)
cv2.imwrite(r"C:\temp\image4.png", cube4)

glfw.destroy_window(window)
glfw.terminate()