游戏开发论坛

 找回密码
 立即注册
搜索
查看: 4080|回复: 6

一个vertex shader的问题

[复制链接]

2

主题

6

帖子

14

积分

新手上路

Rank: 1

积分
14
发表于 2004-5-11 09:12:00 | 显示全部楼层 |阅读模式
最近开始学vertex shader,根据ms提供的帮助文档(Create a Vertex Shader )写了如下一个程序,可死活出不了锁要求的效果,
一个矩形,

#define STRICT
#include <d3d9.h>
#include <windows.h>
#include <windowsx.h>
#include <mmsystem.h>
#include <stdio.h>
#include <tchar.h>
#include <D3DX9.h>
#include "DXUtil.h"
#include "D3DEnumeration.h"
#include "D3DSettings.h"
#include "D3DApp.h"
#include "D3DFile.h"
#include "D3DUtil.h"
#include <commctrl.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
LPDIRECT3DVERTEXSHADER9 m_pVertexShader;

LPD3DXBUFFER pCode;                  // Buffer with the assembled shader code
LPD3DXBUFFER pErrorMsgs;
// A structure for our custom vertex type
struct CUSTOMVERTEX
{
    FLOAT x, y, z;
};
#define  D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ)

//-----------------------------------------------------------------------------
// 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;
    d3dpp.EnableAutoDepthStencil = TRUE;
        d3dpp.MultiSampleType = D3DMULTISAMPLE_2_SAMPLES;
       
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

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

    // Device state would normally be set here

    return S_OK;
}

