游戏开发论坛

 找回密码
 立即注册
搜索
查看: 3683|回复: 4

[HLSL求助]Luna的dx龙书中的顶点着色器没有效果

[复制链接]

6

主题

54

帖子

59

积分

注册会员

Rank: 2

积分
59
发表于 2009-5-14 19:21:00 | 显示全部楼层 |阅读模式
在使用Luna的dx龙书中的顶点着色器(17章那几个例子)的源代码的时候,出现了Error x3025的错误,在google搜了下大概知道(http://bbs.gameres.com/showthread.asp?threadid=115758)错在后来版本的编译器严格规定了语法,于是我将前面的定义改为了:
extern  matrix WorldViewMatrix;
extern  matrix WorldViewProjMatrix;
static vector Color;//这里被初始化成了(0.0f,0.0f,0.0f,0.0f)
static vector LightDirection;
不过新的问题出现了:就是出现的是纯黑色物体,我觉得是颜色值没有没传入而默认的颜色是ff000000所以就黑了,后来单步调试了下发现2个问题:
1.在运行到GetConstantByName函数从hlsl文件中获得的变量之前,用来保存这些变量的句柄在调试下是bad ptr??然后去google上搜索了下在gamedev下找到了和我出一个问题的人(代码都一样~~)http://www.gamedev.net/community/forums/topic.asp?topic_id=524418&forum_id=10&gforum_id=0
但似乎他额是只找到了源头不知道怎么解决
2.在显示的函数中
Device->BeginScene();
                FPS->DrawWritng();

                Device->SetVertexShader(CartoonShader);
                Device->SetTexture(0, ShadeTex);

                D3DXMATRIX WorldView;
                D3DXMATRIX WorldViewProj;
                for(int i = 0; i < 5; i++)
                {
                        WorldView     = WorldMatrices * V;
                        WorldViewProj = WorldMatrices * V * ProjMatrix;

                        CartoonConstTable->SetMatrix(
                                Device,
                                WorldViewHandle,
                                &WorldView);

                        CartoonConstTable->SetMatrix(
                                Device,
                                WorldViewProjHandle,
                                &WorldViewProj);

                        CartoonConstTable->SetVector(
                                Device,
                                ColorHandle,
                                &MeshColors);

                        Meshes->DrawSubset(0);
                }



                Device->EndScene();
                Device-&gtresent(0, 0, 0, 0);

SetVector返回的HRESULT是错误(也就是说设置的值没有通过句柄传入shader中??)???

我已经挂在这个问题上一周了。。想了许久也没弄明白。
哪怕是一点提示我都很感谢的,希望大家能帮助我下

                                                      在Dx里迷失的人。。

6

主题

54

帖子

59

积分

注册会员

Rank: 2

积分
59
 楼主| 发表于 2009-5-14 19:25:00 | 显示全部楼层

Re:[HLSL求助]Luna的dx龙书中的顶点着色器没有效果

例子是Lun红龙书的第17章VS Toon 2 With Outlines
贴下源代码:
HLSL的是(有两个文件,不过画外部轮廓的很奇异的可以执行,目前知道的区别就在于画轮廓的全局变量里没有需要传入修改的vector):
//
// Globals
//

extern matrix WorldViewMatrix;
extern matrix WorldViewProjMatrix;

static vector Color={0.0f,1.0f,0.0f,0.0f};

static vector LightDirection;

//
// Structures
//

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

struct VS_OUTPUT
{
    vector position : POSITION;
    float2 uvCoords : TEXCOORD;
    vector diffuse  : COLOR;
};


//
// Main
//

VS_OUTPUT Main(VS_INPUT input)
{
    // zero out each member in output
    VS_OUTPUT output = (VS_OUTPUT)0;


    // transform vertex position to homogenous clip space
     output.position = mul(input.position, WorldViewProjMatrix);

    //
    // Transform lights and normals to view space.  Set w
    // components to zero since we're transforming vectors.
    // Assume there are no scalings in the world
    // matrix as well.
    //
    LightDirection.w = 0.0f;
    input.normal.w   = 0.0f;
    LightDirection   = mul(LightDirection, WorldViewMatrix);
    input.normal     = mul(input.normal, WorldViewMatrix);

    //
    // Compute the 1D texture coordinate for toon rendering.
    //
    float u = dot(LightDirection, input.normal);

    //
    // Clamp to zero if u is negative because u
    // negative implies the angle between the light
    // and normal is greater than 90 degrees.  And
    // if that is true then the surface receives
    // no light.
    //
    if( u < 0.0f )
        u = 0.0f;

    //
    // Set other tex coord to middle.
    //
    float v = 0.5f;


    output.uvCoords.x = u;
    output.uvCoords.y = v;

    // save color
    output.diffuse = Color;
   
    return output;
}
cpp文件里(贴主要的实现部分)
#include "d3dUtility.h"
#include "silhouetteEdges.h"

//
// Globals
//

IDirect3DDevice9* Device = 0;

const int Width  = 640;
const int Height = 480;

IDirect3DVertexShader9* ToonShader = 0;
ID3DXConstantTable* ToonConstTable = 0;

ID3DXMesh*  Meshes[4] = {0, 0, 0, 0};
D3DXMATRIX  WorldMatrices[4];
D3DXVECTOR4 MeshColors[4];

D3DXMATRIX ProjMatrix;

IDirect3DTexture9* ShadeTex  = 0;

D3DXHANDLE ToonWorldViewHandle     = 0;
D3DXHANDLE ToonWorldViewProjHandle = 0;
D3DXHANDLE ToonColorHandle    = 0;
D3DXHANDLE ToonLightDirHandle = 0;

SilhouetteEdges* MeshOutlines[4] = {0, 0, 0, 0};
IDirect3DVertexShader9* OutlineShader = 0;
ID3DXConstantTable* OutlineConstTable = 0;

D3DXHANDLE OutlineWorldViewHandle = 0;
D3DXHANDLE OutlineProjHandle = 0;

//
// Framework functions
//
bool Setup()
{
        HRESULT hr = 0;

        //
        // Create geometry and compute corresponding world matrix and color
        // for each mesh.
        //

        ID3DXBuffer* adjBuffer[4] = {0, 0, 0, 0};

        D3DXCreateTeapot(Device, &Meshes[0], &adjBuffer[0]);
        D3DXCreateSphere(Device, 1.0f, 20, 20, &Meshes[1], &adjBuffer[1]);
        D3DXCreateTorus(Device, 0.5f, 1.0f, 20, 20, &Meshes[2], &adjBuffer[2]);
        D3DXCreateCylinder(Device, 0.5f, 0.5f, 2.0f, 20, 20, &Meshes[3], &adjBuffer[3]);

        D3DXMatrixTranslation(&WorldMatrices[0],  0.0f,  2.0f, 0.0f);
        D3DXMatrixTranslation(&WorldMatrices[1],  0.0f, -2.0f, 0.0f);
        D3DXMatrixTranslation(&WorldMatrices[2], -3.0f,  0.0f, 0.0f);
        D3DXMatrixTranslation(&WorldMatrices[3],  3.0f,  0.0f, 0.0f);

        MeshColors[0] = D3DXVECTOR4(1.0f, 0.0f, 0.0f, 1.0f);
        MeshColors[1] = D3DXVECTOR4(0.0f, 1.0f, 0.0f, 1.0f);
        MeshColors[2] = D3DXVECTOR4(0.0f, 0.0f, 1.0f, 1.0f);
        MeshColors[3] = D3DXVECTOR4(1.0f, 1.0f, 0.0f, 1.0f);

        //
        // Allocate mesh outlines
        //

        MeshOutlines[0] = new SilhouetteEdges(Device, Meshes[0], adjBuffer[0]);
        MeshOutlines[1] = new SilhouetteEdges(Device, Meshes[1], adjBuffer[1]);
        MeshOutlines[2] = new SilhouetteEdges(Device, Meshes[2], adjBuffer[2]);
        MeshOutlines[3] = new SilhouetteEdges(Device, Meshes[3], adjBuffer[3]);

        d3d::Release<ID3DXBuffer*>(adjBuffer[0]);
        d3d::Release<ID3DXBuffer*>(adjBuffer[1]);
        d3d::Release<ID3DXBuffer*>(adjBuffer[2]);
        d3d::Release<ID3DXBuffer*>(adjBuffer[3]);

        //
        // Compile Toon Shader
        //

        ID3DXBuffer* toonCompiledCode = 0;
        ID3DXBuffer* toonErrorBuffer  = 0;

        hr = D3DXCompileShaderFromFile(
                "toon.txt",
                0,
                0,
                "Main", // entry point function name
                "vs_1_1",
                D3DXSHADER_DEBUG,
                &toonCompiledCode,
                &toonErrorBuffer,
                &ToonConstTable);

        // output any error messages
        if( toonErrorBuffer )
        {
                ::MessageBox(0, (char*)toonErrorBuffer->GetBufferPointer(), 0, 0);
                d3d::Release<ID3DXBuffer*>(toonErrorBuffer);
        }

        if(FAILED(hr))
        {
                ::MessageBox(0, "D3DXCompileShaderFromFile() - FAILED", 0, 0);
                return false;
        }

        hr = Device->CreateVertexShader(
                (DWORD*)toonCompiledCode->GetBufferPointer(),
                &ToonShader);

        if(FAILED(hr))
        {
                ::MessageBox(0, "CreateVertexShader - FAILED", 0, 0);
                return false;
        }

        d3d::Release<ID3DXBuffer*>(toonCompiledCode);

        //
        // Compile Outline shader.
        //

        ID3DXBuffer* outlineCompiledCode = 0;
        ID3DXBuffer* outlineErrorBuffer  = 0;

        hr = D3DXCompileShaderFromFile(
                "outline.txt",
                0,
                0,
                "Main", // entry point function name
                "vs_1_1",
                D3DXSHADER_DEBUG,
                &outlineCompiledCode,
                &outlineErrorBuffer,
                &OutlineConstTable);

        // output any error messages
        if( outlineErrorBuffer )
        {
                ::MessageBox(0, (char*)outlineErrorBuffer->GetBufferPointer(), 0, 0);
                d3d::Release<ID3DXBuffer*>(outlineErrorBuffer);
        }

        if(FAILED(hr))
        {
                ::MessageBox(0, "D3DXCompileShaderFromFile() - FAILED", 0, 0);
                return false;
        }

        hr = Device->CreateVertexShader(
                (DWORD*)outlineCompiledCode->GetBufferPointer(),
                &OutlineShader);

        if(FAILED(hr))
        {
                ::MessageBox(0, "CreateVertexShader - FAILED", 0, 0);
                return false;
        }

        d3d::Release<ID3DXBuffer*>(outlineCompiledCode);

        //
        // Load textures.
        //

        D3DXCreateTextureFromFile(Device, "toonshade.bmp", &ShadeTex);

        Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
        Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);
        Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_NONE);

        //
        // Get Handles
        //

        ToonWorldViewHandle     = ToonConstTable->GetConstantByName(0, "WorldViewMatrix");
        ToonWorldViewProjHandle = ToonConstTable->GetConstantByName(0, "WorldViewProjMatrix");
        ToonColorHandle         = ToonConstTable->GetConstantByName(0, "Color");
        ToonLightDirHandle      = ToonConstTable->GetConstantByName(0, "LightDirection");

        OutlineWorldViewHandle = OutlineConstTable->GetConstantByName(0, "WorldViewMatrix");
        OutlineProjHandle      = OutlineConstTable->GetConstantByName(0, &quotrojMatrix");

        //
        // Set shader constants:
        //
       
        // Light direction:
        D3DXVECTOR4 directionToLight(-0.57f, 0.57f, -0.57f, 0.0f);
        hr=ToonConstTable->SetVector(
                Device,
                ToonLightDirHandle,
                &directionToLight);
        if(FAILED(hr))
        {
                ::MessageBox(0, "ToonLightDirHandle - FAILED", 0, 0);
                return false;
        }

        for(int i=0;i<4;i++)
        {
                hr=ToonConstTable->SetVector(
                                Device,
                                ToonColorHandle,
                                &MeshColors);
                if(FAILED(hr))
        {
                ::MessageBox(0, "ToonColorHandle - FAILED", 0, 0);
                return false;
        }
        }

        ToonConstTable->SetDefaults(Device);
        OutlineConstTable->SetDefaults(Device);

        //
        // Calculate projection matrix.
        //

        D3DXMatrixPerspectiveFovLH(
                &ProjMatrix, D3DX_PI * 0.25f,
                (float)Width / (float)Height, 1.0f, 1000.0f);
        Device->SetRenderState(D3DRS_FILLMODE,D3DFILL_SOLID);
        return true;
}


