summaryrefslogtreecommitdiff
path: root/tests/OffscreenContext.mm
diff options
context:
space:
mode:
authordonbright <hugh.m.bright@gmail.com>2013-03-05 23:47:14 (GMT)
committerdonbright <hugh.m.bright@gmail.com>2013-03-05 23:47:14 (GMT)
commit42f21c3a0850083d245aa3ac346a53e876f0679e (patch)
treefacf41750e0fe27cc4cdaf352c9c8e687011d103 /tests/OffscreenContext.mm
parent422c668dcb538f181683ae51305bf8d3404f48d6 (diff)
parent4734172c3a16cc06b09e4d2131aa8e380bd0f226 (diff)
Merge pull request #288 from openscad/issue11_2
Issue11 2
Diffstat (limited to 'tests/OffscreenContext.mm')
-rw-r--r--tests/OffscreenContext.mm146
1 files changed, 0 insertions, 146 deletions
diff --git a/tests/OffscreenContext.mm b/tests/OffscreenContext.mm
deleted file mode 100644
index a0995fa..0000000
--- a/tests/OffscreenContext.mm
+++ /dev/null
@@ -1,146 +0,0 @@
-#include "OffscreenContext.h"
-#include "imageutils.h"
-#include "fbo.h"
-#include <iostream>
-#include <sstream>
-
-#import <AppKit/AppKit.h> // for NSOpenGL...
-#include <CoreServices/CoreServices.h>
-#include <sys/utsname.h>
-
-#define REPORTGLERROR(task) { GLenum tGLErr = glGetError(); if (tGLErr != GL_NO_ERROR) { std::cout << "OpenGL error " << tGLErr << " while " << task << "\n"; } }
-
-struct OffscreenContext
-{
- NSOpenGLContext *openGLContext;
- NSAutoreleasePool *pool;
- int width;
- int height;
- fbo_t *fbo;
-};
-
-std::string offscreen_context_getinfo(OffscreenContext *ctx)
-{
- std::stringstream out;
-
- struct utsname name;
- uname(&name);
-
- SInt32 majorVersion,minorVersion,bugFixVersion;
-
- Gestalt(gestaltSystemVersionMajor, &majorVersion);
- Gestalt(gestaltSystemVersionMinor, &minorVersion);
- Gestalt(gestaltSystemVersionBugFix, &bugFixVersion);
-
- const char *arch = "unknown";
- if (sizeof(int*) == 4) arch = "32-bit";
- else if (sizeof(int*) == 8) arch = "64-bit";
-
- out << "GL context creator: Cocoa / CGL\n"
- << "PNG generator: Core Foundation\n"
- << "OS info: Mac OS X " << majorVersion << "." << minorVersion << "." << bugFixVersion << " (" << name.machine << " kernel)\n"
- << "Machine: " << arch << "\n";
- return out.str();
-}
-
-OffscreenContext *create_offscreen_context(int w, int h)
-{
- OffscreenContext *ctx = new OffscreenContext;
- ctx->width = w;
- ctx->height = h;
-
- ctx->pool = [NSAutoreleasePool new];
-
- // Create an OpenGL context just so that OpenGL calls will work.
- // Will not be used for actual rendering.
-
- NSOpenGLPixelFormatAttribute attributes[] = {
- NSOpenGLPFAPixelBuffer,
- NSOpenGLPFANoRecovery,
- NSOpenGLPFADepthSize, 24,
- NSOpenGLPFAStencilSize, 8,
-// Took out the acceleration requirement to be able to run the tests
-// in a non-accelerated VM.
-// NSOpenGLPFAAccelerated,
- (NSOpenGLPixelFormatAttribute) 0
- };
- NSOpenGLPixelFormat *pixFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes:attributes] autorelease];
-
- // Create and make current the OpenGL context to render with (with color and depth buffers)
- ctx->openGLContext = [[NSOpenGLContext alloc] initWithFormat:pixFormat shareContext:nil];
- if (!ctx->openGLContext) {
- std::cerr << "Unable to create NSOpenGLContext\n";
- return NULL;
- }
-
- [ctx->openGLContext makeCurrentContext];
-
- // glewInit must come after Context creation and before FBO calls.
- GLenum err = glewInit();
- if (GLEW_OK != err) {
- std::cerr << "Unable to init GLEW: " << glewGetErrorString(err) << std::endl;
- return NULL;
- }
- glew_dump();
-
- ctx->fbo = fbo_new();
- if (!fbo_init(ctx->fbo, w, h)) {
- return NULL;
- }
-
- return ctx;
-}
-
-bool teardown_offscreen_context(OffscreenContext *ctx)
-{
- fbo_unbind(ctx->fbo);
- fbo_delete(ctx->fbo);
-
- /*
- * Cleanup
- */
- [ctx->openGLContext clearDrawable];
- [ctx->openGLContext release];
-
- [ctx->pool release];
- return true;
-}
-
-/*!
- 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;
- // Read pixels from OpenGL
- int samplesPerPixel = 4; // R, G, B and A
- int rowBytes = samplesPerPixel * ctx->width;
- unsigned char *bufferData = (unsigned char *)malloc(rowBytes * ctx->height);
- if (!bufferData) {
- std::cerr << "Unable to allocate buffer for image extraction.";
- return 1;
- }
- glReadPixels(0, 0, ctx->width, ctx->height, GL_RGBA, GL_UNSIGNED_BYTE,
- bufferData);
- REPORTGLERROR("reading pixels from framebuffer");
-
- // Flip it vertically - images read from OpenGL buffers are upside-down
- unsigned char *flippedBuffer = (unsigned char *)malloc(rowBytes * ctx->height);
- if (!flippedBuffer) {
- std::cout << "Unable to allocate flipped buffer for corrected image.";
- return 1;
- }
- flip_image(bufferData, flippedBuffer, samplesPerPixel, ctx->width, ctx->height);
-
- bool writeok = write_png(filename, flippedBuffer, ctx->width, ctx->height);
-
- free(flippedBuffer);
- free(bufferData);
-
- return writeok;
-}
-
-void bind_offscreen_context(OffscreenContext *ctx)
-{
- fbo_bind(ctx->fbo);
-}
contact: Jan Huwald // Impressum