• What is your GameDev Story?

This topic is 4909 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

The following is a simple diffuse shader in HLSL:
matrix ViewMatrix;
matrix ViewProjMatrix;

vector AmbientMtrl;
vector DiffuseMtrl;
vector LightDirection;

vector DiffuseLightIntensity = {0.8f, 0.0f, 0.0f, 1.0f};
vector AmbientLightIntensity = {0.2f, 0.0f, 0.0f, 1.0f};

struct VS_INPUT
{
vector position : POSITION;
vector normal   : NORMAL;
};

struct VS_OUTPUT
{
vector position : POSITION;
vector diffuse  : COLOR;
};

VS_OUTPUT Main(VS_INPUT input)
{
VS_OUTPUT output = (VS_OUTPUT)0;

output.position = mul(input.position, ViewProjMatrix);

input.normal.w   = 0.0f;
input.normal     = mul(input.normal,ViewMatrix);

float s = dot(LightDirection, input.normal);

output.diffuse = (AmbientMtrl * AmbientLightIntensity) +
(s * (DiffuseLightIntensity * DiffuseMtrl));

return output;
}

The only thing I don't understand is the normal transformation. Why are normals transformed in a different way than position vectors? Why must I set the w component to zero? Regards.

Share on other sites
The position is transformed into projection space so that it can be drawn.

In order to compute the diffuse reflection, the light vector and the normal must be in the same space. I assume the normal is in model space, so it must be transformed into into world space or view space (an alternative is to transform the light into the model space). One reason for choosing view space might be that OpenGL doesn't have a separate model-to-world transformation, only a model-to-world-to-view transformation.

Setting normal.w to 0 prevents translation. You don't want to translate the normal, just rotate it.

Share on other sites
The position is tranformed into screen space in the vertex shader for rendering. The vectors for the lighting calculations don't need to be projected, but transformed into view or world space (in your shader its to view space). When you implement point and spot lights you will need the world or view space position to get the light and view vectors. The w component in the normal is not used, you could use a 3D vector and get the same results. Hope this helps.

Share on other sites
Great, thx guys. Yes, it makes more sense now. Normals don't need to be transformed to projection space and they need to respond to rotation to reflect orientation changes, not translation.

Share on other sites
Also, I really need to build a software rasterizer so these fundamentals are entrenched. However, who has the time...

Beyond the mammoth book 'Tricks of the 3D Game Programming Gurus', does anyone have a solid tutorial series on building something like a small wireframe engine?

Or perhaps the equivalent of D3D (to a limited extent)?

Regards.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 11
• 15
• 11
• 11
• Forum Statistics

• Total Topics
634149
• Total Posts
3015834
×