游戏开发论坛

 找回密码
 立即注册
搜索
查看: 3763|回复: 13

一个基础的HLSL问题?请教高手

[复制链接]

182

主题

445

帖子

459

积分

中级会员

Rank: 3Rank: 3

积分
459
QQ
发表于 2005-11-17 18:38:00 | 显示全部楼层 |阅读模式
先看一下fx文件:hlsl.fx
// -------------------------------------------------------------

// -------------------------------------------------------------
// variables that are provided by the application
// -------------------------------------------------------------
float4x4 matWorldViewProj;       
float4x4 matWorld;       
float4 vecLightDir;
texture g_MeshTexture;  

sampler MeshTextureSampler =
sampler_state
{
    Texture = <g_MeshTexture>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};

// -------------------------------------------------------------
// Output channels
// -------------------------------------------------------------
struct VS_OUTPUT
{
    float4 Pos  :       POSITION;
    float4 Diffuse:     COLOR0;
    float2 TexUV :      TEXCOORD0;
    float3 Norm :       TEXCOORD1;
};


// -------------------------------------------------------------
// vertex shader function (input channels)
// -------------------------------------------------------------
VS_OUTPUT VS(float4 Pos : POSITION,float4 Color:COLOR0,float2 TexUV: TEXCOORD0,float3 Norm:NORMAL)
{
    VS_OUTPUT Out;      
    Out.Pos = mul(Pos, matWorldViewProj);        // transform Position
    Out.TexUV=TexUV;
    Out.Diffuse = Color;
    Out.Norm = mul(Norm, matWorld);
    return Out;
}


// -------------------------------------------------------------
// Pixel Shader (input channels)utput channel
// -------------------------------------------------------------

struct PS_OUTPUT
{
    float4 RGBColor : COLOR0;  // Pixel color   
};

PS_OUTPUT PS(VS_OUTPUT In) : COLOR
{
    PS_OUTPUT Output;
    float4 Nom= saturate(dot(In.Norm, vecLightDir)); // diffuse component
    Output.RGBColor= tex2D(MeshTextureSampler, In.TexUV)* In.Diffuse*Nom;
    return Output;
}
// -------------------------------------------------------------
//
// -------------------------------------------------------------
technique TShader
{
    pass P0
    {
        // compile shaders
        VertexShader = compile vs_1_1 VS();
        PixelShader  = compile ps_1_4 PS();
    }
}

//然后是源码:
//-----------------------------------------------------------------------------
// File: Matrices.cpp
//
// Desc: Now that we know how to create a device and render some 2D vertices,
//       this tutorial goes the next step and renders 3D geometry. To deal with
//       3D geometry we need to introduce the use of 4x4 matrices to transform
//       the geometry with translations, rotations, scaling, and setting up our
//       camera.
//
//       Geometry is defined in model space. We can move it (translation),
//       rotate it (rotation), or stretch it (scaling) using a world transform.
//       The geometry is then said to be in world space. Next, we need to
//       position the camera, or eye point, somewhere to look at the geometry.
//       Another transform, via the view matrix, is used, to position and
//       rotate our view. With the geometry then in view space, our last
//       transform is the projection transform, which "projects" the 3D scene
//       into our 2D viewport.
//
//       Note that in this tutorial, we are introducing the use of D3DX, which
//       is a set of helper utilities for D3D. In this case, we are using some
//       of D3DX's useful matrix initialization functions. To use D3DX, simply
//       include <d3dx9.h> and link with d3dx9.lib.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#include <Windows.h>
#include <mmsystem.h>
#include <d3dx9.h>
#include <strsafe.h>




//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3D9             g_pD3D       = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9       g_pd3dDevice = NULL; // Our rendering device
LPDIRECT3DVERTEXBUFFER9 g_pVB        = NULL; // Buffer to hold vertices
LPD3DXEFFECT            g_pEffect    = NULL;
LPDIRECT3DTEXTURE9      g_pTexture   = NULL; // Our texture
D3DXVECTOR4             g_LightDir= D3DXVECTOR4( 0.0f, 0.0f, 1.0f, 1.0f );
// A structure for our custom vertex type
struct CUSTOMVERTEX
{
    FLOAT x, y, z ;      // The untransformed, 3D position for the vertex
    DWORD Color;     // The vertex color
        FLOAT tu, tv;
        FLOAT nx,ny,nz;
};

// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1|D3DFVF_NORMAL)




//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
    // Create the D3D object.
    if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
        return E_FAIL;

    // Set up the structure used to create the D3DDevice
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

    // Create the D3DDevice
    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &g_pd3dDevice ) ) )
    {
        return E_FAIL;
    }

    // Turn off culling, so we see the front and back of the triangle
    g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );

    // Turn off D3D lighting, since we are providing our own vertex colors
    g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );

        g_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, TRUE );

        //创建HLSH
        if( FAILED(D3DXCreateEffectFromFile( g_pd3dDevice, "hlsl.fx", NULL, NULL,
                D3DXSHADER_DEBUG, NULL, &g_pEffect, NULL ) ) )
        {
        return E_FAIL;
        }
        if( g_pEffect != NULL ) g_pEffect->OnResetDevice();
                       
        //设定效果:因为只有一个,所以初始时就设了~
        D3DXHANDLE hTechnique = g_pEffect->GetTechniqueByName( "TShader" );
        g_pEffect->SetTechnique( hTechnique );

       
    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: InitGeometry()
// Desc: Creates the scene geometry
//-----------------------------------------------------------------------------
HRESULT InitGeometry()
{
    // Initialize three vertices for rendering a triangle
    CUSTOMVERTEX g_Vertices[] =
    {
        { -1.0f,-1.0f, 0.0f, 0xffffffff,  0.0f,1.0f ,-1.0f,-1.0f, 0.0f},
        {  1.0f,-1.0f, 0.0f, 0xffffffff,  1.0f,1.0f ,1.0f,-1.0f, 0.0f},
        {  0.0f, 1.0f, 0.0f, 0xffffffff,  0.5f,0.0f ,0.0f, 1.0f, 0.0f },
    };

    // Create the vertex buffer.
    if( FAILED( g_pd3dDevice->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX),
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
    {
        return E_FAIL;
    }

    // Fill the vertex buffer.
    VOID* pVertices;
    if( FAILED( g_pVB->Lock( 0, sizeof(g_Vertices), (void**)&pVertices, 0 ) ) )
        return E_FAIL;
    memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
    g_pVB->Unlock();

        if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "banana.bmp", &g_pTexture ) ) )
    {
        // If texture is not in current folder, try parent folder
        if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "..\\banana.bmp", &g_pTexture ) ) )
        {
            MessageBox(NULL, "Could not find banana.bmp", "Textures.exe", MB_OK);
            return E_FAIL;
        }
    }

        g_pEffect->SetTexture( "g_MeshTexture", g_pTexture );
    return S_OK;
}


//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
    if( g_pTexture != NULL )
        g_pTexture->Release();

    if( g_pEffect != NULL )        
        g_pEffect->Release();

    if( g_pVB != NULL )
        g_pVB->Release();

    if( g_pd3dDevice != NULL )
        g_pd3dDevice->Release();

    if( g_pD3D != NULL )
        g_pD3D->Release();
}



//-----------------------------------------------------------------------------
// Name: SetupMatrices()
// Desc: Sets up the world, view, and projection transform matrices.
//-----------------------------------------------------------------------------
VOID SetupMatrices()
{
    // For our world matrix, we will just rotate the object about the y-axis.
    D3DXMATRIXA16 matWorld;

    UINT  iTime  = timeGetTime() % 1000;
    FLOAT fAngle = iTime * (2.0f * D3DX_PI) / 1000.0f;
    D3DXMatrixRotationY( &matWorld, fAngle );

    D3DXVECTOR3 vEyePt( 0.0f, 3.0f,-5.0f );
    D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
    D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
    D3DXMATRIXA16 matView;
    D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );

    D3DXMATRIXA16 matProj;
    D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );


        //设置光线和眼睛位置
        g_pEffect->SetVector("vecLightDir", &-g_LightDir);
        //g_pEffect->SetVector("vecEye", &D3DXVECTOR4(vEyePt.x,vEyePt.y,vEyePt.z,0));
        //设定VS矩阵
        D3DXMATRIX mWorldViewProj = matWorld * matView * matProj;
        g_pEffect->SetMatrix( "matWorldViewProj", &mWorldViewProj );
        g_pEffect->SetMatrix( "matWorld", &matWorld );
}



