• 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
george7378

Recalculating terrain normals

15 posts in this topic

Hi everyone,

 

I'm trying to get a terrain shader working and I am having problems re-calculating the normals. Here's my shader at the moment:

 

https://www.dropbox.com/s/ykowd08ga9abzaj/LandscapeShaders.fx

 

I'm using a technique I discovered in a paper to determine the normal to the terrain using the heightmap data that sets the height of each vertex in a 2D plane. The problem is that the shading is still totally flat and the normals still point directly up, as they do in the original plane. Can anyone see why? Should I be doing the normal calculation in the pixel shader (which I also tried, doesn't work either). 

 

Thanks.

 

Here's how the terrain looks:

 

[attachment=21893:tershade.jpg]

0

Share this post


Link to post
Share on other sites

Have you tried to make your pixel shader to output the normal instead of the color? This way it is easy to check whether the normals are point at different directions.

 

Cheers!

 

[edit] is your f_LightPower bigger than 0 ? Is your v3_LightDir other than 0,0,0 vector? Those 2 could produce a zero result for lighting. Also, is your ambient less than white?

Edited by kauna
1

Share this post


Link to post
Share on other sites

Yeah, I tried outputting the normal and it gives me a totally green colour across the terrain, i.e. (0, 1, 0), just like on the original plane. Yes, I checked the ambients and light power too. I'll have another mess around with it!

0

Share this post


Link to post
Share on other sites

Have you tried linear filtering for your terrain sampler?

 

You are using point filtering and if the texture coordinates sample the same location of the texture (because of point filtering), this will result n.z and n.x of having a zero value.

 

Cheers!

0

Share this post


Link to post
Share on other sites

Have you tried to use the normal without multiplying it by the world matrix?  Perhaps your world matrix isn't properly being set, which could lead to issues.  Also, why have you chosen to use tex2Dlod for sampling the texture?  It should still work fine, but I was just wondering why you would go that way...

0

Share this post


Link to post
Share on other sites

I'm by no means an expert.  I don't think you need to calculate normals in the shader unless you're terrain is moving every frame, as opposed to the camera moving every frame.  Below is the code I use to get the terrain normals when I initialize new terrain, in C#, which seems to work.  It's not my code, You could probably find a better algorithm for generating the normals, since it doesn't run every frame speed is not a huge factor.    

 

      
  private static Vector3 SimpleCrossFilter(int x, int y, ref float[] heightfield,
    float normalStrength, int width, int length)
        {
            // Create four positions around the specified position
            Point[] pos = new Point[]
            {
                    new Point(x - 1, y), // left
                    new Point(x + 1, y), // right
                    new Point(x, y - 1), // higher
                    new Point(x, y + 1), // lower
            };

            // Get the heightvalues at the four positions we just created
            float[] heights = new float[4];
            for (byte i = 0; i < 4; i++)
            {
                // Check if we can access the array with the current coordinates
                if (pos[i].X >= 0 && pos[i].X < width &&
                    pos[i].Y >= 0 && pos[i].Y < length)
                {
                    int j = pos[i].X + pos[i].Y * width;
                    heights[i] = heightfield[j];
                }
                else
                {
                    // If not, then set value to zero.
                    heights[i] = 0;
                }
            }

            // Perform simple cross filter.
            float dx = heights[0] - heights[1];
            float dz = heights[2] - heights[3];
            float hy = 1.0f / normalStrength;

            // Create and normalize the final normal
            Vector3 normal = new Vector3(dx, hy, dz);
            normal.Normalize();

            return normal;
        }

0

Share this post


Link to post
Share on other sites


Also, why have you chosen to use tex2Dlod for sampling the texture?

 

As far as I know, tex2Dlod is the right function for texture sampling in a vertex shader, there are restrictions in which sampling functions are supported. 

 


 I don't think you need to calculate normals in the shader unless you're terrain is moving every frame,

 

You are right that normal recalculation isn't necessary in the shader, but on the other hand, it allows you to use GPU to calculate something that typically you'd need to do on CPU so, this way you'll reduce CPU load / storage load and also you'll need to send less data to the GPU. The normal calculating function presented here was used in certain Battlefields games, so I guess it has some advantage too. Of course, you'll need to have the height map available for the normal calculations. 

 

Cheers!

0

Share this post


Link to post
Share on other sites

You are right that normal recalculation isn't necessary in the shader, but on the other hand, it allows you to use GPU to calculate something that typically you'd need to do on CPU so, this way you'll reduce CPU load / storage load and also you'll need to send less data to the GPU. The normal calculating function presented here was used in certain Battlefields games, so I guess it has some advantage too. Of course, you'll need to have the height map available for the normal calculations.

But you would make it only once on the CPU, wouldn't you? As opposed to every frame on GPU. (If the terrain is not changing somehow during time.)

