ofxMSAShape3D and binding textures 006

Hi friends.

I have hit a block that I don’t know how to get around sadly. I am trying to do texture mapping with the new bind command built into 006 on the iphone, and Shape3D, and not coming up with anything.

The buttonsOff.draw(0, 320, 320, 320); command works just fine, so I am pretty sure my texture is ok. I am wondering if there are any commands that I am overlooking that will get my opengl in the right state for doing this kind of drawing.

thanks so much for the help everyone.

  
  
void testApp::setup(){	  
	  
	ofBackground(50, 50, 50);  
	ofSetBackgroundAuto(false);  
	  
	glEnableClientState( GL_VERTEX_ARRAY );  // this should be in OF somewhere.    
	glPointSize(60);  
	glEnable(GL_POINT_SMOOTH);  
	  
  
	buttonsOff.loadImage("allBtnsOffW.png");  
  
	myTexture = buttonsOff.getTextureReference();  
	  
	// touch events will be sent to myTouchListener  
	ofxMultiTouch.addListener(this);	  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
	  
	myShape.begin(GL_TRIANGLE_STRIP);  
	myTexture.bind();  
	myShape.setNormal(0, 0, 1);  
	myShape.setColor(1, 0, 0);  
	myShape.setTexCoord(0, 0);  
	myShape.addVertex(0, 0, 0);  
	  
	myShape.setColor(1, 0, 0);  
	myShape.setTexCoord(1, 0);  
	myShape.addVertex(320, 0, 0);  
	  
	myShape.setColor(1, 0, 1);  
	myShape.setTexCoord(0, 1);  
	myShape.addVertex(0, 320, 0);  
	  
	  
	myShape.setColor(0, 1, 1);  
	myShape.setTexCoord(1, 1);  
	myShape.addVertex(320, 320, 0);  
	  
	myShape.setColor(1, 1, 1);  
	myTexture.unbind();  
	  
	myShape.end();  
	  
	buttonsOff.draw(0, 320, 320, 320);  
}  
  

ah, i thought that may be the case :wink:

By default your texture is probably not gonna be GL_TEXTURE_2D but GL_TEXTURE_RECTANGLE_ARB

So the texture coordinates are not 0…1, but arbitrary (based on the size of the texture).

So you can either:

  • set ofDisableArbTex() before loading the image so the texture is garanteed to be GL_TEXTURE_2D, and you can carry on using 0…1 UV coordinates
    OR
  • you can set your texture coordinates as myTexture.getTextureData()->tex_t and myTexture.getTextureData()->tex_u

hope that helps.

currently your code will be stretching the 1 pixel in the top left of the image, across your whole quad.

ah! thanks very much, will try and do that.

I didn’t realize that the iphone supported ARB textures at all, and I thought it was cramming a TEXTURE_2D into the proper arrangement.

when I am using arbitrary textures, does that mean that the texture coordinates will be the pixel size of the texture? or is it some entirely different number that I need to pull from the texture data itself.

thanks for the help ":smiley:

ah sorry, i didn’t realize it was iphone! Yea on iphone textures are forced to GL_TEXTURE_2D, so what i said above doesn’t apply :wink: )

ok, swapped that, and still coming up with nothing.

I am getting numbers out of tex_t and tex_u that make sense (.5312312somethingsomething), and the colors look a little bit darker than without the bind texure, but nothing is happening other than that.

hmm that’s weird. i would have expected tex_u and tex_t to return 1.0 on iphone because the texture is GL_TEXTURE_2D. Also, in setTexCoord() you also should be able to just use texture coordinates 0…1 and ignore tex_u and tex_t. very strange. no idea I"m afraid, if you find out, let us know!

i would have expected tex_u and tex_t to return 1.0

if you’re using GL_TEXTURE_2D and you use a non power of two with loadData, you would see non “1” tex_t coordinates, since it’s a subregion of the larger texture. ie, 320 x 240 is put into 512 x 256, so you have tex_t of (320/512) and tex_u of (240/256).

