gpt4 book ai didi

directx - 关于在 C++ 中的 DirectX9 中使用的 .fx 文件和着色器的一般混淆 - 您究竟如何与应用程序建立连接?

转载 作者:行者123 更新时间:2023-12-01 12:57:09 30 4
gpt4 key购买 nike

基本上,我不太确定如何在 DX 中正确使用 Set 和 Get Parameter 方法来使用 .fx 文件。我的意思是我在任何地方都找不到好的教程。我什至有一本关于 D3D9 的书虽然我得到了其中的大部分,但我仍然无法使用效果文件。更糟糕的是,微软提供的 DirectX 示例中包含了一些微软的 DX 实用程序类以及各种其他不必要的复杂功能,我无法完全理解它通过 2k 行代码。我的意思是我得到了基本的想法(加载,开始,通过传递循环,结束),但是任何人都可以向我指出一些关于一些简单示例的好教程。主要的事情我没有了解如何使用效果参数:(

最佳答案

这是我第一次学习如何在 DirectX9 中使用 HLSL 着色器时写回的引用表。也许它会有所帮助。

在应用程序中:
声明需要的变量:

ID3DXEffect*                shader;


加载 .fx 文件:

D3DXCreateEffectFromFile(   d3dDevice,
_T("filepath.fx"),
0,
0,
0,
0,
&shader,
0
);


清理效果对象(有些人使用 SAFE_RELEASE 宏):

if(shader)
shader->Release();
shader = nullptr;


使用着色器渲染一些东西:

void Application::Render()
{
unsigned passes = 0;
shader->Begin(&passes,0);
for(unsigned i=0;i<passes;++i)
{
shader->BeginPass(i);

// Set uniforms
shader->SetMatrix("gWorld",&theMatrix);
shader->CommitChanges(); // Not necessary if SetWhatevers are done OUTSIDE of a BeginPass/EndPass pair.

/* Insert rendering instructions here */
// BEGIN EXAMPLE:

d3dDevice->SetVertexDeclaration(vertexDecl);
d3dDevice->SetStreamSource(0,vertexBuffer,0,sizeof(VERT));

d3dDevice->SetIndices(indexBuffer);

d3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,numVerts,0,8);

// END EXAMPLE

shader->EndPass();
}
shader->End();
}


在 .FX 文件中:
声明制服(您要在应用程序中设置的变量):

float4x4    gWorld      : WORLD;
float4x4 gViewProj : VIEWPROJECTION;
float gTime : TIME;

Texture2D gDiffuseTexture; // requires a matching sampler
sampler gDiffuseSampler = sampler_state // here's the matching sampler
{
Texture = <gDiffuseTexture>;
FILTER = MIN_MAG_MIP_LINEAR;
AddressU = Wrap;
AddressV = Wrap;
};


定义顶点着色器输入结构:

struct VS_INPUT     // make this match the vertex structure in Application
{
float3 untransformed_pos : POSITION0;
float3 untransformed_nrm : NORMAL0;
float4 color : COLOR0;
float2 uv_coords : TEXCOORD0;
};


定义像素着色器输入结构(顶点着色器输出):

struct PS_INPUT
{
float4 transformed_pos : POSITION0;
float4 transformed_nrm : NORMAL0;
float4 color : COLOR0;
float2 uv_coords : TEXCOORD0;
};


定义顶点着色器:

PS_INPUT    mainVS  (VS_INPUT input)
{
PS_INPUT output;

/* Insert shader instructions here */

return output;
}


定义像素着色器:

float4      mainPS  (PS_INPUT input)    : COLOR
{
/* Insert shader instructions here */

return float4(resultColor,1.0f);
}


定义一个技术:

technique myTechnique
{
// Here is a quick sample
pass FirstPass
{
vertexShader = compile vs_3_0 mainVS();
pixelShader = compile ps_3_0 mainPS();

// Setting a few of the many D3D renderstates via the effect framework
ShadeMode = FLAT; // flat color interpolation across triangles
FillMode = SOLID; // no wireframes, no point drawing.
CullMode = CCW; // cull any counter-clockwise polygons.
}
}

关于directx - 关于在 C++ 中的 DirectX9 中使用的 .fx 文件和着色器的一般混淆 - 您究竟如何与应用程序建立连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9085570/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com