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

Terrain not lighting correctly

15 posts in this topic

Hey guys I have been following the terrain tutorials on Rastertek.com. I'm on Tutorial #3 [url="http://www.rastertek.com/tertut03.html"]http://www.rastertek.com/tertut03.html[/url] where he teaches how to add basic lighting to terrain. What I have been doing is following his code examples and putting it into my code base which is basically the same as his but slightly more compact. Right Now I have a terrain class named TerrainComponent.cpp and it handles loading the terrain from a heightmap, creating the pixel and vertex shaders for the terrain, and rendering it.The problem is that my terrain is coming out completely white with no lighting at all! Here is a solid and a wire frame screenshot of my terrain:

[img]http://img401.imageshack.us/img401/579/terrainbadlighting.jpg[/img]

[img]http://img854.imageshack.us/img854/2826/terrainbadlightingw.jpg[/img]

Here is the screenshot from Rastertek's tutorial:

[img]http://www.rastertek.com/pic0115.gif[/img]

The code is a little long so I attached a zip file containing TerrainComponent.cpp and the terrain vertex and pixel shaders.
I have compared my code to his for hours and hours and I cannot find what I am doing wrong. Please help!!

P.S. Let me know if you need to see more code.
0

Share this post


Link to post
Share on other sites
Stupid answer, but are you sure that your light direction is pointing in a right direction? Like not pointing from bottom to top for example.
0

Share this post


Link to post
Share on other sites
I can't open the zip file, winzip is claiming it's no openable as is. However, if your terrain is returning all-white, then the light either isn't getting factored in (so that no shading takes place), or the shader is pushing the pixel's color value up so that the return of your PS function is a float4(1,1,1,1). Can you post the pixel shader code? (without a zip attachment)
0

Share this post


Link to post
Share on other sites
[quote name='Faelenor' timestamp='1350670865' post='4991855']
The zip is working fine for me.
[/quote]

Yup, nevermind, I used a different browser and it worked fine. Blah.

Edit: OP, what are you setting ambientcolor and diffusecolor to when you call the render? Edited by BCullis
0

Share this post


Link to post
Share on other sites
Sure here is the pixel shader:

[code]
SamplerState SampleType;
cbuffer LightBuffer
{
float4 ambientColor;
float4 diffuseColor;
float3 lightDirection;
float padding;
};
struct PSInput
{
float4 position : SV_POSITION;
float3 normal : NORMAL;
};
float4 PSMain( PSInput input ) : SV_TARGET
{
float3 lightDir;
float lightIntensity;
float4 color;

color = ambientColor;
lightDir = -lightDirection;
lightIntensity = saturate( dot(input.normal, lightDir) );
if( lightIntensity > 0.0f )
{
color += (diffuseColor * lightIntensity);
}
color = saturate( color );
return color;
}

[/code]

And the vertex shader:
[code]
cbuffer MatrixBuffer
{
matrix worldMatrix;
matrix viewMatrix;
matrix projectionMatrix;
};
struct VSInput
{
float4 position : POSITION;
float3 normal : NORMAL;
};
struct PSInput
{
float4 position : SV_POSITION;
float3 normal : NORMAL;
};
PSInput VSMain( VSInput input )
{
PSInput output;
input.position.w = 1.0f;
output.position = mul( input.position, worldMatrix );
output.position = mul( output.position, viewMatrix );
output.position = mul( output.position, projectionMatrix );

output.normal = mul( input.normal, (float3x3) worldMatrix );
output.normal = normalize( output.normal );
return output;
}
[/code]

@Faelenor: I figured it might be something simple like this. I have the same values set for the light direction as Rastertek tutorial: D3DXVECTOR3( 0.0f, 0.0f, 0.75f );
I messed around with these values and when I change the z value to a lower number the terrain gets darker but it is still one solid color.
0

Share this post


Link to post
Share on other sites
I have a sun class (which will hopefully one day represent the sun) and it is the same as the Light Class in the rastertek tutorial. Currently it holds the diffuse direction and ambient values. During the initialization I set these values with this code:

[code]
mpSun->setAmbientColor( 0.005f, 0.005f, 0.005f, 1.0f );
mpSun->setDiffuseColor( 1.0f, 1.0f, 1.0f, 1.0f );
mpSun->setDirection( 0.0f, 0.0f, 0.75f );
[/code]

and each frame when I render the terrain I make this call:

[code]
result = mpTerrain->render
( mpCore->getContext(),
world,
view,
proj,
mpSun->getAmbientColor(),
mpSun->getDiffuseColor(),
mpSun->getDirection()
);
[/code] Edited by kidman171
0

Share this post


Link to post
Share on other sites
What do you get if you set direction to something like (1, -1, 1)?
Also, someone check my math: should the light direction be normalized before calculating the dot product to get a more accurate result re: angle between two vectors? Or does that matter with the followup saturate() call?
0

Share this post


Link to post
Share on other sites
Thanks guys for trying to help me solve my problem.
@ BCullis: I set the direction to (1, -1, 1) and it is still completely white. I also tried normalizing the normals before the dot product calculation but once again I saw no visible changes. I have a feeling the pixel shader is outputting (1, 1, 1, 1) but I don't know where the bad calculations are coming from.

Would it be helpful if I upload the entire Visual Studio solution? Edited by kidman171
0

Share this post


Link to post
Share on other sites
I wouldn't go that far. This is a good time to get familiar with tools like PIX, try capturing some frames with it and check the values of your vertices and the constants.
0

Share this post


Link to post
Share on other sites
Going to take a wild guess as I cannot see much or download the zip on my phone lol. Anyway, its a solid colour, shouldn't it be blended with the terrain colour? Sampling the terrain texture and multiply with the final colour? Also not sure if its just weird naming but intensity is a constant value that's applied to the diffuse light n dot l calculation :S
0

Share this post


Link to post
Share on other sites
Thanks for your input Daniel. I am not attempting to apply any textures at this point, just passing in white as the diffuse color and lighting the terrain based on a light direction. And I just followed the tutorial on Rastertek.com, he used the word intensity so I did as well, that's all I know about the word choice.
0

Share this post


Link to post
Share on other sites
Well I have made some discoveries using the PIX tool. The value of every normal is (0, 0, -1) going into and out of the vertex shader. As a sanity check I ran PIX with the executable from the tutorial and the normals going into the vertex shader are all different (e.g (-0.050, 0.999, 0.0) ). I definitely have something wrong with normals before they even get passed into the graphics card.
0

Share this post


Link to post
Share on other sites
Okay I solved it, it was just a simple typo.

Just for the record, when I was calculating the normals I had this line:
[code]
normals[index].x = (vector1[1] * vector2[2]) - (vector1[2] * vector2[1]);
normals[index].y = (vector1[2] * vector2[0]) - (vector1[0] * vector2[2]);
normals[index].y = (vector1[0] * vector2[1]) - (vector1[1] * vector2[0]);
[/code]

It should be:
[code]
normals[index].x = (vector1[1] * vector2[2]) - (vector1[2] * vector2[1]);
normals[index].y = (vector1[2] * vector2[0]) - (vector1[0] * vector2[2]);
normals[index].z = (vector1[0] * vector2[1]) - (vector1[1] * vector2[0]);
[/code]

It's funny how small typos can make you lose days of coding time. I guess that's the joys of programming!
Thanks to those who tried to help me anyway :)
0

Share this post


Link to post
Share on other sites
They look identical to me? What was the error?

Oh, .y, right. I gotcha'. Edited by StoneMask
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