• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
lougv22

Terrain not displaying correctly in xna 4.0 game

3 posts in this topic

[color=#4A4A4A]I am working on a simple Third-Person Shooter game in XNA 4.0 and I am currently having troubles getting the terrain to display correctly on the screen. Here is what it looks like now:[/color]

[color=#4A4A4A][img]http://i1109.photobucket.com/albums/h427/lukegf22/XNA_TPS_Terrain_0.png[/img][/color]

[color=#4A4A4A]You can see it, but it definitely looks wrong. I am not sure what the problem is so I'll show some of my code related to the terrain, hopefully someone here can figure it out. First off, the terrain is initialized in my LevelCreator class like so:[/color]

[code]private static GameLevel CreateAlienPlanetLevel(Game game)
{
ContentManager Content = game.Content;
GameLevel gameLevel = new GameLevel();

// Camera and lights
AddCameras(game, ref gameLevel);
gameLevel.LightManager = new LightManager();
gameLevel.LightManager.AmbientLightColor = new Vector3(0.1f, 0.1f, 0.1f);
gameLevel.LightManager.Add("MainLight", new PointLight(new Vector3(10000, 10000, 10000), new Vector3(0.2f, 0.2f, 0.2f)));
gameLevel.LightManager.Add("CameraLight", new PointLight(Vector3.Zero, Vector3.One));

game.Services.AddService(typeof(CameraManager), gameLevel.CameraManager);
game.Services.AddService(typeof(LightManager), gameLevel.LightManager);

// Terrain
gameLevel.Terrain = new Terrain(game);
gameLevel.Terrain.Initialize();
gameLevel.Terrain.Load(game.Content, "Terrain1", 8.0f, 1.0f);

// Terrain material
TerrainMaterial terrainMaterial = new TerrainMaterial();
terrainMaterial.LightMaterial = new LightMaterial(new Vector3(0.8f), new Vector3(0.3f), 32.0f);
terrainMaterial.DiffuseTexture1 = GetTextureMaterial(game, "Terrain1", new Vector2(40, 40));
terrainMaterial.DiffuseTexture2 = GetTextureMaterial(game, "Terrain2", new Vector2(25, 25));
terrainMaterial.DiffuseTexture3 = GetTextureMaterial(game, "Terrain3", new Vector2(15, 15));
terrainMaterial.DiffuseTexture4 = GetTextureMaterial(game, "Terrain4", Vector2.One);
terrainMaterial.AlphaMapTexture = GetTextureMaterial(game, "AlphaMap", Vector2.One);
terrainMaterial.NormalMapTexture = GetTextureMaterial(game, "Rockbump", new Vector2(128, 128));
gameLevel.Terrain.Material = terrainMaterial;
game.Services.AddService(typeof(Terrain), gameLevel.Terrain); [/code]

[color=#4A4A4A]The Load method in my Terrain class looks like this:[/color]

[code]public void Load(ContentManager content, string heightMapFileName, float blockScale, float heightScale)
{
if (!_isInitialized)
Initialize();

// Load the height map file
Texture2D heightMapTexture = content.Load<Texture2D>(heightMapFileName);
int heightMapSize = heightMapTexture.Width * heightMapTexture.Height;
_heightMap = new Color[heightMapSize];
heightMapTexture.GetData<Color>(_heightMap);

this._vertexCountX = heightMapTexture.Width;
this._vertexCountZ = heightMapTexture.Height;
this._blockScale = blockScale;
this._heightScale = heightScale;

// Load vertex declaration once
this._vertexDeclaration = new VertexDeclaration(VertexPositionNormalTangentBinormalTexture.VertexElements);

// Generate the terrain's mesh
GenerateTerrainMesh();
_transformation = new Transformation();

// Load effect
_effect = new TerrainEffect(Game.Content.Load<Effect>(TerrainEffect.EFFECT_FILENAME));
_terrainMaterial = new TerrainMaterial();
}[/code]

[color=#4A4A4A]One of the methods I am not quite sure about is GenerateTerrainMesh, also in class Terrain. The reason I am not sure about it is because I ported the original code for it from XNA 3.0 to 4.0 and I know there are some differences there in the VertexBuffer and IndexBuffer class. Here it is:[/color]

[code]private void GenerateTerrainMesh()
{
_numVertices = _vertexCountX * _vertexCountZ;
_numTriangles = (_vertexCountX - 1) * (_vertexCountZ - 1) * 2;

// Generate the terrain indices first
int[] indices = GenerateTerrainIndices();

// Next generate the terrain's vertices
VertexPositionNormalTangentBinormalTexture[] vertices = GenerateTerrainVertices(indices);

// Create a vertex buffer to hold all the vertices
_vertexBuffer = new VertexBuffer(GraphicsDevice, _vertexDeclaration, _numVertices, BufferUsage.WriteOnly);
_vertexBuffer.SetData<VertexPositionNormalTangentBinormalTexture>(vertices);

_indexBuffer = new IndexBuffer(GraphicsDevice, IndexElementSize.ThirtyTwoBits, _numTriangles * 3 * sizeof(int),
BufferUsage.WriteOnly);
_indexBuffer.SetData<int>(indices);
}[/code]

[color=#4A4A4A]Another method I am not sure about, for the same reason as above, is Draw:[/color]

[code]public override void Draw(GameTime gameTime)
{
// Configure TerrainEffect
SetEffectMaterial();

// Set mesh vertex and index buffer
GraphicsDevice.SetVertexBuffers(_vertexBuffer);
GraphicsDevice.Indices = _indexBuffer;

// Loop through all effect passes
foreach (EffectPass pass in _effect.CurrentTechniquePasses)
{
pass.Apply();
// Draw the mesh
GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, _numVertices, 0, _numTriangles);
}
}[/code]

[color=#4A4A4A]And finally, here is how all the files for the terrain are organized in my Content folder:[/color]

[color=#4A4A4A][img]http://i1109.photobucket.com/albums/h427/lukegf22/XNA_TPS_Terrain_Content_0.png[/img][/color]

[color=#4A4A4A]So any ideas why it wouldn't display correctly?[/color]
0

Share this post


Link to post
Share on other sites
[quote name='Darg' timestamp='1332977773' post='4926161']
It looks like your camera might be underneath the terrain?
[/quote]

I am not sure that's the problem. I played around with my camera but that didn't seem to fix the issue. [color=#4A4A4A]Also, I was playing around with the Transformation (translate, rotate, scale) attributes of the Terrain by adding the following line:[/color]

[color=#4A4A4A][CODE]
// Terrain
2 gameLevel.Terrain = new Terrain(game);
3 gameLevel.Terrain.Initialize();
4 gameLevel.Terrain.Load(game.Content, "Terrain1", 8.0f, 1.0f);
5
6 // Terrain material
7 TerrainMaterial terrainMaterial = new TerrainMaterial();
8 terrainMaterial.LightMaterial = new LightMaterial(new Vector3(0.8f), new Vector3(0.3f), 32.0f);
9 terrainMaterial.DiffuseTexture1 = GetTextureMaterial(game, "Terrain1", new Vector2(40, 40));
10 terrainMaterial.DiffuseTexture2 = GetTextureMaterial(game, "Terrain2", new Vector2(25, 25));
11 terrainMaterial.DiffuseTexture3 = GetTextureMaterial(game, "Terrain3", new Vector2(15, 15));
12 terrainMaterial.DiffuseTexture4 = GetTextureMaterial(game, "Terrain4", Vector2.One);
13 terrainMaterial.AlphaMapTexture = GetTextureMaterial(game, "AlphaMap", Vector2.One);
14 terrainMaterial.NormalMapTexture = GetTextureMaterial(game, "Rockbump", new Vector2(128, 128));
15 gameLevel.Terrain.Material = terrainMaterial;
16 gameLevel.Terrain.Transformation = new GameBase.Transformation(new Vector3(0, -2600, 0), new Vector3(0, 200, 0), new Vector3(10, 10, 10));
17 game.Services.AddService(typeof(Terrain), gameLevel.Terrain);
[/CODE][/color]

[color=#4A4A4A]Line #16 is the new one. The first Vector3 in the Transformation constructor is the Translate attribute, second one is Rotate, and last one is Scale. Here are 3 screenshots with 3 different values of the Y Rotate attribute, 120, -120, and 200:[/color]

[color=#4A4A4A][img]http://i1109.photobucket.com/albums/h427/lukegf22/XNA_TPS_Terrain_1.png[/img][/color]

[color=#4A4A4A][img]http://i1109.photobucket.com/albums/h427/lukegf22/XNA_TPS_Terrain_2.png[/img][/color]

[color=#4A4A4A][img]http://i1109.photobucket.com/albums/h427/lukegf22/XNA_TPS_Terrain_3.png[/img][/color]

[color=#4A4A4A]And finally, this is what the game should look like if everything works correctly (this is a screenshot from a YouTube video somebody posted):[/color]

[color=#4A4A4A][img]http://i1109.photobucket.com/albums/h427/lukegf22/XNA_TPSFinal.png[/img][/color]
0

Share this post


Link to post
Share on other sites
One problem that I spotted as I skimmed your code.


[color=#000000]_numVertices [/color][color=#666600]=[/color][color=#000000] _vertexCountX [/color][color=#666600]*[/color][color=#000000] _vertexCountZ[/color][color=#666600];[/color]

[color=#666600]_vertexCountX = width map texture width[/color]
[color=#666600]_vertexCountX = height map texture width[/color]

[color=#666600]Pretend width/height is 1. Then _vertexCountX should be 4, not 1.[/color]

[color=#666600]Try creating a 2x2 height map and getting that to work properly. It's a lot easier to debug.[/color]
0

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  
Followers 0