将纹理传递给网格对象的D3D9效果

时间:2013-02-05 03:19:31

标签: c++ directx textures hlsl mesh

我正在尝试设置一些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);

render_frame function

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;
}

2 个答案:

答案 0 :(得分:1)

以下是一些生成带纹理坐标的球体的代码:http://pastebin.com/GZV8mbbC,通过以下帖子找到:http://www.gamedev.net/topic/629242-d3dxcreatesphere-and-texture/

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

答案 1 :(得分:1)

根本没有提供textcoords,没有任何顶点声明可以提供关于顶点数据的信息(例如,你将文本坐标带到顶点着色器:) :)