@@ -130,6 +130,8 @@ void gr_opengl_flip()
130130 if (Cmdline_gl_finish)
131131 glFinish ();
132132
133+ Assertion (GL_state.ValidForFlip (), " OpenGL state is invalid!" );
134+
133135 current_viewport->swapBuffers ();
134136
135137 opengl_tcache_frame ();
@@ -278,7 +280,11 @@ void gr_opengl_print_screen(const char *filename)
278280
279281 _mkdir (os_get_config_path (" screenshots" ).c_str ());
280282
281- // glReadBuffer(GL_FRONT);
283+ GL_state.PushFramebufferState ();
284+ GL_state.BindFrameBuffer (Cmdline_window_res ? Back_framebuffer : 0 , GL_FRAMEBUFFER);
285+
286+ // Reading from the front buffer here seems to no longer work correctly; that just reads back all zeros
287+ glReadBuffer (Cmdline_window_res ? GL_COLOR_ATTACHMENT0 : GL_FRONT);
282288
283289 // now for the data
284290 if (Use_PBOs) {
@@ -292,7 +298,6 @@ void gr_opengl_print_screen(const char *filename)
292298 glBindBuffer (GL_PIXEL_PACK_BUFFER, pbo);
293299 glBufferData (GL_PIXEL_PACK_BUFFER, (gr_screen.max_w * gr_screen.max_h * 4 ), NULL , GL_STATIC_READ);
294300
295- glReadBuffer (GL_FRONT);
296301 glReadPixels (0 , 0 , gr_screen.max_w , gr_screen.max_h , GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL );
297302
298303 // map the image data so that we can save it to file
@@ -319,6 +324,8 @@ void gr_opengl_print_screen(const char *filename)
319324 glDeleteBuffers (1 , &pbo);
320325 }
321326
327+ GL_state.PopFramebufferState ();
328+
322329 if (pixels != NULL ) {
323330 vm_free (pixels);
324331 }
@@ -329,6 +336,12 @@ SCP_string gr_opengl_blob_screen()
329336 GLubyte* pixels = nullptr ;
330337 GLuint pbo = 0 ;
331338
339+ GL_state.PushFramebufferState ();
340+ GL_state.BindFrameBuffer (Cmdline_window_res ? Back_framebuffer : 0 , GL_FRAMEBUFFER);
341+
342+ // Reading from the front buffer here seems to no longer work correctly; that just reads back all zeros
343+ glReadBuffer (Cmdline_window_res ? GL_COLOR_ATTACHMENT0 : GL_FRONT);
344+
332345 // now for the data
333346 if (Use_PBOs) {
334347 Assert (!pbo);
@@ -341,7 +354,6 @@ SCP_string gr_opengl_blob_screen()
341354 glBindBuffer (GL_PIXEL_PACK_BUFFER, pbo);
342355 glBufferData (GL_PIXEL_PACK_BUFFER, (gr_screen.max_w * gr_screen.max_h * 4 ), NULL , GL_STATIC_READ);
343356
344- glReadBuffer (GL_FRONT);
345357 glReadPixels (0 , 0 , gr_screen.max_w , gr_screen.max_h , GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL );
346358
347359 // map the image data so that we can save it to file
@@ -367,6 +379,8 @@ SCP_string gr_opengl_blob_screen()
367379 glDeleteBuffers (1 , &pbo);
368380 }
369381
382+ GL_state.PopFramebufferState ();
383+
370384 if (pixels != nullptr ) {
371385 vm_free (pixels);
372386 }
@@ -618,7 +632,7 @@ void gr_opengl_get_region(int /*front*/, int w, int h, ubyte *data)
618632
619633}
620634
621- int gr_opengl_save_screen ()
635+ int gr_opengl_save_screen ()
622636{
623637 int i;
624638 ubyte *sptr = NULL , *dptr = NULL ;
@@ -640,7 +654,12 @@ int gr_opengl_save_screen()
640654 }
641655
642656 GLboolean save_state = GL_state.DepthTest (GL_FALSE);
643- glReadBuffer (GL_FRONT_LEFT);
657+
658+ GL_state.PushFramebufferState ();
659+ GL_state.BindFrameBuffer (Cmdline_window_res ? Back_framebuffer : 0 , GL_FRAMEBUFFER);
660+
661+ // Reading from the front buffer here seems to no longer work correctly; that just reads back all zeros
662+ glReadBuffer (Cmdline_window_res ? GL_COLOR_ATTACHMENT0 : GL_FRONT);
644663
645664 if ( Use_PBOs ) {
646665 GLubyte *pixels = NULL ;
@@ -713,6 +732,7 @@ int gr_opengl_save_screen()
713732 GL_saved_screen_id = bm_create (32 , gr_screen.max_w , gr_screen.max_h , GL_saved_screen, 0 );
714733 }
715734
735+ GL_state.PopFramebufferState ();
716736 GL_state.DepthTest (save_state);
717737
718738 return GL_saved_screen_id;
0 commit comments