How to add texture information in "ccSphere" class?

Any question about the main GUI application (frontend)
Jame
Posts: 7
Joined: Wed Jul 20, 2016 2:51 am

How to add texture information in "ccSphere" class?

Post by Jame »

Hello! I am a novice, I want to a picture of the aspect ratio of 2:1 to "ccSphere" class structure, according to the sphere of the medial do fake "ccPlane" class, add a "setAsTexture" method, why stick texture are displayed in the sphere of the outside rather than inside, thank you!
daniel
Site Admin
Posts: 7713
Joined: Wed Oct 13, 2010 7:34 am
Location: Grenoble, France
Contact:

Re: How to add texture information in "ccSphere" class?

Post by daniel »

Can you clarify your question? It's hard to understand...
Daniel, CloudCompare admin
Jame
Posts: 7
Joined: Wed Jul 20, 2016 2:51 am

Re: How to add texture information in "ccSphere" class?

Post by Jame »

daniel wrote:Can you clarify your question? It's hard to understand...
I am sorry,it is that I want to add texture in "ccSphere" class,now i add a "setAsTexture" function in "ccSphere" class like "ccPlane" class, and
rewrite the" buildUp" function. The question is that the texture is show abnormal and displayed in the sphere of the outside rather than inside.
The code is:

Code: Select all

// Build up.
bool ccSphere::PanoBuildUp()
{
	//[1]vertices
	unsigned count = (g_iCol+1)*(g_iRow+1);
	//[2]faces
	unsigned faces = g_iCol*g_iRow*2;

	if (!init(count,true,faces,0))
	{
		ccLog::Error("[ccSphere::buildUp] Not enough memory");
		return false;
	}

	//[3] Initialize the ball coordinate matrix
	float SpherePt[g_iCol+1][g_iRow+1][3];	
	int step_t = 360/g_iCol;
	int step_f = 180/g_iRow;
	for(int theta=0;theta<=360;theta+=step_t)
	{
		for( int fai=180;fai>=0;fai-=step_f)
		{
			SpherePt[theta/step_t][(180-fai)/step_f][0]=cos((float)theta/180.0*M_PI)*sin((float)fai/180.0*M_PI)*2;
			SpherePt[theta/step_t][(180-fai)/step_f][1]=cos((float)fai/180.0*M_PI)*2;
			SpherePt[theta/step_t][(180-fai)/step_f][2]=sin((float)fai/180.0*M_PI)*sin((float)theta/180.0*M_PI)*2;
		} 
	}


	//[4]vertices
	{
		ccPointCloud* verts = vertices();
		assert(verts);

		CCVector3 N,P;
		for(int i=0; i<=g_iCol; ++i)
		{
			for (int j=0; j<=g_iRow; ++j)
			{
				N.x = SpherePt[i][j][0];
				N.y = SpherePt[i][j][1];
				N.z = SpherePt[i][j][2];
				verts->addNorm(N);
				P = N*m_radius;
				verts->addPoint(P);

			}
		}
	}
	//
	
	
	//[5]faces
	{
		assert(m_triVertIndexes);
		for (int i=0; i<g_iCol; ++i)
		{
			for (int j=0; j<g_iRow; ++j)
			{
				unsigned A,B,C,D;
				A = i*(g_iRow+1)+j;  //  Lower left corner
				B = i*(g_iRow+1)+j+1; // The upper left corner
				C = (i+1)*(g_iRow+1)+j+1; // Upper right corner
				D = (i+1)*(g_iRow+1)+j; // Bottom right corner

				
				addTriangle(A, C, B);
				addTriangle(A, D, C);
				
				
			}
		}

	}


	notifyGeometryUpdate();
	showNormals(true);

	return true;
}

Code: Select all