//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
    // Clear the backbuffer to a black color
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );

    // Begin the scene
    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
    {
        // Setup the world, view, and projection matrices
        SetupMatrices();

        // Render the vertex buffer contents
        g_pd3dDevice->SetTexture( 0, g_pTexture );
        g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
        g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
        g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
        g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );

                //按VS,PS渲染
                if( g_pEffect != NULL )
                {
                        UINT nPasses=0;
                        UINT iPass=0;
                        if(SUCCEEDED(g_pEffect->Begin( &nPasses, 0 )))
                        {
                                for(iPass = 0; iPass < nPasses; iPass ++ )
                                {
                                        g_pEffect->BeginPass( 0 );
                                }
                                g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
                                g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
                                g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 1 );
                                g_pEffect->EndPass();
                                g_pEffect->End();
                        }
                }
        // End the scene
        g_pd3dDevice->EndScene();
    }

    // Present the backbuffer contents to the display
    g_pd3dDevice-&gtresent( NULL, NULL, NULL, NULL );
}




//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
    {
        case WM_DESTROY:
            Cleanup();
            PostQuitMessage( 0 );
            return 0;
    }

    return DefWindowProc( hWnd, msg, wParam, lParam );
}
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
    // Register the window class
    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
                      GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                      "D3D Tutorial", NULL };
    RegisterClassEx( &wc );

    // Create the application's window
    HWND hWnd = CreateWindow( "D3D Tutorial", "HLSL学习",
                              WS_OVERLAPPEDWINDOW, 100, 100, 256, 256,
                              GetDesktopWindow(), NULL, wc.hInstance, NULL );

    // Initialize Direct3D
    if( SUCCEEDED( InitD3D( hWnd ) ) )
    {
        // Create the scene geometry
        if( SUCCEEDED( InitGeometry() ) )
        {
            // Show the window
            ShowWindow( hWnd, SW_SHOWDEFAULT );
            UpdateWindow( hWnd );

            // Enter the message loop
            MSG msg;
            ZeroMemory( &msg, sizeof(msg) );
            while( msg.message!=WM_QUIT )
            {
                if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
                {
                    TranslateMessage( &msg );
                    DispatchMessage( &msg );
                }
                else
                    Render();
            }
        }
    }

    UnregisterClass( "D3D Tutorial", wc.hInstance );
    return 0;
}



应该是好懂的。但是为何运行后不显示了。
但是如果顶点格式中去掉法线,fx中也去掉法线。就没问题了,色彩与纹理混合正常。
[em3]

182

主题

445

帖子

459

积分

中级会员

Rank: 3Rank: 3

积分
459
QQ
 楼主| 发表于 2005-11-17 18:40:00 | 显示全部楼层

Re: 一个基础的HLSL问题?请教高手

更不明白的是。如果我将

struct CUSTOMVERTEX
{
    FLOAT x, y, z ;      // The untransformed, 3D position for the vertex
    DWORD Color;     // The vertex color
FLOAT tu, tv;
FLOAT nx,ny,nz;
};

改为

struct CUSTOMVERTEX
{
FLOAT x, y, z ;      // The untransformed, 3D position for the vertex
FLOAT r,g,b,a;     // The vertex color
FLOAT tu, tv;
FLOAT nx,ny,nz;
};

并在初始化顶点数据时设定好值。
那么运行时可以看到法线和纹理对三角形的影响。但是色彩值不对。

182

主题

445

帖子

459

积分

中级会员

Rank: 3Rank: 3

积分
459
QQ
 楼主| 发表于 2005-11-17 18:42:00 | 显示全部楼层

Re: 一个基础的HLSL问题?请教高手

