Sign in to follow this  

HLSL problem

Recommended Posts

Before I render any of my meshes, I can alter position, rotation and scale. This works. The problem arises after I do these modifications. The shader completely ignores the changes I made to the mesh vertex coordinates and only only uses the original mesh info. An example of a terrain I made and the HLSL water acting as if it was still at the origin: "effectinstances" only referenced when declaring mesh... this is my problem. Here is my code for initializing my mesh with shaders.
public BaseMesh(Device _dev, String XFilePath, Coordinate _coord)
			dev = _dev;
			coord = _coord;

			mesh = Mesh.FromFile(Global.settings.InstallPath + "resources/" + XFilePath, MeshFlags.Managed, dev, out effectinstances);
			mesh = mesh.Clone(MeshFlags.Managed, CustomVertex.PositionNormalTextured.Format, dev);
			// Lock the vertex buffer to acces vertices
			CustomVertex.PositionNormalTextured[] lockedVerts = null;
			lockedVerts = mesh.LockVertexBuffer(
				mesh.NumberVertices) as CustomVertex.PositionNormalTextured[];

			// Copy the position data into a simple vector array
			// And, since the mesh is pretty boring and flat, add some jaggedness.
			Vector3[] odeVerts = new Vector3[lockedVerts.Length];
			for(int i = 0; i < lockedVerts.Length; ++i)
				odeVerts[i] = lockedVerts[i].Position;
				odeVerts[i].X *= Coord.Scale.X;
				odeVerts[i].Z *= Coord.Scale.Z;
				odeVerts[i].Y *= Coord.Scale.Y;

				odeVerts[i].X += Coord.Position.X;
				odeVerts[i].Z += Coord.Position.Z;
				odeVerts[i].Y += Coord.Position.Y;

				// propogate the jagginess back to the mesh
				lockedVerts[i].Position = odeVerts[i];

			mesh.OptimizeInPlace( MeshFlags.OptimizeCompact | MeshFlags.OptimizeAttributeSort, (GraphicsStream)null );

			short[] odeIndices = mesh.IndexBuffer.Lock(0, typeof(short), 0, mesh.NumberFaces*3) as short[];

			geom = new ODE.Geoms.TriMesh(odeVerts, odeIndices);

			effect = new Effect[effectinstances.Length];

			for(int z=0; z < effectinstances.Length; z++)
				if(effectinstances[z].EffectFilename != null)
					effect[z] = Effect.FromFile(dev, Global.settings.InstallPath + @"resources\" + effectinstances[z].EffectFilename, null, null, ShaderFlags.None, null);
					effect[z].Technique = effect[z].FindNextValidTechnique(null);


Share this post

Link to post
Share on other sites
When using a vertex shader, this shader should take care of transforming the vertices. The vertices are received in object space (i.e. only transformed with respect to the local object origin). To transform it into world space you need to multiply it with the world matrix in the vertex shader. This also means you must pass the world matrix to your effect instance when you use it. Afterwards, similar steps hold for world-to-view and view-to-screen transformations. Some can be combined, depending on the shader.



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