void PostProcessing::init(unsigned width, unsigned height)
 {
     this->width = width;
     this->height = height;
     
     ofFbo::Settings s;
     s.width = ofNextPow2(width);
     s.height = ofNextPow2(height);
     s.textureTarget = GL_TEXTURE_2D;
     
     // no need to use depth for ping pongs
     for (int i = 0; i < 2; ++i)
     {
         pingPong[i].allocate(s);
     }
     
     s.useDepth = true;
     s.depthStencilInternalFormat = GL_DEPTH_COMPONENT24;
     s.depthStencilAsTexture = true;
     raw.allocate(s);
     
     numPasses = 0;
     currentReadFbo = 0;
     flip = true;
 }
Example #2
0
//----------------------------------------------------------
void ofTexture::allocate(int w, int h, int internalGlDataType, bool bUseARBExtention){
	//our graphics card might not support arb so we have to see if it is supported.
#ifndef TARGET_OPENGLES
	if (bUseARBExtention && GL_ARB_texture_rectangle){
		texData.tex_w = w;
		texData.tex_h = h;
		texData.tex_t = w;
		texData.tex_u = h;
		texData.textureTarget = GL_TEXTURE_RECTANGLE_ARB;
	} else 
#endif
	{
		//otherwise we need to calculate the next power of 2 for the requested dimensions
		//ie (320x240) becomes (512x256)
		texData.tex_w = ofNextPow2(w);
		texData.tex_h = ofNextPow2(h);
		texData.tex_t = w / texData.tex_w;
		texData.tex_u = h / texData.tex_h;
		texData.textureTarget = GL_TEXTURE_2D;
	}
	
	texData.glTypeInternal = internalGlDataType;
	// get the glType (format) and pixelType (type) corresponding to the glTypeInteral (internalFormat)
	ofGetGlFormatAndType(texData.glTypeInternal, texData.glType, texData.pixelType);
	
	// attempt to free the previous bound texture, if we can:
	clear();
	
	glGenTextures(1, (GLuint *)&texData.textureID);   // could be more then one, but for now, just one
	retain(texData.textureID);
	
	glEnable(texData.textureTarget);
	
	glBindTexture(texData.textureTarget, (GLuint)texData.textureID);
#ifndef TARGET_OPENGLES
	// can't do this on OpenGL ES: on full-blown OpenGL, 
	// glInternalFormat and glFormat (GL_LUMINANCE below)
	// can be different; on ES they must be exactly the same.
	//		glTexImage2D(texData.textureTarget, 0, texData.glTypeInternal, (GLint)texData.tex_w, (GLint)texData.tex_h, 0, GL_LUMINANCE, PIXEL_TYPE, 0);  // init to black...
	glTexImage2D(texData.textureTarget, 0, texData.glTypeInternal, (GLint)texData.tex_w, (GLint)texData.tex_h, 0, texData.glType, texData.pixelType, 0);  // init to black...
#else
	glTexImage2D(texData.textureTarget, 0, texData.glTypeInternal, texData.tex_w, texData.tex_h, 0, texData.glTypeInternal, texData.pixelType, 0);
#endif
	
	
	glTexParameterf(texData.textureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameterf(texData.textureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameterf(texData.textureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameterf(texData.textureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	
	glDisable(texData.textureTarget);
	
	texData.width = w;
	texData.height = h;
	texData.bFlipTexture = false;
	texData.bAllocated = true;
}
Example #3
0
void ofTexture::allocate(const ofTextureData & textureData){
	if( textureData.width <= 0.0 || textureData.height <= 0.0 ){
		ofLogError() << "ofTexture::allocate - the ofTextureData structure passed must be set with a width and height.";
		return;
	}

	texData = textureData;
	//our graphics card might not support arb so we have to see if it is supported.
#ifndef TARGET_OPENGLES
	if( texData.textureTarget==GL_TEXTURE_RECTANGLE_ARB && GL_ARB_texture_rectangle ){
		texData.tex_t = texData.width;
		texData.tex_u = texData.height;
		texData.tex_w = texData.width;
		texData.tex_h = texData.height;		
	}else
#endif
	{
		//otherwise we need to calculate the next power of 2 for the requested dimensions
		//ie (320x240) becomes (512x256)
		texData.tex_w = ofNextPow2(texData.width);
		texData.tex_h = ofNextPow2(texData.height);
		texData.tex_t = texData.width / texData.tex_w;
		texData.tex_u = texData.height / texData.tex_h;

		texData.textureTarget = GL_TEXTURE_2D;
	}

	// attempt to free the previous bound texture, if we can:
	clear();

	glGenTextures(1, (GLuint *)&texData.textureID);   // could be more then one, but for now, just one
	retain(texData.textureID);

	glEnable(texData.textureTarget);

	glBindTexture(texData.textureTarget, (GLuint)texData.textureID);
#ifndef TARGET_OPENGLES
	// can't do this on OpenGL ES: on full-blown OpenGL,
	// glInternalFormat and glFormat (GL_LUMINANCE below)
	// can be different; on ES they must be exactly the same.
	//		glTexImage2D(texData.textureTarget, 0, texData.glTypeInternal, (GLint)texData.tex_w, (GLint)texData.tex_h, 0, GL_LUMINANCE, PIXEL_TYPE, 0);  // init to black...
	glTexImage2D(texData.textureTarget, 0, texData.glTypeInternal, (GLint)texData.tex_w, (GLint)texData.tex_h, 0, texData.glType, texData.pixelType, 0);  // init to black...
#else
	glTexImage2D(texData.textureTarget, 0, texData.glTypeInternal, texData.tex_w, texData.tex_h, 0, texData.glTypeInternal, texData.pixelType, 0);
#endif

	glTexParameterf(texData.textureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameterf(texData.textureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameterf(texData.textureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameterf(texData.textureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	glDisable(texData.textureTarget);

	texData.bAllocated = true;

}
Example #4
0
void ofFbo::createAndAttachDepthStencilTexture(GLenum target, GLint internalformat, GLenum format, GLenum type, GLenum  attachment){
	glGenTextures(1, &depthBufferTex.texData.textureID);
	//retainRB(depthBuffer);
	glBindTexture(target, depthBufferTex.texData.textureID);

	glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
#ifndef TARGET_OPENGLES
	glTexParameterf( target, GL_TEXTURE_WRAP_S, GL_CLAMP );
	glTexParameterf( target, GL_TEXTURE_WRAP_T, GL_CLAMP );
#else
	glTexParameterf( target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
	glTexParameterf( target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
#endif

	int textureW = settings.width;
	int textureH = settings.height;
	
	//we have to do all the power-of-two calculations are self. 
	//most of below should be done in ofTexture - so this is all a bit of a hack for now. 
	if( target == GL_TEXTURE_2D ){
		textureW = ofNextPow2(textureW);
		textureH = ofNextPow2(textureH);
	}

	glTexImage2D( target, 0, internalformat, textureW, textureH, 0, format, type, 0 );
	glBindTexture( target, 0 );

	// allocate depthBufferTex as depth buffer;
	depthBufferTex.texData.glTypeInternal = internalformat;
	depthBufferTex.texData.glType = format;
	depthBufferTex.texData.pixelType = type;
	depthBufferTex.texData.textureTarget = target;
	depthBufferTex.texData.bFlipTexture = false;

	depthBufferTex.texData.tex_w	= textureW;
	depthBufferTex.texData.tex_h	= textureH;      
	depthBufferTex.texData.width	= settings.width;
	depthBufferTex.texData.height	= settings.height;

	if( target == GL_TEXTURE_2D ){
		depthBufferTex.texData.tex_t = ofMap(settings.width, 0, depthBufferTex.texData.tex_w, 0, 1, true);
		depthBufferTex.texData.tex_u = ofMap(settings.height, 0, depthBufferTex.texData.tex_h, 0, 1, true);
	}else{
		depthBufferTex.texData.tex_t = textureW;
		depthBufferTex.texData.tex_u = textureH;
	}		

	depthBufferTex.texData.bAllocated = true;

	glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, target, depthBufferTex.texData.textureID, 0);
}
Example #5
0
GLuint ofFbo::createAndAttachRenderbuffer(GLenum internalFormat, GLenum attachmentPoint) {
	GLuint buffer;
	glGenRenderbuffers(1, &buffer);
	glBindRenderbuffer(GL_RENDERBUFFER, buffer);
#ifndef TARGET_OPENGLES
	if(settings.numSamples==0) glRenderbufferStorage(GL_RENDERBUFFER, internalFormat, settings.width, settings.height);
	else glRenderbufferStorageMultisample(GL_RENDERBUFFER, settings.numSamples, internalFormat, settings.width, settings.height);
#else
	glRenderbufferStorage(GL_RENDERBUFFER, internalFormat, ofNextPow2(settings.width), ofNextPow2(settings.height));
#endif
	glFramebufferRenderbuffer(GL_FRAMEBUFFER, attachmentPoint, GL_RENDERBUFFER, buffer);
	return buffer;
}
Example #6
0
ofTexture& ofFbo::getTextureReference(int attachmentPoint) {
	updateTexture(attachmentPoint);
	ofTexture & ref = textures[attachmentPoint];

	//TODO: this should be cached!!!!
    if( ref.texData.textureTarget == GL_TEXTURE_2D ){
        ref.texData.tex_t = ofMap(ref.getWidth(), 0, ofNextPow2(ref.getWidth()), 0, 1, true);
        ref.texData.tex_u = ofMap(ref.getHeight(), 0, ofNextPow2(ref.getHeight()), 0, 1, true);
    }else{
    	ref.texData.tex_t = ref.getWidth();
    	ref.texData.tex_u = ref.getHeight();
    }
    
    return ref;
}
Example #7
0
//----------------------------------------------------------
void ofTextureBak::allocate(int w, int h, int internalGlDataType){

	// 	can pass in anything (320x240) (10x5)
	// 	here we make it power of 2 for opengl (512x256), (16x8)

	if (GLEE_ARB_texture_rectangle){
		tex_w = w;
		tex_h = h;
	} else {
		tex_w = ofNextPow2(w);
		tex_h = ofNextPow2(h);
	}





	if (GLEE_ARB_texture_rectangle){
		tex_t = w;
		tex_u = h;
	} else {
		tex_t = 1.0f;
		tex_u = 1.0f;
	}



	// attempt to free the previous bound texture, if we can:
	clear();

	glGenTextures(1, (GLuint *)textureName);   // could be more then one, but for now, just one

	glEnable(textureTarget);

		glBindTexture(textureTarget, (GLuint)textureName[0]);
		glTexImage2D(textureTarget, 0, internalGlDataType, tex_w, tex_h, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, 0);  // init to black...
		glTexParameterf(textureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameterf(textureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
		glTexParameterf(textureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameterf(textureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	glDisable(textureTarget);

	width = w;
	height = h;
	bFlipTexture = false;
}
Example #8
0
//--------------------------------------------------------------
void testApp::setup() {

	ofBackground(0, 0, 0);
	ofSetVerticalSync(true);


	// billboard particles
	for (int i=0; i<NUM_BILLBOARDS; i++) {
		pos[i].x	  = ofRandomWidth();
		pos[i].y	  = ofRandomHeight();
		vel[i].x	  = ofRandomf();
		vel[i].y	  = ofRandomf();
		home[i]		  = pos[i];
		pointSizes[i] = ofNextPow2(ofRandom(2, 40));
		rotations[i]  = ofRandom(0, 360);
	}

	// set the vertex data
	vbo.setVertexData(pos, NUM_BILLBOARDS, GL_DYNAMIC_DRAW);
	shader.load("shaders/BillboardExample");

	// yes its a fly!
	ofDisableArbTex();
	texture.loadImage("images/fly.png");
}
Example #9
0
//---------------------------------------------------------
void ofSoundStreamSetup(int nOutputs, int nInputs, ofBaseApp * OFSA, int sampleRate, int bufferSize, int nBuffers){

	nInputChannels 		=  nInputs;
	nOutputChannels 	=  nOutputs;
	int device 			=  0;        // default
	OFSAptr 			=  OFSA;

	bufferSize = ofNextPow2(bufferSize);	// must be pow2

	try {
		audio = new RtAudio();
		audio->openStream(	device, nOutputs, device, nInputs, RTAUDIO_FLOAT32,
                      		sampleRate, &bufferSize, nBuffers);
	} catch (RtError &error) {
		error.printMessage();
		//std::exit(EXIT_FAILURE); // need case here
	}

	try {
		audio->setStreamCallback(&receiveAudioBufferAndCallSimpleApp, (void *)NULL);
		audio->startStream();
	} catch (RtError &error) {
		error.printMessage();
	}
}
//------------------------------------------------------------------------------
bool ofRtAudioSoundStream::setup(int outChannels, int inChannels, int _sampleRate, int _bufferSize, int nBuffers){
	if( audio != NULL ){
		close();
	}

	nInputChannels		= inChannels;
	nOutputChannels		= outChannels;

	sampleRate			=  _sampleRate;
	tickCount			=  0;
	bufferSize			= ofNextPow2(_bufferSize);	// must be pow2

	try {
		audio = shared_ptr<RtAudio>(new RtAudio());
	}	catch (RtError &error) {
		error.printMessage();
		return false;
	}

	RtAudio::StreamParameters outputParameters;
	RtAudio::StreamParameters inputParameters;
	if(nInputChannels>0){
		if( inDeviceID >= 0 ){
			inputParameters.deviceId = inDeviceID;
		}else{
			inputParameters.deviceId = audio->getDefaultInputDevice();
		}
		inputParameters.nChannels = nInputChannels;
	}

	if(nOutputChannels>0){
		if( outDeviceID >= 0 ){
			outputParameters.deviceId = outDeviceID;
		}else{
			outputParameters.deviceId = audio->getDefaultOutputDevice();
		}
		outputParameters.nChannels = nOutputChannels;
	}

	unsigned int bufferFrames = (unsigned int)bufferSize; // 256 sample frames

	RtAudio::StreamOptions options;
	options.flags = RTAUDIO_SCHEDULE_REALTIME;
	options.numberOfBuffers = nBuffers;
	options.priority = 1;
	outputBuffer.setDeviceID(outDeviceID);
	inputBuffer.setDeviceID(inDeviceID);

	try {
		audio ->openStream( (nOutputChannels>0)?&outputParameters:NULL, (nInputChannels>0)?&inputParameters:NULL, RTAUDIO_FLOAT32,
							sampleRate, &bufferFrames, &rtAudioCallback, this, &options);
		audio->startStream();
	} catch (RtError &error) {
		error.printMessage();
		return false;
	}
	return true;
}
Example #11
0
//---------------------------------------------------------------------------------
void videoBlob::set(ofxCvBlob myBlob, ofxCvColorImage myImage, ofxCvGrayscaleImage myMask){

	memcpy(&blob, &myBlob, sizeof(ofxCvBlob));

	// now, let's get the data in,
	int w = blob.boundingRect.width;
	int h = blob.boundingRect.height;
	int imgw = myImage.width;
	int imgh = myImage.height;
	int imgx = blob.boundingRect.x;
	int imgy = blob.boundingRect.y;

	unsigned char * blobRGBA = new unsigned char [ w * h * 4 ];
	unsigned char * colorPixels 	= myImage.getPixels();
	unsigned char * grayPixels 		= myMask.getPixels();

	for (int i = 0; i < w; i++){
		for (int j = 0; j < h; j++){
			int posTex = (j * w + i)*4;
			int posGray = ((j+imgy)*imgw + (i + imgx));
			int posCol = posGray * 3;
			blobRGBA[posTex + 0] = colorPixels[posCol + 0];
			blobRGBA[posTex + 1] = colorPixels[posCol + 1];
			blobRGBA[posTex + 2] = colorPixels[posCol + 2];
			blobRGBA[posTex + 3] = grayPixels[posGray];
		}
	}

//	myTexture.clear();
//	myTexture.allocate(w,h,GL_RGBA);

	unsigned char * black = new unsigned char [ofNextPow2(w) * ofNextPow2(h) * 4];
	memset(black, 0, ofNextPow2(w) * ofNextPow2(h) * 4);
//	myTexture.loadData(black, ofNextPow2(w), ofNextPow2(h), GL_RGBA);
//	myTexture.loadData(blobRGBA, w, h, GL_RGBA);

	delete black;
	delete blobRGBA;

	pos.x = blob.centroid.x;
	pos.y = blob.centroid.y;
	scale = 1;
	angle = 0;

}
Example #12
0
 LoopControl::LoopControl(InterfaceListener *_listener, int _id, int _x, int _y, int w, int h, sharedDataContainer *_data) :
 InterfaceObject(_listener, _id, _x, _y), data(_data) {
     setWidth(w);
     setHeight(h);
     maxCaretDist = w  * 0.3;
     minCaretDist = w * 0.05;
     caretDistRange = maxCaretDist - minCaretDist;
     setIsInteractive(true);
     ofFbo::Settings settings;
     settings.width			= ofNextPow2(w);
     settings.height			= ofNextPow2(h);
     fboWidth = settings.width;
     fboHeight = settings.height;
     settings.useDepth		= false;
     settings.useStencil		= false;
     waveFBO.allocate(settings);
     reset();
 }
Example #13
0
void Pixelate::setup() {
    // set fbo settings
    ofFbo::Settings s;
    
    s.width = ofNextPow2(w);
    s.height = ofNextPow2(h);
    s.textureTarget = GL_TEXTURE_2D;
    
    s.useDepth = true;
    s.depthStencilInternalFormat = GL_DEPTH_COMPONENT24;
    s.depthStencilAsTexture = true;
    
    // allocate fbos
//    readFbo.allocate(w, h, GL_RGB);
//    writeFbo.allocate(w, h, GL_RGB);
    readFbo.allocate(s);
    writeFbo.allocate(s);
    
    
    
    readFbo.begin();
    ofClear(255,255,255, 0);
    readFbo.end();
    
    writeFbo.begin();
    ofClear(255,255,255,0);
    writeFbo.end();
    
    // setup shader
    string fragShaderSrc = STRINGIFY(
                                     uniform sampler2D tex;
                                     uniform float xPixels;
                                     uniform float yPixels;
                                     
                                     void main()
                                     {
                                         vec2 texCoords = vec2(floor(gl_TexCoord[0].s * xPixels) / xPixels, floor(gl_TexCoord[0].t * yPixels) / yPixels);
                                         gl_FragColor = texture2D(tex, texCoords);
                                     }
Example #14
0
void ofxFFTBase::setBufferSize(int value) {
    int bufferSizeNew = ofNextPow2(value);
    
    if(bufferSize == bufferSizeNew) {
        return;
    }
    
    bufferSize = bufferSizeNew;
    binSize = (int)(bufferSize * 0.5);
    
    killFFT();
    initFFT();
    initAudioData(fftData, bufferSize);
}
Example #15
0
void ofxFFTBase :: setNoOfBands( int value )
{
	int audioNoOfBandsNew;
	audioNoOfBandsNew	= ofNextPow2( value );
	
	if( audioNoOfBands == audioNoOfBandsNew )
		return;
	
	audioNoOfBands		= OFX_FFT_NO_OF_BANDS;
	audioNoOfBandsHalf	= (int)( audioNoOfBands * 0.5 );
    
    killFFT();
	initFFT();
	initAudioData( rawData, getNoOfBands() );
	initAudioData( fftData, getNoOfBands() );
}
Example #16
0
//---------------------------------------------------------
void ofSoundStreamSetup(int nOutputs, int nInputs, ofBaseApp * OFSA, int sampleRate, int bufferSize, int nBuffers){

	nInputChannels 		=  nInputs;
	nOutputChannels 	=  nOutputs;
	OFSAptr 			=  OFSA;
	bufferSize = ofNextPow2(bufferSize);	// must be pow2


	try {
		audio = new RtAudio();
	}	catch (RtError &error) {
		error.printMessage();
		//std::exit(EXIT_FAILURE); // need case here
	}


	RtAudio::StreamParameters * outputParameters=NULL;
	if(nOutputChannels >0){
		outputParameters = new RtAudio::StreamParameters();
		outputParameters->deviceId = audio->getDefaultOutputDevice();
		outputParameters->nChannels = nOutputChannels;
	}

	RtAudio::StreamParameters * inputParameters = NULL;
	if(nInputChannels>0){
		inputParameters = new RtAudio::StreamParameters;
		inputParameters->deviceId = audio->getDefaultInputDevice();
		inputParameters->nChannels = nInputChannels;
	}

	unsigned int bufferFrames = (unsigned int)bufferSize; // 256 sample frames

	RtAudio::StreamOptions options;
	options.flags = RTAUDIO_SCHEDULE_REALTIME;
	options.numberOfBuffers = nBuffers;
	options.priority = 1;

	try {

		audio ->openStream( outputParameters, inputParameters, RTAUDIO_FLOAT32,
							sampleRate, &bufferFrames, &receiveAudioBufferAndCallSimpleApp, &options);
		audio->startStream();
	} catch (RtError &error) {
		error.printMessage();
	}

}
Example #17
0
void ofApp::initSound(string path) {
    
    killSound();
    
    bool bLoaded = soundPlayer.load(path);
    if(bLoaded == false) {
        return;
    }
    
    sessionDir = ofGetTimestampString("%Y%m%d%H%M%S");
    bool bCreatedDir = ofDirectory::createDirectory(sessionDir);
    if(bCreatedDir == false) {
        return;
    }
    
    ofFile file(path);
    soundBaseName = file.getBaseName();
    soundFileName = file.getFileName();
    file.copyTo(sessionDir + "/" + soundFileName);
    
    fft.startFrameSync(&soundPlayer, numOfSoundFramesPerSecond);
    
    int numOfSoundPixelsTotal = numOfPixelsPerSoundFrame * fft.frameSyncTotal;
    int soundImageSize = sqrt(numOfSoundPixelsTotal);
    soundImageSize = ofNextPow2(soundImageSize);
    
    soundImage.allocate(soundImageSize, soundImageSize, OF_IMAGE_GRAYSCALE);
    soundImage.setColor(ofColor(0, 255));
    soundImage.update();
    
    ofRectangle screenRect(0, 0, ofGetWidth(), ofGetHeight());
    ofRectangle soundImageRect(0, 0, soundImageSize, soundImageSize);
    soundImageRect.scaleTo(screenRect, OF_ASPECT_RATIO_KEEP);
    soundImageRect.x = (int)soundImageRect.x;
    soundImageRect.y = (int)soundImageRect.y;
    soundImageRect.width = (int)soundImageRect.width;
    soundImageRect.height = (int)soundImageRect.height;
    float soundImageScale = soundImageRect.width / (float)soundImageSize;
    
    soundImageMat.makeIdentityMatrix();
    soundImageMat.preMultTranslate(ofVec3f(soundImageRect.x, soundImageRect.y));
    soundImageMat.preMultScale(ofVec3f(soundImageScale, soundImageScale, 1));
    
    ofSetFrameRate(10000);
    ofSetVerticalSync(false);
}
Example #18
0
bool ofxFontStash::setup( string fontFile, float lineHeightPercent , int texDimension /*has to be powerfOfTwo!*/){
	
	if (stash == NULL){
		lineHeight = lineHeightPercent;
		texDimension = ofNextPow2(texDimension);
		stash = sth_create(texDimension,texDimension);
		stashFontID = sth_add_font( stash, ofToDataPath( fontFile ).c_str() );
		if ( stashFontID != 0){
			ofLogNotice("ofxFontStash", "loaded font '%s' in texture (%d x %d)", fontFile.c_str(), texDimension, texDimension );
			return true;
		}else{
			ofLogError("ofxFontStash", "Can't load font! '%s'", fontFile.c_str() );
		}
	}else{
		ofLogError("ofxFontStash", "don't call setup() more than once! %s", fontFile.c_str() );
	}
	return false;
}
bool ofxFontStash::setup(string firstFontFile, float lineHeightPercent , int _texDimension /*has to be powerfOfTwo!*/,
						 bool createMipMaps, int intraCharPadding, float dpiScale_){

	if (stash == NULL){
		dpiScale = dpiScale_;
		extraPadding = intraCharPadding;
		lineHeight = lineHeightPercent;
		texDimension = ofNextPow2(_texDimension);
		stash = ofx_sth_create(texDimension,texDimension, createMipMaps, intraCharPadding, dpiScale);
		stash->doKerning = 0; //kerning disabled by default
		stash->charSpacing = 0.0; //spacing neutral by default
		addFont(firstFontFile);
	}else{
		ofLogError("ofxFontStash") << "don't call setup() more than once!";
	}

	return false;
}
Example #20
0
//--------------------------------------------------------------
void testApp::update() {

	float t   = (ofGetElapsedTimef()) * 0.9f;
	float div = 250.0;

	for (int i=0; i<NUM_BILLBOARDS; i++) {

		// noise
		ofVec3f vec(ofSignedNoise(t, billboardVerts[i].y/div, billboardVerts[i].z/div),
					ofSignedNoise(billboardVerts[i].x/div, t, billboardVerts[i].z/div),
					ofSignedNoise(billboardVerts[i].x/div, billboardVerts[i].y/div, t));

		vec *= 0.3f;
		billboardVels[i] += vec;
		billboardVerts[i] += billboardVels[i];
		billboardVels[i]  *= 0.94f;

		billboardColor[i].a -= 0.002;
		billboardSize[i] += (billboardSizeTarget[i]-billboardSize[i]) * 0.03;

		if(billboardColor[i].a <= 0.0) {
			billboardColor[i].a = 1;
			billboardVerts[i].set(ofRandom(-500, 500),
								  ofRandom(-500, 500),
								  ofRandom(-500, 500));

			// faster to have a power of 2
			billboardSize[i] = 0;
			billboardSizeTarget[i]  = ofNextPow2(ofRandom(4, 64));

		}

	}


	// move the camera around
	float mx = (float)mouseX/(float)ofGetWidth();
	float my = (float)mouseY/(float)ofGetHeight();
	ofVec3f des(mx * 360.0, my * 360.0, 0);
	cameraRotation += (des-cameraRotation) * 0.03;
	zoom += (zoomTarget - zoom) * 0.03;

}
Example #21
0
void faceColorToTexture(ofMesh& mesh, ofImage& image)
{
	vector<ofFloatColor> &color = mesh.getColors();
	int num_face = color.size() / 3;
	
	int tex_size = ofNextPow2(ceil(sqrt(num_face)));
	
	bool arb = ofGetUsingArbTex();
	ofDisableArbTex();
	image.allocate(tex_size, tex_size, OF_IMAGE_COLOR);
	if (arb) ofEnableArbTex();
	
	mesh.clearTexCoords();
	
	image.getPixelsRef().set(0);
	
	float texel_size = (1. / image.getWidth()) * 0.5;
	
	for (int i = 0; i < num_face; i++)
	{
		int u = (i % tex_size);
		int v = (i / tex_size);
		
		ofColor c = color[i * 3];
		
		image.setColor(u, v, c);
		
		float uu = (float)u / image.getWidth() + texel_size;
		float vv = (float)v / image.getHeight() + texel_size;
		
		mesh.addTexCoord(ofVec2f(uu, vv));
		mesh.addTexCoord(ofVec2f(uu, vv));
		mesh.addTexCoord(ofVec2f(uu, vv));
	}
	
	image.update();
	mesh.clearColors();
}
Example #22
0
//--------------------------------------------------------------
void testApp::setup() {

	ofBackground(0, 0, 0);
	ofSetVerticalSync(true);
	ofSetLineWidth(3.0);

	cameraRotation = 0;
	zoom		   = -500;
	zoomTarget     = 200;

	// ------------------------- billboard particles
	for (int i=0; i<NUM_BILLBOARDS; i++) {

		billboardVels[i].set(ofRandomf(), -1.0, ofRandomf());
		billboardVerts[i].set(ofRandom(-500, 500),
							  ofRandom(-500, 500),
							  ofRandom(-500, 500));

		billboardColor[i].set(1, 0, 0, ofRandomuf());

		billboardSize[i]	    = 0;
		billboardSizeTarget[i]  = ofNextPow2(ofRandom(4, 24));	// faster to have a power of 2
	}

	billboardVbo.setVertexData(billboardVerts, NUM_BILLBOARDS, GL_DYNAMIC_DRAW);
	billboardVbo.setColorData(billboardColor, NUM_BILLBOARDS, GL_DYNAMIC_DRAW);

	// load the bilboard shader
	// this is used to change the
	// size of the particle
	billboardShader.load("shaders/BillboardExample");

	// we need to disable ARB textures
	// because we are binding ourselfs
	ofDisableArbTex();
	texture.loadImage("images/dot.png");
}
//--------------------------------------------------------------
void FrameSequenceExtractor::setup(){
	ofBackground(255,255,255);
	ofSetVerticalSync(true);
	frameByframe = false;
    
    passNum = 0;

	// Uncomment this to show movies with alpha channels
	// fingerMovie.setPixelFormat(OF_PIXELS_RGBA);
    
    gui.setup();
    gui.add(fadeFrames.setup("Fade Frames",120,0,360));
    gui.add(inAtPercent.setup("In at %",0,0,100));
    gui.add(outAtPercent.setup("Out at %",100,0,100));
    gui.add(testAtPercent.setup("Test at %",1,0,100));
    gui.add(brightnessSlider.setup("Brightness",50,0,100));
    gui.add(contrastSlider.setup("Contrast",50,0,100));
    
    gui.add(blackSlider.setup("BlackLevel",0,0,90));
    gui.add(whiteSlider.setup("WhiteLevel",100,10,100));
    
    
    gui.add(redSlider.setup("R Scale",100,0,100));
    gui.add(greenSlider.setup("G Scale",100,0,100));
    gui.add(blueSlider.setup("B Scale",100,0,100));
    
    
    gui.add(colorizeR.setup("Colorize R",0.5,0,1));
    gui.add(colorizeG.setup("Colorize G",0.5,0,1));
    gui.add(colorizeB.setup("Colorize B",0.5,0,1));
    gui.add(colorizeL.setup("Colorize L",0.5,0,1));
    gui.add(colorizeA.setup("Colorize Apply",0.5,0,1));
    
    
    
    //gui.add(srcPath.setup("Source File",""));
    gui.add(startButton.setup("START"));
    
	startButton.addListener(this,&FrameSequenceExtractor::startPlayback);
    
    
    // Setup post-processing chain
    post.init(1280,720);
    post.createPass<FxaaPass>()->setEnabled(false);
    post.createPass<RGBShiftPass>()->setEnabled(false);
    
    contrastPass = post.createPass<ContrastPass>();
    contrastPass->setEnabled(false);
    
    rgbScalePass = post.createPass<RGBScalePass>();
    rgbScalePass->setEnabled(true);
    
    levelsPass = post.createPass<BWLevelsPass>();
    levelsPass->setEnabled(true);
    
    offsetPass = post.createPass<OffsetPass>();
    offsetPass->setEnabled(false);
    
    colorizePass = post.createPass<ColorizePass>();
    colorizePass->setEnabled(true);
    
    
    post.createPass<GlowPass>()->setEnabled(false);
    post.createPass<NoiseWarpPass>()->setEnabled(false);
    post.createPass<LimbDarkeningPass>()->setEnabled(false);
    post.createPass<RimHighlightingPass>()->setEnabled(false);
    post.createPass<SSAOPass>()->setEnabled(false);
    post.createPass<ToonPass>()->setEnabled(false);

    post.setFlip(false);
    
    lastJumpedTestFrame = 0;
    
    ofFbo::Settings s;
    s.width = ofNextPow2(1);
    s.height = ofNextPow2(1024);
    s.textureTarget = GL_TEXTURE_2D;
    offsetFbo.allocate(s);
    
    useEffects = false;
}
//------------------------------------------------------------------
void ofTrueTypeFont::loadFont(string filename, int fontsize, bool _bAntiAliased, bool _bFullCharacterSet, bool makeContours){

	bMakeContours = makeContours;

	//------------------------------------------------
	if (bLoadedOk == true){

		// we've already been loaded, try to clean up :

		if (cps != NULL){
			delete[] cps;
		}
		if (texNames != NULL){
			for (int i = 0; i < nCharacters; i++){
				glDeleteTextures(1, &texNames[i]);
			}
			delete[] texNames;
		}
		bLoadedOk = false;
	}
	//------------------------------------------------


	filename = ofToDataPath(filename);

	bLoadedOk 			= false;
	bAntiAlised 		= _bAntiAliased;
	bFullCharacterSet 	= _bFullCharacterSet;
	fontSize			= fontsize;

	//--------------- load the library and typeface
	FT_Library library;
	if (FT_Init_FreeType( &library )){
		ofLog(OF_LOG_ERROR," PROBLEM WITH FT lib");
		return;
	}

	FT_Face face;
	if (FT_New_Face( library, filename.c_str(), 0, &face )) {
		return;
	}

	FT_Set_Char_Size( face, fontsize << 6, fontsize << 6, 96, 96);
	lineHeight = fontsize * 1.43f;

	//------------------------------------------------------
	//kerning would be great to support:
	//ofLog(OF_LOG_NOTICE,"FT_HAS_KERNING ? %i", FT_HAS_KERNING(face));
	//------------------------------------------------------

	nCharacters = bFullCharacterSet ? 256 : 128 - NUM_CHARACTER_TO_START;

	//--------------- initialize character info and textures
	cps       = new charProps[nCharacters];
	texNames  = new GLuint[nCharacters];
	glGenTextures(nCharacters, texNames);

	if(bMakeContours){
		charOutlines.clear();
		charOutlines.assign(nCharacters, ofTTFCharacter());
	}
	//--------------------- load each char -----------------------
	for (int i = 0 ; i < nCharacters; i++){

		//------------------------------------------ anti aliased or not:
		if(FT_Load_Glyph( face, FT_Get_Char_Index( face, (unsigned char)(i+NUM_CHARACTER_TO_START) ), FT_LOAD_DEFAULT )){
			ofLog(OF_LOG_ERROR,"error with FT_Load_Glyph %i", i);
		}

		if (bAntiAlised == true) FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
		else FT_Render_Glyph(face->glyph, FT_RENDER_MODE_MONO);

		//------------------------------------------
		FT_Bitmap& bitmap= face->glyph->bitmap;

		// 3 pixel border around the glyph
		// We show 2 pixels of this, so that blending looks good.
		// 1 pixels is hidden because we don't want to see the real edge of the texture

		border			= 3;
		visibleBorder	= 2;

		if(bMakeContours){
			if( printVectorInfo )printf("\n\ncharacter %c: \n", char( i+NUM_CHARACTER_TO_START ) );

			//int character = i + NUM_CHARACTER_TO_START;
			charOutlines[i] = makeContoursForCharacter( face );
		}

		// prepare the texture:
		int width  = ofNextPow2( bitmap.width + border*2 );
		int height = ofNextPow2( bitmap.rows  + border*2 );


		// ------------------------- this is fixing a bug with small type
		// ------------------------- appearantly, opengl has trouble with
		// ------------------------- width or height textures of 1, so we
		// ------------------------- we just set it to 2...
		if (width == 1) width = 2;
		if (height == 1) height = 2;

		// -------------------------
		// info about the character:
		cps[i].value 			= i;
		cps[i].height 			= face->glyph->bitmap_top;
		cps[i].width 			= face->glyph->bitmap.width;
		cps[i].setWidth 		= face->glyph->advance.x >> 6;
		cps[i].topExtent 		= face->glyph->bitmap.rows;
		cps[i].leftExtent		= face->glyph->bitmap_left;

		// texture internals
		cps[i].tTex             = (float)(bitmap.width + visibleBorder*2)  /  (float)width;
		cps[i].vTex             = (float)(bitmap.rows +  visibleBorder*2)   /  (float)height;

		cps[i].xOff             = (float)(border - visibleBorder) / (float)width;
		cps[i].yOff             = (float)(border - visibleBorder) / (float)height;


		/* sanity check:
		ofLog(OF_LOG_NOTICE,"%i %i %i %i %i %i",
		cps[i].value ,
		cps[i].height ,
		cps[i].width 	,
		cps[i].setWidth 	,
		cps[i].topExtent ,
		cps[i].leftExtent	);
		*/


		// Allocate Memory For The Texture Data.
		unsigned char* expanded_data = new unsigned char[ 2 * width * height];

		//-------------------------------- clear data:
		for(int j=0; j <height;j++) {
			for(int k=0; k < width; k++){
				expanded_data[2*(k+j*width)  ] = 255;   // every luminance pixel = 255
				expanded_data[2*(k+j*width)+1] = 0;
			}
		}


		if (bAntiAlised == true){
			//-----------------------------------
			for(int j=0; j <height; j++) {
				for(int k=0; k < width; k++){
					if ((k<bitmap.width) && (j<bitmap.rows)){
						expanded_data[2*((k+border)+(j+border)*width)+1] = bitmap.buffer[k + bitmap.width*(j)];
					}
				}
			}
			//-----------------------------------
		} else {
			//-----------------------------------
			// true type packs monochrome info in a
			// 1-bit format, hella funky
			// here we unpack it:
			unsigned char *src =  bitmap.buffer;
			for(int j=0; j <bitmap.rows;j++) {
				unsigned char b=0;
				unsigned char *bptr =  src;
				for(int k=0; k < bitmap.width ; k++){
					expanded_data[2*((k+1)+(j+1)*width)] = 255;
					if (k%8==0){ b = (*bptr++);}
					expanded_data[2*((k+1)+(j+1)*width) + 1] =
						b&0x80 ? 255 : 0;
				    b <<= 1;
				}
				src += bitmap.pitch;
			}
			//-----------------------------------
		}


		//Now we just setup some texture paramaters.
		glBindTexture( GL_TEXTURE_2D, texNames[i]);
		#ifndef TARGET_OF_IPHONE
			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
		#endif
   		if (bAntiAlised == true){
			glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
			glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		} else {
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
		}
		glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

		//Here we actually create the texture itself, notice
		//that we are using GL_LUMINANCE_ALPHA to indicate that
		//we are using 2 channel data.

		#ifndef TARGET_OF_IPHONE // gluBuild2DMipmaps doesn't seem to exist in anything i had in the iphone build... so i commented it out
			bool b_use_mipmaps = false;  // FOR now this is fixed to false, could be an option, left in for legacy...
			if (b_use_mipmaps){
				gluBuild2DMipmaps(
					GL_TEXTURE_2D, GL_LUMINANCE_ALPHA, width, height,
					GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, expanded_data);
			} else
		#endif
		{
	    	glTexImage2D( GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, width, height,
			   0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, expanded_data );
		}


		//With the texture created, we don't need to expanded data anymore

    	delete [] expanded_data;

   }

	// ------------- close the library and typeface
	FT_Done_Face(face);
	FT_Done_FreeType(library);
  	bLoadedOk = true;
}
// ----------------------------------------------------------------------------
float * ofxSoundGetSpectrum(int nBands)
{

	ofxSoundInitialize();

	// 	set to 0
	for (int i = 0; i < 8192; i++){
		fftInterpValues[i] = 0;
	}

	// 	check what the user wants vs. what we can do:
	if (nBands > 8192){
		ofLog(OF_LOG_ERROR, "error in ofxSoundGetSpectrum, the maximum number of bands is 8192 - you asked for %i we will return 8192", nBands);
		nBands = 8192;
	} else if (nBands <= 0){
		ofLog(OF_LOG_ERROR, "error in ofxSoundSpectrum, you've asked for %i bands, minimum is 1", nBands);
		return fftInterpValues;
	}

	// 	FMOD needs pow2
	int nBandsToGet = ofNextPow2(nBands);
	if (nBandsToGet < 64) nBandsToGet = 64;  // can't seem to get fft of 32, etc from fmodex

	// 	get the fft
	FMOD_System_GetSpectrum(sys, fftSpectrum, nBandsToGet, 0, FMOD_DSP_FFT_WINDOW_HANNING);

	// 	convert to db scale
	for(int i = 0; i < nBandsToGet; i++){
        fftValues[i] = 10.0f * (float)log10(1 + fftSpectrum[i]) * 2.0f;
	}

	// 	try to put all of the values (nBandsToGet) into (nBands)
	//  in a way which is accurate and preserves the data:
	//

	if (nBandsToGet == nBands){

		for(int i = 0; i < nBandsToGet; i++){
			fftInterpValues[i] = fftValues[i];
		}

	} else {

		float step 		= (float)nBandsToGet / (float)nBands;
		//float pos 		= 0;
		// so for example, if nBands = 33, nBandsToGet = 64, step = 1.93f;
		int currentBand = 0;

		for(int i = 0; i < nBandsToGet; i++){

			// if I am current band = 0, I care about (0+1) * step, my end pos
			// if i > endPos, then split i with me and my neighbor

			if (i >= ((currentBand+1)*step)){

				// do some fractional thing here...
				float fraction = ((currentBand+1)*step) - (i-1);
				float one_m_fraction = 1 - fraction;
				fftInterpValues[currentBand] += fraction * fftValues[i];
				currentBand++;
				// safety check:
				if (currentBand >= nBands){
					ofLog(OF_LOG_ERROR, "ofxSoundGetSpectrum - currentBand >= nBands");
				}

				fftInterpValues[currentBand] += one_m_fraction * fftValues[i];

			} else {
				// do normal things
				fftInterpValues[currentBand] += fftValues[i];
			}
		}

		// because we added "step" amount per band, divide to get the mean:
		for (int i = 0; i < nBands; i++){
			fftInterpValues[i] /= step;
			if (fftInterpValues[i] > 1)fftInterpValues[i] = 1; 	// this seems "wrong"
		}

	}

	return fftInterpValues;
}
//---------------------------------------------------------
void ofxPortaudioSoundStream::setup(int nOutputs, int nInputs, ofBaseApp * OFSA, unsigned int sampleRate, unsigned int bufferSize, unsigned int nBuffers){

	//FROM PORTAUDIO WIKI
	//err = Pa_Initialize();
	//taken to inialised routine

	cout << "ANR_PA_class: setting up with "<<nInputs << "inputs "<< " and "<<nOutputs<< " outputs and buffersize "<< bufferSize << endl;
	
	if( err != paNoError ) goto errorLine;

	
	if (nInputs > 0){
		setInput(OFSA);
		
		//inputParameters = new PaStreamParameters;
		inputParameters.device = deviceID;
		inputParameters.channelCount = nInputs;
		inputParameters.sampleFormat = paFloat32;
		
			if (inputParameters.device >= 0){
			inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency;
			}else{
				cout <<"NO LATENCY PROBLEM" << endl;
			}

		
		inputParameters.hostApiSpecificStreamInfo = NULL;
		cout << "set up input with "<<inputParameters.channelCount <<" channels"<<endl;
	}
	
	if (nOutputs > 0){
		setOutput(OFSA);
	//	outputParameters = new PaStreamParameters;
		outputParameters.device = deviceID;
		outputParameters.channelCount = nOutputs;
		outputParameters.sampleFormat = paFloat32;
		if (outputParameters.device >= 0){
			outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowInputLatency;
		}
	else{
		cout << "suggested output latency error!!! - " << endl;
		}
		
		
		outputParameters.hostApiSpecificStreamInfo = NULL;
		cout << "set up output with "<<outputParameters.channelCount <<" channels"<<endl;
	}

	cout << "starting stream " << endl;
	
	nInputChannels 		=  nInputs;
	nOutputChannels 	=  nOutputs;
	OFSAptr 			=  OFSA;
	
	bufferSize = ofNextPow2(bufferSize);

	//could add in flag
	//	streamOptions.numberOfBuffers = nBuffers;
	//	streamOptions.flags = paNoFlag;// RTAUDIO_SCHEDULE_REALTIME; - from RtAudio version
	//	streamOptions.priority = 1;
	
	err = Pa_OpenStream( &audio,
						nInputs==0?NULL:&inputParameters,
						nOutputs==0?NULL:&outputParameters,
						sampleRate,
						bufferSize,
						paNoFlag,
						&anrPaAudioCallback,//ofxPortaudioSoundStreamCallback,
						this );

	cout << "error text from opening is " << Pa_GetErrorText( err ) << endl;
	
	err = Pa_StartStream( audio );
	if( err != paNoError ){
		ofLog(OF_LOG_ERROR,"PortAudio error: %s\n",Pa_GetErrorText( err ));
	}
	
	
	return;	
errorLine:
	cout << "got an error"<< endl;
	return;
errorLineNoDevice:
	cout << "got an error there's no device"<< endl;
	return;
errorLineOutput:
	cout << "got an error there's on output"<< endl;
	return;
	

	
}
Example #27
0
void ofTexture::allocate(const ofTextureData & textureData, int glFormat, int pixelType){
	if( textureData.width <= 0.0 || textureData.height <= 0.0 ){
		ofLogError("ofTexture") << "allocate(): ofTextureData has 0 width and/or height: " << textureData.width << "x" << textureData.height;
		return;
	}

	texData = textureData;
	//our graphics card might not support arb so we have to see if it is supported.
#ifndef TARGET_OPENGLES
	if( texData.textureTarget==GL_TEXTURE_RECTANGLE_ARB && ofGLSupportsNPOTTextures() ){
		texData.tex_w = texData.width;
		texData.tex_h = texData.height;
		texData.tex_t = texData.width;
		texData.tex_u = texData.height;
	}else
#endif
	{
		if(ofGLSupportsNPOTTextures()){
			texData.tex_w = texData.width;
			texData.tex_h = texData.height;
		}else{
			//otherwise we need to calculate the next power of 2 for the requested dimensions
			//ie (320x240) becomes (512x256)
			texData.tex_w = ofNextPow2(texData.width);
			texData.tex_h = ofNextPow2(texData.height);
		}

		texData.tex_t = texData.width / texData.tex_w;
		texData.tex_u = texData.height / texData.tex_h;

#ifndef TARGET_OPENGLES
		if( texData.textureTarget==GL_TEXTURE_RECTANGLE_ARB ) texData.textureTarget = GL_TEXTURE_2D;
#endif
	}

	// attempt to free the previous bound texture, if we can:
	clear();

	glGenTextures(1, (GLuint *)&texData.textureID);   // could be more then one, but for now, just one
	retain(texData.textureID);

	enableTextureTarget(0);

	glBindTexture(texData.textureTarget, (GLuint)texData.textureID);
	glTexImage2D(texData.textureTarget, 0, texData.glTypeInternal, (GLint)texData.tex_w, (GLint)texData.tex_h, 0, glFormat, pixelType, 0);  // init to black...

	glTexParameterf(texData.textureTarget, GL_TEXTURE_MAG_FILTER, texData.magFilter);
	glTexParameterf(texData.textureTarget, GL_TEXTURE_MIN_FILTER, texData.minFilter);
	glTexParameterf(texData.textureTarget, GL_TEXTURE_WRAP_S, texData.wrapModeHorizontal);
	glTexParameterf(texData.textureTarget, GL_TEXTURE_WRAP_T, texData.wrapModeVertical);

	#ifndef TARGET_PROGRAMMABLE_GL
		if (!ofIsGLProgrammableRenderer()){
			glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
		}
	#endif
	disableTextureTarget(0);



	texData.bAllocated = true;

}
void PixelTexture::allocate(int w, int h, int internalGlDataType, bool bUseARBExtention){
	
	//our graphics card might not support arb so we have to see if it is supported.
#ifndef TARGET_OPENGLES
	if (bUseARBExtention && GL_ARB_texture_rectangle){
		texData.tex_w = w;
		texData.tex_h = h;
		texData.tex_t = w;
		texData.tex_u = h;
		texData.textureTarget = GL_TEXTURE_RECTANGLE_ARB;
	} else 
#endif
	{
		//otherwise we need to calculate the next power of 2 for the requested dimensions
		//ie (320x240) becomes (512x256)
		texData.tex_w = ofNextPow2(w);
		texData.tex_h = ofNextPow2(h);
		texData.tex_t = 1.0f;
		texData.tex_u = 1.0f;
		texData.textureTarget = GL_TEXTURE_2D;
	}
	
	texData.glTypeInternal = internalGlDataType;
	
	
	// MEMO: todo, add more types
	switch(texData.glTypeInternal) {
#ifndef TARGET_OPENGLES	
		case GL_RGBA32F_ARB:
		case GL_RGBA16F_ARB:
			internal.glType = GL_RGBA;
			internal.pixelType = GL_FLOAT;
			break;
			
		case GL_RGB32F_ARB:
			internal.glType = GL_RGB;
			internal.pixelType = GL_FLOAT;
			break;
			
		case GL_LUMINANCE32F_ARB:
			internal.glType = GL_LUMINANCE;
			internal.pixelType = GL_FLOAT;
			break;
#endif			
			
		default:
			internal.glType = GL_LUMINANCE;
			//internal.pixelType	= GL_UNSIGNED_BYTE;
	}
	
	// attempt to free the previous bound texture, if we can:
	clear();
	
	glGenTextures(1, (GLuint *)&texData.textureID);   // could be more then one, but for now, just one
	
	glEnable(texData.textureTarget);
	
	glBindTexture(texData.textureTarget, (GLuint)texData.textureID);
	
	glTexParameterf(texData.textureTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameterf(texData.textureTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameterf(texData.textureTarget, GL_TEXTURE_WRAP_S, GL_NEAREST);
	glTexParameterf(texData.textureTarget, GL_TEXTURE_WRAP_T, GL_NEAREST);
	
#ifndef TARGET_OPENGLES
	// can't do this on OpenGL ES: on full-blown OpenGL, 
	// internalGlDataType and glDataType (GL_LUMINANCE below)
	// can be different; on ES they must be exactly the same.
	//		glTexImage2D(texData.textureTarget, 0, texData.glTypeInternal, (GLint)texData.tex_w, (GLint)texData.tex_h, 0, GL_LUMINANCE, PIXEL_TYPE, 0);  // init to black...
	glTexImage2D(texData.textureTarget, 0, texData.glTypeInternal, (GLint) texData.tex_w, (GLint) texData.tex_h, 0, internal.glType, internal.pixelType, 0);  // init to black...
#else
	glTexImage2D(texData.textureTarget, 0, texData.glTypeInternal, texData.tex_w, texData.tex_h, 0, texData.glTypeInternal, GL_UNSIGNED_BYTE, 0);
#endif
	
	
	
	
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	
	glDisable(texData.textureTarget);
	
	texData.width = w;
	texData.height = h;
	texData.bFlipTexture = false;
	texData.bAllocated = true;
	
	width = w;
	height = h;
}
Example #29
0
void ofTexture::allocate(const ofTextureData & textureData, int glFormat, int pixelType){
#ifndef TARGET_OPENGLES
	if(texData.textureTarget == GL_TEXTURE_2D || texData.textureTarget == GL_TEXTURE_RECTANGLE_ARB){
#else
	if(texData.textureTarget == GL_TEXTURE_2D){
#endif
		if( textureData.width <= 0.0 || textureData.height <= 0.0 ){
			ofLogError("ofTexture") << "allocate(): ofTextureData has 0 width and/or height: " << textureData.width << "x" << textureData.height;
			return;
		}
	}

	texData = textureData;
	//our graphics card might not support arb so we have to see if it is supported.
#ifndef TARGET_OPENGLES
	if( texData.textureTarget==GL_TEXTURE_RECTANGLE_ARB && ofGLSupportsNPOTTextures() ){
		texData.tex_w = texData.width;
		texData.tex_h = texData.height;
		texData.tex_t = texData.width;
		texData.tex_u = texData.height;
	}else if(texData.textureTarget == GL_TEXTURE_2D)
#endif
	{
		if(ofGLSupportsNPOTTextures()){
			texData.tex_w = texData.width;
			texData.tex_h = texData.height;
		}else{
			//otherwise we need to calculate the next power of 2 for the requested dimensions
			//ie (320x240) becomes (512x256)
			texData.tex_w = ofNextPow2(texData.width);
			texData.tex_h = ofNextPow2(texData.height);
		}

		texData.tex_t = texData.width / texData.tex_w;
		texData.tex_u = texData.height / texData.tex_h;
	}

	// attempt to free the previous bound texture, if we can:
	clear();

	glGenTextures(1, (GLuint *)&texData.textureID);   // could be more then one, but for now, just one
	retain(texData.textureID);

#ifndef TARGET_OPENGLES
	if(texData.textureTarget == GL_TEXTURE_2D || texData.textureTarget == GL_TEXTURE_RECTANGLE_ARB){
#else
	if(texData.textureTarget == GL_TEXTURE_2D){
#endif
		glBindTexture(texData.textureTarget,texData.textureID);
		glTexImage2D(texData.textureTarget, 0, texData.glInternalFormat, (GLint)texData.tex_w, (GLint)texData.tex_h, 0, glFormat, pixelType, 0);  // init to black...

		glTexParameterf(texData.textureTarget, GL_TEXTURE_MAG_FILTER, texData.magFilter);
		glTexParameterf(texData.textureTarget, GL_TEXTURE_MIN_FILTER, texData.minFilter);
		glTexParameterf(texData.textureTarget, GL_TEXTURE_WRAP_S, texData.wrapModeHorizontal);
		glTexParameterf(texData.textureTarget, GL_TEXTURE_WRAP_T, texData.wrapModeVertical);

		#ifndef TARGET_PROGRAMMABLE_GL
			if (!ofIsGLProgrammableRenderer()){
				glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
			}
		#endif
		glBindTexture(texData.textureTarget,0);
	}

	texData.bAllocated = true;

#ifdef TARGET_ANDROID
	registerTexture(this);
#endif
}


void ofTexture::setRGToRGBASwizzles(bool rToRGBSwizzles){
#ifndef TARGET_OPENGLES
	glBindTexture(texData.textureTarget,texData.textureID);
	if(rToRGBSwizzles){
		if(texData.glInternalFormat==GL_R8 ||
				texData.glInternalFormat==GL_R16 ||
				texData.glInternalFormat==GL_R32F){
			 glTexParameteri(texData.textureTarget, GL_TEXTURE_SWIZZLE_R, GL_RED);
			 glTexParameteri(texData.textureTarget, GL_TEXTURE_SWIZZLE_G, GL_RED);
			 glTexParameteri(texData.textureTarget, GL_TEXTURE_SWIZZLE_B, GL_RED);

		}else if(texData.glInternalFormat==GL_RG8 ||
				texData.glInternalFormat==GL_RG16 ||
				texData.glInternalFormat==GL_RG32F){
			 glTexParameteri(texData.textureTarget, GL_TEXTURE_SWIZZLE_R, GL_RED);
			 glTexParameteri(texData.textureTarget, GL_TEXTURE_SWIZZLE_G, GL_RED);
			 glTexParameteri(texData.textureTarget, GL_TEXTURE_SWIZZLE_B, GL_RED);
			 glTexParameteri(texData.textureTarget, GL_TEXTURE_SWIZZLE_A, GL_GREEN);
		}
	}else{
		if(texData.glInternalFormat==GL_R8 ||
				texData.glInternalFormat==GL_R16 ||
				texData.glInternalFormat==GL_R32F){
			 glTexParameteri(texData.textureTarget, GL_TEXTURE_SWIZZLE_R, GL_RED);
			 glTexParameteri(texData.textureTarget, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
			 glTexParameteri(texData.textureTarget, GL_TEXTURE_SWIZZLE_B, GL_BLUE);

		}else if(texData.glInternalFormat==GL_RG8 ||
				texData.glInternalFormat==GL_RG16 ||
				texData.glInternalFormat==GL_RG32F){
			 glTexParameteri(texData.textureTarget, GL_TEXTURE_SWIZZLE_R, GL_RED);
			 glTexParameteri(texData.textureTarget, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
			 glTexParameteri(texData.textureTarget, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
			 glTexParameteri(texData.textureTarget, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
		}
	}
	glBindTexture(texData.textureTarget,0);
#endif
}

void ofTexture::setSwizzle(GLenum srcSwizzle, GLenum dstChannel){
#ifndef TARGET_OPENGLES
	glBindTexture(texData.textureTarget,texData.textureID);
	glTexParameteri(texData.textureTarget, srcSwizzle, dstChannel);
	glBindTexture(texData.textureTarget,0);
#endif
}
void ofxFBOTexture::allocate(int w, int h, int internalGlDataType, int numSamples) {
	_isActive = false;


	//OpenGL supported technologies checking.
	
	if(!bSupportsFBO){
		ofLog(OF_LOG_ERROR, "ofxFBOTexture: GL_EXT_framebuffer_object not supported by current OpenGL renderer. "
							" For Apple machines, more info at http://developer.apple.com/graphicsimaging/opengl/capabilities");
		return;
	}
	
	if(!bSupportsMulti && numSamples){
		ofLog(OF_LOG_WARNING, "ofxFBOTexture: GL_EXT_framebuffer_multisample not supported by current OpenGL renderer."
							" Falling back to non-multisampled mode. To disable this message, specify 0 samples."
							" Apple machines, more info at http://developer.apple.com/graphicsimaging/opengl/capabilities");
		numSamples = 0;
	}
	
	if(!bSupportsBlit && numSamples){
		
		//if there's no blit, then it's not worth using multi
		
		ofLog(OF_LOG_WARNING, "ofxFBOTexture: GL_EXT_framebuffer_blit not supported by current OpenGL renderer."
			  " Falling back to non-multisampled mode. To disable this message, specify 0 samples."
			  " Apple machines, more info at http://developer.apple.com/graphicsimaging/opengl/capabilities");
		numSamples = 0;
	}
	
	
	if(maxSamples < numSamples){
		ofLog(OF_LOG_WARNING, "ofxFBOTexture: requested samples too high. Using GL_SAMPLES instead.");
		numSamples = maxSamples;
	}
	
	
	//validate requested dimensions to see that they are within limits.
	
	if(maxTextureSize < w || maxRenderBufferSize < w){
		ofLog(OF_LOG_WARNING, "ofxFBOTexture: requested width was too large. Using largest allowed value instead.");
		if(maxTextureSize < w) w = maxTextureSize;
		if(maxRenderBufferSize < w) w = maxRenderBufferSize;
	}
	
	if( maxTextureSize < h || maxRenderBufferSize < h){
		ofLog(OF_LOG_WARNING, "ofxFBOTexture: requested height was too large. Using largest allowed value instead.");
		if(maxTextureSize < h) h = maxTextureSize;
		if(maxRenderBufferSize < h) h = maxRenderBufferSize;
	}
	

	/**
		 validates arg3 for legacy function calling style
		 i dont check for 1 because 1 is a valid internal color format
		 so we're not sure if the person is passing a boolean or a color format
		 so we can't accuse them of doing the legacy boolean thing.
		 Even if that stops the FBO, the other color format error will help them out.
	 */
	if(internalGlDataType==0){
		ofLog(OF_LOG_WARNING, "ofxFBOTexture::allocate( _ , _ , HERE , _ ) "
			  "The calling statement passed a boolean value like an older version of FBO object. "
			  "This argument has changed to be a color format "
			  "like GL_RGB, GL_RGBA, GL_RGBA16F_ARB, and GL_RGBA32F_ARB. "
			  "Defaulting to GL_RGBA.");
		
		internalGlDataType = GL_RGBA;
	}
	
	
	
	// attempt to free the previous bound texture, if we can:
	clean();
	
	texData.width = w;
	texData.height = h;
	this->numSamples = numSamples;
	
/*
 *GLEE_ARB_texture_rectangle thows errors in the latest from github (post 0062?) so i commented this out

 */
	//#ifndef TARGET_OPENGLES	
//    if (GLEE_ARB_texture_rectangle){
//        texData.tex_w = w;
//        texData.tex_h = h;
//        texData.textureTarget = GL_TEXTURE_RECTANGLE_ARB;
//    } else
//#endif	
//	{	
        texData.tex_w = ofNextPow2(w);
        texData.tex_h = ofNextPow2(h);
//    }
	
//#ifndef TARGET_OPENGLES	
//	if (GLEE_ARB_texture_rectangle){
//		texData.tex_t = w;
//		texData.tex_u = h;
//	} else
//#endif	
//	{
		texData.tex_t = w/texData.tex_w;
		texData.tex_u = h/texData.tex_h;
	//}
	
	texData.width = w;
	texData.height = h;
	texData.bFlipTexture = true;
	texData.glTypeInternal = internalGlDataType;
	
#ifndef TARGET_OPENGLES	
	switch(texData.glTypeInternal) {
		case GL_RGBA32F_ARB:
		case GL_RGBA16F_ARB:
			texData.glType		= GL_RGBA;
			texData.pixelType	= GL_FLOAT;
			pixels				= new float[w * h * 4];
			break;			
		default:
			texData.glType		= texData.glTypeInternal;
			texData.pixelType	= GL_UNSIGNED_BYTE;
			pixels				= new float[w * h * 4];
			break;
	}
#else
	texData.glType		= GL_RGBA;
	texData.pixelType	= GL_UNSIGNED_BYTE;
	pixels				= new unsigned char[w * h * 4];
#endif			
	
	
	// create & setup texture
	glGenTextures(1, (GLuint *)&texData.textureID);   // could be more then one, but for now, just one
	glBindTexture(texData.textureTarget, (GLuint)texData.textureID);
	glTexParameterf(texData.textureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameterf(texData.textureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexParameterf(texData.textureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameterf(texData.textureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexImage2D(texData.textureTarget, 0, texData.glTypeInternal, texData.tex_w, texData.tex_h, 0, texData.glType, texData.pixelType, 0);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	
	
#ifndef TARGET_OPENGLES
	glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, (GLint *) &oldFramebuffer);
	
	if(numSamples ){
		// MULTISAMPLE //
		
		//THEO Create the render buffer for depth
		glGenRenderbuffersEXT(1, &depthBuffer);
		glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthBuffer);
		glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER_EXT, numSamples, GL_DEPTH_COMPONENT, texData.tex_w, texData.tex_h);
		
		//THEO multi sampled color buffer
		glGenRenderbuffersEXT(1, &colorBuffer);
		glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, colorBuffer);
		glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER_EXT, numSamples, texData.glTypeInternal, texData.tex_w, texData.tex_h);
		
		//THEO create fbo for multi sampled content and attach depth and color buffers to it
		glGenFramebuffersEXT(1, &mfbo);
		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mfbo);
		glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, colorBuffer);
		glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthBuffer);
		
	}else{
		//THEO Create the render buffer for depth
		glGenRenderbuffersEXT(1, &depthBuffer);
		glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthBuffer);
		glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, texData.tex_w, texData.tex_h);
		glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthBuffer);
	}
	
	// create & setup FBO
	glGenFramebuffersEXT(1, &fbo);
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
	
	// attach it to the FBO so we can render to it
	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, texData.textureTarget, (GLuint)texData.textureID, 0);
	
	GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
	if(status != GL_FRAMEBUFFER_COMPLETE_EXT) {
		
		//validate arg3 after the fact because it's worth letting them try that format.
		
		if(internalGlDataType != GL_RGBA &&
		   internalGlDataType != GL_RGB &&
		   internalGlDataType != GL_RGBA16F_ARB &&
		   internalGlDataType != GL_RGBA32F_ARB){
			
			ofLog(OF_LOG_ERROR,  "ofxFBOTexture: Failed to initialize. "
				  "I noticed that the calling statement did not passed an expected color format "
				  "like GL_RGB, GL_RGBA, GL_RGBA16F_ARB, and GL_RGBA32F_ARB. You might try one of those. "
				  "ofxFBOTexture::allocate( _ , _ , HERE , _ ) ");
			
		}else{
			ofLog(OF_LOG_ERROR, "ofxFBOTexture: Failed to initialize.");
		}
		
		
		
		std::exit(1);
	}
	clear(0, 0, 0, 0);
	
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, oldFramebuffer);
#else
	
	if(numSamples ){
		ofLog(OF_LOG_WARNING, "Multi-sampling not supported on OpenGL ES");
	}else{
	}
	
	
	glGetIntegerv(GL_FRAMEBUFFER_BINDING_OES, (GLint *) &oldFramebuffer);
	
	// create & setup FBO
	glGenFramebuffersOES(1, &fbo);
	glBindFramebufferOES(GL_FRAMEBUFFER_OES, fbo);
	
	// attach it to the FBO so we can render to it
	glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, texData.textureTarget, (GLuint)texData.textureID, 0);
	
	glBindFramebufferOES(GL_FRAMEBUFFER_OES, oldFramebuffer);
	
#endif	
	
	
	texData.bAllocated = true;
}