Click here to Skip to main content
15,887,328 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Okay guys for probably 24 hours now I've been going through numerous websites and tutorials on textured terrain in XNA 4. I believe I've gotten pretty close to my solution however I think I've finally hit a wall. I've been using Riemers XNA tutorials for the most part, now let me post a bit of code so you guys can see what I'm doing:

Here's the method that does it all (besides draw):
C#
private void LoadVertices()
        {
            VertexPositionNormalTexture[] terrainVertices = SetUpTerrainVertices();
            int[] terrainIndices = SetUpTerrainIndices();
            terrainVertices = CalculateNormals(terrainVertices, terrainIndices);
            CopyToTerrainBuffers(terrainVertices, terrainIndices);
        }


Now here's the method "SetUpTerrainVerticies":
C#
private VertexPositionNormalTexture[] SetUpTerrainVertices()
        {
            VertexPositionNormalTexture[] terrainVertices = new VertexPositionNormalTexture[terrainWidth * terrainHeight];

            for (int x = 0; x < terrainWidth; x++)
            {
                for (int y = 0; y < terrainHeight; y++)
                {
                    terrainVertices[x + y * terrainWidth].Position = new Vector3(x, heightData[x, y], -y);
                    terrainVertices[x + y * terrainWidth].TextureCoordinate.X = (float)x / 30.0f;
                    terrainVertices[x + y * terrainWidth].TextureCoordinate.Y = (float)y / 30.0f;
                }
            }

            return terrainVertices;
        }



And SetUpTerrainIndicies():
C#
private int[] SetUpTerrainIndices()
        {
            int[] indices = new int[(terrainWidth - 1) * (terrainHeight - 1) * 6];
            int counter = 0;
            for (int y = 0; y < terrainHeight - 1; y++)
            {
                for (int x = 0; x < terrainWidth - 1; x++)
                {
                    int lowerLeft = x + y * terrainWidth;
                    int lowerRight = (x + 1) + y * terrainWidth;
                    int topLeft = x + (y + 1) * terrainWidth;
                    int topRight = (x + 1) + (y + 1) * terrainWidth;

                    indices[counter++] = topLeft;
                    indices[counter++] = lowerRight;
                    indices[counter++] = lowerLeft;

                    indices[counter++] = topLeft;
                    indices[counter++] = topRight;
                    indices[counter++] = lowerRight;
                }
            }

            return indices;
        }


CalculateNormals():
private VertexPositionNormalTexture[] CalculateNormals(VertexPositionNormalTexture[] vertices, int[] indices)
         {
             for (int i = 0; i < vertices.Length; i++)
                 vertices[i].Normal = new Vector3(0, 0, 0);
             for (int i = 0; i < indices.Length / 3; i++)
             {
                 int index1 = indices[i * 3];
                 int index2 = indices[i * 3 + 1];
                 int index3 = indices[i * 3 + 2];
                 Vector3 side1 = vertices[index1].Position - vertices[index3].Position;
                 Vector3 side2 = vertices[index1].Position - vertices[index2].Position;
                 Vector3 normal = Vector3.Cross(side1, side2);
                 vertices[index1].Normal += normal;
                 vertices[index2].Normal += normal;
                 vertices[index3].Normal += normal;
             }
             for (int i = 0; i < vertices.Length; i++)
                 vertices[i].Normal.Normalize();
             return vertices;
         }


And lastly, CopyToTerrainBuffers():
C#
private void CopyToTerrainBuffers(VertexPositionNormalTexture[] vertices, int[] indices)
         {
             terrainVertexBuffer = new VertexBuffer(device, typeof(VertexPositionNormalTexture), vertices.Length, BufferUsage.WriteOnly);
             terrainVertexBuffer.SetData(vertices);

             terrainIndexBuffer = new IndexBuffer(device, typeof(int), indices.Length, BufferUsage.WriteOnly);
             terrainIndexBuffer.SetData(indices);
         }


As for the .fx file itself, I don't believe it is the problem because I'm able to do everything besides displaying the texture.

Now here's the drawing code aswell:

myEffect.Parameters["xView"].SetValue(camera.viewMatrix);
           myEffect.Parameters["xProjection"].SetValue(camera.projectionMatrix);
           myEffect.Parameters["xWorld"].SetValue(mapMatrix);
           myEffect.Parameters["xEnableLighting"].SetValue(true);
           Vector3 lightDirection = new Vector3(1.0f, -1.0f, -1.0f);
           lightDirection.Normalize();
           myEffect.Parameters["xLightDirection"].SetValue(lightDirection);
           myEffect.Parameters["xAmbient"].SetValue(0.1f);
           myEffect.CurrentTechnique = myEffect.Techniques["Textured"];
           myEffect.Parameters["xTexture"].SetValue(grassTexture);
           foreach (EffectPass pass in myEffect.CurrentTechnique.Passes)
           {
               pass.Apply();
               device.SetVertexBuffer(terrainVertexBuffer);
               device.Indices = terrainIndexBuffer;
               int noVertices = terrainVertexBuffer.VertexCount;
               int noTriangles = terrainIndexBuffer.IndexCount / sizeof(int) / 3;
               device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, noVertices, 0, noTriangles);
           }


When I run this all I see is a black screen. If I draw without textures (just colored) it works fine and I see my map colored white like I told it to.

Any help is appreciated, I'm on the verge of simply copy and pasting someone else's sample project and building on top of that without knowing how it works >.<<br mode="hold" />
Thank you so much in advance, I'm probably making a silly mistake somewhere. I just started XNA 3D programming a couple days ago so I wouldn't be surprised :P
Posted
Comments
Sergey Alexandrovich Kryukov 13-Mar-11 23:49pm    
How many of the "numerous websites" have you been doing per hour? :-)
--SA
Raztor0 13-Mar-11 23:51pm    
honestly I've been on Google for 11 hours today, admittedly i haven't really taken the time to read many of the tutorials in depth because most were written for older xna versions

1 solution

Well I solved this on my own after more troubleshooting. The problem was pretty silly (as I assumed). I wasn't positioning the map on the screen for my camera to see >.<<br mode="hold" />
I solved my problem by adding this line before performing all my texture rendering:
mapMatrix = Matrix.CreateTranslation(-terrainWidth / 2.0f, 0, terrainLength / 2.0f);


Thanks to everyone who took the time to read my post at least :P
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900