Sign in to follow this  
kociolek

Nishita sky very bad color

Recommended Posts

Well, i'm working on this, and in result of all - i'm getting incorrect sky color. I'm think error is somewhere in Mie/Ray lookup table generation. Anybody can help? * Mie and Rayleigh scattering generation Microsoft.Xna.Framework.Graphics.PackedVector.HalfVector4[] MieLooUpTable = new Microsoft.Xna.Framework.Graphics.PackedVector.HalfVector4[RayleighMieN* (RayleighMieN / 2)]; Microsoft.Xna.Framework.Graphics.PackedVector.HalfVector4[] RayleighLookUpTable = new Microsoft.Xna.Framework.Graphics.PackedVector.HalfVector4[MieLooUpTable.Length]; int index = 0; float factorY = 1.0f / (RayleighMieN); float factorX = 1.0f / (RayleighMieN / 2); float X = 0; float Y = 0; /* * float fCosxz = 0.5403023f * x / SizeMinusOne; for (int y = 0; y < m_nSize; y++) { float fCosy = (MathHelper.TwoPi) * y / SizeMinusOne; * */ for (int x = 0; x < RayleighMieN/2; x++) { for (int y = 0; y < RayleighMieN; y++) { Y = y * factorY; // (-127 + y) * factorY; X = x * factorX; //(-63 + x) * factorX; float AngleY = (MathHelper.TwoPi) * x * InvRayleighMieNLessOne.X; float AngleXZ = MathHelper.Pi * y * InvRayleighMieNLessOne.Y;//0.5403023f * y * InvRayleighMieNLessOne.Y; Vector2 Tex0 = new Vector2(X, Y);//(new Vector2(X-.5f, Y-.5f)/* - 0.5*/) * InvRayleighMieNLessOne; Vector3 PointPv = new Vector3(0, InnerRadius + 1e-3f, 0); // float AngleY = 100f * Tex0.X * PI / 180f; // float AngleXZ = PI * Tex0.Y; Vector3 Direction = new Vector3(sin(AngleY) * cos(AngleXZ), cos(AngleY), sin(AngleY) * sin(AngleXZ)); Direction.Normalize(); float fFarPvPa = HitOuterSphere(PointPv, Direction); Vector3 Ray = Direction; Vector3 PointP = PointPv; float fSampleLength = fFarPvPa / iNumSamples; float fScaledLength = fSampleLength * fScale; Vector3 v3SampleRay = Ray * fSampleLength; PointP += v3SampleRay * 0.5f; Vector3 RayleighSum = new Vector3(); Vector3 v3MieSum = new Vector3(); for (int k = 0; k < iNumSamples; k++) { float PointPHeight = PointP.Length(); // Density Ratio at Point P Vector2 DensityRatio = GetDensityRatio(PointPHeight); DensityRatio *= fScaledLength; // P->Viewer OD Vector2 ViewerOD = t(PointP, PointPv); // P->Sun OD float dFarPPc = HitOuterSphere(PointP, vSunDir); Vector2 SunOD = t(PointP, PointP + vSunDir * dFarPPc); // Calculate the attenuation factor for the sample ray Vector2 ODP = SunOD + ViewerOD; Vector3 v3Attenuation = new Vector3( (float)Math.Exp(-Kr4PI * InvWavelength4.X * ODP.X - Km4PI * ODP.Y), (float)Math.Exp(-Kr4PI * InvWavelength4.Y * ODP.X - Km4PI * ODP.Y), (float)Math.Exp(-Kr4PI * InvWavelength4.Z * ODP.X - Km4PI * ODP.Y) ); RayleighSum += DensityRatio.X * v3Attenuation; v3MieSum += DensityRatio.Y * v3Attenuation; // Move the position to the center of the next sample ray PointP += v3SampleRay; } Vector3 RayLeigh = RayleighSum * KrESun; Vector3 Mie = v3MieSum * KmESun; RayLeigh *= InvWavelength4; Mie *= WavelengthMie; MieLooUpTable[index] = new Microsoft.Xna.Framework.Graphics.PackedVector.HalfVector4(new Vector4(Mie, 1)); RayleighLookUpTable[index] = new Microsoft.Xna.Framework.Graphics.PackedVector.HalfVector4(new Vector4(RayLeigh, 1)); index++; } } txMie.SetData<Microsoft.Xna.Framework.Graphics.PackedVector.HalfVector4>(MieLooUpTable); txRay.SetData<Microsoft.Xna.Framework.Graphics.PackedVector.HalfVector4>(RayleighLookUpTable); *Sky dome gen float mod = 100;//aka sphere R short La = (short)(DomeN / 2); short Lo = (short)(DomeN); int DVSize = Lo * La; ushort DISize = (ushort)((Lo - 1) * (La - 1) * 2); DVSize *= 2; DISize *= 2; VertexPositionTexture[] SDVert = new VertexPositionTexture[La * Lo * 2]; uint nextI = 0; for (int i = 0; i < Lo; i++) { double dDistribution = (1.0 - Math.Exp(-0.5 * (i*10.0) / (double)(DomeN - 1))); double MoveXZ = 100.0 * dDistribution * Math.PI / 180.0; // double MoveXZ = //Math.Cos(1) * i / (Lo - 1);// //100.0 * ((float)i / (float)(Lo - 1)) * Math.PI / 180.0; for (int j = 0; j < La; j++) { //const double MoveY = (D3DX_PI * 2.0) * j / (DomeN - 1) ; double MoveY = 2*Math.PI * (float)j / (float)(La - 1); SDVert[nextI].Position = new Vector3((float)(Math.Sin(MoveXZ) * Math.Cos(MoveY)), (float)(Math.Cos(MoveXZ)), (float)(Math.Sin(MoveXZ) * Math.Sin(MoveY))) * mod; SDVert[nextI].TextureCoordinate = new Vector2((float)j / (float)(La-1), (float)i / (float)(Lo-1)); //new Vector2(0.5f / (float)(Lo) + i / (float)(Lo), 0.5f / (float)(La) + j / (float)(La)); nextI++; } } SkyDomeVB = new VertexBuffer(pDEV, SDVert.Length * VertexPositionTexture.SizeInBytes, BufferUsage.WriteOnly); SkyDomeVB.SetData<VertexPositionTexture>(SDVert); short[] Indices = new short[(La - 1) * (Lo - 1) * 6]; int indicesIndex = 0; for (short i = 0; i < Lo - 1; i++) { for (short j = 0; j < La - 1; j++) { Indices[indicesIndex++] = (short)(i * La + j); Indices[indicesIndex++] = (short)((i + 1) * La + j); Indices[indicesIndex++] = (short)((i + 1) * La + j + 1); Indices[indicesIndex++] = (short)((i + 1) * La + j + 1); Indices[indicesIndex++] = (short)(i * La + j + 1); Indices[indicesIndex++] = (short)(i * La + j); } } SkyDomeIB = new IndexBuffer(pDEV, sizeof(short) * Indices.Length, BufferUsage.WriteOnly, IndexElementSize.SixteenBits); SkyDomeIB.SetData<short>(Indices);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this