using ofFbo as a Texture on a ofSphere [RE-RESOLVED]

Hi there,
For a project of mine i would like to draw to an ofFbo and use that as a texture via ofFbo::getTextureReference().
As a test i built a small project where i tried to do it, but no chance. The sphere will stay black whatever i do.
My h-file:

  
#pragma once  
  
#include "ofMain.h"  
  
class testApp : public ofBaseApp{  
	public:  
		void setup();  
		void update();  
		void draw();  
		  
		void keyPressed(int key);  
		void keyReleased(int key);  
		void mouseMoved(int x, int y);  
		void mouseDragged(int x, int y, int button);  
		void mousePressed(int x, int y, int button);  
		void mouseReleased(int x, int y, int button);  
		void windowResized(int w, int h);  
		void dragEvent(ofDragInfo dragInfo);  
		void gotMessage(ofMessage msg);  
  
		ofImage earth, texta;  
		GLUquadricObj *quadric;  
		ofTexture text;  
		ofFbo fbo;  
};  
  

my cpp-file:

  
#include "testApp.h"  
  
//--------------------------------------------------------------  
void testApp::setup(){  
	ofBackground(0,0,0);  
  
	ofSetFrameRate(60);  
	ofSetVerticalSync(true);  
	ofDisableArbTex(); //needed for textures to work with gluSphere  
	earth.loadImage("earth.jpg");  
	//text = earth.getTextureReference();  
  
	fbo.allocate(2048, 1024, GL_RGB, 2);  
  
  
	int xMax = earth.getWidth()*3;  
	int yMax = earth.getHeight();  
	unsigned char pixels[xMax * yMax];  
  
	for(int y = 0; y < yMax; y++) {  
		for(int x = 0 ; x < xMax; x++) {  
			pixels[x+y] = (int)(255 * ofRandomuf());  
		}  
	}  
	//earth.reloadTexture();  
	//texta.setFromPixels(pixels);  
	text.allocate(xMax/3, yMax, GL_RGB);  
	text.loadData(pixels, xMax/3, yMax, GL_RGB);  
}  
//--------------------------------------------------------------  
void testApp::update(){  
  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
	fbo.begin();  
			glDisable(GL_DEPTH_TEST);  
			ofSetColor(255);  
			//ofSphere(20);  
			earth.draw(20,20);  
	fbo.end();  
	glEnable(GL_DEPTH_TEST); //enable depth comparisons and update the depth buffer  
	//fbo.draw(0,0);  
	//change origin to center  
	ofTranslate(ofGetWidth()/2, ofGetHeight()/2, 0);  
  
	//rotate sphere over time  
	ofRotateY(ofGetFrameNum());  
	//ofRotateX(); //north pole facing up  
  
	//bind and draw texture  
	//texta.draw(0,0);  
	ofSetColor(255);  
	fbo.getTextureReference().bind();  
	ofScale(10, 10, 10);  
	//ofSphere( 200);  
	glBegin (GL_QUADS);  
	glTexCoord2f (0.0, 0.0);  
	glVertex3f (0.0, 0.0, 0.0);  
	glTexCoord2f (1.0, 0.0);  
	glVertex3f (10.0, 0.0, 0.0);  
	glTexCoord2f (1.0, 1.0);  
	glVertex3f (10.0, 10.0, 0.0);  
	glTexCoord2f (0.0, 1.0);  
	glVertex3f (0.0, 10.0, 0.0);  
	glEnd ();  
	fbo.getTextureReference().unbind();  
	//fbo.draw(0,0);  
}  
  

you can also download all the files (including the textures) her -> http://dl.dropbox.com/u/8807406/planetRenderTest.zip

the issue is that you’re using normalised texture coords (0…1)
where as we’re using rectangular textures in oF, i.e. the coords are (0…getWidth() or getHeight() )

i.e. try:

  
  
    glBegin (GL_QUADS);    
    glTexCoord2f (0.0, 0.0);    
    glVertex3f (0.0, 0.0, 0.0);    
    glTexCoord2f (fbo.getWidth(), 0.0);    
    glVertex3f (10.0, 0.0, 0.0);    
    glTexCoord2f (fbo.getWidth(), fbo.getHeight());    
    glVertex3f (10.0, 10.0, 0.0);    
    glTexCoord2f (0.0, fbo.getHeight());    
    glVertex3f (0.0, 10.0, 0.0);    
    glEnd ();    
  

another solution is a texture transform matrix:

  
  
glMatrixMode(GL_TEXTURE);  
glPushMatrix();  
ofScale(fbo.getWidth(), fbo.getHeight());  
glMatrixMode(GL_MODELVIEW);  
  
//draw here as before  
  
  
  
glMatrixMode(GL_TEXTURE);  
glPopMatrix();  
glMatrixMode(GL_MODELVIEW);  
  

Thanks alot, my texture is now on showing on my sphere :smiley:

Hi,

I’m having the same problem again, all the stuff that i renderer into the fbo doesn’t show up anymore on the Sphere anymore and i don’t see why… :frowning:
I rechecked the code to be sure that not the the solution posted here is posing a problem.

i’m using the dev-branch of openFrameworks and also tried it with the master-branch.

you can find the actual version of my code here: https://github.com/faebser/PlanetRender

Simple but annoying solution:

  
ofDisableArbTex(); //needed for textures to work with gluSphere   

has to be called after rendering of the FBOs.
Code in git-repo has been completely reworked and should be clear now, if not please ask.