You need to send less data to the GPU, that's right. But that would probably require some profiling, whether it's better to be sending less data and calculating some on the GPU or to be sending more data and spare some GPU time.

Edited by Tom KQT
2

Share this post


Link to post
Share on other sites

Well, as I told, the method has been used in a AAA level game, so I guess that they did the profiling. Of course hardware changes and everything changes, so you can always redo the profiling. 

 

With CPU calculated normals, you'll still do texture read in the shader and probably some unpacking depending on the texture format. With the GPU version you'll do 4 texture reads, but probably they won't be much slower. The amount of ALUs in the GPU version is so small that it won't change much. 

 

So, probably calculating the normals on the GPU won't be any slower than the CPU version. 

 

 

Cheers!

Edited by kauna
0

Share this post


Link to post
Share on other sites

Another option is to store the pre-calculated normals in the heightmap.

Edited by phil_t
0

Share this post


Link to post
Share on other sites

Hi!

 

Pre-calculating these normals would be done by the D3DX9 API.

D3DXComputeNormals() 

I know that is the obsolete function, but it works and would be called while assets are being built (not in the release app).

This API implements a good algorithm.

0

Share this post


Link to post
Share on other sites

Well, as I told, the method has been used in a AAA level game, so I guess that they did the profiling. Of course hardware changes and everything changes, so you can always redo the profiling. 

 

With CPU calculated normals, you'll still do texture read in the shader and probably some unpacking depending on the texture format. With the GPU version you'll do 4 texture reads, but probably they won't be much slower. The amount of ALUs in the GPU version is so small that it won't change much. 

 

So, probably calculating the normals on the GPU won't be any slower than the CPU version. 

 

 

Cheers!

 

Tom KQT has it right here - if you terrain isn't changing over time, then you should just calculate the normal on the CPU and store it with the terrain vertices.  Otherwise you are recalculating the same thing every frame on the GPU, which is a huge waste of computation and bandwidth.  If there is a AAA game that does this, I would be interested to know which game you are referring to - maybe their terrain was destructible or something like that...

 

Did you see my question from above - have you tried to remove the world matrix from the calculation to see if it helps?

0

Share this post


Link to post
Share on other sites

http://dice.se/wp-content/uploads/Chapter5-Andersson-Terrain_Rendering_in_Frostbite.pdf

 

Page 42. 

 

"Over the last decade, the computational power of consumer GPUs has been exceeding 

the Moore’s law, graphics chips becoming faster and faster with every generation. At the 
same time, memory size and bandwidth increases do not match the jumps in GPU 
compute. Realizing this trend, it makes sense to try to calculate much of the terrain 
shading and texture compositing in the shaders instead of storing it all in textures."
 
Anyway, I think that this thread is dead since the OP hasn't posted any new information. 
 
Cheers!
0

Share this post


Link to post
Share on other sites

Sorry for not replying and thanks for the ideas everyone, since my terrain is static and isn't changing per-frame, I think the best idea may be to create another texture-based container to store the normals which can then be looked up in the shader. I'll see how it goes :)

1

Share this post


Link to post
Share on other sites

Well, as I told, the method has been used in a AAA level game, so I guess that they did the profiling.

Just a small note on this. Don't take AAA games in general as your holy grail as far as technology is concerned. Sure, there are some developers that push the boundaries further and produce extremely optimized and technicaly advanced games. But there are plenty of AAA games that are made in very short time and with focus on other elements, not optimizations.

For example, I think it was on this forum - somebody showed how incredible hight was the amount of draw calls in the early version of Civilisation V. They had a separate draw call for every single icon on the screen, including all those food/wealth/production icons on each tile (if you know the game). That probably won't be the best way, will it? And btw I think this changed in the later versions of the game.

Edited by Tom KQT
0

Share this post


Link to post
Share on other sites

The draw call problem with CIV 5 and terrain normal calculating are two quite different thing, the first is caused by artists not having optimized engine for the purpose and second by programmers optimizing certain aspects of the game engine.

 

I agree that a certain amount of criticism is always good when learning new things. The normal calculating method was described in the engine documentations which was about_new tech and new ways of doing things, not in the hobby sections of some discussion area. My point here, those people publishing things on the net have quite solid understanding of what they are writing.

 

Indeed, in the later versions of the game, they changed the terrain rendering code and what is stored in textures etc. But once again, it was a question needs. The BFBC2 terrain rendering could go only so far. They improved / changed the code because mixing so many terrain layers became prohibitive in the sense of performance. I used exactly the same code earlier in my hobby projects and I was pleased in it's performance. However, later my needs became different and I adopter another method where I couldn't use the code anymore. Anyhow, a GPU, when correctly used has pretty much computing power and few used ALUs won't affect the performance pretty much at all. 

 

Cheers!

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