- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试弄清楚我的 HLSL 代码发生了什么,但我无法调试它,因为 C++ 没有给出任何错误。该应用程序在我运行时就关闭了。我正在尝试为我制作的 3d 平面添加照明。下面是我的 HLSL。当我的像素着色器方法返回结构 "outColor"时出现问题。如果我将返回值更改回结构“psInput”,一切都会恢复正常。我的光 vector 和颜色在 fx 文件的顶部
// PS_INPUT - input variables to the pixel shader
// This struct is created and fill in by the
// vertex shader
cbuffer Variables
{
matrix Projection;
matrix World;
float TimeStep;
};
struct PS_INPUT
{
float4 Pos : SV_POSITION;
float4 Color : COLOR0;
float3 Normal : TEXCOORD0;
float3 ViewVector : TEXCOORD1;
};
float specpower = 80.0f;
float3 camPos = float3(0.0f, 9.0, -256.0f);
float3 DirectLightColor = float3(1.0f, 1.0f, 1.0f);
float3 DirectLightVector = float3(0.0f, 0.602f, 0.70f);
float3 AmbientLightColor = float3(1.0f, 1.0f, 1.0f);
/***************************************
* Lighting functions
***************************************/
/*********************************
* CalculateAmbient -
* inputs -
* vKa material's reflective color
* lightColor - the ambient color of the lightsource
* output - ambient color
*********************************/
float3 CalculateAmbient(float3 vKa, float3 lightColor)
{
float3 vAmbient = vKa * lightColor;
return vAmbient;
}
/*********************************
* CalculateDiffuse -
* inputs -
* material color
* The color of the direct light
* the local normal
* the vector of the direct light
* output - difuse color
*********************************/
float3 CalculateDiffuse(float3 baseColor, float3 lightColor, float3 normal, float3 lightVector)
{
float3 vDiffuse = baseColor * lightColor * saturate(dot(normal, lightVector));
return vDiffuse;
}
/*********************************
* CalculateSpecular -
* inputs -
* viewVector
* the direct light vector
* the normal
* output - specular highlight
*********************************/
float CalculateSpecular(float3 viewVector, float3 lightVector, float3 normal)
{
float3 vReflect = reflect(lightVector, normal);
float fSpecular = saturate(dot(vReflect, viewVector));
fSpecular = pow(fSpecular, specpower);
return fSpecular;
}
/*********************************
* LightingCombine -
* inputs -
* ambient component
* diffuse component
* specualr component
* output - phong color color
*********************************/
float3 LightingCombine(float3 vAmbient, float3 vDiffuse, float fSpecular)
{
float3 vCombined = vAmbient + vDiffuse + fSpecular.xxx;
return vCombined;
}
////////////////////////////////////////////////
// Vertex Shader - Main Function
///////////////////////////////////////////////
PS_INPUT VS(float4 Pos : POSITION, float4 Color : COLOR, float3 Normal : NORMAL)
{
PS_INPUT psInput;
float4 newPosition;
newPosition = Pos;
newPosition.y = sin((newPosition.x * TimeStep) + (newPosition.z / 3.0f)) * 5.0f;
// Pass through both the position and the color
psInput.Pos = mul(newPosition , Projection );
psInput.Color = Color;
psInput.ViewVector = normalize(camPos - psInput.Pos);
return psInput;
}
///////////////////////////////////////////////
// Pixel Shader
///////////////////////////////////////////////
//Anthony!!!!!!!!!!! Find out how color works when multiplying them
float4 PS(PS_INPUT psInput) : SV_Target
{
float3 normal = -normalize(psInput.Normal);
float3 vAmbient = CalculateAmbient(psInput.Color, AmbientLightColor);
float3 vDiffuse = CalculateDiffuse(psInput.Color, DirectLightColor, normal, DirectLightVector);
float fSpecular = CalculateSpecular(psInput.ViewVector, DirectLightVector, normal);
float4 outColor;
outColor.rgb = LightingCombine(vAmbient, vDiffuse, fSpecular);
outColor.a = 1.0f;
//Below is where the error begins
return outColor;
}
// Define the technique
technique10 Render
{
pass P0
{
SetVertexShader( CompileShader( vs_4_0, VS() ) );
SetGeometryShader( NULL );
SetPixelShader( CompileShader( ps_4_0, PS() ) );
}
}
下面是我的一些 C++ 代码。我展示这个的原因是因为它几乎是创建表面法线供我的着色器评估的原因。用于照明
modelObject.numIndices = sizeof(indices) / sizeof(DWORD);
// compute normals for each face in the model
for (unsigned int i = 0; i < modelObject.numIndices; i+=3)
{
D3DXVECTOR3 v0 = vertices[indices[i]].pos;
D3DXVECTOR3 v1 = vertices[indices[i + 1]].pos;
D3DXVECTOR3 v2 = vertices[indices[i + 2]].pos;
D3DXVECTOR3 normal;
D3DXVECTOR3 cross;
D3DXVec3Cross(&cross, &D3DXVECTOR3(v2 - v0), &D3DXVECTOR3(v1 - v0));
D3DXVec3Normalize(&normal, &cross);
// assign the computed normal to each vertex in this face
vertices[indices[i]].normal = normal;
vertices[indices[i + 1]].normal = normal;
vertices[indices[i + 2]].normal = normal;
}
以下是我的完整 C++ 代码。展示图纸并要求通行证
#include "MyGame.h"
typedef struct
{
ID3D10Effect* pEffect;
ID3D10EffectTechnique* pTechnique;
//vertex information
ID3D10Buffer* pVertexBuffer;
ID3D10Buffer* pIndicesBuffer;
ID3D10InputLayout* pVertexLayout;
UINT numVertices;
UINT numIndices;
}ModelObject;
ModelObject modelObject;
// World Matrix
D3DXMATRIX WorldMatrix;
// View Matrix
D3DXMATRIX ViewMatrix;
// Projection Matrix
D3DXMATRIX ProjectionMatrix;
ID3D10EffectMatrixVariable* pProjectionMatrixVariable = NULL;
//grid information
#define NUM_COLS 16
#define NUM_ROWS 16
#define CELL_WIDTH 32
#define CELL_HEIGHT 32
#define NUM_VERTSX (NUM_COLS + 1)
#define NUM_VERTSY (NUM_ROWS + 1)
// timer variables
LARGE_INTEGER timeStart;
LARGE_INTEGER timeEnd;
LARGE_INTEGER timerFreq;
double currentTime;
float anim_rate;
// Variable to hold how long since last frame change
float lastElaspedFrame = 0;
// How long should the frames last
float frameDuration = 0.5;
bool MyGame::InitDirect3D()
{
if(!DX3dApp::InitDirect3D())
{
return false;
}
// Get the timer frequency
QueryPerformanceFrequency(&timerFreq);
float freqSeconds = 1.0f / timerFreq.QuadPart;
lastElaspedFrame = 0;
D3D10_RASTERIZER_DESC rastDesc;
rastDesc.FillMode = D3D10_FILL_WIREFRAME;
rastDesc.CullMode = D3D10_CULL_FRONT;
rastDesc.FrontCounterClockwise = true;
rastDesc.DepthBias = false;
rastDesc.DepthBiasClamp = 0;
rastDesc.SlopeScaledDepthBias = 0;
rastDesc.DepthClipEnable = false;
rastDesc.ScissorEnable = false;
rastDesc.MultisampleEnable = false;
rastDesc.AntialiasedLineEnable = false;
ID3D10RasterizerState *g_pRasterizerState;
mpD3DDevice->CreateRasterizerState(&rastDesc, &g_pRasterizerState);
//mpD3DDevice->RSSetState(g_pRasterizerState);
// Set up the World Matrix
D3DXMatrixIdentity(&WorldMatrix);
D3DXMatrixLookAtLH(&ViewMatrix, new D3DXVECTOR3(200.0f, 60.0f, -20.0f), new D3DXVECTOR3(200.0f, 50.0f, 0.0f), new D3DXVECTOR3(0.0f, 1.0f, 0.0f));
// Set up the projection matrix
D3DXMatrixPerspectiveFovLH(&ProjectionMatrix, (float)D3DX_PI * 0.5f, (float)mWidth/(float)mHeight, 0.1f, 100.0f);
pTimeVariable = NULL;
if(!CreateObject())
{
return false;
}
return true;
}
//These are actions that take place after the clearing of the buffer and before the present
void MyGame::GameDraw()
{
static float rotationAngle = 0.0f;
// create the rotation matrix using the rotation angle
D3DXMatrixRotationY(&WorldMatrix, rotationAngle);
rotationAngle += (float)D3DX_PI * 0.0f;
// Set the input layout
mpD3DDevice->IASetInputLayout(modelObject.pVertexLayout);
// Set vertex buffer
UINT stride = sizeof(VertexPos);
UINT offset = 0;
mpD3DDevice->IASetVertexBuffers(0, 1, &modelObject.pVertexBuffer, &stride, &offset);
mpD3DDevice->IASetIndexBuffer(modelObject.pIndicesBuffer, DXGI_FORMAT_R32_UINT, 0);
pTimeVariable->SetFloat((float)currentTime);
// Set primitive topology
mpD3DDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
// Combine and send the final matrix to the shader
D3DXMATRIX finalMatrix = (WorldMatrix * ViewMatrix * ProjectionMatrix);
pProjectionMatrixVariable->SetMatrix((float*)&finalMatrix);
// make sure modelObject is valid
// Render a model object
D3D10_TECHNIQUE_DESC techniqueDescription;
modelObject.pTechnique->GetDesc(&techniqueDescription);
// Loop through the technique passes
for(UINT p=0; p < techniqueDescription.Passes; ++p)
{
modelObject.pTechnique->GetPassByIndex(p)->Apply(0);
// draw the cube using all 36 vertices and 12 triangles
mpD3DDevice->DrawIndexed(modelObject.numIndices,0,0);
}
}
//Render actually incapsulates Gamedraw, so you can call data before you actually clear the buffer or after you
//present data
void MyGame::Render()
{
// Get the start timer count
QueryPerformanceCounter(&timeStart);
currentTime += anim_rate;
DX3dApp::Render();
QueryPerformanceCounter(&timeEnd);
anim_rate = ( (float)timeEnd.QuadPart - (float)timeStart.QuadPart ) / timerFreq.QuadPart;
}
bool MyGame::CreateObject()
{
VertexPos vertices[NUM_VERTSX * NUM_VERTSY];
for(int z=0; z < NUM_VERTSY; ++z)
{
for(int x = 0; x < NUM_VERTSX; ++x)
{
vertices[x + z * NUM_VERTSX].pos.x = (float)x * CELL_WIDTH;
vertices[x + z * NUM_VERTSX].pos.z = (float)z * CELL_HEIGHT;
vertices[x + z * NUM_VERTSX].pos.y = (float)(rand() % CELL_HEIGHT);
vertices[x + z * NUM_VERTSX].color = D3DXVECTOR4(1.0, 0.0f, 0.0f, 0.0f);
}
}
DWORD indices[NUM_COLS * NUM_ROWS * 6];
int curIndex = 0;
for(int z=0; z < NUM_ROWS; ++z)
{
for(int x = 0; x < NUM_COLS; ++x)
{
int curVertex = x + (z * NUM_VERTSX);
indices[curIndex] = curVertex;
indices[curIndex + 1] = curVertex + NUM_VERTSX;
indices[curIndex + 2] = curVertex + 1;
indices[curIndex + 3] = curVertex + 1;
indices[curIndex + 4] = curVertex + NUM_VERTSX;
indices[curIndex + 5] = curVertex + NUM_VERTSX + 1;
curIndex += 6;
}
}
modelObject.numIndices = sizeof(indices) / sizeof(DWORD);
// compute normals for each face in the model
for (unsigned int i = 0; i < modelObject.numIndices; i+=3)
{
D3DXVECTOR3 v0 = vertices[indices[i]].pos;
D3DXVECTOR3 v1 = vertices[indices[i + 1]].pos;
D3DXVECTOR3 v2 = vertices[indices[i + 2]].pos;
D3DXVECTOR3 normal;
D3DXVECTOR3 cross;
D3DXVec3Cross(&cross, &D3DXVECTOR3(v2 - v0), &D3DXVECTOR3(v1 - v0));
D3DXVec3Normalize(&normal, &cross);
// assign the computed normal to each vertex in this face
vertices[indices[i]].normal = normal;
vertices[indices[i + 1]].normal = normal;
vertices[indices[i + 2]].normal = normal;
}
//Create Layout
D3D10_INPUT_ELEMENT_DESC layout[] = {
{"POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT, 0 , 0, D3D10_INPUT_PER_VERTEX_DATA, 0},
{"COLOR",0,DXGI_FORMAT_R32G32B32A32_FLOAT, 0 , 12, D3D10_INPUT_PER_VERTEX_DATA, 0},
{"NORMAL",0,DXGI_FORMAT_R32G32B32A32_FLOAT, 0 , 28, D3D10_INPUT_PER_VERTEX_DATA, 0}
};
UINT numElements = (sizeof(layout)/sizeof(layout[0]));
modelObject.numVertices = sizeof(vertices)/sizeof(VertexPos);
//Create buffer desc
D3D10_BUFFER_DESC bufferDesc;
bufferDesc.Usage = D3D10_USAGE_DEFAULT;
bufferDesc.ByteWidth = sizeof(VertexPos) * modelObject.numVertices;
bufferDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA initData;
initData.pSysMem = vertices;
//Create the buffer
HRESULT hr = mpD3DDevice->CreateBuffer(&bufferDesc, &initData, &modelObject.pVertexBuffer);
if(FAILED(hr))
return false;
bufferDesc.ByteWidth = sizeof(DWORD) * modelObject.numIndices;
bufferDesc.BindFlags = D3D10_BIND_INDEX_BUFFER;
initData.pSysMem = indices;
hr = mpD3DDevice->CreateBuffer(&bufferDesc, &initData, &modelObject.pIndicesBuffer);
if(FAILED(hr))
return false;
/////////////////////////////////////////////////////////////////////////////
//Set up fx files
LPCWSTR effectFilename = L"effect.fx";
modelObject.pEffect = NULL;
hr = D3DX10CreateEffectFromFile(effectFilename,
NULL,
NULL,
"fx_4_0",
D3D10_SHADER_ENABLE_STRICTNESS,
0,
mpD3DDevice,
NULL,
NULL,
&modelObject.pEffect,
NULL,
NULL);
if(FAILED(hr))
return false;
pProjectionMatrixVariable = modelObject.pEffect->GetVariableByName("Projection")->AsMatrix();
pTimeVariable = modelObject.pEffect->GetVariableByName("TimeStep")->AsScalar();
//Dont sweat the technique. Get it!
LPCSTR effectTechniqueName = "Render";
modelObject.pTechnique = modelObject.pEffect->GetTechniqueByName(effectTechniqueName);
if(modelObject.pTechnique == NULL)
return false;
//Create Vertex layout
D3D10_PASS_DESC passDesc;
modelObject.pTechnique->GetPassByIndex(0)->GetDesc(&passDesc);
hr = mpD3DDevice->CreateInputLayout(layout, numElements,
passDesc.pIAInputSignature,
passDesc.IAInputSignatureSize,
&modelObject.pVertexLayout);
if(FAILED(hr))
return false;
return true;
}
最佳答案
有一个应用程序,一个随 DirectX 一起提供的可执行文件,fxc.exe。您可以使用它来消除着色器中的语法错误!
关于c++ - HLSL 和光照问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2796293/
在 HLSL/Directx11 中工作时,我看到有两种绑定(bind) 3D 渲染目标的方法:绑定(bind)整个目标或在指定图层时绑定(bind)它。 如果绑定(bind)整个目标,如何在 HLS
我正在将一些 OpenCL 代码转换为 DirectCompute,需要在计算着色器中处理 8 位字符串,但找不到“字节”或“字符”的 HLSL 数据类型。 OpenCL 支持“char”类型,所以我
我是一个初学者像素着色器作家,我遇到了一些麻烦。我想获取 256x256、16 位输入 (DXGI_FORMAT_R16_UINT) 图像,并通过 256x256 查找纹理 (DXGI_FORMAT_
我正在寻找可以将 cg/hlsl 混合着色器转换为 glsl es 着色器的工具。我已经尝试过 hlsl2glsl,它不理解关键字“extern”,尽管它是一个可行的 hlsl 关键字,并且查看了 c
我正在寻找可以将 cg/hlsl 混合着色器转换为 glsl es 着色器的工具。我已经尝试过 hlsl2glsl,它不理解关键字“extern”,尽管它是一个可行的 hlsl 关键字,并且查看了 c
在过去一个月左右的时间里,我一直在努力学习 DirectX。所以我一直在 DirectX 9 和 10 之间来回混合。我在两者中看到的主要变化之一是如何在显卡中处理 vector 。 我注意到的一个重
统一缓冲区和常量缓冲区有什么区别? 它们是完全分开的还是可以将制服视为在恒定缓冲区中?换句话说,如果你想设置一个uniform,你需要一个常量缓冲区还是有其他方法? 我问是因为我有四个变量(float
在详细介绍之前,我想概述一下问题: 我使用 RWStructuredBuffers 来存储我的计算着色器 (CS) 的输出。由于顶点和像素着色器无法从 RWStructuredBuffers 中读取,
似乎此功能的文档记录很差。在 DirectX SDK 中的几何着色器教程中,没有使用 CreateGeometryShaderWithStreamOutput 的示例,也没有任何线程可以解释它的基础知
在着色器模型 3.0 中,我很确定这是一个不,但我还是想问这个, 在着色器模型 5.0 中,您可以在顶点着色器中对纹理进行采样吗? 如果我想为每个顶点提供大量补充信息,我有哪些选择? 编辑: 显然可以
我有一个 HLSL 像素着色器: struct PixelShaderInput { float4 pos : SV_POSITION; float2 texCoord : TEX
我正在尝试实现基于 GPU 的几何裁剪贴图,但在将简单的高度图应用于我的地形时遇到问题。对于高度图,我使用表面格式为“single”的简单纹理。我从 Catalinz's XNA blog 获取了纹理
我想知道 HLSL 中的那些输入和输出语义是干什么用的? 即为什么我必须写那个 TEXCOORD0; struct VS_OUTPUT { float2 tc : TEXCOORD0; }; 当
我找不到任何关于如何在 HLSL 中获取数组长度的文档或示例。 我将一组灯光推送到 hlsl 着色器,我想做一个 for(int i=0; i
HLSL 是否有像 GLSL 这样的常见矩阵的预定义变量? 我在找 gl_ProjectionMatrix和 gl_ModelViewMatrix分别? 谢谢! 最佳答案 不,不幸的是它没有。 您必须
我正在尝试在我的几何着色器中实现行进立方体算法。所以我将我的数据网格放入 Texture3D 中。现在我想在几何着色器中查找数据,这会引发错误“无法将表达式映射到 gs_4_0 指令集” 这是他抛出错
我正在尝试在 HLSL 中制作类似 Photoshop 的斜角效果。 困难的部分是找到一个点与最近边缘的距离(其中 alpha=0) 任何人有任何想法如何做到这一点? 最好的事物, 软件 最佳答案 花
所以,好吧。我正在尝试在我的小型游戏引擎中实现法线贴图,但我无法让它工作。 当我只使用每个顶点法线进行照明时,一切都很好,但是如果我尝试使用法线贴图进行照明,那么一切都会分崩离析。 我知道我有正确的
我正在编写一个着色器(HLSL),并且需要将颜色值打包为R32格式。我发现了用于将浮点数打包为R8G8B8A8格式的各种代码,但它们似乎都没有相反的作用。我的目标是SM3.0,因此(afaik)位操作
我有一个着色器,我想在其中移动顶点着色器中的一半顶点。我试图从性能的角度决定最好的方法,因为我们正在处理超过 100,000 个顶点,所以速度至关重要。我查看了 3 种不同的方法:(伪代码,但足以给你
我是一名优秀的程序员,十分优秀!