cleanup in Snapshot()

This commit is contained in:
Matthias Hochsteger 2019-12-13 10:20:19 +01:00
parent a06189ac4e
commit c8901ba46b
3 changed files with 27 additions and 30 deletions

View File

@ -47,12 +47,10 @@ extern void (*glBindFramebuffer) (GLenum target, GLuint framebuffer);
extern void (*glBindRenderbuffer) (GLenum target, GLuint renderbuffer); extern void (*glBindRenderbuffer) (GLenum target, GLuint renderbuffer);
extern void (*glDeleteFramebuffers) (GLsizei n, const GLuint *framebuffers); extern void (*glDeleteFramebuffers) (GLsizei n, const GLuint *framebuffers);
extern void (*glDeleteRenderbuffers) (GLsizei n, const GLuint *renderbuffers); 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 (*glGenFramebuffers) (GLsizei n, GLuint *framebuffers);
extern void (*glGenRenderbuffers) (GLsizei n, GLuint *renderbuffers); extern void (*glGenRenderbuffers) (GLsizei n, GLuint *renderbuffers);
extern void (*glRenderbufferStorage) (GLenum target, GLenum internalformat, GLsizei width, GLsizei height); extern void (*glRenderbufferStorage) (GLenum target, GLenum internalformat, GLsizei width, GLsizei height);
extern void (*glFramebufferRenderbuffer) (GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer); extern void (*glFramebufferRenderbuffer) (GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer);
extern void (*glNamedFramebufferReadBuffer) ( GLuint framebuffer, GLenum mode);
#endif #endif
DLL_HEADER void LoadOpenGLFunctionPointers(); DLL_HEADER void LoadOpenGLFunctionPointers();
#endif // INCOPENGL_HPP___ #endif // INCOPENGL_HPP___

View File

@ -783,7 +783,12 @@ namespace netgen
std::vector<unsigned char> Snapshot( int w, int h ) std::vector<unsigned char> Snapshot( int w, int h )
{ {
glMatrixMode(GL_PROJECTION); // save current settings
GLint viewport[4];
glGetIntegerv (GL_VIEWPORT, viewport);
glMatrixMode (GL_PROJECTION);
glPushMatrix();
glLoadIdentity(); glLoadIdentity();
double pnear = 0.1; double pnear = 0.1;
@ -791,50 +796,48 @@ namespace netgen
gluPerspective(20.0f, double(w) / h, pnear, pfar); gluPerspective(20.0f, double(w) / h, pnear, pfar);
glMatrixMode(GL_MODELVIEW); glMatrixMode (GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glViewport(0,0,w,h);
GLuint fb = 0; GLuint fb = 0;
glGenFramebuffers(1, &fb); glGenFramebuffers(1, &fb);
glBindFramebuffer(GL_FRAMEBUFFER, fb); glBindFramebuffer(GL_FRAMEBUFFER, fb);
GLuint tex; // create, reserve and attach color and depth renderbuffer
glGenTextures(1, &tex); 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); glBindRenderbuffer(GL_RENDERBUFFER, rbs[1]);
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);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, w, h); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, w, h);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthrenderbuffer); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbs[1]);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex, 0);
// check if framebuffer status is complete
if(int fbstatus; (fbstatus = glCheckFramebufferStatus(GL_FRAMEBUFFER)) != GL_FRAMEBUFFER_COMPLETE) if(int fbstatus; (fbstatus = glCheckFramebufferStatus(GL_FRAMEBUFFER)) != GL_FRAMEBUFFER_COMPLETE)
cerr << "no frame buffer " << fbstatus << endl; cerr << "no frame buffer " << fbstatus << endl;
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glViewport(0,0,w,h);
glPushMatrix();
visual_scene->DrawScene(); visual_scene->DrawScene();
glFinish(); glFinish();
glPopMatrix();
std::vector<unsigned char> buffer(w*h*3); std::vector<unsigned char> buffer(w*h*3);
glPixelStorei(GL_UNPACK_ALIGNMENT,1); glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glPixelStorei(GL_PACK_ALIGNMENT,1); glPixelStorei(GL_PACK_ALIGNMENT,1);
glReadPixels (0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, &buffer[0]); glReadPixels (0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, &buffer[0]);
glDeleteRenderbuffers(1, &depthrenderbuffer); glDeleteRenderbuffers(2, rbs);
glDeleteTextures(1, &tex);
glDeleteFramebuffers(1, &fb); 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; return buffer;
} }

