Skip to main content
gamedev.net
🔒 Locked

Multitexturing & 3D textures...

Started by Yezu 4 replies 2.2k views
Original Post
Yezu
Yezu
Hi! I have two questions... I'm having a problem with using 3d textures and multitexturing. I have a 3d texture and I am unable to rasterize it on the polygon using glMultiTexCoord3f(...); there's nothing where the texture should be It seems only to work with glTexCoord3d(...); Is there another way to combine these two techinques? The second thing is that I want to create a 3d texture form a series of .tga files. And it seems I'm not doing it right either. And I can't find anything useful on this subject on the web... The code is: (based on NeHe .tga loading tutorial)


	GLubyte TGAheader[12] = {0,0,2,0,0,0,0,0,0,0,0,0};
	GLubyte TGAcompare[12];				
	GLubyte header[6];				
	GLuint bytesPerPixel;				
	GLuint imageSize;				
	GLuint temp;						
	GLuint type = GL_RGBA;

	TextureImage texture;


	int HEIGHT;
	int WIDTH;
	int BPP;

	FILE *file1 = fopen(filename1, "rb");	

	fread(TGAcompare,1,sizeof(TGAcompare),file1);
	fread(header,1,sizeof(header),file1);

	texture.width  = header[1] * 256 + header[0];	
	texture.height = header[3] * 256 + header[2];	

	texture.bpp = header[4];			        	
	bytesPerPixel = texture.bpp/8;			                
	imageSize = texture.width*texture.height*bytesPerPixel;	

	texture.imageData=(GLubyte *)malloc(imageSize);

	fread(texture.imageData, 1, imageSize, file1);

	for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel)		
	{								
		temp=texture.imageData;				
		texture.imageData = texture.imageData[i + 2];	
		texture.imageData[i + 2] = temp;			
	}

	fclose (file1);	

	HEIGHT = texture.height;
	WIDTH = texture.width;
	BPP = texture.bpp;

	unsigned char *tex3D = (GLubyte *)malloc(WIDTH * HEIGHT * 4 * BPP);

	for(int i = 0 ; i < HEIGHT*WIDTH ; i++)
		tex3D[i + WIDTH * HEIGHT * LAYER * BPP] = texture.imageData;

	
	///CUT!
        ///Here I do just about the same thing, with different files, and
        ///incrementing the LAYER.
        ///END CUT!



	glGenTextures(1, &Tex);	
	glBindTexture(GL_TEXTURE_3D, Tex);
	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
	glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB8, WIDTH, HEIGHT, 4, 0, type, GL_UNSIGNED_BYTE, tex3D);

Can anybody see the flaw?
_______All your base are belong to us.
MARS_999
MARS_999
If you want to use a 3d texture than you need to load up X amount of 2D textures. X being how many you want for the depth and than recombine that into a large array and than upload that to GL.

So

unsigned char temp[256][256][4];//orunsigned cha ***temp = NULL;//allocate memory here with new using a loopthan use temp to store that data for each of the 4 textures you are loading.
Yezu
Yezu
I figured out how to properly load 3d textures, but still I have a problem with displaying them.

I'm applying a 3d texture to a heightmap, a technique rather popular recently.
The texture has 4 layers. Can anybody tell me what the proper cooridinates XYZ of for the texturemapping should be? Anything I do creates me a completely white landscape.

My code so far is:

	glActiveTexture(GL_TEXTURE3);	glBindTexture(GL_TEXTURE_3D, a3dTex);	glEnable(GL_TEXTURE_3D);		for(int i = lX - ViewDistance ; i < ViewDistance + lX && i < X-1 ; i++)		{			for(int j = lZ - ViewDistance ; j < ViewDistance + lZ && j < Z-1 ; j++)			{					glBegin(GL_TRIANGLE_STRIP);						glNormal3f(Normals[j].X, Normals[j].Y, Normals[j].Z); 					glMultiTexCoord3f(GL_TEXTURE2, 0.0f, (int)Map[j]%4, 0.0f);					glVertex3f(i, Map[j], j);					glNormal3f(Normals[i+1][j].X, Normals[i+1][j].Y, Normals[i+1][j].Z); 					glMultiTexCoord3f(GL_TEXTURE2, 1.0f, (int)Map[i+1][j]%4, 0.0f);					glVertex3f(i+1, Map[i+1][j], j);					glNormal3f(Normals[j+1].X, Normals[j+1].Y, Normals[j+1].Z); 					glMultiTexCoord3f(GL_TEXTURE2, 0.0f, (int)Map[j+1]%4, 1.0f);					glVertex3f(i, Map[j+1], j+1);					glNormal3f(Normals[i+1][j+1].X, Normals[i+1][j+1].Y, Normals[i+1][j+1].Z);					glMultiTexCoord3f(GL_TEXTURE2, 1.0f, (int)Map[i+1][j+1]%4, 1.0f);					glVertex3f(i+1, Map[i+1][j+1], j+1);								glEnd();				}			}				glActiveTexture(GL_TEXTURE2);		glDisable(GL_TEXTURE_3D);


Does any one have a suggestion what to put in glMultiTexCoord3(); ??
_______All your base are belong to us.
MARS_999
MARS_999
Z is going to be the slice you want to use. So you need xy for the current image and z = 0, next xy z = 1 ect...

Now to determine what slice you need you will need to determine that yourself. And xy should probably be 0-1 just as if it was glTexCood2f()
Yezu
Yezu
Thanks, that helped...
I guess I've also figured out other things that were wrong....
First of all notice that the glActiveTexture() has texture unit 3 and I use texture unit 2 later on. Secondly my funcion that loaded files was suposed to pass the GLuint value by reference and I forgot the '&' sign in the definition.

Everything works fine now. I must have been very tired, it was 1:00 AM on my side :P

Right now I have another question. Right now I have a terrain witch is mipmaped and a 3d texture on it. However that texture is somewhat 'edgy'. Is there a way to mipmap it or smooth it out another way? I haven't been able to find anything useful on the net concerning this, so far. Any ideas?
_______All your base are belong to us.
MARS_999
MARS_999
IIRC, since you are using 3D textures if you try to mipmap them, you will get bleeding from the slices. You can try GL_GENERATE_MIPMAP and see what you get though...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.