OpenGL texture mapping issue in new class

Hi All

I’m new to OpenGL and have a problem. I’ve made a GL_TRIANGLE_STRIP mesh and have managed to apply a texture to it. When moving this code from testApp to a new class, the texture doesn’t appear to be applied. The 3D animation still works but I just get a white plane.

Here’s the setup() method in testApp:

  
void testApp::setup()  
{  
	ofEnableAlphaBlending();  
  
	img.loadImage("image.jpg");  
	texture.allocate(400, 400, GL_RGB, true);  
	texture.loadData(img.getPixels(), 400, 400, GL_RGB);  
	  
	glEnable(GL_DEPTH_TEST);  
	glDepthFunc(GL_LEQUAL);  
		  
	int i;  
	int n = MESH_AMOUNT;  
	for (i = 0; i < n; i++)   
	{  
		Mesh m;  
		m.setInitProps();  
		_meshes.push_back(m);  
	}  
}  

I loop through all the Mesh classes in testApp::draw() and call the following Mesh::draw() method:

  
void Mesh::draw(ofImage img, ofTexture texture, float rot)  
{  
	int texSize = 4;  
	int vertSize = 4;  
	  
	glEnable(texture.getTextureData().textureTarget);  
	glTranslated(400, 150, 0);  
	glRotated(rot, 1.0, 0.5, 0.2);  
	  
	glBegin(GL_TRIANGLE_STRIP);  
	  
	int y;  
	int n = img.height / texSize;  
	for (y = 0; y < n; y++)  
	{  
		int x;  
		int n = img.width / texSize;  
		for (x = 0; x < n; x++)  
		{  
			int z =	0;	//ofRandom(-20, 20);	//cos(x * HALF_PI) * 2;  
			if (y % 2 == 0)  
			{  
				glTexCoord3f(texSize * x, texSize * y, 0.0); glVertex3f(vertSize * x, vertSize * y, z);  
				glTexCoord3f(texSize * x, texSize, 0.0); glVertex3f(vertSize * x, vertSize, z);  
			}  
			else   
			{  
				glTexCoord3f(texSize * abs(x - n), texSize * y, 0.0); glVertex3f(vertSize * abs(x - n), vertSize * y, z);  
				glTexCoord3f(texSize * abs(x - n), texSize, 0.0); glVertex3f(vertSize * abs(x - n), vertSize, z);  
			}  
		}  
	}  
	  
	glEnd();  
}  

Any help would be greatly appreciated.

OK so after a little more research I came across Memo’s MSAShape3D class. I swapped the old code for the following and I’m having exactly the same problem:

  
  
int texSize = 4;	  
int vertSize = 4;  
  
glEnable(texture.getTextureData().textureTarget);	  
glTranslated(400, 150, 0);  
glRotated(rot, 1.0, 0.5, 0.2);  
  
glClear(GL_DEPTH_BUFFER_BIT);  
glEnable(GL_DEPTH_TEST);  
  
myObj.begin(GL_TRIANGLE_STRIP);  
  
int y;  
int n = img.height / texSize;  
for (y = 0; y < n; y++)  
{  
	int x;  
	int n = img.width / texSize;  
	for (x = 0; x < n; x++)  
	{  
		int z =	0;	//ofRandom(-20, 20);	//cos(x * HALF_PI) * 2;  
		if (y % 2 == 0)  
		{  
			myObj.setTexCoord(texSize * x, texSize * y);   
			myObj.addVertex(vertSize * x, vertSize * y, z);  
			myObj.setTexCoord(texSize * x, texSize);   
			myObj.addVertex(vertSize * x, vertSize, z);  
		}  
		else   
		{  
			myObj.setTexCoord(texSize * abs(x - n), texSize * y);   
			myObj.addVertex(vertSize * abs(x - n), vertSize * y, z);  
			myObj.setTexCoord(texSize * abs(x - n), texSize);   
			myObj.addVertex(vertSize * abs(x - n), vertSize, z);  
		}  
	}  
}  
  
myObj.end();  
  

In testApp the texture is applied perfectly, in Mesh it appears white.

Any ideas?

OK so after some further investigation I found that I can’t draw any ofTexture in a non-testApp class. This happens even if I set up the ofTexture in testApp and pass it into Mesh.

Is this a known issue? Surely I should be able to do this.

Here’s the project:

http://augmatic.co.uk/download/OpenGL-test.zip

I’m not sure I follow exactly what you’re trying to do, but you’ll want to change what you’re passing to the setInitProps() and draw() methods to pass a pointer:

  
  
void Mesh::setInitProps(ofImage* img, ofTexture* texture, float rot)  
  
void Mesh::draw(ofImage* img, ofTexture* texture, float rot)  

that gets rid of the errors you might have seen b/c the ofTexture acts a little strangely when being passed by reference.

When drawing a texture atop a mesh you want to do something like the following:

  
  
texture->bind();  
myObj.draw();  
texture->unbind();  
  

Star. Thanks Joshua. That worked a treat.

Love the book btw. Always pushing it onto new oFers.