//---------------------------------------------------------- void ofTexture::loadData(const ofFloatPixels & pix, int glFormat){ if(!isAllocated()){ allocate(pix.getWidth(), pix.getHeight(), ofGetGlInternalFormat(pix), ofGetUsingArbTex(), glFormat, ofGetGlType(pix)); } ofSetPixelStoreiAlignment(GL_UNPACK_ALIGNMENT,pix.getWidth(),pix.getBytesPerChannel(),ofGetNumChannelsFromGLFormat(glFormat)); loadData(pix.getData(), pix.getWidth(), pix.getHeight(), glFormat, ofGetGlType(pix)); }
//---------------------------------------------------------- void ofTexture::loadData(const ofFloatPixels & pix){ if(!isAllocated()){ allocate(pix); }else{ ofSetPixelStoreiAlignment(GL_UNPACK_ALIGNMENT,pix.getBytesStride()); loadData(pix.getData(), pix.getWidth(), pix.getHeight(), ofGetGlFormat(pix), ofGetGlType(pix)); } }
void ofTexture::readToPixels(ofFloatPixels & pixels) const { #ifndef TARGET_OPENGLES pixels.allocate(texData.width,texData.height,ofGetImageTypeFromGLType(texData.glInternalFormat)); ofSetPixelStoreiAlignment(GL_PACK_ALIGNMENT,pixels.getWidth(),pixels.getBytesPerChannel(),pixels.getNumChannels()); glBindTexture(texData.textureTarget,texData.textureID); glGetTexImage(texData.textureTarget,0,ofGetGlFormat(pixels),GL_FLOAT,pixels.getData()); glBindTexture(texData.textureTarget,0); #endif }
//---------------------------------------------------------- void ofTexture::copyTo(ofBufferObject & buffer) const{ ofSetPixelStoreiAlignment(GL_PACK_ALIGNMENT,getWidth(),ofGetBytesPerChannelFromGLType(ofGetGlTypeFromInternal(texData.glInternalFormat)),ofGetNumChannelsFromGLFormat(ofGetGLFormatFromInternal(texData.glInternalFormat))); buffer.bind(GL_PIXEL_PACK_BUFFER); glBindTexture(texData.textureTarget,texData.textureID); glGetTexImage(texData.textureTarget,0,ofGetGLFormatFromInternal(texData.glInternalFormat),ofGetGlTypeFromInternal(texData.glInternalFormat),0); glBindTexture(texData.textureTarget,0); buffer.unbind(GL_PIXEL_PACK_BUFFER); }
//---------------------------------------------------------- void ofTexture::readToPixels(ofShortPixels & pixels) const { #ifndef TARGET_OPENGLES pixels.allocate(texData.width,texData.height,ofGetImageTypeFromGLType(texData.glTypeInternal)); ofSetPixelStoreiAlignment(GL_PACK_ALIGNMENT,pixels.getWidth(),pixels.getBytesPerChannel(),pixels.getNumChannels()); bind(); glGetTexImage(texData.textureTarget,0,ofGetGlFormat(pixels),GL_UNSIGNED_SHORT,pixels.getData()); unbind(); #endif }
void ftAverageVelocity::update() { int width = averageFbo.getWidth(); int height = averageFbo.getHeight(); ofTextureData& data = averageFbo.getTexture().getTextureData(); ofFloatPixels dinges; dinges.getBytesPerChannel(); ofSetPixelStoreiAlignment(GL_PACK_ALIGNMENT,width,4,2); glBindTexture(data.textureTarget, data.textureID); glGetTexImage(data.textureTarget, 0, GL_RG, GL_FLOAT, floatPixelData); glBindTexture(data.textureTarget, 0); // ----- presumably slower method ----- // averageFbo.bind(); // glReadPixels(0, 0, width, height, GL_RG, GL_FLOAT, floatPixelData); // averageFbo.unbind(); // ----- presumably slower method ----- int totalPixels = width * height; ofVec2f velocity, totalVelocity(0,0); float magnitude; totalMagnitude = 0; for (int i=0; i<totalPixels; i++) { velocity.x = floatPixelData[i*2]; velocity.y = floatPixelData[i*2+1]; totalVelocity += velocity; magnitude = velocity.length(); totalMagnitude += magnitude; } averageMagnitude = totalMagnitude / totalPixels; direction = totalVelocity.normalize(); pMagnitude.set(averageMagnitude); pMagnitude.set(totalMagnitude); pDirection.set(direction); // cout << averageMagnitude << " " << averageDirection << " " << endl; }
void ofxTexture3d::loadData(void * data, int w, int h, int d, int xOffset, int yOffset, int zOffset, int glFormat) { if(glFormat!=texData.glType) { ofLogError() << "ofxTexture3d::loadData() failed to upload format " << ofGetGlInternalFormatName(glFormat) << " data to " << ofGetGlInternalFormatName(texData.glType) << " texture" <<endl; return; } if(w > texData.tex_w || h > texData.tex_h || d > texData.tex_d) { ofLogError() << "ofxTexture3d::loadData() failed to upload " << w << "x" << h << "x" << d << " data to " << texData.tex_w << "x" << texData.tex_h << "x" << texData.tex_d << " texture"; return; } ofSetPixelStoreiAlignment(GL_UNPACK_ALIGNMENT,w,1,ofGetNumChannelsFromGLFormat(glFormat)); glEnable(texData.textureTarget); glBindTexture(texData.textureTarget, (GLuint) texData.textureID); glTexSubImage3D(texData.textureTarget, 0, xOffset, yOffset, zOffset, w, h, d, texData.glType, texData.pixelType, data); glDisable(texData.textureTarget); }
// get pixels from a fbo or texture // untested void ftUtil::toPixels(ofTexture& _tex, ofPixels& _pixels) { ofTextureData& texData = _tex.getTextureData(); int format = texData.glInternalFormat; int readFormat, numChannels; switch(format){ case GL_R8: readFormat = GL_RED, numChannels = 1; break; // or is it GL_R case GL_RG8: readFormat = GL_RG, numChannels = 2; break; case GL_RGB8: readFormat = GL_RGB, numChannels = 3; break; case GL_RGBA8: readFormat = GL_RGBA, numChannels = 4; break; default: ofLogWarning("ftUtil") << "toPixels: " << "can only read char texturs to ofPixels"; return; } if (_pixels.getWidth() != texData.width || _pixels.getHeight() != texData.height || _pixels.getNumChannels() != numChannels) { _pixels.allocate(texData.width, texData.height, numChannels); } ofSetPixelStoreiAlignment(GL_PACK_ALIGNMENT, texData.width, 1, numChannels); glBindTexture(texData.textureTarget, texData.textureID); glGetTexImage(texData.textureTarget, 0, readFormat, GL_UNSIGNED_BYTE, _pixels.getData()); glBindTexture(texData.textureTarget, 0); }
//---------------------------------------------------------- void ofTexture::loadData(const float * data, int w, int h, int glFormat){ ofSetPixelStoreiAlignment(GL_UNPACK_ALIGNMENT,w,4,ofGetNumChannelsFromGLFormat(glFormat)); loadData(data, w, h, glFormat, GL_FLOAT); }
//---------------------------------------------------------- void ofTexture::loadData(const unsigned short * data, int w, int h, int glFormat){ ofSetPixelStoreiAlignment(GL_UNPACK_ALIGNMENT,w,2,ofGetNumChannelsFromGLFormat(glFormat)); loadData(data, w, h, glFormat, GL_UNSIGNED_SHORT); }
void ofSetPixelStoreiAlignment(GLenum pname, int w, int bpc, int numChannels){ int stride = w * numChannels * bpc; ofSetPixelStoreiAlignment(pname,stride); }
//---------------------------------------------------------- void ofTexture::loadData(const ofFloatPixels & pix, int glFormat){ ofSetPixelStoreiAlignment(GL_UNPACK_ALIGNMENT,pix.getWidth(),pix.getBytesPerChannel(),ofGetNumChannelsFromGLFormat(glFormat)); loadData(pix.getData(), pix.getWidth(), pix.getHeight(), glFormat, ofGetGlType(pix)); }
//---------------------------------------------------------- void ofTexture::loadData(const ofShortPixels & pix){ ofSetPixelStoreiAlignment(GL_UNPACK_ALIGNMENT,pix.getBytesStride()); loadData(pix.getData(), pix.getWidth(), pix.getHeight(), ofGetGlFormat(pix), ofGetGlType(pix)); }