Skip to main content
gamedev.net
🔒 Locked

Update Texture

Started by crslimin 2 replies 600+ views
Original Post
crslimin
crslimin
I am building a terrain with a texture and a group of objects with another texture. Before I load the group objects, I can update the texture for terrain (eg. I load a.tif image as initial texture on terrain, then I load b.tif to update the texture on terrain). But after I load the group objects with texture c.tif (the objects in the group have the same texture c.tif image), I reload a.tif, however, the texture on terrain become grey color without texture anymore. When I turn off the texture c.tif for the group objects. Then the terrain texture can be updated again. I am using the code below, hope someone can help. Thanks in advance. (note: I use the same LoadGLTextures() see below, to load texture image a.tif, or b.tif binding to texture[0] and load c.tif binding to TreeTexure[0])

int CRender::LoadGLTextures(LPSTR strName, textureImage *texim, GLuint *tex, BOOL GroundTex)
{
	int Status=FALSE;
	GLuint tformat; 
	char str[256];

	LoadTIF(strName, texim, GroundTex, tformat);

	Status=TRUE;

	glGenTextures(1, tex);

	glBindTexture(GL_TEXTURE_2D, *tex);
	glTexImage2D(GL_TEXTURE_2D, 0, tformat, texim->width, texim->height, 0, tformat, GL_UNSIGNED_BYTE, texim->data);
	sprintf(str, "texim->width=%d texim->height=%d", texim->width, texim->height);
	MessageBox(NULL, str,"Test", MB_OK);
		
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

	if(texim->data)
		FreeMem(texim->data);

	return Status;
}

void CRender::RenderDem(SHORT pHeightMap[], int MapWidth, int MapHeight, int DEMwidth, int DEMdepth)
{
	int X = 0, Y = 0;
	double x, y, z;

	xSpacing=(double)MapWidth/(double)(DEMwidth-1);
	zSpacing = (double)MapHeight/(double)(DEMdepth-1);

	int xDimension=DEMwidth;
	int zDimension=DEMdepth;
	float deltaS= 1.0f / (float)(xDimension-1);
	float deltaT = 1.0f / (float)(zDimension-1);
	float s = 0.0f;
	float t = 0.0f;
	GLdouble v1[3], v2[3], norm[3];

	if(!pHeightMap) return;

	glBindTexture(GL_TEXTURE_2D, texture[0]);
	glEnable(GL_TEXTURE_2D);

	glColor3ub(255, 255, 255);

	glBegin( GL_QUADS );
	
	for ( X = 0; X < DEMwidth-1; X++ )
	{
		t=1.0f;
		for ( Y = 0; Y < DEMdepth-1; Y++ )
		{
			x = -((double)(xDimension-1)) * xSpacing / 2.0+X*xSpacing;
			y = pHeightMap[X+Y*DEMwidth];
			z = -((double)(zDimension-1)) * zSpacing / 2.0+Y*zSpacing;
			
			v1[0] = xSpacing;
			v1[1] = pHeightMap[X+Y*DEMwidth+1]-pHeightMap[X+Y*DEMwidth];
			v1[2] = 0.0;

			v2[0] = 0.0;
			v2[1] = pHeightMap[X+Y*DEMwidth+DEMwidth]-pHeightMap[X+Y*DEMwidth];
			v2[2] = -zSpacing;

			crossprod(v1, v2, norm);
			norm_normalize(norm);
			glNormal3dv(norm);

			glTexCoord2f(s, t);
			glVertex3d(x, y, z);

			x = -((double)(xDimension-1)) * xSpacing / 2.0+X*xSpacing;
			y = pHeightMap[X+(Y+1)*DEMwidth];
			z = -((double)(zDimension-1)) * zSpacing / 2.0+(Y+1)*zSpacing;

			v1[0] = xSpacing;
			v1[1] = pHeightMap[X+(Y+1)*DEMwidth+1]-pHeightMap[X+(Y+1)*DEMwidth];
			v1[2] = 0.0;

			v2[0] = 0.0;
			v2[1] = pHeightMap[X+(Y+1)*DEMwidth+DEMwidth]-pHeightMap[X+(Y+1)*DEMwidth];
			v2[2] = -zSpacing;

			crossprod(v1, v2, norm);
			norm_normalize(norm);
			if(Y!=DEMdepth-2)
				glNormal3dv(norm);

			glTexCoord2f(s, t-deltaT);
			glVertex3d(x, y, z);

			x = -((double)(xDimension-1)) * xSpacing / 2.0+(X+1)*xSpacing;
			y = pHeightMap[(X+1)+(Y+1)*DEMwidth];
			z = -((double)(zDimension-1)) * zSpacing / 2.0+(Y+1)*zSpacing;

			v1[0] = xSpacing;
			v1[1] = pHeightMap[(X+1)+(Y+1)*DEMwidth+1]-pHeightMap[(X+1)+(Y+1)*DEMwidth];
			v1[2] = 0.0;

			v2[0] = 0.0;
			v2[1] = pHeightMap[(X+1)+(Y+1)*DEMwidth+DEMwidth]-pHeightMap[(X+1)+(Y+1)*DEMwidth];
			v2[2] = -zSpacing;

			crossprod(v1, v2, norm);
			norm_normalize(norm);
			if(Y!=DEMdepth-2 && X!=DEMwidth-2)
				glNormal3dv(norm);

			glTexCoord2f(s+deltaS, t-deltaT);
			glVertex3d(x, y, z);

			x = -((double)(xDimension-1)) * xSpacing / 2.0+(X+1)*xSpacing;
			y = pHeightMap[(X+1)+Y*DEMwidth];
			z = -((double)(zDimension-1)) * zSpacing / 2.0+Y*zSpacing;

			v1[0] = xSpacing;
			v1[1] = pHeightMap[(X+1)+Y*DEMwidth+1]-pHeightMap[(X+1)+Y*DEMwidth];
			v1[2] = 0.0;

			v2[0] = 0.0;
			v2[1] = pHeightMap[(X+1)+Y*DEMwidth+DEMwidth]-pHeightMap[(X+1)+Y*DEMwidth];
			v2[2] = -zSpacing;

			crossprod(v1, v2, norm);
			norm_normalize(norm);
			if(X!=DEMwidth-2)
				glNormal3dv(norm);

			glTexCoord2f(s+deltaS, t);
			glVertex3d(x, y, z);

			t-=deltaT;
		}
		s+=deltaS;
	}
	glEnd();
}

