Original Post
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]) [Edited by - phantom on December 21, 2005 8:11:57 AM]
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);
…
}