PYOPENGL saves the image to memory

Although the drawing and rendering capabilities of OpenGL are very powerful, what should I do if I want to use OpenGL rendered pictures in other places? You can save the screenshots of the pictures in the opengl window, but what about automatic saving? Automatic saving can be done by reading the data in the opengl cache:

def grap(w, h):
'''
w:窗口的宽
h:窗口的高
'''
    data = []
    glReadBuffer(GL_FRONT)
	#从缓冲区中的读出的数据是字节数组
    data = glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE)
    arr = numpy.zeros((h*w*3), dtype=np.uint8)
    for i in range(0, len(data), 3):
    	#由于opencv中使用的是BGR而opengl使用的是RGB所以arr[i] = data[i+2],而不是arr[i] = data[i]
        arr[i] = data[i+2]
        arr[i+1] = data[i+1]
        arr[i+2] = data[i]
    arr = numpy.reshape(arr, (h, w, 3))
    #因为opengl和OpenCV在Y轴上是颠倒的,所以要进行垂直翻转,可以查看cv2.flip函数
    cv2.flip(arr, 0, arr)
    cv2.putText(arr, "Opencv", (40,40), cv2.FONT_ITALIC, 1, (0,255,0))
    cv2.putText(arr, "Miha_Singh", (40, 70), cv2.FONT_ITALIC, 1, (0, 255, 0))
    cv2.imshow('scene', arr)
    cv2.waitKey(1)
    

Effect picture:
Insert picture description here

After getting the cached data in opengl, you can process the obtained pictures as you want, and saving is no problem at all. But there is still an unresolved problem. If I want to get the pictures processed by OpenGL, I must display the window that displays the pictures of OpenGL at the same time. If I only want to get the pictures processed by OpenGL but I don’t want OpenGL to display the pictures (off-screen rendering). ),how should I do it? If you know, please leave a message, thank you very much!

Guess you like

Origin blog.csdn.net/Miha_Singh/article/details/85008578