HRESULT InitVB()
{
    // Initialize three vertices for rendering a triangle
        CUSTOMVERTEX vertices[]=
        {
                //  x        y      z   
                { -1.0f, -1.0f, 0.0f },
                { +1.0f, -1.0f, 0.0f },
                { +1.0f, +1.0f, 0.0f },
                { -1.0f, +1.0f, 0.0f },
        };
       
        HRESULT hr;
                     
        D3DVERTEXELEMENT9 decl[] =
        {
                { 0, 0,  D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
                        D3DDECL_END()
        };
       
        D3DXAssembleShaderFromFile("../geometry/VertexShader.vsh", NULL, NULL, 0,  
                &pCode, &pErrorMsgs);   
       
        hr = g_pd3dDevice->CreateVertexShader( (DWORD*)pCode->GetBufferPointer(),
                &m_pVertexShader);
        pCode->Release();
       


    if( FAILED( hr = g_pd3dDevice->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX),
                0, D3DFVF_CUSTOMVERTEX,
                D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
    {
        return E_FAIL;
    }

    VOID* pVertices;
    if( FAILED( g_pVB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) )
                return E_FAIL;
        memcpy( pVertices, vertices, sizeof(vertices) );
        g_pVB->Unlock();
   
       
    return S_OK;
}





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

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

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




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

    // Begin the scene
    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
    {


                HRESULT hr;

                D3DXVECTOR3 vEyePt( 0.0f,   2.0f, 4.9f);
                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,mat,mat1;
                D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 3.0f, 0.1f, 10.0f );

               
                D3DXMatrixMultiply(&mat,&matView,&matProj);


                hr = g_pd3dDevice->SetVertexShaderConstantF( 0, (float*)&mat, 4 );
               
               
               
                float color[4] = {1.0,1.0,1.0,0};
                hr = g_pd3dDevice->SetVertexShaderConstantF( 4, (float*)&color, 1 );
               
               
               
                // Declare and define the constant vertex color.

               
                // Turn off specular light contribution because the specular value
                //   is not written in the vertex shader.
                g_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, FALSE );
               
                // Render the output.
                hr = g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
                hr = g_pd3dDevice->SetVertexShader( m_pVertexShader );
                g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX );
   
                hr = g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 2 );
               

        // 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 02: Vertices",
                              WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
                              GetDesktopWindow(), NULL, wc.hInstance, NULL );

    // Initialize Direct3D
    if( SUCCEEDED( InitD3D( hWnd ) ) )
    {
        // Create the vertex buffer
        if( SUCCEEDED( InitVB() ) )
        {
            // 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;
}

shader files


vs_1_1               // version instruction
dcl_position v0      // declare position register
m4x4 oPos, v0, c0    // transform vertices by view/projection matrix
mov oD0, c4          // move diffuse color to output color register
[em17] [em17] [em17]

3

主题

86

帖子

94

积分

注册会员

Rank: 2

积分
94
QQ
发表于 2004-5-11 10:35:00 | 显示全部楼层

Re:一个vertex shader的问题

hr = g_pd3dDevice->SetVertexShaderConstantF( 0, (float*)&mat, 4 );
这句之前,mat转置试试

建议在顶点格式中加上diffuse,看看fixed pipeline能否正常显示

因为不知道到底实际显示的情况如何,只能随便提提了

27

主题

169

帖子

169

积分

注册会员

Rank: 2

积分
169
发表于 2004-5-11 11:18:00 | 显示全部楼层

Re:一个vertex shader的问题

首先强调一点,这个程序有太多需要改进的地方,但本着是对于新手学习而言所以没有做过多的更改,是在源代码的基础上能少改一点是一点,但结果保证是对的

1>删除d3dpp.EnableAutoDepthStencil = TRUE;
        d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

2>CUSTOMVERTEX vertices[]=
{
//  x        y      z   
{ -1.0f, -1.0f, 0.0f },
{ -1.0f, +1.0f, 0.0f },
{ +1.0f, 1.0f, 0.0f },

{1.0f,-1.0f,0.0f },
};

3>D3DXVECTOR3 vEyePt( 0.0f,   2.0f, -4.9f);

27

主题

169

帖子

169

积分

注册会员

Rank: 2

积分
169
发表于 2004-5-11 11:26:00 | 显示全部楼层

Re: 一个vertex shader的问题

源码

sf_2004511112625.rar

119.46 KB, 下载次数:

6

主题

444

帖子

457

积分

中级会员

Rank: 3Rank: 3

积分
457
发表于 2004-5-11 16:37:00 | 显示全部楼层

Re:一个vertex shader的问题

倒,几个月前就有相似的问题,怎么又来了?
首先,既然是VertexShader,那么SetFVF干吗?
定义了D3DVERTEXELEMENT9 decl[] 却不把它编译为VertexDeclaration(用CreateVertexDeclaration)且也不用SetVertexDeclaration来调用,那么你定义它干什么?哎……
再解释一下吧,SetFVF是Fixed Function Pipeline专用的,它在定义了顶点元素在顶点流中的偏移的同时也定义了顶点元素的处理方式,也就是说它同时定义了格式和方法。作为Programmable pipeline来说就不需要使用SetFVF,而应该使用SetVertexDeclaration来顶以顶点数据在顶点流中的位置和属性,用SetVertexShader来定义顶点元素处理方式,这样才是正确的。
哎,学习要扎实,至少要明白程序的每句都是怎么回事,不是光从哪里抄了点代码就能学会的,要善于思考。发现问题(比如这个程序里的decl[]完全没用到就是非常明显的问题啊),初学者更需要这样,否则将来会吃苦头的。

46

主题

238

帖子

238

积分

中级会员

Rank: 3Rank: 3

积分
238
发表于 2004-5-11 20:58:00 | 显示全部楼层

Re:一个vertex shader的问题

我也问过同样的问题,看看以前的帖子把。

2

主题

6

帖子

14

积分

新手上路

Rank: 1

积分
14
 楼主| 发表于 2004-5-12 20:39:00 | 显示全部楼层

Re:一个vertex shader的问题

实在惭愧,
不过,dx的帮助文档有些说的不是很清楚,而且里面有一些错误。
大家都是看什么资料的。能不能推荐一下,多谢了!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-7-1 00:09

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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