// load image texture.
bool ccSphere::setAsTexture(const QImage& image)
{

 	if (image.isNull())
 	{
 		ccLog::Warning("[ccSphere::setAsTexture] Invalid texture image!");
 		return false;
 	}
 
 	//texture coordinates
 	TextureCoordsContainer* texCoords = getTexCoordinatesTable();
 	if (!texCoords)
 	{
 		texCoords = new TextureCoordsContainer();
 		if (!texCoords->reserve(g_iCol*g_iRow*4))
 		{
 			//not enough memory
 			ccLog::Warning("[ccSphere::setAsTexture] Not enough memory!");
 			delete texCoords;
 			return false;
 		}
 
 		//[2] Texture image coordinate matrix
 		float ImagePt[g_iCol+1][g_iRow+1][2];	
 		for(int i=0;i<(g_iCol+1);i++)
 		{
 			for(int j=0;j<(g_iRow+1);j++)
 			{
 				ImagePt[i][j][0]= (float)i/g_iCol;					// X
 				ImagePt[i][j][1]= (float)j/g_iRow;					// Y
 			}
 		}
 			
 
 		//create default texture coordinates
 		float TA[2] = { 0.0f, 0.0f };
 		float TB[2] = { 0.0f, 1.0f };
 		float TC[2] = { 1.0f, 1.0f };
 		float TD[2] = { 1.0f, 0.0f };
 		for (int i=0;i<g_iCol;++i)
 		{
 			for (int j=0;j<g_iRow;++j)
 			{
//  Lower left corner
 				TA[0]=ImagePt[i][j][0];
 				TA[1]=ImagePt[i][j][1];
 
 				// The upper left corner
 				TB[0]=ImagePt[i][j][0];
 				TB[1]=ImagePt[i][j+1][1];
 
 				// Upper right corner
 				TC[0]=ImagePt[i+1][j+1][0];
 				TC[1]=ImagePt[i+1][j+1][1];
 
 				// Bottom right corner
 				TD[0]=ImagePt[i+1][j][0];
 				TD[1]=ImagePt[i][j][1];
 
 				texCoords->addElement(TA);
				texCoords->addElement(TC);
 				texCoords->addElement(TB);
 				texCoords->addElement(TD);
 			}
 		}
 
 		setTexCoordinatesTable(texCoords);
 	}
 
 	if (!hasPerTriangleTexCoordIndexes())
 	{
 		if (!reservePerTriangleTexCoordIndexes())
 		{
 			//not enough memory
 			ccLog::Warning("[ccPlane::setAsTexture] Not enough memory!");
 			setTexCoordinatesTable(0);
 			removePerTriangleMtlIndexes();
 			return false;
 		}
 
 		//[3] set default texture indexes.
 		for (int i=0; i<g_iCol; ++i)
 		{
 			for (int j=0; j<g_iRow; ++j)
 			{
 				unsigned A,B,C,D;
 				A = i*(g_iRow)+j;  //  Lower left corner
 				B = i*(g_iRow)+j+1; // The upper left corner
 				C = (i+1)*(g_iRow)+j+1; // Upper right corner
 				D = (i+1)*(g_iRow)+j; // Bottom right corner
 
				
 				addTriangleTexCoordIndexes(A, C, B);
 				addTriangleTexCoordIndexes(A, D, C);


 			}
 		}
 	}
 
 	if (!hasPerTriangleMtlIndexes())
 	{
 		if (!reservePerTriangleMtlIndexes())
 		{
 			//not enough memory
 			ccLog::Warning("[ccPlane::setAsTexture] Not enough memory!");
 			setTexCoordinatesTable(0);
 			removePerTriangleTexCoordIndexes();
 			return false;
 		}
 
 	
 		//[3] set default material indexes
 		for (int i=0; i<g_iCol; ++i)
 		{
 			for (int j=0; j<g_iRow; ++j)
 			{
 				
  				addTriangleMtlIndex(0);
  				addTriangleMtlIndex(0);
 
 			}
 		}
 	}
 
 	//set material
 	if (!getMaterialSet())
 		setMaterialSet(new ccMaterialSet());
 	ccMaterialSet* materialSet = const_cast<ccMaterialSet*>(getMaterialSet());
 	assert(materialSet);
 	//remove old material (if any)
 	materialSet->clear();
 	//add new material
 	{
 		ccMaterial::Shared material(new ccMaterial("texture"));
 		material->setTexture(image,QString(),false);
 		materialSet->addMaterial(material);
 	}
 
 	showMaterials(true);
 
 	return true;

}
Jame
Posts: 7
Joined: Wed Jul 20, 2016 2:51 am

Re: How to add texture information in "ccSphere" class?

Post by Jame »

daniel wrote:Can you clarify your question? It's hard to understand...
The aspect ratio of the picture is 2: 1, just like the attach file:"image1.jpg".Thanks very much!
daniel
Site Admin
Posts: 7713
Joined: Wed Oct 13, 2010 7:34 am
Location: Grenoble, France
Contact:

Re: How to add texture information in "ccSphere" class?

Post by daniel »

I can't see the image. It would be very helpful ;)
Daniel, CloudCompare admin
daniel
Site Admin
Posts: 7713
Joined: Wed Oct 13, 2010 7:34 am
Location: Grenoble, France
Contact:

Re: How to add texture information in "ccSphere" class?

Post by daniel »

Oh, and can you show us a snapshot of the current result?
Daniel, CloudCompare admin
Jame
Posts: 7
Joined: Wed Jul 20, 2016 2:51 am

Re: How to add texture information in "ccSphere" class?

Post by Jame »

daniel wrote:Oh, and can you show us a snapshot of the current result?
Ok! The snapshot of the current result like attach file "snapshot.jpg", I user "ccViewer" to display:
Besides the variable “g_iCol” and "g_iRow" is:

Code: Select all

const int g_iCol=90;//Setting the image many times do longitudinal split
const int g_iRow=45;//Setting the image many times do lateral split
Jame
Posts: 7
Joined: Wed Jul 20, 2016 2:51 am

Re: How to add texture information in "ccSphere" class?

Post by Jame »

daniel wrote:I can't see the image. It would be very helpful ;)
Can't you see the image? I added it by the "Add files" button of "Attachement". ...
daniel
Site Admin
Posts: 7713
Joined: Wed Oct 13, 2010 7:34 am
Location: Grenoble, France
Contact:

Re: How to add texture information in "ccSphere" class?

Post by daniel »

Can you see it?

Once you select it, check that the 'status' is a green check symbol. And just to be sure, click on 'place inline' (with the cursor at end of your message for instance).
Daniel, CloudCompare admin
Jame
Posts: 7
Joined: Wed Jul 20, 2016 2:51 am

Re: How to add texture information in "ccSphere" class?

Post by Jame »

daniel wrote:Oh, and can you show us a snapshot of the current result?
"image1.jpg":
Image
The snapshot of the result:
"snapshot.jpg"
Image
Post Reply