diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/CMakeLists.txt | 42 | ||||
| -rw-r--r-- | tests/FindGLEW.cmake | 15 | ||||
| -rw-r--r-- | tests/OffscreenContext.cc | 43 | ||||
| -rw-r--r-- | tests/OffscreenContextWGL.cc | 117 | ||||
| -rw-r--r-- | tests/OffscreenView.h | 9 | ||||
| -rw-r--r-- | tests/cgalpngtest.cc | 28 | 
6 files changed, 194 insertions, 60 deletions
| diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b9a5355..060e772 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -8,25 +8,32 @@ if(NOT CMAKE_BUILD_TYPE)    set(CMAKE_BUILD_TYPE Debug)  endif() -set(WIN32_STATIC_BUILD "True") + +# +# Windows +# + +if(WIN32) +  set(WIN32_STATIC_BUILD "True") +endif()  if(WIN32_STATIC_BUILD)    if(${CMAKE_BUILD_TYPE} STREQUAL "Debug") -    set(EMSG "\nTo build Win32 STATIC OpenSCAD you must run cmake .. -DCMAKE_BUILD_TYPE=Release") -    set(EMSG "${EMSG} \nand replace /MD with /MT in CMakeCache.txt") +    set(EMSG "\nTo build Win32 STATIC OpenSCAD tests you must run") +    set(EMSG "${EMSG} \ncmake .. -DCMAKE_BUILD_TYPE=Release") +    set(EMSG "${EMSG} \nthen replace /MD with /MT in CMakeCache.txt")      set(EMSG "${EMSG} \ni.e. sed -i s/\\/MD/\\/MT/ CMakeCache.txt")  +    set(EMSG "${EMSG} \nthen re-run cmake ..")       message(FATAL_ERROR ${EMSG})    endif()  endif() -# Win32 linker debugging -# If you uncomment this, use nmake -F Makefile > log.txt  -# (the link log can be several megabytes long) +# Turn off Eigen SIMD optimization  if(WIN32) -  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -VERBOSE") +  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DEIGEN_DONT_ALIGN")  endif() -# Disable some warnings in Windows  +# Disable warnings  if(WIN32)    # too long decorated names    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4503") @@ -38,6 +45,18 @@ if(WIN32)    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_CRT_SECURE_NO_DEPRECATE")  endif() +# Debugging - if you uncomment, use nmake -f Makefile > log.txt (the log is big) +if(WIN32) +  # Linker debugging +  #set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -VERBOSE") + +  # Compiler debugging +  # you have to pass -DCMAKE_VERBOSE_MAKEFILE=ON to cmake when you run it.  +endif() + +# Compilation debugging + +  #  # Build test apps  # @@ -89,7 +108,7 @@ include_directories(${OPENCSG_INCLUDE_DIR})  # GLEW -if () +if (NOT $ENV{MACOSX_DEPLOY_DIR} STREQUAL "")    set(GLEW_DIR "$ENV{MACOSX_DEPLOY_DIR}")  endif() @@ -161,10 +180,13 @@ set(COMMON_SOURCES  # Offscreen OpenGL context source code  #  if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") +	message(STATUS "Offscreen OpenGL Context - using Apple CGL")  	set(OFFSCREEN_CTX_SOURCE "OffscreenContext.mm")  elseif(UNIX) +	message(STATUS "Offscreen OpenGL Context - using Unix GLX")  	set(OFFSCREEN_CTX_SOURCE "OffscreenContext.cc")  elseif(WIN32) +	message(STATUS "Offscreen OpenGL Context - using Microsoft WGL")  	set(OFFSCREEN_CTX_SOURCE "OffscreenContextWGL.cc")  endif() @@ -212,7 +234,7 @@ target_link_libraries(cgaltest ${CGAL_LIBRARY} ${CGAL_3RD_PARTY_LIBRARIES} ${QT_  #  # cgalpngtest  # -add_executable(cgalpngtest cgalpngtest.cc OffscreenView.cc ${OFFSCREEN_CTX_SOURCE} imageutils.cc fbo.cc +add_executable(cgalpngtest cgalpngtest.cc OffscreenView.cc bboxhelp.cc ${OFFSCREEN_CTX_SOURCE} imageutils.cc fbo.cc                             ../src/CGALRenderer.cc ../src/CGAL_Nef_polyhedron.cc ../src/cgalutils.cc                             ../src/CSGTermEvaluator.cc ../src/CGALEvaluator.cc ../src/CGALCache.cc                             ../src/PolySetCGALEvaluator.cc ../src/qhash.cc diff --git a/tests/FindGLEW.cmake b/tests/FindGLEW.cmake index ffbdcc6..f49d546 100644 --- a/tests/FindGLEW.cmake +++ b/tests/FindGLEW.cmake @@ -10,19 +10,20 @@  # a few lines of this file are based on the LGPL code found at   # http://openlibraries.org/browser/trunk/FindGLEW.cmake?rev=1383  +  IF (WIN32) +	IF (WIN32_STATIC_BUILD) # passed from caller +		SET(GLEW_LIB_SEARCH_NAME glew32s.lib) # static, non-debug (Release) +	ELSE () +		SET(GLEW_LIB_SEARCH_NAME glew32.lib) # other. untested with OpenSCAD +	ENDIF() +  	FIND_PATH( GLEW_INCLUDE_PATH GL/glew.h  		$ENV{PROGRAMFILES}/GLEW/include  		${PROJECT_SOURCE_DIR}/src/nvgl/glew/include  		DOC "The directory where GL/glew.h resides")  	FIND_LIBRARY( GLEW_LIBRARY - -		# Static linked Release (non-Debug) version -		NAMES glew32s.lib  - -		# Dynamic linked Release (non-Debug) version -		# NAMES glew32.lib  - +		NAMES ${GLEW_LIB_SEARCH_NAME}  		PATHS  		$ENV{PROGRAMFILES}/GLEW/lib  		${PROJECT_SOURCE_DIR}/src/nvgl/glew/bin diff --git a/tests/OffscreenContext.cc b/tests/OffscreenContext.cc index 01ab9c4..f5eabe0 100644 --- a/tests/OffscreenContext.cc +++ b/tests/OffscreenContext.cc @@ -1,9 +1,11 @@ -/* +/*  Create an OpenGL context without creating an OpenGL Window. for Linux. -based on +See Also + glxgears.c by Brian Paul from mesa-demos (mesa3d.org) + http://cgit.freedesktop.org/mesa/demos/tree/src/xdemos?id=mesa-demos-8.0.1   http://www.opengl.org/sdk/docs/man/xhtml/glXIntro.xml   http://www.mesa3d.org/brianp/sig97/offscrn.htm   http://glprogramming.com/blue/ch07.html @@ -59,20 +61,13 @@ bool create_glx_dummy_window(OffscreenContext &ctx)  {    /*    create a dummy X window without showing it. (without 'mapping' it) -  save information to the ctx +  and save information to the ctx. -  based on http://www.opengl.org/sdk/docs/man/xhtml/glXIntro.xml -  which was originally Copyright © 1991-2006 Silicon Graphics, Inc. -  licensed under the SGI Free Software B License. -  See http://oss.sgi.com/projects/FreeB/. +  This purposely does not use glxCreateWindow, to avoid crashes, +  "failed to create drawable" errors, and Mesa "WARNING: Application calling  +    GLX 1.3 function when GLX 1.3 is not supported! This is an application bug!" -  also based on glxgears.c by Brian Paul from mesa-demos (mesa3d.org) - -  purposely does not use glxCreateWindow, to avoid crashes, -  "failed to create drawable" errors, and Mesa "This is an application bug!" -  warnings about GLX 1.3.  - -  this function will alter ctx.openGLContext and ctx.xwindow if successfull +  This function will alter ctx.openGLContext and ctx.xwindow if successfull    */    int attributes[] = { @@ -86,8 +81,8 @@ bool create_glx_dummy_window(OffscreenContext &ctx)    Display *dpy = ctx.xdisplay; -  int numReturned = 0; -  GLXFBConfig *fbconfigs = glXChooseFBConfig( dpy, DefaultScreen(dpy), attributes, &numReturned ); +  int num_returned = 0; +  GLXFBConfig *fbconfigs = glXChooseFBConfig( dpy, DefaultScreen(dpy), attributes, &num_returned );    if ( fbconfigs == NULL ) {      cerr << "glXChooseFBConfig failed\n";      return false; @@ -100,9 +95,9 @@ bool create_glx_dummy_window(OffscreenContext &ctx)      return false;    } +  // can't depend on xWin==NULL at failure. use a custom Xlib error handler instead.    original_xlib_handler = XSetErrorHandler( XCreateWindow_error );    Window xWin = XCreateSimpleWindow( dpy, DefaultRootWindow(dpy), 0,0,10,10, 0,0,0 ); -  // can't depend on xWin==NULL at failure. catch Xlib Errors instead.    XSync( dpy, false );     if ( XCreateWindow_failed ) {       XFree( visinfo ); @@ -110,7 +105,8 @@ bool create_glx_dummy_window(OffscreenContext &ctx)      return false;        }    XSetErrorHandler( original_xlib_handler ); -  // do not call XMapWindow - keep the window hidden + +  // Most programs would call XMapWindow here. But we don't, to keep the window hidden    GLXContext context = glXCreateNewContext( dpy, fbconfigs[0], GLX_RGBA_TYPE, NULL, True );    if ( context == NULL ) { @@ -153,13 +149,15 @@ Bool create_glx_dummy_context(OffscreenContext &ctx)      return False;    } +  // glxQueryVersion is not always reliable. Use it, but then +  // also check to see if GLX 1.3 functions exist  +    glXQueryVersion(ctx.xdisplay, &major, &minor);    if ( major==1 && minor<=2 && glXGetVisualFromFBConfig==NULL ) {      cerr << "Error: GLX version 1.3 functions missing. "          << "Your GLX version: " << major << "." << minor << endl;    } else { -    // if glXGetVisualFromFBConfig exists, pretend we have >=1.3      result = create_glx_dummy_window(ctx);    } @@ -198,7 +196,12 @@ OffscreenContext *create_offscreen_context(int w, int h)      return NULL;    } -  glewInit(); //must come after Context creation and before FBO calls. +  // glewInit must come after Context creation and before FBO calls. +  glewInit();  +  if (GLEW_OK != err) { +    fprintf(stderr, "Unable to init GLEW: %s\n", glewGetErrorString(err)); +    exit(1); +  }    glewCheck();    ctx->fbo = fbo_new(); diff --git a/tests/OffscreenContextWGL.cc b/tests/OffscreenContextWGL.cc new file mode 100644 index 0000000..7f3703d --- /dev/null +++ b/tests/OffscreenContextWGL.cc @@ -0,0 +1,117 @@ +/* + +Create an OpenGL context without creating an OpenGL Window. for Windows. + +*/ + +#include "OffscreenContext.h" +#include "printutils.h" +#include "imageutils.h" +#include "fbo.h" +#include <vector> +#include <GL/gl.h> + +using namespace std; + +struct OffscreenContext +{ +//  GLXContext openGLContext; +  int width; +  int height; +  fbo_t *fbo; +}; + +void offscreen_context_init(OffscreenContext &ctx, int width, int height) +{ +  ctx.width = width; +  ctx.height = height; +  ctx.fbo = NULL; +} + + +void glewCheck() { +#ifdef DEBUG +  cerr << "GLEW version " << glewGetString(GLEW_VERSION) << "\n"; +  cerr << (const char *)glGetString(GL_RENDERER) << "(" << (const char *)glGetString(GL_VENDOR) << ")\n" +       << "OpenGL version " << (const char *)glGetString(GL_VERSION) << "\n"; +  cerr  << "Extensions: " << (const char *)glGetString(GL_EXTENSIONS) << "\n"; + +  if (GLEW_ARB_framebuffer_object) { +    cerr << "ARB_FBO supported\n"; +  } +  if (GLEW_EXT_framebuffer_object) { +    cerr << "EXT_FBO supported\n"; +  } +  if (GLEW_EXT_packed_depth_stencil) { +    cerr << "EXT_packed_depth_stencil\n"; +  } +#endif +} + +OffscreenContext *create_offscreen_context(int w, int h) +{ +  OffscreenContext *ctx = new OffscreenContext; +  offscreen_context_init( *ctx, w, h ); + +  // before an FBO can be setup, a GLX context must be created +  // this call alters ctx->xDisplay and ctx->openGLContext  +  //  and ctx->xwindow if successfull +  cerr << "WGL not implemented\n"; +/* +  if (!create_glx_dummy_context( *ctx )) { +    return NULL; +  } + +  glewInit(); //must come after Context creation and before FBO calls. +  glewCheck(); + +  ctx->fbo = fbo_new(); +  if (!fbo_init(ctx->fbo, w, h)) { +    return NULL; +  } + +*/ +  ctx = NULL; +  return ctx; +} + +bool teardown_offscreen_context(OffscreenContext *ctx) +{ +  if (ctx) { +    fbo_unbind(ctx->fbo); +    fbo_delete(ctx->fbo); +    return true; +  } +  return false; +} + +/*! +  Capture framebuffer from OpenGL and write it to the given filename as PNG. +*/ +bool save_framebuffer(OffscreenContext *ctx, const char *filename) +{ +  if (!ctx || !filename) return false; +  int samplesPerPixel = 4; // R, G, B and A +  vector<GLubyte> pixels(ctx->width * ctx->height * samplesPerPixel); +  glReadPixels(0, 0, ctx->width, ctx->height, GL_RGBA, GL_UNSIGNED_BYTE, &pixels[0]); + +  // Flip it vertically - images read from OpenGL buffers are upside-down +  int rowBytes = samplesPerPixel * ctx->width; +  unsigned char *flippedBuffer = (unsigned char *)malloc(rowBytes * ctx->height); +  if (!flippedBuffer) { +    std::cerr << "Unable to allocate flipped buffer for corrected image."; +    return 1; +  } +  flip_image(&pixels[0], flippedBuffer, samplesPerPixel, ctx->width, ctx->height); + +  bool writeok = write_png(filename, flippedBuffer, ctx->width, ctx->height); + +  free(flippedBuffer); + +  return writeok; +} + +void bind_offscreen_context(OffscreenContext *ctx) +{ +  if (ctx) fbo_bind(ctx->fbo); +} diff --git a/tests/OffscreenView.h b/tests/OffscreenView.h index c57f07e..e3c8579 100644 --- a/tests/OffscreenView.h +++ b/tests/OffscreenView.h @@ -1,15 +1,6 @@  #ifndef OFFSCREENVIEW_H_  #define OFFSCREENVIEW_H_ -// workaround Eigen SIMD alignment problems -#ifndef __APPLE__  -#define EIGEN_DONT_VECTORIZE 1 -#define EIGEN_DISABLE_UNALIGNED_ARRAY_ASSERT 1 -#endif -#ifdef _MSC_VER -#define EIGEN_DONT_ALIGN -#endif -  #include "OffscreenContext.h"  #include <Eigen/Core>  #include <Eigen/Geometry> diff --git a/tests/cgalpngtest.cc b/tests/cgalpngtest.cc index fee44e8..d43e810 100644 --- a/tests/cgalpngtest.cc +++ b/tests/cgalpngtest.cc @@ -23,10 +23,6 @@   *   */ -#ifdef _MSC_VER -#define EIGEN_DONT_ALIGN -#endif -  #include "myqhash.h"  #include "openscad.h"  #include "node.h" @@ -88,6 +84,11 @@ struct CsgInfo  	OffscreenView *glview;  }; + +extern Vector3d getBoundingCenter(BoundingBox bbox); +extern double getBoundingRadius(BoundingBox bbox); + +  int main(int argc, char **argv)  {  	if (argc != 2) { @@ -217,21 +218,19 @@ int main(int argc, char **argv)  	CGALRenderer cgalRenderer(N); -/*	Eigen::AlignedBox<double, 3> bbox; +	BoundingBox bbox;  	if (cgalRenderer.polyhedron) {  		CGAL::Bbox_3 cgalbbox = cgalRenderer.polyhedron->bbox(); -		bbox = BoundingBox(Vector3d(cgalbbox.xmin(), cgalbbox.ymin(), cgalbbox.zmin()), -				 Vector3d(cgalbbox.xmax(), cgalbbox.ymax(), cgalbbox.zmax())); -	} -	else if (cgalRenderer.polyset) { +		Vector3d min = Vector3d(cgalbbox.xmin(), cgalbbox.ymin(), cgalbbox.zmin()); +		Vector3d max = Vector3d(cgalbbox.xmax(), cgalbbox.ymax(), cgalbbox.zmax()); +		bbox = BoundingBox(min, max); +	} else if (cgalRenderer.polyset) {  		bbox = cgalRenderer.polyset->getBoundingBox();  	} -	Vector3d center = (bbox.min() + bbox.max()) / 2; -	double radius = (bbox.max() - bbox.min()).norm() / 2; -*/ -	Vector3d center(0,0,0); -	double radius = 5.0; +	Vector3d center = getBoundingCenter(bbox); +	double radius = getBoundingRadius(bbox); +  	Vector3d cameradir(1, 1, -0.5);  	Vector3d camerapos = center - radius*2*cameradir;  	csgInfo.glview->setCamera(camerapos, center); @@ -246,3 +245,4 @@ int main(int argc, char **argv)  	return 0;  } + | 
