?
Solved

XNA 4.0 Enviroment Lighting / Shadowing

Posted on 2011-09-09
5
Medium Priority
?
1,102 Views
Last Modified: 2013-12-26
I have been working on a game as my first project with XNA 4.0. I am wanting to add a main light source (The Sun), that casts shadows on my objects. Most of the tutorials I see out there, are for 3d Models. However, I built my game on TriangleStrips to build cubes.  Below you can see the play dug a single cube.
 first.jpg

And here the player dug deep down:
 second.jpg
As you can see, theres no lighting or shading at all.  I want to be able to make it darker as you dig, making it realistic.

And here, the player places a couple of blocks that I want to shade the bottoms of the extended cubes, and cast a shadow on the cubes below.
 third.jpg


I've spent the good part of the day learning the origins of HLSL, how it works, and created my own Effect file, and have completely removed the usage of the BasicEffect. (I couldn't get the pre-built in lighting features of the BasicEffect to do what I wanted). Can someone help me out, or point me into the right direction? I have googled, and the examples I've found were either for 3d models, or for a prior version of XNA, or the code was for OpenGL. :/ Thanks in Advance


0
Comment
Question by:Iamtehbest
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 

Author Comment

by:Iamtehbest
ID: 36515184
And here's a copy of my Effect:

float4x4 xWorldViewProjection;
Texture xColoredTexture;



sampler ColoredTextureSampler = sampler_state 
{ texture = <xColoredTexture> ; 
  magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; 
  AddressU = mirror; 
  AddressV = mirror;};

struct VertexIn
{
	float4 position : POSITION;
	float2 textureCoordinates : TEXCOORD0;
};

struct VertexOut
{
	float4 Position : POSITION;
	float2 textureCoordinates : TEXCOORD0;
};

VertexOut VertexShaderFunction(VertexIn input)
{
	VertexOut Output = (VertexOut)0;
	
	Output.Position =mul(input.position, xWorldViewProjection);
	Output.textureCoordinates = input.textureCoordinates;

	return Output;
}

float4 PixelShaderFunction(VertexOut input) : COLOR0
{
	float4 output = tex2D(ColoredTextureSampler, input.textureCoordinates);

	return output;
}

technique Textured
{
    pass Pass0
    {
        VertexShader = compile vs_2_0 VertexShaderFunction();
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}

Open in new window

0
 
LVL 12

Expert Comment

by:satsumo
ID: 37091605
Are you still wanting an answer to this question?  I would have answered before, the site didn't show this in the area I look for questions although its in one of the branches from that area.
0
 

Author Comment

by:Iamtehbest
ID: 37116148
Absolutely.  I did manage to do some shadowing, as far as if you dig deep.  But in Image #3, the grassy area would not cast a shadow.  Any help would be deeply appreciated.
0
 
LVL 12

Accepted Solution

by:
satsumo earned 2000 total points
ID: 37117257
You're making something in the style of Minecraft, right?  Minecraft uses vertex lighting, where the light is part of the diffuse color of a vertex.  The vertex shader would pass the vertex color to the pixel shader and that would multiply the vertex color and texture color to get the final result.

In Minecraft type game, the lighting only changes when you modify the world.  There's no need to calculate it in a shader.  You store just keep color values in the vertex data and change it when the player alters something.

Your environment is drawn as a series of quads.  You need a function to calculate the color at each corner of a quad.  The simplest way to do this is to have perhaps 16 rays projecting from each vertex.  How many rays reach the sky defines how bright the vertex is.  The rays should spray out in a pyramid shape.

If your environment is a grid of cubes, you will able to intersect rays with it quickly.  Lighting always involves collision detection.
0
 

Author Closing Comment

by:Iamtehbest
ID: 37193867
Thanks man.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question