小编ack*_*ckh的帖子

在DirectX中取消投影屏幕坐标时出现意外结果

为了能够确定用户是否点击了我的任何3D对象,我试图将点击的屏幕坐标转换为矢量,然后我用它来检查我的三角形是否被击中.为此,我使用DirectX提供的XMVector3Unproject方法,并且我在C++/CX中实现了所有内容.

我面临的问题是,未预测屏幕坐标导致的矢量根本不像我预期的那样.下图说明了这一点:

由非投影产生的矢量 点击发生时的光标位置(以黄色突出显示)在左侧的等轴测视图中可见.一旦我点击,由于未投影而产生的矢量出现在图像中指示的模型后面,因为白线穿透模型.因此,它不是始于光标位置而是在等轴测视图中进入屏幕,而是出现在完全不同的位置.

当我在等轴测视图中水平移动鼠标时,单击并在此之后垂直移动鼠标并单击下面的图案.两个图像中的所有线代表由点击产生的矢量.该模型已被删除,以获得更好的可见性.

由非投影产生的矢量源自相同位置 从上面的图像可以看出,所有矢量似乎都来自相同的位置.如果我更改视图并重复该过程,则会出现相同的模式但具有不同的矢量原点.

不同的视角,不同的载体起源

以下是我用来提出这个问题的代码片段.首先,我使用下面的代码接收光标位置,并将其与绘图区域的宽度和高度一起传递给我的"SelectObject"方法:

void Demo::OnPointerPressed(Object^ sender, PointerEventArgs^ e)
{
  Point currentPosition = e->CurrentPoint->Position;

  if(m_model->SelectObject(currentPosition.X, currentPosition.Y, m_renderTargetWidth, m_renderTargetHeight))
  {
    m_RefreshImage = true;
  }
}
Run Code Online (Sandbox Code Playgroud)

"SelectObject"方法如下所示:

