- Home /
glReadPixels in native plugin shows different results for different Unity versions
Hi,
I'm trying to use glReadPixels in a native plugin to get every frame and store it into an image on Windows. The C# code(bound to a camera) is:
IEnumerator OnPostRender()
{
TakeSnapshot();
yield return null;
}
and the C++ code of the plugin is:
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API TakeSnapshot()
{
cv::Mat m_ScrFrm;
glReadPixels(0, 0, m_ScrFrm.cols, m_ScrFrm.rows, GL_BGR_EXT, GL_UNSIGNED_BYTE, m_ScrFrm.data);
int errorcode=glGetError();
}
in which m_ScrFrm is an opencv mat format. I have forced the program to be built with OpenGLCore.
When using Unity 5.1, I can get the image out correctly; However when I use Unity 5.3 or 5.4 build the same project and run, it fails and glGetError() returns 1282 error. I also tried to call the TakeSnapshot() function after yield return new WaitForEndOfFrame(), the same problem exists.
Has anyone met the same problem please? How to solve it?
In other words, how to get the snapshot of the frame buffer for each frame in Unity5.3/4 pls?
Answer by amonitzer · Oct 20, 2016 at 11:03 PM
The Unity docs can help you there:
In order to do any rendering from the plugin, you should call GL.IssuePluginEvent from your script, which will cause your plugin to be called from the render thread. For example, if you call GL.IssuePluginEvent from the camera's OnPostRender function, you get a plugin callback immediately after the camera has finished rendering.
Thank you @amonitzer . I put the native glReadPixels function in the callback of GL.IssuePluginEvent to ensure that it is called from the render thread, and it works!
Hi kyewong, how do you put the parameters to the glReadPixels(...)? if I try to do so GL.IssuePluginEvent(glReadPixels(0, 0, Screen.width, Screen.height, GL_RGBA, GL_UNSIGNED_BYTE, buffer), 1);, I got an error, that "Argument 1 cannot convert from void to System.Int.Ptr" Argument 1 is gl-function.
Your answer
Follow this Question
Related Questions
Where does Unity5 look for native plugin dependencies in the editor on Windows? 1 Answer
Standalone player -force-opengl arguments 0 Answers
Application.CaptureScreenshot() Fails on PC/Mac 2 Answers
Forcing OpenGL in Windows Editor 1 Answer
How to take screenshot of certain/all display(s) when using unity multi display (ie. 3 x display)? 0 Answers