src/glrenderer.cpp

changeset 1171
430ffa371d2a
parent 1170
2045a395213a
child 1172
3defab8cfd93
equal deleted inserted replaced
1170:2045a395213a 1171:430ffa371d2a
803 { 803 {
804 if (m_objectAtCursor == object) 804 if (m_objectAtCursor == object)
805 m_objectAtCursor = nullptr; 805 m_objectAtCursor = nullptr;
806 } 806 }
807 807
808 // ============================================================================= 808 /*
809 // 809 * Returns an image containing the current render of the scene.
810 QByteArray GLRenderer::capturePixels() 810 */
811 { 811 QImage GLRenderer::screenCapture()
812 QByteArray result; 812 {
813 result.resize (4 * width() * height()); 813 // Read the current render to a buffer of pixels. We use RGBA format even though the image should be fully opaque at all times.
814 m_takingScreenCapture = true; 814 // This is because apparently GL_RGBA/GL_UNSIGNED_BYTE is the only setting pair that is guaranteed to actually work!
815 update(); // Smile! 815 // ref: https://www.khronos.org/registry/OpenGL-Refpages/es2.0/xhtml/glReadPixels.xml
816 m_takingScreenCapture = false; 816 QByteArray pixelData;
817 glReadPixels(0, 0, width(), height(), GL_RGBA, GL_UNSIGNED_BYTE, result.data()); 817 pixelData.resize(4 * width() * height());
818 return result; 818 glReadPixels(0, 0, width(), height(), GL_RGBA, GL_UNSIGNED_BYTE, pixelData.data());
819
820 // Prepare the image and return it. It appears that GL and Qt formats have red and blue swapped and the Y axis flipped.
821 QImage image {reinterpret_cast<const uchar*>(pixelData.constData()), width(), height(), QImage::Format_ARGB32};
822 return image.rgbSwapped().mirrored();
819 } 823 }
820 824
821 /* 825 /*
822 * Show a tooltip if the cursor is currently hovering over a camera icon. 826 * Show a tooltip if the cursor is currently hovering over a camera icon.
823 */ 827 */

mercurial