0

我正在尝试设置一些 DX9 代码来渲染着色器,因为我希望更深入地研究它们。我有一个非常基本的着色器,可以在 FX Composer 中很好地编译和显示,但是在设置我的代码以在我的程序中渲染后,由于某种原因纹理不显示,网格只是深灰色。我认为这可能与 DirectX 处理网格的方式有关,但我不太确定。这是相关的代码片段,一如既往,提前感谢!

.fx 文件

float4x4 world_m    : WORLD;
float4x4 view_m     : VIEW;
float4x4 projection_m   : PROJECTION;

Texture gWorldTexture;

sampler2D image = 
sampler_state
{
    Texture = <gWorldTexture>;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
    MipFilter = LINEAR;
    AddressU = CLAMP;
    AddressV = CLAMP;
};

struct VertexIn
{
 float4 pos     : POSITION;
 float2 texco    : TEXCOORD;
};

struct VertexOut
{
 float4 pos     : POSITION;
 float2 texco       : TEXCOORD;
};

struct pixelInput
{   
 float2 texco    : TEXCOORD0;
};

VertexOut  mainVS(VertexIn input)
{
 VertexOut output = (VertexOut)0;
 float4x4 worldview_m       = mul(world_m, view_m);
 float4x4 worldViewProj_m   = mul(worldview_m, projection_m);
 output.pos = mul( input.pos, worldViewProj_m );
 output.texco = input.texco;
 return output;
}

float4 mainPS(pixelInput input) : COLOR0
{
 return tex2D( image, input.texco );
}

technique technique0 
{
 pass p0 
 {
     VertexShader = compile vs_2_0 mainVS();
     PixelShader = compile ps_2_0 mainPS();
 }
}

声明

LPD3DXMESH meshSphere;
IDirect3DTexture9 *texture;
LPD3DXEFFECT effect;
D3DXHANDLE gWorldTexture;
D3DXHANDLE technique;

gfx_init 函数

LPD3DXBUFFER errorlog;
D3DXCreateTextureFromFile(d3ddev, "earth.png", &texture);    

D3DXCreateEffectFromFile(d3ddev, "dxtshader.fx", 0, 0,     D3DXSHADER_ENABLE_BACKWARDS_COMPATIBILITY, 0, &effect, &errorlog);

D3DXCreateSphere(d3ddev, 5.0f, 25, 25, &meshSphere, NULL);

渲染帧函数

void render_frame(void)
{
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

    d3ddev->BeginScene();

    D3DXMATRIX matView;
    D3DXMatrixLookAtLH(&matView,
    &D3DXVECTOR3 (0.0f, 3.0f, 15.0f),
    &D3DXVECTOR3 (0.0f, 0.0f, 0.0f),
    &D3DXVECTOR3 (0.0f, 1.0f, 0.0f));
    effect->SetMatrix("view_m", &matView);

    D3DXMATRIX matProjection;
    D3DXMatrixPerspectiveFovLH(&matProjection,
                               D3DXToRadian(45),
                               (FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT,
                               1.0f,
                               100.0f);
    effect->SetMatrix("projection_m", &matProjection);

    static float index = 0.0f; index+=0.03f;
    D3DXMATRIX matRotateY;
    D3DXMatrixRotationY(&matRotateY, index);
    effect->SetMatrix("world_m", &matRotateY);

    effect->Begin(NULL, NULL);
        effect->BeginPass(0);

        effect->SetTexture("gWorldTexture", texture);
            meshSphere->DrawSubset(0);

        effect->EndPass();
    effect->End();

    d3ddev->EndScene(); 

    d3ddev->Present(NULL, NULL, NULL, NULL);

    return;
}
4

2 回答 2

1

下面是一些代码来生成一个带有纹理坐标的球体:http: //pastebin.com/GZV8mbbC,通过这篇文章找到:http ://www.gamedev.net/topic/629242-d3dxcreatesphere-and-texture/

我认为纹理坐标很可能是您的问题。

于 2013-02-05T09:04:57.163 回答
1

确实根本没有提供文本坐标,没有任何类型的顶点声明来提供有关顶点数据的信息(例如,您将文本坐标带到顶点着色器:))

于 2013-02-05T09:26:00.250 回答