Jump to content
  • Advertisement
Sign in to follow this  
Dragon_Strike

terrain lighting using shaders

This topic is 4441 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

well im tryying to do terrain lighting using shaders but im not get satisfying results.. and hoped u might have some suggestions... here is my code: calcualteing horizonmap and normalmap

void CTERRAIN::CalculateNormals( )
{
	CVECTOR A, B, C, D, E;
	CVECTOR CA, CB, CD, CE;

	CVECTOR AverageNormal; 
	CVECTOR NormalACB,NormalBCE,NormalECD,NormalDCA;

	for (int z = 1; z < m_iSize; z++)
	{
		for (int x = 1; x < m_iSize; x++)
		{	
			A.Set( float(x  ),GetHeight(x, z+1), float(z+1) ); 
			B.Set( float(x-1),GetHeight(x-1, z) , float(z  ) ); 
			C.Set( float(x  ),GetHeight(x, z) , float(z  ) ); 
			D.Set( float(x+1),GetHeight(x+1, z) , float(z  ) ); 
			E.Set( float(x  ),GetHeight(x, z-1), float(z-1) ); 
			
			CA = A - C;
			CB = B - C;
			CD = D - C;
			CE = E - C;

			NormalACB = CA.Cross(CB);
			NormalBCE = CB.Cross(CE);
			NormalECD = CE.Cross(CD);
			NormalDCA = CD.Cross(CA);

			NormalACB.Normalize();
			NormalBCE.Normalize();
			NormalECD.Normalize();
			NormalDCA.Normalize();

			AverageNormal = (NormalACB + NormalBCE + NormalECD + NormalDCA)/4.0f;
	
			AverageNormal.Normalize();

			NormalMap[(x+z*m_iSize)*4+0] = (AverageNormal.x+1.0f)*0.5f;
			NormalMap[(x+z*m_iSize)*4+1] = (AverageNormal.y+1.0f)*0.5f;
			NormalMap[(x+z*m_iSize)*4+2] = (AverageNormal.z+1.0f)*0.5f;
		}
	}
}

void CTERRAIN::CalculateHorizon()
{

	PHorizonMap = new CARRAY<float>(m_iSize);		

	for (int  z = 0; z < m_iSize; z++)
	{
		for (int x = 0; x < m_iSize; x++)
		{
			float maxSlope = 0;
			for (int i = x-1; i >= 0; i--)
			{
				float slope = 0;
				slope =  float(GetHeight(i, z)-GetHeight(x, z))/float(i-x);

				if (slope > maxSlope)
					maxSlope = slope;
				
			}
			NormalMap[(x+z*m_iSize)*4+3] = maxSlope;

		}
	}
}






the array NormalMap is set as an 16bit float RGBA texture shaders:
// VERTEX
#version 110

void main()
{
	
	gl_TexCoord[0] = gl_MultiTexCoord0;
	gl_Position = ftransform();	
}

//FRAGMENT
#version 110

uniform sampler2D normals;

void main()
{
	vec3 normal = texture2D(normals, gl_TexCoord[0].st).rgb;
	float horizon = texture2D(normals, gl_TexCoord[0].st).a;
	
	vec3 Sun = vec3(cos(radians(30.0)), sin(radians(30.0)), 0.0);
	float sunslope= Sun.y/Sun.x;
	 
	normal = (normal+1.0)/2.0;
	
	float brightness = dot(Sun , normal);
	
	if (sunslope <= horizon)
	{
		brightness = brightness*0.4;
	}
	
	gl_FragColor = vec4(brightness,brightness,brightness,1.0);	
}





and this is what it look like now... Image Hosted by ImageShack.us as u can see im getting small unsymmetric white areas where i shold have shadows.. this is how id like it too look... using the following code instead of shaders:

	CVECTOR Sun;
	Sun.comptopolar(SunAngle);
	Sun.Normalize();
	float sunslope = Sun.y/Sun.x;

	CVECTOR Normal;

	for (int z = 1; z < m_iSize-1; z++)
	{
		for (int x = 1; x < m_iSize-1; x++)
		{	

			Normal = NormalMap[x+(z*m_iSize)];
					
			float diffuse = Normal.Dot(Sun); // diffuse

			if (diffuse < 0)
			{
				diffuse = 0-diffuse;
			}
	
			float shadow = 1.0f; 		

	
			if ( sunslope < PHorizonMap->GetValue(x,z))
			{
						shadow = 0.4f;	
			}

			float brightness= diffuse*shadow;

			Light.SetColor1C(x, z,  brightness*255.0f);
		}
	}



Image Hosted by ImageShack.us [Edited by - Dragon_Strike on September 21, 2006 5:01:40 PM]

Share this post


Link to post
Share on other sites
Advertisement
I didn't look through all of your code but maybe it's because of this "normal = (normal+1.0)/2.0;" in fragment shader. Shouldn't it be "normal = normal*2.0 - 1.0;" ?

Share this post


Link to post
Share on other sites
Quote:
Original post by ivarbug
I didn't look through all of your code but maybe it's because of this "normal = (normal+1.0)/2.0;" in fragment shader. Shouldn't it be "normal = normal*2.0 - 1.0;" ?


i think that was one of the problems... ill try it..thx... now its the shadows.. any ideas?

EDIT: ive solved both problems... anyways.. the next thing i wanted to ask is howi could blur the shadows i get by using the hozin map? i get very sharp edges at the shadows edges

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!