Terrain multitexturing and alpha masks

Started by
23 comments, last by TriloByte 20 years, 4 months ago
quote:Original post by toddgrayson
If the terrain is showing up black that means that you are not getting alpha values. Try creating an array of all 1''s and use that for the alpha layer texture and render only 1 layer. If this still doesn''t work I would check to see what is rendering before and after the terrain, also check to see if lighting is enabled when you are rendering the terrain as this may also have some effect. I am assuming that the demo works for you. If nothing else works send me the section of your code that you are using this for and I will see if anything catches my eye.


i tried an array of all 1''s, and i also tried rendering only the terrain, still doesnt work... and ive been rendering only one layer the whole time too..

the alpha values are ok, ive checked by debugging at runtime, i pause the app inside the for loop for setting each value in the array, and every value is definately getting applied.

yeah your demo works for me

yes lighting is off

im not sure what i should send to ya, ill show you my frame routine, my routine for drawing terrains, and my routine for creating the alpha texture, since those seem like the most logical functions that apply to this specific technique

beware, this code has been growing for 3 weeks, in a very un-tidy manner, i basically test anything and everything that has to do with terrain stuff in this app...

the create alpha texture
bool CreateAlphaTexture(UINT &texture, LPSTR strFileName){	float *mydata=NULL;	mydata=new float[1024*1024];	AUX_RGBImageRec *pImage=NULL;	pImage=auxDIBImageLoad(strFileName);	int x,y;	for(x=0;x < 1024;x++)	{		for(y=0;y < 1024;y++)		{			mydata[x+y*1024]=1.0f;			//mydata[x+y*1024]=(float)pImage->data[x+y*1024*3]/255;		}	}	delete[ ] pImage->data;	pImage=NULL;	glPixelStorei(GL_UNPACK_ALIGNMENT,1);	glGenTextures(1,&texture);	glBindTexture(GL_TEXTURE_2D,texture);	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);	glTexImage2D(GL_TEXTURE_2D,0,GL_ALPHA,1024,1024,0,GL_ALPHA,GL_FLOAT,mydata);	return true;}


//the render terrain function (different from draw terrain)
void RenderHeightMap(BYTE *pHeightMap,int pass,CFrustum *aFrustum,float modx,float mody,float modz,bool detail){	if(!pHeightMap) return;		if(detail)	{		glActiveTextureARB(GL_TEXTURE0_ARB);		glEnable(GL_TEXTURE_2D);				glBindTexture(GL_TEXTURE_2D,g_Texture[pass]);		if(g_bDetail)		{			glActiveTextureARB(GL_TEXTURE1_ARB);			glEnable(GL_TEXTURE_2D);			glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);			glTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 2);			glBindTexture(GL_TEXTURE_2D, g_Texture[4]);			glMatrixMode(GL_TEXTURE);				glLoadIdentity();				glScalef((float)g_DetailScale,(float)g_DetailScale,1);			glMatrixMode(GL_MODELVIEW);		}	}	int ix;	int iz;	glBegin(GL_TRIANGLES);	for(iz=0;iz < MAP_SIZE-STEP_SIZE;iz+=STEP_SIZE)	{		//glBegin(GL_TRIANGLES);		for(ix=0;ix < MAP_SIZE-STEP_SIZE;ix+=STEP_SIZE)		{			if(aFrustum->CubeInFrustum((ix*SCALE)+(modx*SCALE),(Height(pHeightMap,ix,iz)*HEIGHT_RATIO*SCALE)+(mody*SCALE),(iz*SCALE)+(modz*SCALE),STEP_SIZE*SCALE))			{			float tmpx1,tmpx2,tmpx3;			float tmpy1,tmpy2,tmpy3;			float tmpz1,tmpz2,tmpz3;			tmpx1=ix;			tmpy1=Height(pHeightMap,ix,iz)*HEIGHT_RATIO;			tmpz1=iz;			tmpx2=ix;			tmpy2=Height(pHeightMap,ix,iz+STEP_SIZE)*HEIGHT_RATIO;			tmpz2=iz+STEP_SIZE;			tmpx3=ix+STEP_SIZE;			tmpy3=Height(pHeightMap,ix+STEP_SIZE,iz)*HEIGHT_RATIO;			tmpz3=iz;			if(aFrustum->TriangleInFrustum(tmpx1*SCALE,tmpy1*SCALE,tmpz1*SCALE,tmpx2*SCALE,tmpy2*SCALE,tmpz2*SCALE,tmpx3*SCALE,tmpy2*SCALE,tmpz3*SCALE,modx*SCALE,mody*SCALE,modz*SCALE))			{				if(detail)				{					SetTextureCoord(tmpx1,tmpz1);					glVertex3f(tmpx1,tmpy1,tmpz1);					SetTextureCoord(tmpx2,tmpz2);					glVertex3f(tmpx2,tmpy2,tmpz2);					SetTextureCoord(tmpx3,tmpz3);					glVertex3f(tmpx3,tmpy3,tmpz3);					gtrianglesdrawn++;				}				else				{					SetTextureCoordNoDetail(tmpx1,tmpz1);					glVertex3f(tmpx1,tmpy1,tmpz1);					SetTextureCoordNoDetail(tmpx2,tmpz2);					glVertex3f(tmpx2,tmpy2,tmpz2);					SetTextureCoordNoDetail(tmpx3,tmpz3);					glVertex3f(tmpx3,tmpy3,tmpz3);					gtrianglesdrawn++;				}			}			tmpx1=ix;			tmpy1=Height(pHeightMap,ix,iz+STEP_SIZE)*HEIGHT_RATIO;			tmpz1=iz+STEP_SIZE;			tmpx2=ix+STEP_SIZE;			tmpy2=Height(pHeightMap,ix+STEP_SIZE,iz+STEP_SIZE)*HEIGHT_RATIO;			tmpz2=iz+STEP_SIZE;			tmpx3=ix+STEP_SIZE;			tmpy3=Height(pHeightMap,ix+STEP_SIZE,iz)*HEIGHT_RATIO;			tmpz3=iz;			if(aFrustum->TriangleInFrustum(tmpx1*SCALE,tmpy1*SCALE,tmpz1*SCALE,tmpx2*SCALE,tmpy2*SCALE,tmpz2*SCALE,tmpx3*SCALE,tmpy3*SCALE,tmpz3*SCALE,modx*SCALE,mody*SCALE,modz*SCALE))			{				if(detail)				{					SetTextureCoord(tmpx1,tmpz1);					glVertex3f(tmpx1,tmpy1,tmpz1);					SetTextureCoord(tmpx2,tmpz2);					glVertex3f(tmpx2,tmpy2,tmpz2);					SetTextureCoord(tmpx3,tmpz3);					glVertex3f(tmpx3,tmpy3,tmpz3);					gtrianglesdrawn++;				}				else				{					SetTextureCoordNoDetail(tmpx1,tmpz1);					glVertex3f(tmpx1,tmpy1,tmpz1);					SetTextureCoordNoDetail(tmpx2,tmpz2);					glVertex3f(tmpx2,tmpy2,tmpz2);					SetTextureCoordNoDetail(tmpx3,tmpz3);					glVertex3f(tmpx3,tmpy3,tmpz3);					gtrianglesdrawn++;				}			}			}		}		//glEnd();	}	glEnd();	if(detail)	{		glActiveTextureARB(GL_TEXTURE1_ARB);		glDisable(GL_TEXTURE_2D);		glActiveTextureARB(GL_TEXTURE0_ARB);				glDisable(GL_TEXTURE_2D);	}}


yeah, dont say anything about that, haha, its messy

//texture coord function for nodetail texturing
void SetTextureCoordNoDetail(float x, float z){	float u =  x / ((float)MAP_SIZE-(float)STEP_SIZE);	float v =  z / ((float)MAP_SIZE-(float)STEP_SIZE);	glTexCoord2f(u,1-v);}


//frame function!
void RenderScene(){	gtrianglesdrawn=0;	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	glLoadIdentity();	/*if(keys[VK_LEFT]) {g_3DModel.RotateMe(200);}	if(keys[VK_RIGHT]) {g_3DModel.RotateMe(-200);}	if(keys[VK_UP]) {g_3DModel.ChangeAnimationRun();g_3DModel.MoveMD2Forward(1000.0f);}	if(!keys[VK_UP]) g_3DModel.ChangeAnimationIdle();	g_3DModel.ClingToTerrain(g_HeightMap);	g_Camera.SetThirdPerson(g_3DModel.Position.x,g_3DModel.Direction.y,g_3DModel.Position.z);	g_Camera.SetLookAt(g_3DModel.Position.x,g_3DModel.Position.y,g_3DModel.Position.z);*/	g_Camera.Look();	g_Frustum.CalculateFrustum();	//g_3DModel.UpdateMe();	DrawTerrain();	//DrawModel();	//DrawSkyBox();	//DrawWater();	//DrawFog();	char mytext[30];	sprintf(mytext,"Triangles: %i\n",gtrianglesdrawn);	SetWindowText(g_hWnd,mytext);	SwapBuffers(g_hDC);									}


//finally (is exhausted from so much copy and pasting)
the draw terrain function
void DrawTerrain(){	glPushMatrix();		glScalef((float)SCALE,(float)SCALE,(float)SCALE);		glDisable(GL_BLEND);		glColorMask(0,0,0,1);		glEnable(GL_TEXTURE_2D);		glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);		glBindTexture(GL_TEXTURE_2D,g_Texture[1]);		glColor4f(1,1,1,1);		RenderHeightMap(g_HeightMap,1,&g_Frustum);		glColorMask(1,1,1,0);		glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);		glEnable(GL_BLEND);		glBlendFunc(GL_DST_ALPHA,GL_ONE_MINUS_DST_ALPHA);		glBindTexture(GL_TEXTURE_2D,g_Texture[0]);		glColor4f(1,1,1,1);		RenderHeightMap(g_HeightMap,0,&g_Frustum);		glDisable(GL_BLEND);	glPopMatrix();}


hopefully it doesnt take all your energy to weed through the mess, thanks for helping so far
--FirekingOwner/LeaderFiregames Development &Blackdragon Studios
Advertisement
You are welcome.

Everything looks good except for:
RenderHeightMap(g_HeightMap,0,&g_Frustum);

It looks like it is still seeing detail as true when rendering.

I copied and pasted the RenderHeightMap funtion into my code minus all the sections containing if(detail) and all works fine.

Try commenting out all the if(detail) code and try again. If that does not work then the only other place I could see a problem being is in the actual initialization code. Ie set pixel format.
that sucks

cuz i have detail=false by default in the header declaration of that function (and there are break points in the RenderHeightMap in each of the detail branches, it never breaks there, so detail is indeed false) so nope its not going into those detail parts (hence the reason i wrote the SetTexturecoordNoDetail function)

and i copied your set pixel format desciptor from your code since its a very mobile section of code (you can copy + paste from one app to another app without problems), so im using your settings there
--FirekingOwner/LeaderFiregames Development &Blackdragon Studios
Sorry, I can''t help more. My only other guess is that something is being enabled that is conflicting in the initialization code. Ex glEnable().
oh no....

i just messed around some more with the code, and its really bad now..

i was trying something else, some nice sky, and render the terrain like normal again, only this time it wont render normal like before, its still black! i go back and look through everything, how is it possible for this to be black im saying? turns out, i had some backwards culling, some undefined fog (which i think just makes every black if you dont ever call fog functions, yet you enable it), and all sorts of stuff

after i fixed that, to make things even worse, i enabled my model to be drawn in the scene, and now everything looks different (actually better, but why?) i check the model drawing functions (and any other function thats called in its branch), and nothing is happening other than switching the culling to front, draw the model, switch culling back to back face...

and to go even WORSE, the sky flickers now, as i move over the terrain. if i stand still, the sky stays the same color, if i move, it changes colors rapidly. If i move the camera and dont see any of the terrain, the sky stays the same color, so much confusion. Once again, i turn on the model to be drawn, and now the sky doesnt flicker at all... jesus... this is such a mess

im going to my room now, to build an open gl wrapper to prevent this from happening again

ill try your method again, with all the problems i found

thanks for being so patient with me

ignore spelling mistakes, grammer errors, and incorrect punctuation please.
--FirekingOwner/LeaderFiregames Development &Blackdragon Studios

This topic is closed to new replies.

Advertisement