HRESULT InitGeometry()
{
就像这样。
    CUSTOMVERTEX g_Vertices[] =
    {
        { -1.0f,-1.0f, 0.0f,     1.0f,1.0f,1.0f,1.0f,  0.0f,1.0f ,-1.0f,-1.0f, 0.0f},
        {  1.0f,-1.0f, 0.0f, 1.0f,1.0f,1.0f,1.0f, 1.0f,1.0f ,1.0f,-1.0f, 0.0f},
        {  0.0f, 1.0f, 0.0f, 1.0f,1.0f,1.0f,1.0f,  0.5f,0.0f ,0.0f, 1.0f, 0.0f },
    };


我之前一直用DWORD,在纹理与色彩做混合时没问题。
为何一加法线运算。色彩就不对了呢。???????????

121

主题

2029

帖子

2034

积分

金牌会员

Rank: 6Rank: 6

积分
2034
QQ
发表于 2005-11-17 19:39:00 | 显示全部楼层

Re: 一个基础的HLSL问题?请教高手

先用猴子一类的试验一下吧。

65

主题

225

帖子

232

积分

中级会员

Rank: 3Rank: 3

积分
232
发表于 2005-11-18 12:35:00 | 显示全部楼层

Re: 一个基础的HLSL问题?请教高手

大哥,DWORD是32位的,你用float4变成了32*4=128位啦

182

主题

445

帖子

459

积分

中级会员

Rank: 3Rank: 3

积分
459
QQ
 楼主| 发表于 2005-11-18 12:53:00 | 显示全部楼层

Re: 一个基础的HLSL问题?请教高手

是啊。
但你看这个hlsl.fx
// -------------------------------------------------------------
// 全局变量
// -------------------------------------------------------------
float4x4 matWorldViewProj;       
float4x4 matWorld;       
float4 vecLightDir;
texture g_MeshTexture;  
// -------------------------------------------------------------
// 纹理过滤方式
// -------------------------------------------------------------
sampler TextureSampler =
sampler_state
{
    Texture = <g_MeshTexture>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};

// -------------------------------------------------------------
// 输出顶点信息结构
// -------------------------------------------------------------
struct VS_OUTPUT
{
    float4 Pos  :       POSITION;
    float4 Diffuse    : COLOR0;
    float2 TexUV :      TEXCOORD0;
};


// -------------------------------------------------------------
// 顶点渲染指令段
// -------------------------------------------------------------
VS_OUTPUT VS(float4 Pos : POSITION,float4 Dif : COLOR0, float2 TexUV: TEXCOORD0)
{
    VS_OUTPUT Out;      
    Out.Pos = mul(Pos, matWorldViewProj);        // transform Position
    Out.TexUV=TexUV;
    Out.Diffuse = Dif;
    return Out;
}


// -------------------------------------------------------------
// 像素渲染指令段
// -------------------------------------------------------------

struct PS_OUTPUT
{
    float4 RGBColor : COLOR0;  // Pixel color   
};

PS_OUTPUT PS(VS_OUTPUT In) : COLOR
{
    PS_OUTPUT Output;
    Output.RGBColor= tex2D(TextureSampler, In.TexUV) * In.Diffuse;
    return Output;
}
// -------------------------------------------------------------
// 管道效果指令段
// -------------------------------------------------------------
technique TShader
{
    pass P0
    {
        // 编译相应的指令段
        VertexShader = compile vs_1_1 VS();
        PixelShader  = compile ps_1_4 PS();
    }
}


//下面是源程序:
//-----------------------------------------------------------------------------
// File: Matrices.cpp
//
// Desc: Now that we know how to create a device and render some 2D vertices,
//       this tutorial goes the next step and renders 3D geometry. To deal with
//       3D geometry we need to introduce the use of 4x4 matrices to transform
//       the geometry with translations, rotations, scaling, and setting up our
//       camera.
//
//       Geometry is defined in model space. We can move it (translation),
//       rotate it (rotation), or stretch it (scaling) using a world transform.
//       The geometry is then said to be in world space. Next, we need to
//       position the camera, or eye point, somewhere to look at the geometry.
//       Another transform, via the view matrix, is used, to position and
//       rotate our view. With the geometry then in view space, our last
//       transform is the projection transform, which "projects" the 3D scene
//       into our 2D viewport.
//
//       Note that in this tutorial, we are introducing the use of D3DX, which
//       is a set of helper utilities for D3D. In this case, we are using some
//       of D3DX's useful matrix initialization functions. To use D3DX, simply
//       include <d3dx9.h> and link with d3dx9.lib.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#include <Windows.h>
#include <mmsystem.h>
#include <d3dx9.h>
#include <strsafe.h>




//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3D9             g_pD3D       = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9       g_pd3dDevice = NULL; // Our rendering device
LPDIRECT3DVERTEXBUFFER9 g_pVB        = NULL; // Buffer to hold vertices
LPD3DXEFFECT            g_pEffect    = NULL;
LPDIRECT3DTEXTURE9      g_pTexture   = NULL; // Our texture
D3DXVECTOR4             g_LightDir= D3DXVECTOR4( 0.0f, 0.0f, 1.0f, 1.0f );
// A structure for our custom vertex type
struct CUSTOMVERTEX
{
    FLOAT x, y, z;      // The untransformed, 3D position for the vertex
    DWORD color;        // The vertex color
        FLOAT tu, tv;
};

// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)




//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
    // Create the D3D object.
    if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
        return E_FAIL;

    // Set up the structure used to create the D3DDevice
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

    // Create the D3DDevice
    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &g_pd3dDevice ) ) )
    {
        return E_FAIL;
    }

    // Turn off culling, so we see the front and back of the triangle
    g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );

    // Turn off D3D lighting, since we are providing our own vertex colors
    g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );

        //创建HLSH
        if( FAILED(D3DXCreateEffectFromFile( g_pd3dDevice, "hlsl.fx", NULL, NULL,
                D3DXSHADER_DEBUG, NULL, &g_pEffect, NULL ) ) )
        {
        return E_FAIL;
        }
        if( g_pEffect != NULL ) g_pEffect->OnResetDevice();
                       
        //设定效果:因为只有一个,所以初始时就设了~
        D3DXHANDLE hTechnique = g_pEffect->GetTechniqueByName( "TShader" );
        g_pEffect->SetTechnique( hTechnique );

       
    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: InitGeometry()
