//---------------------------------------------------------- void ofGLRenderer::setupScreenPerspective(float width, float height, ofOrientation orientation, bool vFlip, float fov, float nearDist, float farDist) { if(width == 0) width = ofGetWidth(); if(height == 0) height = ofGetHeight(); float viewW = ofGetViewportWidth(); float viewH = ofGetViewportHeight(); float eyeX = viewW / 2; float eyeY = viewH / 2; float halfFov = PI * fov / 360; float theTan = tanf(halfFov); float dist = eyeY / theTan; float aspect = (float) viewW / viewH; if(nearDist == 0) nearDist = dist / 10.0f; if(farDist == 0) farDist = dist * 10.0f; glMatrixMode(GL_PROJECTION); glLoadIdentity(); ofMatrix4x4 persp; persp.makePerspectiveMatrix(fov, aspect, nearDist, farDist); loadMatrix( persp ); //gluPerspective(fov, aspect, nearDist, farDist); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); ofMatrix4x4 lookAt; lookAt.makeLookAtViewMatrix( ofVec3f(eyeX, eyeY, dist), ofVec3f(eyeX, eyeY, 0), ofVec3f(0, 1, 0) ); loadMatrix( lookAt ); //gluLookAt(eyeX, eyeY, dist, eyeX, eyeY, 0, 0, 1, 0); //note - theo checked this on iPhone and Desktop for both vFlip = false and true if(ofDoesHWOrientation()){ if(vFlip){ glScalef(1, -1, 1); glTranslatef(0, -height, 0); } }else{ if( orientation == OF_ORIENTATION_UNKNOWN ) orientation = ofGetOrientation(); switch(orientation) { case OF_ORIENTATION_180: glRotatef(-180, 0, 0, 1); if(vFlip){ glScalef(1, -1, 1); glTranslatef(-width, 0, 0); }else{ glTranslatef(-width, -height, 0); } break; case OF_ORIENTATION_90_RIGHT: glRotatef(-90, 0, 0, 1); if(vFlip){ glScalef(-1, 1, 1); }else{ glScalef(-1, -1, 1); glTranslatef(0, -height, 0); } break; case OF_ORIENTATION_90_LEFT: glRotatef(90, 0, 0, 1); if(vFlip){ glScalef(-1, 1, 1); glTranslatef(-width, -height, 0); }else{ glScalef(-1, -1, 1); glTranslatef(-width, 0, 0); } break; case OF_ORIENTATION_DEFAULT: default: if(vFlip){ glScalef(1, -1, 1); glTranslatef(0, -height, 0); } break; } } }
//---------------------------------------------------------- void ofGLRenderer::setupScreenOrtho(float width, float height, ofOrientation orientation, bool vFlip, float nearDist, float farDist) { if(width == 0) width = ofGetWidth(); if(height == 0) height = ofGetHeight(); float viewW = ofGetViewportWidth(); float viewH = ofGetViewportHeight(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); ofSetCoordHandedness(OF_RIGHT_HANDED); if(vFlip) { ofMatrix4x4 ortho = ofMatrix4x4::newOrthoMatrix(0, width, height, 0, nearDist, farDist); ofSetCoordHandedness(OF_LEFT_HANDED); } ofMatrix4x4 ortho = ofMatrix4x4::newOrthoMatrix(0, viewW, 0, viewH, nearDist, farDist); glMultMatrixf(ortho.getPtr()); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //note - theo checked this on iPhone and Desktop for both vFlip = false and true if(ofDoesHWOrientation()){ if(vFlip){ glScalef(1, -1, 1); glTranslatef(0, -height, 0); } }else{ if( orientation == OF_ORIENTATION_UNKNOWN ) orientation = ofGetOrientation(); switch(orientation) { case OF_ORIENTATION_180: glRotatef(-180, 0, 0, 1); if(vFlip){ glScalef(1, -1, 1); glTranslatef(-width, 0, 0); }else{ glTranslatef(-width, -height, 0); } break; case OF_ORIENTATION_90_RIGHT: glRotatef(-90, 0, 0, 1); if(vFlip){ glScalef(-1, 1, 1); }else{ glScalef(-1, -1, 1); glTranslatef(0, -height, 0); } break; case OF_ORIENTATION_90_LEFT: glRotatef(90, 0, 0, 1); if(vFlip){ glScalef(-1, 1, 1); glTranslatef(-width, -height, 0); }else{ glScalef(-1, -1, 1); glTranslatef(-width, 0, 0); } break; case OF_ORIENTATION_DEFAULT: default: if(vFlip){ glScalef(1, -1, 1); glTranslatef(0, -height, 0); } break; } } }
void ofImage_<PixelType>::grabScreen(int _x, int _y, int _w, int _h){ allocate(_w, _h, OF_IMAGE_COLOR); int sh = ofGetViewportHeight(); // if we are in a FBO or other viewport, this fails: ofGetHeight(); #ifndef TARGET_OPENGLES if(ofIsVFlipped()){ _y = sh - _y; _y -= _h; // top, bottom issues } glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT ); // be nice to anyone else who might use pixelStore glPixelStorei(GL_PACK_ALIGNMENT, 1); glReadPixels(_x, _y, _w, _h, ofGetGlFormat(pixels), GL_UNSIGNED_BYTE, pixels.getPixels()); // read the memory.... glPopClientAttrib(); int sizeOfOneLineOfPixels = pixels.getWidth() * pixels.getBytesPerPixel(); PixelType * tempLineOfPix = new PixelType[sizeOfOneLineOfPixels]; PixelType * linea; PixelType * lineb; for (int i = 0; i < pixels.getHeight()/2; i++){ linea = pixels.getPixels() + i * sizeOfOneLineOfPixels; lineb = pixels.getPixels() + (pixels.getHeight()-i-1) * sizeOfOneLineOfPixels; memcpy(tempLineOfPix, linea, sizeOfOneLineOfPixels); memcpy(linea, lineb, sizeOfOneLineOfPixels); memcpy(lineb, tempLineOfPix, sizeOfOneLineOfPixels); } delete [] tempLineOfPix; #else int sw = ofGetViewportWidth(); int numPixels = width*height; if( numPixels == 0 ){ ofLogError("ofImage") << "grabScreen(): unable to grab screen, image width and/or height are 0: " << width << "x" << height; return; } int numRGBA = numPixels*4; GLubyte *bufferRGBA = (GLubyte *) malloc(numRGBA); if(ofGetOrientation() == OF_ORIENTATION_DEFAULT || ofDoesHWOrientation()) { if(ofIsVFlipped()){ _y = sh - _y; // screen is flipped vertically. _y -= _h; } glPixelStorei(GL_PACK_ALIGNMENT, 1); glReadPixels(_x, _y, _w, _h, GL_RGBA, GL_UNSIGNED_BYTE, bufferRGBA); for(int y = 0; y < _h; y++){ for(int x = 0; x < _w; x++){ int i = y * _w * 3 + x * 3; int j = (_h-1-y) * _w * 4 + x * 4; // rotate 90. pixels.getPixels()[i] = bufferRGBA[j]; pixels.getPixels()[i+1] = bufferRGBA[j+1]; pixels.getPixels()[i+2] = bufferRGBA[j+2]; } } } else if(ofGetOrientation() == OF_ORIENTATION_180) { if(ofIsVFlipped()){ _x = sw - _x; // screen is flipped horizontally. _x -= _w; } glPixelStorei(GL_PACK_ALIGNMENT, 1); glReadPixels(_x, _y, _w, _h, GL_RGBA, GL_UNSIGNED_BYTE, bufferRGBA); for(int y = 0; y < _h; y++){ for(int x = 0; x < _w; x++){ int i = y * _w * 3 + x * 3; int j = y * _w * 4 + (_w-1-x) * 4; // rotate 90. pixels.getPixels()[i] = bufferRGBA[j]; pixels.getPixels()[i+1] = bufferRGBA[j+1]; pixels.getPixels()[i+2] = bufferRGBA[j+2]; } } } else if(ofGetOrientation() == OF_ORIENTATION_90_RIGHT) { swap(_w,_h); swap(_x,_y); if(!ofIsVFlipped()){ _x = sw - _x; // screen is flipped horizontally. _x -= _w; _y = sh - _y; // screen is flipped vertically. _y -= _h; } glPixelStorei(GL_PACK_ALIGNMENT, 1); glReadPixels(_x, _y, _w, _h, GL_RGBA, GL_UNSIGNED_BYTE, bufferRGBA); for(int y = 0; y < _h; y++){ for(int x = 0; x < _w; x++){ int i = x * _h * 3 + y * 3; int j = y * _w * 4 + x * 4; pixels.getPixels()[i] = bufferRGBA[j]; pixels.getPixels()[i+1] = bufferRGBA[j+1]; pixels.getPixels()[i+2] = bufferRGBA[j+2]; } } } else if(ofGetOrientation() == OF_ORIENTATION_90_LEFT) { swap(_w, _h); swap(_x, _y); if(ofIsVFlipped()){ _x = sw - _x; // screen is flipped horizontally. _x -= _w; _y = sh - _y; // screen is flipped vertically. _y -= _h; } glPixelStorei(GL_PACK_ALIGNMENT, 1); glReadPixels(_x, _y, _w, _h, GL_RGBA, GL_UNSIGNED_BYTE, bufferRGBA); for(int y = 0; y < _h; y++){ for(int x = 0; x < _w; x++){ int i = x * _h * 3 + y * 3; int j = (_h-1-y) * _w * 4 + (_w-1-x) * 4; pixels.getPixels()[i] = bufferRGBA[j]; pixels.getPixels()[i+1] = bufferRGBA[j+1]; pixels.getPixels()[i+2] = bufferRGBA[j+2]; } } } free(bufferRGBA); #endif update(); }