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

Terrain (normal vector calculation)

9 posts in this topic

Hi, I am trying to create my own terrain class.So far I've done the heightmap loading function and index/vertexbuffer generation functions,it can draw itself too.But I stuck at calculating the normal vectors.I have searched a little bit and I found 2 approaches. 1)duplicating the vertex normals(msdn says it) 2)getting the avarage vertex normal for the first solution I have no idea how to do it, my vertex struct can only hold one normal vector.. for the second I "imagined" a solution which requires more than 500.000 normal vector calculation for a 256*256 map, I don't think it is the proper way.And this is my first time writing a terrain engine so it seems a lil bit complex here. What should I do? A vertex is used for 6 different triangles. should I make a buffer that holds all plane normal vectors for triangles(this makes, I suppose, 130k triangles) , and then call the neighboor triangles for every vertex and get their avarage? Thanks, any help would be appreciated :) btw: I am using vc++ 2008, and directx 9.0..
0

Share this post


Link to post
Share on other sites
The most common approach is, I think, to use the neighboring vertices to calculate the normal. Basically, if you draw a line from the vertex just north of the one you are trying to find the normal for to the one just south and then calculate the normal to that line, you've got an approximate normal to your vertex.

Obviously, this is CPU (or GPU) intensive. Therefore, if your terrain does not deform, you should only calculate these normals once. If your terrain does deform, try to only recalculate the normals that have changed. If your terrain deforms heavily and over large areas, move the calculations onto the GPU.
0

Share this post


Link to post
Share on other sites
hopefully my terrain doesn't change during the game(at least for now..) but I would be happy if you can explain it more, should I first calculate normal vectors of the lines and buffer them ? Than it becomes a little bit easier but I have my doubts if I could make a good "neighboor" calculation, I mean in the middle its not that hard but edges are harder.
0

Share this post


Link to post
Share on other sites
normal vector calculation

As you know, you should just add up all the six adjoining edges of the triangles, and then devide that number by 6... But since this is CPU/GPU intensive, you could do two things:

1. Have them pre-calculated in a custom file format
2. Have them calculated on the fly, beginning at your current position working outwards.

terrain deformation
If this concerns something like an explosion, you could very easily calculate this with a bounding sphere...

if ((VertexOnTerrain - sphere.center) * (VertexOnTerrain - sphere.center) < (sphere.radius * sphere.radius)) returns true

then you should recalculate the normal... To make this even more effective, add some quadtrees, or octrees, I don't know what kind of game you're making. Lighting might look strange if you did it another way.
0

Share this post


Link to post
Share on other sites
Your heightmap is just a two dimensional image with height values. To compute the normal for each pixel you just need to do finite differencing.

The following pseudo code could expain it:

vec3f u(1.0f, 0.0f, map(x + 1, y));
vec3f v(0.0f, 1.0f, map(x, y + 1));
normals[x][y] = normalize(cross(u, v));

This will also work (maybe better)

vec3f u(1.0f, 0.0f, map(x + 1, y) - map(x - 1, y));
vec3f v(0.0f, 1.0f, map(x, y + 1) - map(x, y - 1));
normals[x][y] = normalize(cross(u, v));

You will have to special case the border pixels since they do not have the full set of neighbors.
1

Share this post


Link to post
Share on other sites
Hey again, I was thinking about your solution Trenki. You basicly build 2 lines from the previous vertexes(according to z and y axes) to following vertexes and cross product them.

But I didnt understand where did 1.0f's and 0.0f's come from and which axe do you use as "height".I am now confused because I looked at some different coordinate systems. So save me from this vagueness please : )
0

Share this post


Link to post
Share on other sites
x increases horizontaly, y is in the vertical direction and the height is simply the height of the terrain which is stored in the 2d heightmap image.
The 0.0f and 1.0f values for the vectors x and y coordinates were just random values. You could add a scaling factor to these and get different results depending on the range of the heightmap values.

Basically what this code does is to create two vectors (one in the x and one in the y direction) which points from the current heightmap sample to the adjacent sample. From these two vectors you can compute the cross product to get another vector which is perpendicular to both. This one can be used as the normal.

In practice this is no different than making a quad or triangle mesh from the heightmap image and computing the normals for each vertex but since the input is already a heightmap we can make a shortcut.
0

Share this post


Link to post
Share on other sites
I wrote an algorithm using your idea and I think it works, but I have encountered a further problem, as you can see on the image the base heightmap is drawn but there are lots of other meaningless vertexes.Do you know what causes this? I am positive that you have seen many of these "errors" but this is my first time ^^
where is the error ? maybe at the loading code or vertex/index buffer creation code, or maybe even at the drawing code?

I will post if you wish a part of my code.
here is the screenshot:
http://img117.imageshack.us/img117/153/adszpw7.jpg

btw: I can draw a flat heightmap without any problems.
0

Share this post


Link to post
Share on other sites
Well, I've seen like 100 open source heightmap loaders, those must include what you want, right?

http://www.videotutorialsrock.com/opengl_tutorial/terrain/home.php
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=34
http://www.swiftless.com/tutorials/terrain/terraintuts.html

I'm not sure if this helps, but it's my effort to help lol. I've never dealt with heightmaps btw.
0

Share this post


Link to post
Share on other sites
Hi, I am implementing ambient occlusion as a project at university (FFPipeline) and I have a problem with normal calculation. I have a TGA image with unsigned bytes (0, 255). I load the image with a helper function filling the 1-dimension container as a vector, which has all the vertices in once (3 coordinates by three, in order to use it alongside as vertex array later).

So I have calculated the faces of the mess as
Face 1: (Pi,j , Pi,j+1 , Pi+1,j+1) and Face 2:(Pi,j , Pi+1,j , Pi+1,j+1).

I have calculated all the normal vectors per face and now I have to combine them in order to keep the normal vectors per vertex, but I can't find an efficient algorithm no matter what I have tried. Internal vertices of the 3D Terrain are adjacent to 6 faces each. Is there a common technique calculating this?

For example, consider the following drawing. Do you have in mind a technique that iteratively will find that for the internal pointed vertex the 0,1,2,12,13,14 faced are adjacent?

http://i23.photobucket.com/albums/b352/grnemo/0001.jpg

So my problem is algorithmic, somehow :P

Thank you very much!!!


edit: Just melting on googling and found this
The Sliding Six Algorithm <http://www.gamedev.net/reference/articles/article2264.asp>

Thx GameDev.net

[Edited by - grnemo on May 29, 2008 10:13:43 AM]
1

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