diff options
-rw-r--r-- | tests/CMakeLists.txt | 33 | ||||
-rw-r--r-- | tests/FindGLEW.cmake | 9 | ||||
-rw-r--r-- | tests/OffscreenContext.cc | 125 | ||||
-rw-r--r-- | tests/OffscreenContext.h | 6 | ||||
-rw-r--r-- | tests/OffscreenView.cc | 5 | ||||
-rw-r--r-- | tests/csgtermtest.cc | 7 |
6 files changed, 174 insertions, 11 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index fb86448..1d3a004 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -57,6 +57,23 @@ if (NOT OPENCSG_INCLUDE_DIR) endif() include_directories(${OPENCSG_INCLUDE_DIR}) +# SDL (for OpenCSG on Linux & other platforms) +if (NOT $ENV{MACOSX_DEPLOY_DIR} STREQUAL "") + message(STATU "SDL not needed for Mac OSX") +else() + find_package(SDL REQUIRED) + if (NOT SDL_FOUND) + message(FATAL_ERROR "SDL not found. needed for OpenCSG testing on this platform") + else() + message(STATUS "SDL library found in " ${SDL_LIBRARY}) + message(STATUS "SDL header found in " ${SDL_INCLUDE_DIR}) + set(OPENGL_LIBRARY ${OPENGL_LIBRARY} ${SDL_LIBRARY}) + endif() +endif() +include_directories(${SDL_INCLUDE_DIR}) + +# GLEW + if (NOT $ENV{MACOSX_DEPLOY_DIR} STREQUAL "") set(GLEW_DIR "$ENV{MACOSX_DEPLOY_DIR}") endif() @@ -148,8 +165,14 @@ target_link_libraries(cgaltest ${CGAL_LIBRARY} ${CGAL_3RD_PARTY_LIBRARIES} ${QT_ # # opencsgtest # -add_executable(opencsgtest opencsgtest.cc OffscreenView.cc OffscreenContext.mm - ../src/opencsgrenderer.cc ../src/throwntogetherrenderer.cc ../src/CSGTermEvaluator.cc ../src/cgal.cc ../src/CGALEvaluator.cc +if (NOT $ENV{MACOSX_DEPLOY_DIR} STREQUAL "") + set(OFFSCREEN_SOURCE "OffscreenContext.mm") +else() + set(OFFSCREEN_SOURCE "OffscreenContext.cc") +endif() + +add_executable(opencsgtest opencsgtest.cc OffscreenView.cc ${OFFSCREEN_SOURCE} + ../src/cgal.cc ../src/OpenCSGRenderer.cc ../src/ThrownTogetherRenderer.cc ../src/CSGTermEvaluator.cc ../src/CGALEvaluator.cc ../src/PolySetCGALEvaluator.cc ../src/qhash.cc ../src/nef2dxf.cc ../src/cgaladv_minkowski2.cc ../src/cgaladv_minkowski3.cc ${COMMON_SOURCES}) @@ -184,7 +207,11 @@ add_cmdline_test(csgtexttest txt ${MINIMAL_FILES}) add_cmdline_test(csgtermtest txt ${MINIMAL_FILES}) # Add cgaltest tests to CTest -LIST(APPEND CGALTEST_FILES ${FEATURES_FILES}) +LIST(APPEND CGALTEST_FILES ${CMAKE_SOURCE_DIR}/../testdata/scad/minimal/cube.scad) +LIST(APPEND CGALTEST_FILES ${CMAKE_SOURCE_DIR}/../testdata/scad/minimal/sphere.scad) +LIST(APPEND CGALTEST_FILES ${CMAKE_SOURCE_DIR}/../testdata/scad/minimal/cylinder.scad) +LIST(APPEND CGALTEST_FILES ${CMAKE_SOURCE_DIR}/../testdata/scad/features/background-modifier.scad) +LIST(APPEND CGALTEST_FILES ${CMAKE_SOURCE_DIR}/../testdata/scad/features/highlight-modifier.scad) LIST(APPEND CGALTEST_FILES ${CMAKE_SOURCE_DIR}/../examples/example001.scad) add_cmdline_test(cgaltest stl ${CGALTEST_FILES}) diff --git a/tests/FindGLEW.cmake b/tests/FindGLEW.cmake index bccb20a..a784990 100644 --- a/tests/FindGLEW.cmake +++ b/tests/FindGLEW.cmake @@ -7,6 +7,9 @@ # GLEW_LIBRARY # +# 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) FIND_PATH( GLEW_INCLUDE_PATH GL/glew.h $ENV{PROGRAMFILES}/GLEW/include @@ -20,15 +23,15 @@ IF (WIN32) ${PROJECT_SOURCE_DIR}/src/nvgl/glew/lib DOC "The GLEW library") ELSE (WIN32) - message(${GLEW_DIR}) + MESSAGE( "-- GLEW_DIR value:" ${GLEW_DIR}) FIND_PATH( GLEW_INCLUDE_PATH GL/glew.h - PATHS + PATHS /usr/include /usr/local/include ${GLEW_DIR}/include NO_DEFAULT_PATH DOC "The directory where GL/glew.h resides") FIND_LIBRARY( GLEW_LIBRARY NAMES GLEW glew - PATHS + PATHS /usr/lib /usr/local/lib ${GLEW_DIR}/lib NO_DEFAULT_PATH DOC "The GLEW library") diff --git a/tests/OffscreenContext.cc b/tests/OffscreenContext.cc new file mode 100644 index 0000000..f2a7e9c --- /dev/null +++ b/tests/OffscreenContext.cc @@ -0,0 +1,125 @@ +#include "OffscreenContext.h" + +#include <GL/gl.h> +#include <GL/glu.h> // for gluCheckExtension +#include <SDL.h> + +// Simple error reporting macros to help keep the sample code clean +#define REPORT_ERROR_AND_EXIT(desc) { std::cout << desc << "\n"; return false; } +#define NULL_ERROR_EXIT(test, desc) { if (!test) REPORT_ERROR_AND_EXIT(desc); } + +struct OffscreenContext +{ + int width; + int height; + GLuint fbo; + GLuint colorbo; + GLuint depthbo; +}; + + +OffscreenContext *create_offscreen_context(int w, int h) +{ + OffscreenContext *ctx = new OffscreenContext; + ctx->width = w; + ctx->height = h; + + // dummy window + SDL_Init(SDL_INIT_VIDEO); + SDL_SetVideoMode(10,10,32,SDL_OPENGL); + + /* + * Test if framebuffer objects are supported + */ + const GLubyte* strExt = glGetString(GL_EXTENSIONS); + GLboolean fboSupported = gluCheckExtension((const GLubyte*)"GL_EXT_framebuffer_object", strExt); + if (!fboSupported) + REPORT_ERROR_AND_EXIT("Your system does not support framebuffer extension - unable to render scene"); + /* + * Create an FBO + */ + GLuint renderBuffer = 0; + GLuint depthBuffer = 0; + // Depth buffer to use for depth testing - optional if you're not using depth testing + glGenRenderbuffersEXT(1, &depthBuffer); + glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthBuffer); + glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, w, h); + REPORTGLERROR("creating depth render buffer"); + + // Render buffer to use for imaging + glGenRenderbuffersEXT(1, &renderBuffer); + glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, renderBuffer); + glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA8, w, h); + REPORTGLERROR("creating color render buffer"); + ctx->fbo = 0; + glGenFramebuffersEXT(1, &ctx->fbo); + glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, ctx->fbo); + REPORTGLERROR("binding framebuffer"); + + glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, + GL_RENDERBUFFER_EXT, renderBuffer); + REPORTGLERROR("specifying color render buffer"); + + if (glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) != + GL_FRAMEBUFFER_COMPLETE_EXT) + REPORT_ERROR_AND_EXIT("Problem with OpenGL framebuffer after specifying color render buffer."); + + glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, + GL_RENDERBUFFER_EXT, depthBuffer); + REPORTGLERROR("specifying depth render buffer"); + + if (glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) != + GL_FRAMEBUFFER_COMPLETE_EXT) + REPORT_ERROR_AND_EXIT("Problem with OpenGL framebuffer after specifying depth render buffer."); + + return ctx; +} + +bool teardown_offscreen_context(OffscreenContext *ctx) +{ + // "un"bind my FBO + glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); + + /* + * Cleanup + */ + return true; +} + +void write_targa(const char *filename, GLubyte *pixels, int width, int height) +{ + FILE *f = fopen( filename, "w" ); + int y; + if (f) { + GLubyte header[] = { + 00,00,02, 00,00,00, 00,00,00, 00,00,00, + 0xff & width, 0xff & width >> 8, + 0xff & height, 0xff & height >> 8, + 32, 0x20 }; // next-to-last = bit depth + fwrite( header, sizeof(header), 1, f); + for (y=height-1; y>=0; y--) + fwrite( pixels + y*width*4, 4, width, f); + fclose(f); + } +} + +bool save_framebuffer(OffscreenContext *ctx, const char *filename) +{ + /* + * Extract the resulting rendering as an image + */ + + int samplesPerPixel = 4; // R, G, B and A + + GLubyte pixels[ ctx->width * ctx->height * samplesPerPixel ]; + glReadPixels(0, 0, ctx->width, ctx->height, GL_BGRA, GL_UNSIGNED_BYTE, pixels); + printf("writing %s\n",filename); + write_targa(filename,pixels,ctx->width, ctx->height); + + return true; +} + +void bind_offscreen_context(OffscreenContext *ctx) +{ + glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, ctx->fbo); +} diff --git a/tests/OffscreenContext.h b/tests/OffscreenContext.h index 0300bcb..f1c7123 100644 --- a/tests/OffscreenContext.h +++ b/tests/OffscreenContext.h @@ -1,7 +1,13 @@ #ifndef OFFSCREENCONTEXT_H_ #define OFFSCREENCONTEXT_H_ +#ifdef Q_WS_MAC #include <OpenGL/OpenGL.h> +#else +#include <GL/glew.h> +#include <GL/gl.h> +#endif + #include <iostream> // for error output #define REPORTGLERROR(task) { GLenum tGLErr = glGetError(); if (tGLErr != GL_NO_ERROR) { std::cout << "OpenGL error " << tGLErr << " while " << task << "\n"; } } diff --git a/tests/OffscreenView.cc b/tests/OffscreenView.cc index f137041..9565908 100644 --- a/tests/OffscreenView.cc +++ b/tests/OffscreenView.cc @@ -1,8 +1,11 @@ #include <GL/glew.h> #include "OffscreenView.h" #include <opencsg.h> -#include "Renderer.h" +#include "renderer.h" #include <math.h> +#include <stdio.h> +#include <string.h> +#include <cstdlib> #define FAR_FAR_AWAY 100000.0 diff --git a/tests/csgtermtest.cc b/tests/csgtermtest.cc index 42d22a0..9be09fc 100644 --- a/tests/csgtermtest.cc +++ b/tests/csgtermtest.cc @@ -122,7 +122,7 @@ int main(int argc, char **argv) AbstractModule *root_module; ModuleInstantiation root_inst; - AbstractNode *root_node; + const AbstractNode *root_node; QFileInfo fileInfo(filename); handle_dep(filename); @@ -154,10 +154,9 @@ int main(int argc, char **argv) // cout << tree.getString(*root_node) << "\n"; - vector<CSGTerm*> highlights; - vector<CSGTerm*> background; CSGTermEvaluator evaluator(tree); - CSGTerm *root_term = evaluator.evaluateCSGTerm(*root_node, highlights, background); + vector<CSGTerm*> empty = vector<CSGTerm*>(); + CSGTerm *root_term = evaluator.evaluateCSGTerm(*root_node, empty, empty); // cout << "Stored terms: " << evaluator.stored_term.size() << "\n"; // for (map<int, class CSGTerm*>::iterator iter = evaluator.stored_term.begin(); |