bool Display(float timeDelta)
{
        if( Device )
        {
                //
                // Update the scene: Allow user to rotate around scene.
                //
               
                static float angle  = (3.0f * D3DX_PI) / 2.0f;
                static float height = 5.0f;
       
                if( ::GetAsyncKeyState(VK_LEFT) & 0x8000f )
                        angle -= 0.5f * timeDelta;

                if( ::GetAsyncKeyState(VK_RIGHT) & 0x8000f )
                        angle += 0.5f * timeDelta;

                if( ::GetAsyncKeyState(VK_UP) & 0x8000f )
                        height += 5.0f * timeDelta;

                if( ::GetAsyncKeyState(VK_DOWN) & 0x8000f )
                        height -= 5.0f * timeDelta;

                D3DXVECTOR3 position( cosf(angle) * 7.0f, height, sinf(angle) * 7.0f );
                D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
                D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
                D3DXMATRIX view;
                D3DXMatrixLookAtLH(&view, &position, &target, &up);

                //
                // Render
                //

                Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
                Device->BeginScene();

                D3DXMATRIX worldView;
                D3DXMATRIX worldViewProj;

                // Draw Cartoon
                Device->SetVertexShader(ToonShader);
                Device->SetTexture(0, ShadeTex);

                for(int i = 0; i < 4; i++)
                {
                        worldView = WorldMatrices * view;
                        worldViewProj = WorldMatrices * view * ProjMatrix;

                        ToonConstTable->SetMatrix(
                                Device,
                                ToonWorldViewHandle,
                                &worldView);

                        ToonConstTable->SetMatrix(
                                Device,
                                ToonWorldViewProjHandle,
                                &worldViewProj);

                       

                        Meshes->DrawSubset(0);
                }

                // Draw Outlines.  
                Device->SetVertexShader(OutlineShader);
                Device->SetTexture(0, 0);

                Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

                for(int i = 0; i < 4; i++)
                {
                        worldView = WorldMatrices * view;

                        OutlineConstTable->SetMatrix(
                                Device,
                                OutlineWorldViewHandle,
                                &worldView);

                        OutlineConstTable->SetMatrix(
                                Device,
                                OutlineProjHandle,
                                &ProjMatrix);

                        MeshOutlines->render();
                }

                Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);

                Device->EndScene();
                Device->Present(0, 0, 0, 0);
        }
        return true;
}