bool Model::SelectObject(float screenX, float screenY, float screenWidth, float screenHeight)
{
  XMMATRIX projectionMatrix = XMLoadFloat4x4(&m_modelViewProjectionConstantBufferData->projection);
  XMMATRIX viewMatrix       = XMLoadFloat4x4(&m_modelViewProjectionConstantBufferData->view);
  XMMATRIX modelMatrix      = XMLoadFloat4x4(&m_modelViewProjectionConstantBufferData->model);

  XMVECTOR v = XMVector3Unproject(XMVectorSet(screenX, screenY, 5.0f, 0.0f),
                                  0.0f,
                                  0.0f,
                                  screenWidth,
                                  screenHeight,
                                  0.0f,
                                  1.0f,
                                  projectionMatrix,
                                  viewMatrix,
                                  modelMatrix);

  XMVECTOR rayOrigin = XMVector3Unproject(XMVectorSet(screenX, screenY, 0.0f, 0.0f),
                                          0.0f,
                                          0.0f,
                                          screenWidth,
                                          screenHeight,
                                          0.0f,
                                          1.0f, …
Run Code Online (Sandbox Code Playgroud)

c++ directx 3d projection raycasting

11
推荐指数
2
解决办法
613
查看次数

Dapper multi insert返回插入的对象

使用Dapper我想实现一个采用IEnumberable类型对象的方法User.现在,User看起来如下:

public class User
{
  public int UserId { get; internal set; }
  public DateTime DateCreated { get; internal set; }
  public DateTime DateChanged { get; internal set; }
  public string Username { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这里的关键在于UserId,DateCreatedDateChanged永远不会通过对象进行设置,因此internal关键字.相反,数据库将填充这些值.

因为对象因此被修改为插入操作的一部分,所以我想返回另一个IEnumerable类型的对象,User但这次填充了相应的属性.

最近我意识到我可以让Dapper循环遍历下面的User对象IEnumerable:

public int Insert(IEnumerable<User> users)
{
  string sql = string.Format("INSERT INTO [User] (Username) VALUES (@Username)");
  return GetOpenConnection().Execute<User>(sql, …
Run Code Online (Sandbox Code Playgroud)

sql dapper sql-insert

8
推荐指数
1
解决办法
3679
查看次数

在 C++ 程序中使用 libcurl 会导致 Visual C++ 编译器发出错误

我已经使用 Visual C++ 编译器在 Windows 上将 libcurl 编译成一个 dll。除了编译后的 libcurl 源代码外,该 dll 还包含一个简单的测试程序,如下所示:

标题 ( HttpClient.h):

#pragma once

#include <string>

#include "curl/curl.h"


namespace My::Project
{
  class HttpClient
  {
  public:
    HttpClient();
    ~HttpClient();
    std::string Get(std::string address);

  private:
    CURL* easy_handle;
  };
}
Run Code Online (Sandbox Code Playgroud)

实施(HttpClient.cpp):

#include "HttpClient.h"

#include <iostream>

using namespace My::Project


HttpClient::HttpClient()
{
  easy_handle = curl_easy_init();
  curl_global_init(CURL_GLOBAL_ALL);
}


HttpClient::~HttpClient()
{
  curl_global_cleanup();
}


static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp)
{
  ((std::string*)userp)->append((char*)contents, size * nmemb);
  return size * nmemb; …
Run Code Online (Sandbox Code Playgroud)

c++ curl libcurl visual-c++

5
推荐指数
0
解决办法
1588
查看次数

如何在 WSL 2 Ubuntu 中更新 npm

我正在尝试将 Windows 10 上的 Linux Windows 子系统中运行的古老 npm 3.5.2 更新到最新版本。

\n

我正在运行的命令如下:

\n
sudo npm install -g npm@latest\n
Run Code Online (Sandbox Code Playgroud)\n

但是,它失败了,我得到的只是以下输出:

\n
WARN engine npm@7.20.5: wanted: {"node":">=10"} (current: {"node":"8.10.0","npm":"3.5.2"})\n/usr/local/lib\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 (empty)\nsudo apt install wsl\nnpm ERR! Linux 5.10.16.3-microsoft-standard-WSL2\nnpm ERR! argv "/usr/bin/node" "/usr/bin/npm" "install" "-g" "npm@latest"atest\nnpm ERR! node v8.10.0\nnpm ERR! npm  v3.5.2\nnpm ERR! path /usr/local/lib/node_modules/.staging/@npmcli/ci-detect-c7bf9552\nnpm ERR! code ENOENT\nnpm ERR! errno -2\nnpm ERR! syscall rename\n\nnpm ERR! enoent ENOENT: no such file or directory, rename '/usr/local/lib/node_modules/.staging/@npmcli/ci-detect-c7bf9552' -> '/usr/local/lib/node_modules/npm/node_modules/@npmcli/ci-detect'\nnpm ERR! enoent ENOENT: no such file or directory, …
Run Code Online (Sandbox Code Playgroud)

ubuntu node.js npm windows-10 windows-subsystem-for-linux

5
推荐指数
1
解决办法
2万
查看次数

GLM 数学库是否与 Apple 的金属着色语言兼容?

我准备将一个使用 C++ 编写的 OpenGL 的 iOS 应用移植到 Apple 的 Metal 中。目标是完全摆脱 OpenGL 并用 Metal 取而代之。

OpenGL 代码是分层的,我试图只替换渲染器,即实际调用 OpenGL 函数的类。然而,整个代码库利用 GLM 数学库来表示向量和矩阵。

例如,有一个提供视图和投影矩阵的相机类。它们都是类型glm::mat4,并且只是传递给 GLSL 顶点着色器,在那里它们与mat4GLSL 给出的数据类型兼容。我想利用该相机类将这些矩阵发送到 Metal 顶点着色器。现在,我不确定是否glm::mat4与 Metal 兼容float4x4.

我没有一个可以测试这个的工作示例,因为我实际上只是从 Metal 开始并且在网上找不到任何有用的东西。

所以我的问题如下:

  1. 是GLM类型,如glm::mat4glm::vec4与金属的兼容float4x4/ float4
  2. 如果问题 1. 的答案是肯定的,如果我在 Metal 着色器中直接使用 GLM 类型,我有什么缺点吗?

关于问题 2. 的背景是我遇到了 Apple 的 SIMD 库,它提供了另一组数据类型,在这种情况下我无法使用,对吗?

该应用程序仅适用于 iOS,我根本不在乎在 macOS 上运行 Metal。

代码片段(最好是 Objective-C(是的,不是开玩笑))将非常受欢迎。

opengl objective-c ios glm-math metal

4
推荐指数
1
解决办法
1576
查看次数

Git 列出已更改的文件,但没有任何更改

这是极其基本的问题“为什么 Git 告诉我文件已更改但 diff 显示没有更改?”的无数个版本。类似的问题已发布在这里这里,但这些答案都没有帮助。

我的场景如下:

.gitattributes向现有的 Git 存储库添加了一个文件,其中包含多个已存在的提交。该文件的内容.gitattributes如下所示:

* text=auto

*.bat text eol=crlf
*.cmd text eol=crlf
*.ps1 text eol=crlf

*.sh text eol=lf

*.csproj   text eol=crlf
*.filters  text eol=crlf
*.props    text eol=crlf
*.sqlproj  text eol=crlf
*.sln      text eol=crlf
*.vcxitems text eol=crlf
*.vcxproj  text eol=crlf

*.cs        text
*.config    text
*.jmx       text
*.json      text
*.sql       text
*.tt        text
*.ttinclude text
*.wxi       text
*.wxl       text
*.wxs       text
*.xaml      text
*.xml       text

*.bmp binary
*.gif binary …
Run Code Online (Sandbox Code Playgroud)

windows git

4
推荐指数
1
解决办法
5624
查看次数

访问浮点数的 4 个字节是否违反 C++ 别名规则

我需要读取文件的二进制内容并将提取的字节转换为单精度浮点数。如何做到这一点已经在这里被问过。这个问题确实有正确的答案,但我想知道特定的答案是否实际上是有效的 C++ 代码。

该答案给出了以下代码:

float bytesToFloat(uint8_t *bytes, bool big_endian) {
    float f;
    uint8_t *f_ptr = (uint8_t *) &f;
    if (big_endian) {
        f_ptr[3] = bytes[0];
        f_ptr[2] = bytes[1];
        f_ptr[1] = bytes[2];
        f_ptr[0] = bytes[3];
    } else {
        f_ptr[3] = bytes[3];
        f_ptr[2] = bytes[2];
        f_ptr[1] = bytes[1];
        f_ptr[0] = bytes[0];
    }
    return f;
}
Run Code Online (Sandbox Code Playgroud)

这实际上是有效的 C++ 代码吗?我不确定它是否违反任何别名规则。

请注意,我的目标平台是大端字节序,其中浮点数保证至少为 32 位长。

c++ floating-point endianness language-lawyer single-precision

3
推荐指数
1
解决办法
166
查看次数

协程的每个执行路径上是否有必要有一个返回void的co_return语句

我想知道下面的代码是否是有效的 C++ 代码,或者如果不使用co_return会导致未定义的行为。

IAsyncAction MyClass::MyCoroutine()
{
  co_await someOtherClassInstance.SomeCoroutine();
}
Run Code Online (Sandbox Code Playgroud)

即是否需要将代码调整如下?

IAsyncAction MyClass::MyCoroutine()
{
  co_await someOtherClassInstance.SomeCoroutine();
  co_return;
}
Run Code Online (Sandbox Code Playgroud)

如果行为不是未定义的,那么最佳实践是什么(总是添加co_return或不添加)以及这样做的理由是什么?

c++ coroutine c++-winrt c++-coroutine

2
推荐指数
1
解决办法
530
查看次数