Blame c++/contourgl/glcontext.cpp

49e693
/*
49e693
    ......... 2015 Ivan Mahonin
49e693
49e693
    This program is free software: you can redistribute it and/or modify
49e693
    it under the terms of the GNU General Public License as published by
49e693
    the Free Software Foundation, either version 3 of the License, or
49e693
    (at your option) any later version.
49e693
49e693
    This program is distributed in the hope that it will be useful,
49e693
    but WITHOUT ANY WARRANTY; without even the implied warranty of
49e693
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
49e693
    GNU General Public License for more details.
49e693
49e693
    You should have received a copy of the GNU General Public License
49e693
    along with this program.  If not, see <http: licenses="" www.gnu.org="">.</http:>
49e693
*/
49e693
49e693
#include <cassert></cassert>
49e693
49e693
#include <iostream></iostream>
49e693
#include <iomanip></iomanip>
49e693
49e693
#include "glcontext.h"
49e693
49e693
49e693
using namespace std;
49e693
49e693
49e693
#define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
49e693
#define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
49e693
typedef GLXContext (*GLXCREATECONTEXTATTRIBSARBPROC)(Display*, GLXFBConfig, GLXContext, Bool, const int*);
49e693
49e693
f29469
GlContext::GlContext(int width, int height, bool hdr, bool multisample, int samples):
49e693
	display(),
49e693
	pbuffer(),
49e693
	context(),
49e693
	texture_id(),
49e693
	framebuffer_id(),
49e693
	renderbuffer_id(),
49e693
	multisample_texture_id(),
49e693
	multisample_renderbuffer_id(),
49e693
	multisample_framebuffer_id()
49e693
{
49e693
49e693
	// options
49e693
f29469
	int framebuffer_width = width;
f29469
	int framebuffer_height = height;
f29469
	int framebuffer_samples = samples;
49e693
49e693
	// display
49e693
49e693
	display = XOpenDisplay(NULL);
49e693
	assert(display);
49e693
49e693
	// config
49e693
49e693
	int config_attribs[] = {
49e693
		GLX_DOUBLEBUFFER,      False,
49e693
		GLX_RED_SIZE,          8,
49e693
		GLX_GREEN_SIZE,        8,
49e693
		GLX_BLUE_SIZE,         8,
49e693
		GLX_ALPHA_SIZE,        8,
49e693
		GLX_DEPTH_SIZE,        24,
49e693
		GLX_STENCIL_SIZE,      8,
49e693
		GLX_ACCUM_RED_SIZE,    8,
49e693
		GLX_ACCUM_GREEN_SIZE,  8,
49e693
		GLX_ACCUM_BLUE_SIZE,   8,
49e693
		GLX_ACCUM_ALPHA_SIZE,  8,
49e693
		GLX_DRAWABLE_TYPE,     GLX_PBUFFER_BIT,
49e693
		None };
49e693
	int nelements = 0;
49e693
	GLXFBConfig *configs = glXChooseFBConfig(display, 0, config_attribs, &nelements);
49e693
	assert(configs != NULL && nelements > 0);
49e693
	GLXFBConfig config = configs[0];
49e693
	assert(config);
49e693
49e693
	// pbuffer
49e693
49e693
	int pbuffer_attribs[] = {
a04770
		GLX_PBUFFER_WIDTH, 256,
a04770
		GLX_PBUFFER_HEIGHT, 256,
49e693
		None };
49e693
	pbuffer = glXCreatePbuffer(display, config, pbuffer_attribs);
49e693
	assert(pbuffer);
49e693
49e693
	// context
49e693
49e693
	int context_attribs[] = {
49e693
		GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
49e693
		GLX_CONTEXT_MINOR_VERSION_ARB, 3,
49e693
		None };
49e693
	GLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = (GLXCREATECONTEXTATTRIBSARBPROC) glXGetProcAddress((const GLubyte*)"glXCreateContextAttribsARB");
49e693
	context = glXCreateContextAttribsARB(display, config, NULL, True, context_attribs);
49e693
	assert(context);
49e693
f29469
	use();
49e693
49e693
	// frame buffer
49e693
49e693
	GLenum internal_format = hdr ? GL_RGBA16F : GL_RGBA;
49e693
	GLenum color_type = hdr ? GL_FLOAT : GL_UNSIGNED_BYTE;
49e693
49e693
	glGenTextures(1, &texture_id);
49e693
	glBindTexture(GL_TEXTURE_2D, texture_id);
49e693
	glTexImage2D(GL_TEXTURE_2D, 0, internal_format, framebuffer_width, framebuffer_height, 0, GL_RGBA, color_type, NULL);
49e693
49e693
	glGenRenderbuffers(1, &renderbuffer_id);
49e693
	glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer_id);
