diff options
| -rw-r--r-- | tests/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | tests/OffscreenView.cc | 102 | ||||
| -rw-r--r-- | tests/OffscreenView.h | 2 | ||||
| -rw-r--r-- | tests/csgtestcore.cc | 104 | 
4 files changed, 109 insertions, 100 deletions
| diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f604f73..5605f6a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -303,6 +303,7 @@ target_link_libraries(tests-cgal tests-common)  add_library(tests-nocgal STATIC ${NOCGAL_SOURCES})  target_link_libraries(tests-nocgal tests-common)  add_library(tests-offscreen STATIC ${OFFSCREEN_SOURCES}) +# set_target_properties(tests-offscreen PROPERTIES COMPILE_FLAGS "-DENABLE_OPENCSG -DENABLE_CGAL ${CGAL_CXX_FLAGS_INIT}")  #  # echotest diff --git a/tests/OffscreenView.cc b/tests/OffscreenView.cc index 9c8964c..61d5818 100644 --- a/tests/OffscreenView.cc +++ b/tests/OffscreenView.cc @@ -1,7 +1,6 @@  #include <GL/glew.h>  #include "OffscreenView.h"  #include "system-gl.h" -#include <opencsg.h>  #include "renderer.h"  #include <math.h>  #include <stdio.h> @@ -57,105 +56,13 @@ void OffscreenView::initializeGL()  	glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);  	glEnable(GL_COLOR_MATERIAL); -#ifdef ENABLE_OPENCSG -	const char *openscad_disable_gl20_env = getenv("OPENSCAD_DISABLE_GL20"); -	if (openscad_disable_gl20_env && !strcmp(openscad_disable_gl20_env, "0")) -		openscad_disable_gl20_env = NULL; -	if (glewIsSupported("GL_VERSION_2_0") && openscad_disable_gl20_env == NULL) -	{ -		const char *vs_source = -			"uniform float xscale, yscale;\n" -			"attribute vec3 pos_b, pos_c;\n" -			"attribute vec3 trig, mask;\n" -			"varying vec3 tp, tr;\n" -			"varying float shading;\n" -			"void main() {\n" -			"  vec4 p0 = gl_ModelViewProjectionMatrix * gl_Vertex;\n" -			"  vec4 p1 = gl_ModelViewProjectionMatrix * vec4(pos_b, 1.0);\n" -			"  vec4 p2 = gl_ModelViewProjectionMatrix * vec4(pos_c, 1.0);\n" -			"  float a = distance(vec2(xscale*p1.x/p1.w, yscale*p1.y/p1.w), vec2(xscale*p2.x/p2.w, yscale*p2.y/p2.w));\n" -			"  float b = distance(vec2(xscale*p0.x/p0.w, yscale*p0.y/p0.w), vec2(xscale*p1.x/p1.w, yscale*p1.y/p1.w));\n" -			"  float c = distance(vec2(xscale*p0.x/p0.w, yscale*p0.y/p0.w), vec2(xscale*p2.x/p2.w, yscale*p2.y/p2.w));\n" -			"  float s = (a + b + c) / 2.0;\n" -			"  float A = sqrt(s*(s-a)*(s-b)*(s-c));\n" -			"  float ha = 2.0*A/a;\n" -			"  gl_Position = p0;\n" -			"  tp = mask * ha;\n" -			"  tr = trig;\n" -			"  vec3 normal, lightDir;\n" -			"  normal = normalize(gl_NormalMatrix * gl_Normal);\n" -			"  lightDir = normalize(vec3(gl_LightSource[0].position));\n" -			"  shading = abs(dot(normal, lightDir));\n" -			"}\n"; - -		const char *fs_source = -			"uniform vec4 color1, color2;\n" -			"varying vec3 tp, tr, tmp;\n" -			"varying float shading;\n" -			"void main() {\n" -			"  gl_FragColor = vec4(color1.r * shading, color1.g * shading, color1.b * shading, color1.a);\n" -			"  if (tp.x < tr.x || tp.y < tr.y || tp.z < tr.z)\n" -			"    gl_FragColor = color2;\n" -			"}\n"; - -		GLuint vs = glCreateShader(GL_VERTEX_SHADER); -		glShaderSource(vs, 1, (const GLchar**)&vs_source, NULL); -		glCompileShader(vs); - -		GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); -		glShaderSource(fs, 1, (const GLchar**)&fs_source, NULL); -		glCompileShader(fs); - -		GLuint edgeshader_prog = glCreateProgram(); -		glAttachShader(edgeshader_prog, vs); -		glAttachShader(edgeshader_prog, fs); -		glLinkProgram(edgeshader_prog); - -		shaderinfo[0] = edgeshader_prog; -		shaderinfo[1] = glGetUniformLocation(edgeshader_prog, "color1"); -		shaderinfo[2] = glGetUniformLocation(edgeshader_prog, "color2"); -		shaderinfo[3] = glGetAttribLocation(edgeshader_prog, "trig"); -		shaderinfo[4] = glGetAttribLocation(edgeshader_prog, "pos_b"); -		shaderinfo[5] = glGetAttribLocation(edgeshader_prog, "pos_c"); -		shaderinfo[6] = glGetAttribLocation(edgeshader_prog, "mask"); -		shaderinfo[7] = glGetUniformLocation(edgeshader_prog, "xscale"); -		shaderinfo[8] = glGetUniformLocation(edgeshader_prog, "yscale"); - -		GLenum err = glGetError(); -		if (err != GL_NO_ERROR) { -			fprintf(stderr, "OpenGL Error: %s\n", gluErrorString(err)); -		} - -		GLint status; -		glGetProgramiv(edgeshader_prog, GL_LINK_STATUS, &status); -		if (status == GL_FALSE) { -			int loglen; -			char logbuffer[1000]; -			glGetProgramInfoLog(edgeshader_prog, sizeof(logbuffer), &loglen, logbuffer); -			fprintf(stderr, "OpenGL Program Linker Error:\n%.*s", loglen, logbuffer); -		} else { -			int loglen; -			char logbuffer[1000]; -			glGetProgramInfoLog(edgeshader_prog, sizeof(logbuffer), &loglen, logbuffer); -			if (loglen > 0) { -				fprintf(stderr, "OpenGL Program Link OK:\n%.*s", loglen, logbuffer); -			} -			glValidateProgram(edgeshader_prog); -			glGetProgramInfoLog(edgeshader_prog, sizeof(logbuffer), &loglen, logbuffer); -			if (loglen > 0) { -				fprintf(stderr, "OpenGL Program Validation results:\n%.*s", loglen, logbuffer); -			} -		} -	} -#endif /* ENABLE_OPENCSG */ +  }  void OffscreenView::resizeGL(int w, int h)  { -#ifdef ENABLE_OPENCSG -	shaderinfo[9] = w; -	shaderinfo[10] = h; -#endif +	this->width = w; +	this->height = h;  	glViewport(0, 0, w, h);  	w_h_ratio = sqrt((double)w / (double)h);  } @@ -226,9 +133,6 @@ void OffscreenView::paintGL()  	glColor3d(1.0, 0.0, 0.0);  	if (this->renderer) { -#ifdef ENABLE_OPENCSG -		OpenCSG::setContext(0); -#endif  		this->renderer->draw(showfaces, showedges);  	}  } diff --git a/tests/OffscreenView.h b/tests/OffscreenView.h index 2e35921..8b98b29 100644 --- a/tests/OffscreenView.h +++ b/tests/OffscreenView.h @@ -27,6 +27,8 @@ public:  	GLint shaderinfo[11];  	OffscreenContext *ctx; +	size_t width; +	size_t height;  private:  	Renderer *renderer;  	double w_h_ratio; diff --git a/tests/csgtestcore.cc b/tests/csgtestcore.cc index c962023..418738d 100644 --- a/tests/csgtestcore.cc +++ b/tests/csgtestcore.cc @@ -14,6 +14,7 @@  #include "CGALEvaluator.h"  #include "PolySetCGALEvaluator.h" +#include <opencsg.h>  #include "OpenCSGRenderer.h"  #include "ThrownTogetherRenderer.h" @@ -128,6 +129,102 @@ po::variables_map parse_options(int argc, char *argv[])  	return vm;  } +void enable_opencsg_shaders( OffscreenView *glview ) +{ +	bool ignore_gl_version = true; +	const char *openscad_disable_gl20_env = getenv("OPENSCAD_DISABLE_GL20"); +	if (openscad_disable_gl20_env && !strcmp(openscad_disable_gl20_env, "0")) +		openscad_disable_gl20_env = NULL; +	if (glewIsSupported("GL_VERSION_2_0") && openscad_disable_gl20_env == NULL ) +	{ +		const char *vs_source = +			"uniform float xscale, yscale;\n" +			"attribute vec3 pos_b, pos_c;\n" +			"attribute vec3 trig, mask;\n" +			"varying vec3 tp, tr;\n" +			"varying float shading;\n" +			"void main() {\n" +			"  vec4 p0 = gl_ModelViewProjectionMatrix * gl_Vertex;\n" +			"  vec4 p1 = gl_ModelViewProjectionMatrix * vec4(pos_b, 1.0);\n" +			"  vec4 p2 = gl_ModelViewProjectionMatrix * vec4(pos_c, 1.0);\n" +			"  float a = distance(vec2(xscale*p1.x/p1.w, yscale*p1.y/p1.w), vec2(xscale*p2.x/p2.w, yscale*p2.y/p2.w));\n" +			"  float b = distance(vec2(xscale*p0.x/p0.w, yscale*p0.y/p0.w), vec2(xscale*p1.x/p1.w, yscale*p1.y/p1.w));\n" +			"  float c = distance(vec2(xscale*p0.x/p0.w, yscale*p0.y/p0.w), vec2(xscale*p2.x/p2.w, yscale*p2.y/p2.w));\n" +			"  float s = (a + b + c) / 2.0;\n" +			"  float A = sqrt(s*(s-a)*(s-b)*(s-c));\n" +			"  float ha = 2.0*A/a;\n" +			"  gl_Position = p0;\n" +			"  tp = mask * ha;\n" +			"  tr = trig;\n" +			"  vec3 normal, lightDir;\n" +			"  normal = normalize(gl_NormalMatrix * gl_Normal);\n" +			"  lightDir = normalize(vec3(gl_LightSource[0].position));\n" +			"  shading = abs(dot(normal, lightDir));\n" +			"}\n"; + +		const char *fs_source = +			"uniform vec4 color1, color2;\n" +			"varying vec3 tp, tr, tmp;\n" +			"varying float shading;\n" +			"void main() {\n" +			"  gl_FragColor = vec4(color1.r * shading, color1.g * shading, color1.b * shading, color1.a);\n" +			"  if (tp.x < tr.x || tp.y < tr.y || tp.z < tr.z)\n" +			"    gl_FragColor = color2;\n" +			"}\n"; + +		GLuint vs = glCreateShader(GL_VERTEX_SHADER); +		glShaderSource(vs, 1, (const GLchar**)&vs_source, NULL); +		glCompileShader(vs); + +		GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); +		glShaderSource(fs, 1, (const GLchar**)&fs_source, NULL); +		glCompileShader(fs); + +		GLuint edgeshader_prog = glCreateProgram(); +		glAttachShader(edgeshader_prog, vs); +		glAttachShader(edgeshader_prog, fs); +		glLinkProgram(edgeshader_prog); + +		glview->shaderinfo[0] = edgeshader_prog; +		glview->shaderinfo[1] = glGetUniformLocation(edgeshader_prog, "color1"); +		glview->shaderinfo[2] = glGetUniformLocation(edgeshader_prog, "color2"); +		glview->shaderinfo[3] = glGetAttribLocation(edgeshader_prog, "trig"); +		glview->shaderinfo[4] = glGetAttribLocation(edgeshader_prog, "pos_b"); +		glview->shaderinfo[5] = glGetAttribLocation(edgeshader_prog, "pos_c"); +		glview->shaderinfo[6] = glGetAttribLocation(edgeshader_prog, "mask"); +		glview->shaderinfo[7] = glGetUniformLocation(edgeshader_prog, "xscale"); +		glview->shaderinfo[8] = glGetUniformLocation(edgeshader_prog, "yscale"); + +		GLenum err = glGetError(); +		if (err != GL_NO_ERROR) { +			fprintf(stderr, "OpenGL Error: %s\n", gluErrorString(err)); +		} + +		GLint status; +		glGetProgramiv(edgeshader_prog, GL_LINK_STATUS, &status); +		if (status == GL_FALSE) { +			int loglen; +			char logbuffer[1000]; +			glGetProgramInfoLog(edgeshader_prog, sizeof(logbuffer), &loglen, logbuffer); +			fprintf(stderr, "OpenGL Program Linker Error:\n%.*s", loglen, logbuffer); +		} else { +			int loglen; +			char logbuffer[1000]; +			glGetProgramInfoLog(edgeshader_prog, sizeof(logbuffer), &loglen, logbuffer); +			if (loglen > 0) { +				fprintf(stderr, "OpenGL Program Link OK:\n%.*s", loglen, logbuffer); +			} +			glValidateProgram(edgeshader_prog); +			glGetProgramInfoLog(edgeshader_prog, sizeof(logbuffer), &loglen, logbuffer); +			if (loglen > 0) { +				fprintf(stderr, "OpenGL Program Validation results:\n%.*s", loglen, logbuffer); +			} +		} +	} +	glview->shaderinfo[9] = glview->width; +	glview->shaderinfo[10] = glview->height; +} +  int csgtestcore(int argc, char *argv[], test_type_e test_type)  {  	bool sysinfo_dump = false; @@ -270,6 +367,8 @@ int csgtestcore(int argc, char *argv[], test_type_e test_type)  		fprintf(stderr,"Can't create OpenGL OffscreenView. Code: %i. Exiting.\n", error);  		exit(1);  	} +	enable_opencsg_shaders(csgInfo.glview); +  	if (sysinfo_dump) cout << info_dump(csgInfo.glview);  	BoundingBox bbox = csgInfo.root_chain->getBoundingBox(); @@ -289,8 +388,11 @@ int csgtestcore(int argc, char *argv[], test_type_e test_type)  	else  		csgInfo.glview->setRenderer(&opencsgRenderer); -	csgInfo.glview->paintGL(); +	OpenCSG::setContext(0); +	OpenCSG::setOption(OpenCSG::OffscreenSetting, OpenCSG::FrameBufferObject); +	csgInfo.glview->paintGL(); +	  	csgInfo.glview->save(outfilename);  	Builtins::instance(true); | 