// Desc: Creates the scene geometry
//-----------------------------------------------------------------------------
HRESULT InitGeometry()
{
    // Initialize three vertices for rendering a triangle
    CUSTOMVERTEX g_Vertices[] =
    {
        { -1.0f,-1.0f, 0.0f, 0x00ff00ff,0.0f,1.0f },
        {  1.0f,-1.0f, 0.0f, 0xff0000ff,1.0f,1.0f },
        {  0.0f, 1.0f, 0.0f, 0xff00ffff,0.5f,0.0f },
    };

    // Create the vertex buffer.
    if( FAILED( g_pd3dDevice->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX),
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
    {
        return E_FAIL;
    }

    // Fill the vertex buffer.
    VOID* pVertices;
    if( FAILED( g_pVB->Lock( 0, sizeof(g_Vertices), (void**)&pVertices, 0 ) ) )
        return E_FAIL;
    memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
    g_pVB->Unlock();

        if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "banana.bmp", &g_pTexture ) ) )
    {
        // If texture is not in current folder, try parent folder
        if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "..\\banana.bmp", &g_pTexture ) ) )
        {
            MessageBox(NULL, "Could not find banana.bmp", "Textures.exe", MB_OK);
            return E_FAIL;
        }
    }

        g_pEffect->SetTexture( "g_MeshTexture", g_pTexture );
    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
    if( g_pTexture != NULL )
        g_pTexture->Release();

    if( g_pEffect != NULL )        
        g_pEffect->Release();

    if( g_pVB != NULL )
        g_pVB->Release();

    if( g_pd3dDevice != NULL )
        g_pd3dDevice->Release();

    if( g_pD3D != NULL )
        g_pD3D->Release();
}



//-----------------------------------------------------------------------------
// Name: SetupMatrices()
// Desc: Sets up the world, view, and projection transform matrices.
//-----------------------------------------------------------------------------
VOID SetupMatrices()
{
    // For our world matrix, we will just rotate the object about the y-axis.
    D3DXMATRIXA16 matWorld;

    UINT  iTime  = timeGetTime() % 1000;
    FLOAT fAngle = iTime * (2.0f * D3DX_PI) / 1000.0f;
    D3DXMatrixRotationY( &matWorld, fAngle );

    D3DXVECTOR3 vEyePt( 0.0f, 3.0f,-5.0f );
    D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
    D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
    D3DXMATRIXA16 matView;
    D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );

    D3DXMATRIXA16 matProj;
    D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );

        //设定VS矩阵
        D3DXMATRIX mWorldViewProj = matWorld * matView * matProj;
        g_pEffect->SetMatrix( "matWorldViewProj", &mWorldViewProj );

}