View File

@ -4896,12 +4896,10 @@ void (*glBindFramebuffer) (GLenum target, GLuint framebuffer);
void (*glBindRenderbuffer) (GLenum target, GLuint renderbuffer); void (*glBindRenderbuffer) (GLenum target, GLuint renderbuffer);
void (*glDeleteFramebuffers) (GLsizei n, const GLuint *framebuffers); void (*glDeleteFramebuffers) (GLsizei n, const GLuint *framebuffers);
void (*glDeleteRenderbuffers) (GLsizei n, const GLuint *renderbuffers); void (*glDeleteRenderbuffers) (GLsizei n, const GLuint *renderbuffers);
void (*glFramebufferTexture) (GLenum target, GLenum attachment, GLuint texture, GLint level);
void (*glGenFramebuffers) (GLsizei n, GLuint *framebuffers); void (*glGenFramebuffers) (GLsizei n, GLuint *framebuffers);
void (*glGenRenderbuffers) (GLsizei n, GLuint *renderbuffers); void (*glGenRenderbuffers) (GLsizei n, GLuint *renderbuffers);
void (*glRenderbufferStorage) (GLenum target, GLenum internalformat, GLsizei width, GLsizei height); void (*glRenderbufferStorage) (GLenum target, GLenum internalformat, GLsizei width, GLsizei height);
void (*glFramebufferRenderbuffer) (GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer); void (*glFramebufferRenderbuffer) (GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer);
void (*glNamedFramebufferReadBuffer) ( GLuint framebuffer, GLenum mode);
DLL_HEADER void LoadOpenGLFunctionPointers() { DLL_HEADER void LoadOpenGLFunctionPointers() {
#ifdef USE_BUFFERS #ifdef USE_BUFFERS
@ -4918,12 +4916,10 @@ DLL_HEADER void LoadOpenGLFunctionPointers() {
glBindRenderbuffer = (decltype(glBindRenderbuffer )) wglGetProcAddress("glBindRenderbuffer"); glBindRenderbuffer = (decltype(glBindRenderbuffer )) wglGetProcAddress("glBindRenderbuffer");
glDeleteFramebuffers = (decltype(glDeleteFramebuffers )) wglGetProcAddress("glDeleteFramebuffers"); glDeleteFramebuffers = (decltype(glDeleteFramebuffers )) wglGetProcAddress("glDeleteFramebuffers");
glDeleteRenderbuffers = (decltype(glDeleteRenderbuffers )) wglGetProcAddress("glDeleteRenderbuffers"); glDeleteRenderbuffers = (decltype(glDeleteRenderbuffers )) wglGetProcAddress("glDeleteRenderbuffers");
glFramebufferTexture = (decltype(glFramebufferTexture )) wglGetProcAddress("glFramebufferTexture");
glGenFramebuffers = (decltype(glGenFramebuffers )) wglGetProcAddress("glGenFramebuffers"); glGenFramebuffers = (decltype(glGenFramebuffers )) wglGetProcAddress("glGenFramebuffers");
glGenRenderbuffers = (decltype(glGenRenderbuffers )) wglGetProcAddress("glGenRenderbuffers"); glGenRenderbuffers = (decltype(glGenRenderbuffers )) wglGetProcAddress("glGenRenderbuffers");
glRenderbufferStorage = (decltype(glRenderbufferStorage )) wglGetProcAddress("glRenderbufferStorage"); glRenderbufferStorage = (decltype(glRenderbufferStorage )) wglGetProcAddress("glRenderbufferStorage");
glFramebufferRenderbuffer = (decltype(glFramebufferRenderbuffer )) wglGetProcAddress("glFramebufferRenderbuffer"); glFramebufferRenderbuffer = (decltype(glFramebufferRenderbuffer )) wglGetProcAddress("glFramebufferRenderbuffer");
glNamedFramebufferReadBuffer = (decltype(glNamedFramebufferReadBuffer )) wglGetProcAddress("glNamedFramebufferReadBuffer");
} }
#else // WIN32 #else // WIN32
DLL_HEADER void LoadOpenGLFunctionPointers() { } DLL_HEADER void LoadOpenGLFunctionPointers() { }