• 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
Medo Mex

Spotlight

7 posts in this topic

blink.png

 

The spot light is only working when I set a bump map, but when I don't set bump map it doesn't work.

 

The following doesn't work:

float3 n = normalize(IN.Normal);

The following works very well:

float3 n = normalize(tex2D(normalMap, IN.UV).rgb * 2.0f - 1.0f);

And here is what I do in Vertex Shader:

OUT.Normal = mul(IN.Normal, (float3x3)worldInverseTransposeMatrix);
Edited by Medo3337
0

Share this post


Link to post
Share on other sites

@Migi0027: I think it's set, I'm using Visual Studio 2010

 

Shader VS_INPUT and VS_OUTPUT:

struct VS_INPUT
{
   float3 Pos : POSITION;
   float2 UV : TEXCOORD0;
   float3 Normal : NORMAL;
};

struct VS_OUTPUT
{
   float4 Pos : SV_POSITION;
   float2 UV : TEXCOORD0;
   float3 Normal : NORMAL;
   float4 worldPos : TEXCOORD6;
}

C++ Mesh Clone:

D3DVERTEXELEMENT9 g_vertexElements[] =
{
    {0,  0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
    {0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
    {0, 20, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL,   0},
    D3DDECL_END()
};

mesh->CloneMesh(D3DXMESH_32BIT | D3DXMESH_MANAGED, g_vertexElements, m_d3dDevice, &mesh);
0

Share this post


Link to post
Share on other sites

1.

How is this possible to even compile?

struct VS_OUTPUT
{
   float4 Pos : SV_POSITION; // sv_position dx10+ semantic
   float2 UV : TEXCOORD0;
   float3 Normal : NORMAL;
   float4 worldPos : TEXCOORD6;
}

Usually when you output from vertex shader you would use TEXCOORD[n] semantics, and set it in order:

struct VS_OUTPUT
{
   float4 Pos : POSITION;
   float2 UV : TEXCOORD0;
   float3 Normal : TEXCOORD1;
   float4 worldPos : TEXCOORD2;
}

2.

mesh->CloneMesh(D3DXMESH_32BIT | D3DXMESH_MANAGED, g_vertexElements, m_d3dDevice, &mesh);

If mesh doesn't have normals and you cloned it with new vertex declaration like that, you just reserved space for normals but they doesn't have correct values, so you need to compute them and update mesh VB.

To check if mesh have normals (or any other vertex component) you would use code like this:

D3DVERTEXELEMENT9 decl[MAX_FVF_DECL_SIZE];

        hr = dxMesh->GetDeclaration(decl);
        if(FAILED(hr))
           ...error

        bool haveNormals    = false;

        for(UINT i = 0; i < MAX_FVF_DECL_SIZE; ++i)
        {
            if(decl[i].Stream == 0xFF || decl[i].Stream != 0)
            {
                break;
            }

            if(decl[i].Usage == D3DDECLUSAGE_NORMAL)
            {
                haveNormals      = true;
                break;
            }
        }
 
    if(haveNormal)
        MessageBox(0, "YES!", 0, 0);
    else
        MessageBox(0, "NO!", 0, 0);

3. As to why spotlight isn't working, you are not giving enough information to help you.

Edited by belfegor
0

Share this post


Link to post
Share on other sites

@belfegor: I tested now, the mesh do have normals before I clone it.

 

Even the spot light that appear when I add a bump map is a little weird when I'm too close:

[attachment=16965:spotlight.png]

 

When I don't set a bump map, here is what I get:

[attachment=16966:spotlight.png]

 

Pixel Shader Spotlight:

// NOTICE: "lightDirection" is the actual direction of the spot light

float3 lightDir = (lightPosition - IN.worldPos) / lightRange;
float atten = saturate(1.0f - dot(lightDir, lightDir));

float2 cosAngles = cos(float2(lightSpotOuterCone, lightSpotInnerCone) * 0.5f);
float spotDot = dot(normalize(-lightDir), normalize(lightDirection));
float spotEffect = smoothstep(cosAngles[0], cosAngles[1], spotDot);
    
atten *= spotEffect;

float3 viewDir = IN.worldPos - cameraPos;
float3 halfVector = normalize(normalize(lightDir) + normalize(viewDir));

float3 n = normalize(IN.Normal);
float3 h = normalize(halfVector);
float3 l = normalize(lightDir);
    
float nDotL = saturate(dot(n, l));
float nDotH = saturate(dot(n, h));
float power = pow(nDotH, materialPower); //(nDotL == 0.0f) ? 0.0f : pow(nDotH, materialPower);   

float4 FinalDiffuse = materialDiffuse * lightDiffuse;
float4 FinalSpecular = materialSpecular * lightSpecular;

float4 colorF = (materialAmbient + globalAmbient) +
                (FinalDiffuse * nDotL * atten) + (FinalSpecular * power * atten);

colorF.a = 1.0;
return colorF * tex2D(colorMap, IN.UV);

 

0

Share this post


Link to post
Share on other sites

If you use "bump map(ing)" then your must have/compute binormal/tangent vectors in mesh VB.

And i am pretty sure that "viewDir" should be "to camera vector" so:

//float3 viewDir = IN.worldPos - cameraPos;
float3 viewDir = cameraPos - IN.worldPos;
0

Share this post


Link to post
Share on other sites

@belfegor: The following resolved the weird light with bump map, now the bump map works perfectly with spot light

//float3 viewDir = IN.worldPos - cameraPos;
float3 viewDir = cameraPos - IN.worldPos;

However, If I don't set bump map, I don't see the spot light anymore.

 

If you use "bump map(ing)" then your must have/compute binormal/tangent vectors in mesh VB

 

Not sure how and how that could affect the spot light...

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