//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
    // Clear the backbuffer to a black color
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );

    // Begin the scene
    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
    {
        // Setup the world, view, and projection matrices
        SetupMatrices();

        // Render the vertex buffer contents
        g_pd3dDevice->SetTexture( 0, g_pTexture );
        g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
        g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
        g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
        g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );

                //按VS,PS渲染
                if( g_pEffect != NULL )
                {
                        UINT nPasses=0;
                        UINT iPass=0;
                        if(SUCCEEDED(g_pEffect->Begin( &nPasses, 0 )))
                        {
                                for(iPass = 0; iPass < nPasses; iPass ++ )
                                {
                                        g_pEffect->BeginPass( 0 );
                                }
                                g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
                                g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
                                g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 1 );
                                g_pEffect->EndPass();
                                g_pEffect->End();
                        }
                }
        // End the scene
        g_pd3dDevice->EndScene();
    }

    // Present the backbuffer contents to the display
    g_pd3dDevice-&gtresent( NULL, NULL, NULL, NULL );
}




//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
    {
        case WM_DESTROY:
            Cleanup();
            PostQuitMessage( 0 );
            return 0;
    }

    return DefWindowProc( hWnd, msg, wParam, lParam );
}




//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
    // Register the window class
    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
                      GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                      "D3D Tutorial", NULL };
    RegisterClassEx( &wc );

    // Create the application's window
    HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 03: Matrices",
                              WS_OVERLAPPEDWINDOW, 100, 100, 256, 256,
                              GetDesktopWindow(), NULL, wc.hInstance, NULL );

    // Initialize Direct3D
    if( SUCCEEDED( InitD3D( hWnd ) ) )
    {
        // Create the scene geometry
        if( SUCCEEDED( InitGeometry() ) )
        {
            // Show the window
            ShowWindow( hWnd, SW_SHOWDEFAULT );
            UpdateWindow( hWnd );

            // Enter the message loop
            MSG msg;
            ZeroMemory( &msg, sizeof(msg) );
            while( msg.message!=WM_QUIT )
            {
                if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
                {
                    TranslateMessage( &msg );
                    DispatchMessage( &msg );
                }
                else
                    Render();
            }
        }
    }

    UnregisterClass( "D3D Tutorial", wc.hInstance );
    return 0;
}



这个却能正常显示。真的。不信你运行一下。
我也纳闷。
为什么DWORD却可以正常传入float4中呢?

182

主题

445

帖子

459

积分

中级会员

Rank: 3Rank: 3

积分
459
QQ
 楼主| 发表于 2005-11-18 12:55:00 | 显示全部楼层

Re: 一个基础的HLSL问题?请教高手

我正在研究。尝试用MONKEY。有结果了再来说。高手有懂的。说一下啊
~谢谢

65

主题

518

帖子

521

积分

高级会员

Rank: 4

积分
521
发表于 2005-11-19 06:19:00 | 显示全部楼层

Re: 一个基础的HLSL问题?请教高手

就像前面说的那样,颜色在进入gpu之前是DWORD。VS和PS里的数据是128位的,一旦输出到frame buffer就有缩回32位了。

182

主题

445

帖子

459

积分

中级会员

Rank: 3Rank: 3

积分
459
QQ
 楼主| 发表于 2005-11-20 14:40:00 | 显示全部楼层

Re: 一个基础的HLSL问题?请教高手

OK~THANK YOU!

121

主题

2029

帖子

2034

积分

金牌会员

Rank: 6Rank: 6

积分
2034
QQ
发表于 2005-11-20 16:16:00 | 显示全部楼层

Re: 一个基础的HLSL问题?请教高手

少贴点代码。。。我滚轮都累死了。。。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-1-22 18:17

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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