- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
长期阅读。第一次海报。我遇到了一个问题,它已经破坏了几个小时的脑细胞并且没有快速进展。所以我想我应该把它推到这里,让新的目光看到它......
基本上,我正在尝试使用 C++ 和 DirectX 11 创建一个 2D 游戏。我之前使用过 C# 和 XNA,并且我在 DirectX 中制作了一些 3D 游戏,但没有这么大。
我正在尝试使用基本相机和基本 Sprite 批处理在屏幕中央绘制一个 Sprite ,但现在没有任何东西出现,我终究无法弄清楚为什么。我会发布一些代码来提供一个想法。
这是我在相机类中设置所有矩阵的地方,它们保持这种状态(目前,稍后会更改为移动等):
XMStoreFloat4x4(&m_world, XMMatrixIdentity());
XMStoreFloat4x4(&m_proj, XMMatrixOrthographicOffCenterLH(0.0f, width, height, 0.0f, 0.0f, 1.0f));
m_position = XMFLOAT3(width / 2, height / 2, 0);
m_look = XMFLOAT3(0, 0, 1);
m_up = XMFLOAT3(0, 1, 0);
XMVECTOR p = XMLoadFloat3(&m_position);
XMVECTOR l = XMLoadFloat3(&m_look);
XMVECTOR u = XMLoadFloat3(&m_up);
XMStoreFloat4x4(&m_view, XMMatrixLookToLH(p, l, u));
然后将其传递给 Sprite 批处理开始方法,该方法将信息传递给着色器:
D3D11_MAPPED_SUBRESOURCE data;
ZeroMemory(&data, sizeof(data));
m_deviceContext->Map(m_cBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &data);
ConstantBuffer* cb = (ConstantBuffer*)data.pData;
XMMATRIX w = XMLoadFloat4x4(&world);
XMMATRIX v = XMLoadFloat4x4(&view);
XMMATRIX p = XMLoadFloat4x4(&proj);
w = XMMatrixTranspose(w);
v = XMMatrixTranspose(v);
p = XMMatrixTranspose(p);
XMStoreFloat4x4(&world, w);
XMStoreFloat4x4(&view, v);
XMStoreFloat4x4(&proj, p);
cb->World = world;
cb->View = view;
cb->Proj = proj;
m_deviceContext->Unmap(m_cBuffer, 0);
最后是着色器本身:
cbuffer ConstantBuffer : register(b0)
{
float4x4 World;
float4x4 View;
float4x4 Proj;
};
SamplerState sam : register(s0);
Texture2D tex : register(t0);
struct VertexIn
{
float3 Position : POSITION;
float2 Texture : TEXCOORD;
float4 Color : COLOR;
};
struct PixelIn
{
float4 Position : SV_POSITION;
float2 Texture : TEXCOORD;
float4 Color : COLOR;
};
PixelIn VS(VertexIn vin)
{
float4 position = float4(vin.Position, 1.0f);
position = mul(position, World);
position = mul(position, View);
position = mul(position, Proj);
PixelIn vout;
vout.Position = position;
vout.Texture = vin.Texture;
vout.Color = vin.Color;
return vout;
}
float4 PS(PixelIn pin) : SV_Target
{
float4 textureColor = tex.Sample(sam, pin.Texture);
return pin.Color * textureColor;
}
如您所见,非常简单的东西。但是屏幕上根本没有任何内容。我试过完全不理会矩阵,忽略世界矩阵,使用透视投影矩阵,使用观察而不是观察矩阵,通过 XMMatrixOrthographicLH 使用 (0, 0, 0) 作为中心并将 Sprite 位置转换为屏幕空间。
我什至大大简化了 Sprite 批处理,限制它一次只能绘制一个 Sprite !
我正在使用不可变索引缓冲区(0、1、2、0、2、3)和动态顶点缓冲区,其更新如下:
D3D11_MAPPED_SUBRESOURCE data;
ZeroMemory(&data, sizeof(data));
m_deviceContext->Map(m_vBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &data);
Vertex* vertex = (Vertex*)data.pData;
for(UINT i = 0; i < sprite->Vertices.size(); ++i)
{
vertex[i] = sprite->Vertices[i];
}
m_deviceContext->Unmap(m_vBuffer, 0);
m_deviceContext->DrawIndexed(6, 0, 0);
我使用类似的方法来渲染 3D 模型。事实上,这更难,因为我有一个动态索引缓冲区和更多常量缓冲区数据,而且着色器要复杂得多。
我认为这可能是数据在某处丢失的问题,但它似乎一直在正确传递,直到 DrawIndexed 方法调用。我已经仔细检查了缓冲区的创建和状态,目前将它放在 CULL_NONE 上只是为了确保它没有被剔除。
为了清楚起见,我将发布缓冲区创建和 Sprite 创建:
HRESULT result = S_OK;
device->GetImmediateContext(&m_deviceContext);
D3D11_BUFFER_DESC cbd;
ZeroMemory(&cbd, sizeof(cbd));
cbd.ByteWidth = sizeof(ConstantBuffer);
cbd.Usage = D3D11_USAGE_DYNAMIC;
cbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
cbd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
cbd.MiscFlags = 0;
cbd.StructureByteStride = 0;
result = device->CreateBuffer(&cbd, 0, &m_cBuffer);
if (FAILED(result))
{
return 0;
}
vector<short> indices;
indices.push_back(0);
indices.push_back(1);
indices.push_back(2);
indices.push_back(0);
indices.push_back(2);
indices.push_back(3);
D3D11_SUBRESOURCE_DATA indexData;
ZeroMemory(&indexData, sizeof(indexData));
indexData.pSysMem = &indices;
D3D11_BUFFER_DESC ibd;
ZeroMemory(&ibd, sizeof(ibd));
ibd.ByteWidth = 6 * sizeof(short);
ibd.Usage = D3D11_USAGE_IMMUTABLE;
ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
ibd.CPUAccessFlags = 0;
ibd.MiscFlags = 0;
ibd.StructureByteStride = 0;
result = device->CreateBuffer(&ibd, &indexData, &m_iBuffer);
if (FAILED(result))
{
return 0;
}
D3D11_BUFFER_DESC vbd;
ZeroMemory(&vbd, sizeof(vbd));
vbd.ByteWidth = 4 * sizeof(Vertex);
vbd.Usage = D3D11_USAGE_DYNAMIC;
vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vbd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
vbd.MiscFlags = 0;
vbd.StructureByteStride = 0;
result = device->CreateBuffer(&vbd, 0, &m_vBuffer);
if (FAILED(result))
{
return 0;
}
return 1;
Sprite :
ID3D11Resource* resource;
ZeroMemory(&resource, sizeof(resource));
texture->GetResource(&resource);
ID3D11Texture2D* tex = (ID3D11Texture2D*)resource;
D3D11_TEXTURE2D_DESC t;
ZeroMemory(&t, sizeof(t));
tex->GetDesc(&t);
Vertex v[4];
ZeroMemory(&v, sizeof(v));
v[0].Position = XMFLOAT3((float)destinationRectangle.value.left, (float)destinationRectangle.value.top, z);
v[1].Position = XMFLOAT3((float)destinationRectangle.value.right, (float)destinationRectangle.value.top, z);
v[2].Position = XMFLOAT3((float)destinationRectangle.value.right, (float)destinationRectangle.value.bottom, z);
v[3].Position = XMFLOAT3((float)destinationRectangle.value.left, (float)destinationRectangle.value.bottom, z);
v[0].Texture = XMFLOAT2((float)(sourceRectangle.value.left / t.Width), (float)(sourceRectangle.value.top / t.Height));
v[1].Texture = XMFLOAT2((float)(sourceRectangle.value.right / t.Width), (float)(sourceRectangle.value.top / t.Height));
v[2].Texture = XMFLOAT2((float)(sourceRectangle.value.right / t.Width), (float)(sourceRectangle.value.bottom / t.Height));
v[3].Texture = XMFLOAT2((float)(sourceRectangle.value.left / t.Width), (float)(sourceRectangle.value.bottom / t.Height));
v[0].Color = color;
v[1].Color = color;
v[2].Color = color;
v[3].Color = color;
Sprite* sprite = new Sprite();
sprite->Vertices.push_back(v[0]);
sprite->Vertices.push_back(v[1]);
sprite->Vertices.push_back(v[2]);
sprite->Vertices.push_back(v[3]);
sprite->Texture = texture;
m_sprites.push_back(sprite);
我会生气吗?我是不是很傻?或者只是有一种更简单的方法来做到这一点?谢谢。感谢您的帮助!
还要指出的是,我正在绘制的 Sprite 在 800x600 屏幕上有一个矩形(左 = 300,右 = 500,上 = 350,下 = 450),全白。纹理被正确加载,我什至尝试只在着色器中输出颜色以确保没有运气。
哦,我尝试绘制的 Sprite 的 Z 值设置为 0.5f,尽管我尝试将其设置为不同的值,但这是我期望使用它的值。
最佳答案
感谢 MJP 在另一个网站上找到了答案。问题出在这一行:
indexData.pSysMem = &indices;
本来应该是这样的:
indexData.pSysMem = &indices[0];
显然,我发送的是指向整个 vector 而不是第一个实例的指针,因此索引缓冲区不知道我在做什么!
就像我说的,我只是从 XNA 编程开始 DirectX 编程。 ;)
我希望这对处于类似位置的其他人有所帮助。
关于c++ - DirectX 11 雪碧 2D 相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14178758/
有人能给我一些代码示例,了解如何从相机捕获“完整”图像,然后在“startActivityForResult”中将其转换为字节,以及显示在 imageView 中的位图。任何帮助将不胜感激。 山姆 最
我有一个带有两个圆的组,当我用平移过渡移动其中一个时,我应该看到静止的一个保持在中心(位于场景图的中间),而另一个移动。取而代之的是,“摄像机”跟随移动的圆圈,使其看起来好像都在分开。 有没有一种方法
我希望停止 Phonegap 将图片保存到 iOS 上的相机胶卷。 我发现了一种可能的解决方案,但我不太喜欢它。该解决方案需要通过删除这行代码来 fork phonegap API。 UIImageW
我最近开始使用 JOGL,我知道如何在 Canvas 上创建和绘制对象,但我找不到有关如何设置和旋转相机的教程或说明。我只找到了源代码,但由于我对此很陌生,所以它没有太大帮助。 有人知道一个好的教程或
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 8 年前。 Improve this ques
我想实现这样的目标: 因此,您捕获和处理的唯一内容是矩形中的区域。我需要这个,因为我想做一些 OCR 而我不想要整个屏幕。 最佳答案 也许可以帮助您这个来源,(在相机预览顶部绘制一个边界框以捕获部分图
我正在开发一个 Android 应用程序,我希望我的应用程序能够拍照并显示它们。我的问题是它制作了图片,将它存储在 SD 卡上但没有在我的应用程序中显示它,ImageView 保持空白。这是我的代码:
我正在研究 Android 相机教程,SDK 11。出于某种原因,我在 handleCameraPhoto() 中得到了一个空指针。我唯一看到的是“无法将结果 ResultInfo{who=null,
我一直在尝试实现此代码,我在其中从相机捕获图像并将其显示在我的图像查看器中,然后可以将其设置为墙纸。它在我的模拟器中运行良好,但在我的手机上,当我拍摄图像并单击“确定”时,它会强制关闭。有什么帮助吗?
我想将预览中的某些像素更改为比其余像素暗一点。假设我希望预览的上半部分比预览的下半部分暗一点。 我试图像这样使用 setPreviewCallback: camera.setPreviewCallba
我想为我的应用程序启用相机/照片库设置。我可以显示警报以编程方式启用这些吗就像应用程序启动时一样。我已附上图片以显示我希望看到我的应用程序的类似设置 对于我的应用程序,我没有看到此设置。 我的问题是这
所以我正在开发类似于相机应用程序的东西。它在原生 OpenGL 纹理上绘制实时相机预览。如果按下按钮,则会以原始形式捕获静态图像(可能比实时预览分辨率更高)。 这是我的代码的大纲。为了清楚起见,我没有
我想在我的 Android 应用程序中启动相机 Activity ,我知道该怎么做。我想问一下当相机 Activity 结束时,我如何检查它是否是用户拍摄的照片或视频? 已更新 我有一个对话框,其中询
我在横向模式下有自定义全屏摄像头。设备尺寸为 1024 x 600。 支持的预览尺寸列表 宽 x 高 176 x 144 320 x 240 352 x 288 528 x 432 640 x 480
请看下图!! 所以在这张图片中...您可以看到底部的弹出窗口,当用户“点击”“相机”按钮时,它会为用户提供 3 个选项。 那是什么观点?它是一个模态视图 Controller ,在选择一个选项时被关闭
我发布了一个应用程序,其中一项基本功能是允许用户拍照,然后将该照片保存在其外部存储上的特定文件夹中。 一切似乎都运行良好,但我现在收到两份报告,声称在拍照后单击“完成”退出相机(并返回到 Activi
我在尝试实时更换相机时遇到问题,它适用于本地视频,但远程人员看不到新相机,但仍然可以看到旧相机。我试图停止流并再次初始化,但仍然无法正常工作。这只是我的一些代码。 我到处搜索,但找不到解决方案。有人可
一个简单但非常复杂的问题:为 Flutter 相机添加点击对焦功能的最佳方法是什么? 我在整个万维网上搜索了优雅的解决方案,但一无所获。 你有想法吗? 最佳答案 我可能会迟到,但你可以试试 adv_c
我正在尝试使用 gluLookAt 方法设置 3D 相机。所以我有一个 10x10x10 的立方体,现在我想在那个立方体内移动相机。我有这样的事情: gluLookAt( camera->x,came
我赠送一个 UIImagePickerController在我的应用程序中通过在 sheet 中呈现逻辑修饰符。简而言之,以下三种类型处理显示和关闭 UIImagePickerController 的
我是一名优秀的程序员,十分优秀!