49e693
	glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, framebuffer_width, framebuffer_height);
49e693
49e693
	glGenFramebuffers(1, &framebuffer_id);
49e693
	glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer_id);
49e693
	glFramebufferRenderbuffer(GL_READ_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, renderbuffer_id);
49e693
	glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture_id, 0);
49e693
49e693
	glGenTextures(1, &multisample_texture_id);
49e693
	glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, multisample_texture_id);
49e693
	glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, framebuffer_samples, internal_format, framebuffer_width, framebuffer_height, GL_TRUE);
49e693
49e693
	glGenRenderbuffers(1, &multisample_renderbuffer_id);
49e693
	glBindRenderbuffer(GL_RENDERBUFFER, multisample_renderbuffer_id);
49e693
	glRenderbufferStorageMultisample(GL_RENDERBUFFER, framebuffer_samples, GL_STENCIL_INDEX8, framebuffer_width, framebuffer_height);
49e693
49e693
	glGenFramebuffers(1, &multisample_framebuffer_id);
49e693
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, multisample_framebuffer_id);
49e693
	glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, multisample_renderbuffer_id);
49e693
	glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, multisample_texture_id, 0);
49e693
f29469
	//cout << "Framebuffer status:" << setbase(16)
f29469
	//	 << " 0x" << glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER)
f29469
	//	 << " 0x" << glCheckFramebufferStatus(GL_READ_FRAMEBUFFER)
f29469
	//	 << setbase(10) << endl;
49e693
f29469
	if (multisample)
49e693
		glEnable(GL_MULTISAMPLE);
49e693
	else
49e693
		glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer_id);
49e693
49e693
	// view port
49e693
49e693
	glViewport(0, 0, framebuffer_width, framebuffer_height);
a04770
a04770
	check();
49e693
}
49e693
49e693
GlContext::~GlContext() {
49e693
	glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
a04770
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
49e693
	glBindTexture(GL_TEXTURE_2D, 0);
a04770
	glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);
a04770
	glBindRenderbuffer(GL_RENDERBUFFER, 0);
a04770
a04770
	glDeleteFramebuffers(1, &framebuffer_id);
a04770
	glDeleteRenderbuffers(1, &renderbuffer_id);
49e693
	glDeleteTextures(1, &texture_id);
49e693
49e693
	glDeleteFramebuffers(1, &multisample_framebuffer_id);
49e693
	glDeleteRenderbuffers(1, &multisample_renderbuffer_id);
49e693
	glDeleteTextures(1, &multisample_texture_id);
49e693
f29469
	unuse();
49e693
	glXDestroyContext(display, context);
49e693
	glXDestroyPbuffer(display, pbuffer);
49e693
	XCloseDisplay(display);
49e693
}
49e693
f29469
void GlContext::use()
f29469
	{ glXMakeContextCurrent(display, pbuffer, pbuffer, context); }
f29469
void GlContext::unuse()
f29469
	{ glXMakeContextCurrent(display, None, None, NULL); }
f29469
49e693
void GlContext::check(const std::string &s) {
49e693
	if (GLenum error = glGetError())
49e693
		cout << s << " GL error: 0x" << setbase(16) << error << setbase(10) << endl;
49e693
}