void CRender::RenderDynObjs(float th, double **px, double y, double **pz, int *ObjCount, int GroupCount)
{
	double **x, **z;
	x = (double **)malloc(GroupCount*sizeof(double *));
	z = (double **)malloc(GroupCount*sizeof(double *));
	assert(x!=NULL && z!=NULL);
	
	for(int i = 0; i < GroupCount; i++)
	{
		x = (double *)malloc(ObjCount*sizeof(double));
		z = (double *)malloc(ObjCount*sizeof(double));
		assert(x!=NULL && z!=NULL);
	}

	glColor3f(1.0f, 1.0f, 1.0f);
	glEnable(GL_TEXTURE_2D);

	glBindTexture(GL_TEXTURE_2D, TreeTexture[0]);

	for(int j = 0; j < GroupCount; j++)
	{
		for(int i = 0; i < ObjCount[j]; i++)
		{
			x[j] = px[j] - mapcx;
			z[j] = mapcy - pz[j];
			if(j == 0)
			{
				glBegin( GL_QUADS ); //27Sep05
					glTexCoord2f(0, 1);
					glVertex3d(x[j]-5, y+15-th, z[j]);
					glTexCoord2f(1, 1);
					glVertex3d(x[j]+5, y+15-th, z[j]);
					glTexCoord2f(1, 0);
					glVertex3d(x[j]+5, y, z[j]);
					glTexCoord2f(0, 0);
					glVertex3d(x[j]-5, y, z[j]);
				glEnd();
				glBegin( GL_QUADS );
					glTexCoord2f(0, 1);
					glVertex3d(x[j], y+15-th, z[j]+5);
					glTexCoord2f(1, 1);
					glVertex3d(x[j], y+15-th, z[j]-5);
					glTexCoord2f(1, 0);
					glVertex3d(x[j], y, z[j]-5);
					glTexCoord2f(0, 0);
					glVertex3d(x[j], y, z[j]+5);
				glEnd();
			}
			else
			{
				glBegin( GL_QUADS );
					glTexCoord2f(0, 1);
					glVertex3d(x[j]-8, y+15+th, z[j]);
					glTexCoord2f(1, 1);
					glVertex3d(x[j]+8, y+15+th, z[j]);
					glTexCoord2f(1, 0);
					glVertex3d(x[j]+8, y, z[j]);
					glTexCoord2f(0, 0);
					glVertex3d(x[j]-8, y, z[j]);
				glEnd();
				glBegin( GL_QUADS );
					glTexCoord2f(0, 1);
					glVertex3d(x[j], y+15+th, z[j]+8);
					glTexCoord2f(1, 1);
					glVertex3d(x[j], y+15+th, z[j]-8);
					glTexCoord2f(1, 0);
					glVertex3d(x[j], y, z[j]-8);
					glTexCoord2f(0, 0);
					glVertex3d(x[j], y, z[j]+8);
				glEnd();
			}
		}
		
	}
}