ah of course, i generally try and use power of 2 textures so never ran into that problem. so Jon, yea you do need to use tex_u and tex_t!

HA! bingo bango, I got it working…

looks like there is a bug in the addVertex function… it needs to be changed to this:

  
  
	inline void addVertex(float x, float y, float z = 0) {  
#ifndef USE_IMMEDIATE_MODE  
		if(safeMode) {  
			if(numVertices >= reservedSize) reserve(reservedSize * 1.1);		// if we hit limit, increase reserve by 10%  
			memcpy(normalArray + numVertices*3, curNormal, 3*SIZEOF_FLOAT);  
			memcpy(colorArray + numVertices*4, curColor, 4*SIZEOF_FLOAT);  
			memcpy(texCoordArray + numVertices*2, curTexCoord, 2*SIZEOF_FLOAT);  
		} else {  
			if(normalEnabled) memcpy(normalArray + numVertices*3, curNormal, 3*SIZEOF_FLOAT);  
			if(colorEnabled) memcpy(colorArray + numVertices*4, curColor, 4*SIZEOF_FLOAT);  
			if(texCoordEnabled) memcpy(texCoordArray + numVertices*2, curTexCoord, 2*SIZEOF_FLOAT);  
		}  
		  
		vertexArray[vertexIndex++]		= x;  
		vertexArray[vertexIndex++]		= y;  
		vertexArray[vertexIndex++]		= z;  
		  
		numVertices++;  
#else  
		glVertex3f(x, y, z);  
#endif	  
	}  
  

Note, I have no fucking clue what I am doing, but this seems to work…

ah wow, that is a serious copy/paste bug right there! sorry bout that, and thanks for finding it! :wink:
(I’ve updated it on the SVN thanks)

Hi

I am trying to use an image as texture with in ofxMSAShape3D code.
but i just can get it to work.

i am not sure if i allocate the wrong size, set up the texture wrong, the the u and v texCoord mapping wrong.

A small example would be so helpful. I am trying to get this working on the iPhone.

Anyone?

Thanks,
Stephan.

ok. i got it.

.h

  
#ifndef _TEST_APP  
#define _TEST_APP  
#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);  
	  
	int w,h;  
	unsigned char 	* imgPixels;  
	ofTexture myTexture;  
		ofImage myImg;  
};  
#endif	  

.cpp

  
#include "testApp.h"  
#include "ofxMSAShape3D.h"  
ofxMSAShape3D myShape;  
  
//--------------------------------------------------------------  
void testApp::setup(){	   
	  
	ofDisableArbTex();  
	  
	myImg.loadImage("images/berlin2.jpg");  
	myImg.setImageType(OF_IMAGE_COLOR);  
	  
	 w = 512; //1000;  
	 h = 256; //60;  
  
	imgPixels = myImg.getPixels();  
	myTexture.allocate(w,h,GL_RGB);	  
	myTexture.loadData(imgPixels, w,h, GL_RGB);  
	//myTexture = myImg.getTextureReference();  
}  
  
  
//--------------------------------------------------------------  
void testApp::update(){  
	ofBackground(255,255,255);	  
}  
  
  
//--------------------------------------------------------------  
void testApp::draw(){	  
	ofSetColor(0xFFFFFF);  
  
	myImg.draw(w,h);  
	myTexture.draw(0,h);  
	  
	myShape.begin(GL_QUAD_STRIP);  
	myTexture.bind();  
	  
	myShape.setTexCoord(0, 0);  
	myShape.addVertex(0, 0, 0);  
	  
	myShape.setTexCoord(1, 0);  
	myShape.addVertex(w, 0, 0);  
	  
	myShape.setTexCoord(0, 1);  
	myShape.addVertex(0, h, 0);  
	  
	myShape.setTexCoord(1, 1);  
	myShape.addVertex(w, h, 0);  
	  
	myShape.end();  
	myTexture.unbind();  
}