From c8901ba46b04e90c6ada66c21395c0f666cbac79 Mon Sep 17 00:00:00 2001 From: Matthias Hochsteger Date: Fri, 13 Dec 2019 10:20:19 +0100 Subject: [PATCH] cleanup in Snapshot() --- libsrc/include/incopengl.hpp | 2 -- libsrc/visualization/mvdraw.cpp | 51 +++++++++++++++-------------- libsrc/visualization/vssolution.cpp | 4 --- 3 files changed, 27 insertions(+), 30 deletions(-) diff --git a/libsrc/include/incopengl.hpp b/libsrc/include/incopengl.hpp index c4630a09..8cff36af 100644 --- a/libsrc/include/incopengl.hpp +++ b/libsrc/include/incopengl.hpp @@ -47,12 +47,10 @@ extern void (*glBindFramebuffer) (GLenum target, GLuint framebuffer); extern void (*glBindRenderbuffer) (GLenum target, GLuint renderbuffer); extern void (*glDeleteFramebuffers) (GLsizei n, const GLuint *framebuffers); extern void (*glDeleteRenderbuffers) (GLsizei n, const GLuint *renderbuffers); -extern void (*glFramebufferTexture) (GLenum target, GLenum attachment, GLuint texture, GLint level); extern void (*glGenFramebuffers) (GLsizei n, GLuint *framebuffers); extern void (*glGenRenderbuffers) (GLsizei n, GLuint *renderbuffers); extern void (*glRenderbufferStorage) (GLenum target, GLenum internalformat, GLsizei width, GLsizei height); extern void (*glFramebufferRenderbuffer) (GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer); -extern void (*glNamedFramebufferReadBuffer) ( GLuint framebuffer, GLenum mode); #endif DLL_HEADER void LoadOpenGLFunctionPointers(); #endif // INCOPENGL_HPP___ diff --git a/libsrc/visualization/mvdraw.cpp b/libsrc/visualization/mvdraw.cpp index 1142755a..65b89b05 100644 --- a/libsrc/visualization/mvdraw.cpp +++ b/libsrc/visualization/mvdraw.cpp @@ -783,7 +783,12 @@ namespace netgen std::vector Snapshot( int w, int h ) { - glMatrixMode(GL_PROJECTION); + // save current settings + GLint viewport[4]; + glGetIntegerv (GL_VIEWPORT, viewport); + + glMatrixMode (GL_PROJECTION); + glPushMatrix(); glLoadIdentity(); double pnear = 0.1; @@ -791,50 +796,48 @@ namespace netgen gluPerspective(20.0f, double(w) / h, pnear, pfar); - glMatrixMode(GL_MODELVIEW); + glMatrixMode (GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); + glViewport(0,0,w,h); GLuint fb = 0; glGenFramebuffers(1, &fb); glBindFramebuffer(GL_FRAMEBUFFER, fb); - GLuint tex; - glGenTextures(1, &tex); + // create, reserve and attach color and depth renderbuffer + GLuint rbs[2]; + glGenRenderbuffers(2, rbs); + glBindRenderbuffer(GL_RENDERBUFFER, rbs[0]); + glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, w, h); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbs[0]); - glBindTexture(GL_TEXTURE_2D, tex); - - glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA, w, h, 0,GL_RGBA, GL_UNSIGNED_BYTE, 0); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - - GLuint depthrenderbuffer; - glGenRenderbuffers(1, &depthrenderbuffer); - glBindRenderbuffer(GL_RENDERBUFFER, depthrenderbuffer); + glBindRenderbuffer(GL_RENDERBUFFER, rbs[1]); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, w, h); - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthrenderbuffer); - - glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex, 0); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbs[1]); + // check if framebuffer status is complete if(int fbstatus; (fbstatus = glCheckFramebufferStatus(GL_FRAMEBUFFER)) != GL_FRAMEBUFFER_COMPLETE) cerr << "no frame buffer " << fbstatus << endl; - glBindFramebuffer(GL_FRAMEBUFFER, fb); - glViewport(0,0,w,h); - - glPushMatrix(); visual_scene->DrawScene(); glFinish(); - glPopMatrix(); std::vector buffer(w*h*3); glPixelStorei(GL_UNPACK_ALIGNMENT,1); glPixelStorei(GL_PACK_ALIGNMENT,1); glReadPixels (0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, &buffer[0]); - glDeleteRenderbuffers(1, &depthrenderbuffer); - glDeleteTextures(1, &tex); + glDeleteRenderbuffers(2, rbs); glDeleteFramebuffers(1, &fb); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + // restore previous settings + glViewport(viewport[0], viewport[1], viewport[2], viewport[3]); + glMatrixMode (GL_PROJECTION); + glPopMatrix(); + glMatrixMode (GL_MODELVIEW); + glPopMatrix(); return buffer; } diff --git a/libsrc/visualization/vssolution.cpp b/libsrc/visualization/vssolution.cpp index 624e95f7..d0aa8468 100644 --- a/libsrc/visualization/vssolution.cpp +++ b/libsrc/visualization/vssolution.cpp @@ -4896,12 +4896,10 @@ void (*glBindFramebuffer) (GLenum target, GLuint framebuffer); void (*glBindRenderbuffer) (GLenum target, GLuint renderbuffer); void (*glDeleteFramebuffers) (GLsizei n, const GLuint *framebuffers); void (*glDeleteRenderbuffers) (GLsizei n, const GLuint *renderbuffers); -void (*glFramebufferTexture) (GLenum target, GLenum attachment, GLuint texture, GLint level); void (*glGenFramebuffers) (GLsizei n, GLuint *framebuffers); void (*glGenRenderbuffers) (GLsizei n, GLuint *renderbuffers); void (*glRenderbufferStorage) (GLenum target, GLenum internalformat, GLsizei width, GLsizei height); void (*glFramebufferRenderbuffer) (GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer); -void (*glNamedFramebufferReadBuffer) ( GLuint framebuffer, GLenum mode); DLL_HEADER void LoadOpenGLFunctionPointers() { #ifdef USE_BUFFERS @@ -4918,12 +4916,10 @@ DLL_HEADER void LoadOpenGLFunctionPointers() { glBindRenderbuffer = (decltype(glBindRenderbuffer )) wglGetProcAddress("glBindRenderbuffer"); glDeleteFramebuffers = (decltype(glDeleteFramebuffers )) wglGetProcAddress("glDeleteFramebuffers"); glDeleteRenderbuffers = (decltype(glDeleteRenderbuffers )) wglGetProcAddress("glDeleteRenderbuffers"); - glFramebufferTexture = (decltype(glFramebufferTexture )) wglGetProcAddress("glFramebufferTexture"); glGenFramebuffers = (decltype(glGenFramebuffers )) wglGetProcAddress("glGenFramebuffers"); glGenRenderbuffers = (decltype(glGenRenderbuffers )) wglGetProcAddress("glGenRenderbuffers"); glRenderbufferStorage = (decltype(glRenderbufferStorage )) wglGetProcAddress("glRenderbufferStorage"); glFramebufferRenderbuffer = (decltype(glFramebufferRenderbuffer )) wglGetProcAddress("glFramebufferRenderbuffer"); - glNamedFramebufferReadBuffer = (decltype(glNamedFramebufferReadBuffer )) wglGetProcAddress("glNamedFramebufferReadBuffer"); } #else // WIN32 DLL_HEADER void LoadOpenGLFunctionPointers() { }