int CTestView::DrawGLScene(GLvoid)
{
	m_CRender.RenderDem(pDEMheight, iMapWidth, iMapHeight, iDEMwidth, iDEMdepth);

	if(TreeStatus) 
	  m_CRender.RenderDynObjs(tree_height/20.0f, pX, dY, pZ, pObjCount, iGroupCount);
}

void CTestView::OnOpenGroundTexture() 
{
	…
	m_CRender.LoadGLTextures(GroundTexturePathName, &m_CRender.teximg, &m_CRender.texture[0], TRUE);
	…
}

void CTestView::OnOpenTreeTexture() 
{
	…
	m_CRender.LoadGLTextures(TreeTexturePathName, &m_CRender.TreeTeximg, &m_CRender.TreeTexture[0], FALSE);
	…
}

[Edited by - phantom on December 21, 2005 8:11:57 AM]
dawidjoubert
dawidjoubert
What do u mean load texture?

The texture loading code has not been given (not the function the initialization where you say what textures names are)
----------------------------http://djoubert.co.uk
crslimin
crslimin
Load texture means load tiff format image for texture. The texture name is defined in class CRender:
GLuint texture[1];
GLuint TreeTexture[1];
ie. name texture[0] for terrain and TreeTexture[0] for the group objects which use the same texteture.

Below is the code for loading image.
struct IMAGEIO_API textureImage
{
unsigned long *start;
int width, height, no_bits, samplesperpix;
unsigned char *data;
};
IMAGEIO_API void LoadTIF(char *Filename, textureImage *texim, BOOL GroundTex, GLuint &tformat);

void LoadTIF(char *Filename, textureImage *texim, BOOL GroundTex, GLuint &tformat)
{
TIFF *fi=NULL;
char str[256];
unsigned char *y, *ptr;
int i, j;

if((fi=TIFFOpen(Filename, "rb"))==NULL)
{
sprintf(str, "Cannot open %s for read.\n", Filename);
MessageBox(NULL,str,"Error", MB_OK);
exit(0);
}

texim->no_bits=0;
texim->samplesperpix=0;
TIFFSeekFile(fi, 0L, SEEK_SET);
TIFFGetField(fi, TIFFTAG_STRIPOFFSETS, &(texim->start));
TIFFGetField(fi, TIFFTAG_IMAGEWIDTH, &(texim->width));
TIFFGetField(fi, TIFFTAG_IMAGELENGTH, &(texim->height));
TIFFGetField(fi, TIFFTAG_BITSPERSAMPLE, &(texim->no_bits));
TIFFGetField(fi, TIFFTAG_SAMPLESPERPIXEL, &(texim->samplesperpix));

if((texim->no_bits*texim->samplesperpix)!=24 && (texim->no_bits*texim->samplesperpix)!=32)
{
MessageBox(NULL, "The image is not a 24-bits or 32-bits image.", "Error", MB_OK);
exit(0);
}
if(texim->samplesperpix==3)
tformat = GL_RGB;
else
tformat = GL_RGBA;

texim->data=(unsigned char*)malloc(texim->width*texim->height*texim->samplesperpix*sizeof(unsigned char));
y = (unsigned char*)malloc(texim->width*texim->samplesperpix*sizeof(unsigned char));

TIFFSeekFile(fi, texim->start[0], SEEK_SET);
for(i=0; iheight; i++)
{
ptr=y;
TIFFReadFile(fi, y, texim->samplesperpix*texim->width*sizeof(unsigned char));
for(j=0; jsamplesperpix*texim->width; j++)
{
texim->data[(texim->height-1-i)*texim->samplesperpix*texim->width+j]=*ptr;
ptr++;
}
}
free(y);
}
crslimin
crslimin
I have resolved my problem, after removing sprintf() and MessageBox() in CRender::LoadGLTextures(LPSTR strName, textureImage *texim, GLuint *tex, BOOL GroundTex). But I don't understand that why MessageBox caused the problem. Is there anyone know?

Topic Locked

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

Sign in to reply to this topic.