11

主题

650

帖子

651

积分

高级会员

Rank: 4

积分
651
发表于 2009-5-14 21:39:00 | 显示全部楼层

Re:[HLSL求助]Luna的dx龙书中的顶点着色器没有效果

同学 qrli同学不是说了那个数不能改么(怎么又是qrli大人)

就式说extern的值不能修改  也就是说错误是因为该shader修改了
那不修改他不就得了
但问题是必须要用到修改的呀 那你new一个vector/float4 然后把不能修改的赋值 给这个不就醒了
    float4 LD=LightDirection;

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

    //
    // Compute the 1D texture coordinate for toon rendering.
    //
    float u = dot(LD, input.normal);

6

主题

54

帖子

59

积分

注册会员

Rank: 2

积分
59
 楼主| 发表于 2009-5-16 12:32:00 | 显示全部楼层

Re: Re:[HLSL求助]Luna的dx龙书中的顶点着色器没有效果

resplendence: Re:[HLSL求助]Luna的dx龙书中的顶点着色器没有效果

同学 qrli同学不是说了那个数不能改么(怎么又是qrli大人)

就式说extern的值不能修改  也就是说错误是因...


自己傻了。。。你说的那个我也曾改过,不过自己把
vector Color;

vector LightDirection;
改成了

static vector Color;

static vector LightDirection;
......所以才会在
hr=ToonConstTable->SetVector(
                Device,
                ToonLightDirHandle,
                &directionToLight);
        if(FAILED(hr))
        {
                ::MessageBox(0, "ToonLightDirHandle - FAILED", 0, 0);
                return false;
        }
一直失败。。。多谢了哈,唉,对自己的愚笨都无奈了。。。

0

主题

1

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2009-5-23 15:49:00 | 显示全部楼层

Re:[HLSL求助]Luna的dx龙书中的顶点着色器没有效果

你用的SDK 应该版本应该比较新, 用原来的SDK 可以不修改代码2008年前的
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-12-20 07:48

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表