- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我对 Open GL 和 c++ 还很陌生,遇到过创建第一人称相机的问题。我不懂矩阵数学,所以这对我来说更加困难。到目前为止,为了计算相机的旋转,我已经这样做了:
void CameraFP::calculate_view() {
m_view = glm::rotate(m_view, this->get_rotation_x(), glm::vec3(1, 0, 0));
m_view = glm::rotate(m_view, this->get_rotation_y(), glm::vec3(0, 1, 0));
}
每次更新调用都会调用该函数。
为了通过鼠标处理相机的旋转,我做了以下操作:
void CameraFP::process_mouse(sf::Window *app) {
GLfloat mouse_x = app->GetInput().GetMouseX();
GLfloat mouse_y = app->GetInput().GetMouseY();
GLfloat mouse_x_delta = std::max(mouse_x, old_mouse_x) - std::min(mouse_x, old_mouse_x);
GLfloat mouse_y_delta = std::max(mouse_y, old_mouse_y) - std::min(mouse_y, old_mouse_y);
y_rot += mouse_x_delta / (float)app->GetWidth();
x_rot += mouse_y_delta / (float)app->GetHeight();
this->old_mouse_x = mouse_x;
this->old_mouse_y = mouse_y;
app->SetCursorPosition(app->GetWidth() / 2, app->GetHeight() / 2);
}
为了处理运动,我做了以下事情:
void CameraFP::process_keyboard(sf::Window *app) {
const sf::Input *input = &app->GetInput();
if (input->IsKeyDown(sf::Key::W)) {
position.z += 1.0f / 100.0f;
}
if (input->IsKeyDown(sf::Key::S)) {
position.z -= 1.0f / 100.0f;
}
if (input->IsKeyDown(sf::Key::A)) {
position.x -= 1.0f / 100.0f;
}
if (input->IsKeyDown(sf::Key::D)) {
position.x += 1.0f / 100.0f;
}
}
我的问题在于我的相机没有朝你面对的方向移动,而且它永远不会停止旋转:/。另外,如果你能给我指点一个指南或一些关于矩阵数学的东西,那就太棒了:)
编辑 2:'
我刚刚启动了一个新的过程鼠标功能,它沿着屏幕的 x 轴移 Action 为相机的 y 旋转正确。但是,无论我向左还是向右移动鼠标都没有关系,这两个 Action 都会使我向右旋转。与 3d 空间中的 x 轴相同,但这是向下发生的。关于造成这种情况的原因有什么想法吗?
void CameraFP::process_mouse(sf::Window *app) {
GLfloat mouse_x = app->GetInput().GetMouseX();
GLfloat mouse_y = app->GetInput().GetMouseY();
GLfloat mouse_x_delta = old_mouse_x - mouse_x;
GLfloat mouse_y_delta = old_mouse_y - mouse_y;
if (mouse_x_delta > 0) {
y_rot += mouse_x_delta / (float)app->GetWidth() * 0.1f;
} else if (mouse_x_delta < 0) {
y_rot -= mouse_x_delta / (float)app->GetWidth() * 0.1f;
}
if (mouse_y_delta > 0) {
x_rot += mouse_y_delta / (float)app->GetWidth() * 0.1f;
} else if (mouse_y_delta < 0) {
x_rot -= mouse_y_delta / (float)app->GetWidth() * 0.1f;
}
if (mouse_x != old_mouse_x) {
m_view = glm::rotate(m_view, y_rot, glm::vec3(0, 1, 0));
}
if (mouse_y != old_mouse_y) {
m_view = glm::rotate(m_view, x_rot, glm::vec3(1, 0, 0));
}
this->old_mouse_x = mouse_x;
this->old_mouse_y = mouse_y;
app->SetCursorPosition(app->GetWidth() / 2, app->GetHeight() / 2);
}
最佳答案
目前我没有时间查找我的相机代码,但我现在可以告诉你的是,你的运动计算完全错误。
你没有将相机旋转考虑在内帐户。你必须做这样的事情:
if (input->IsKeyDown(sf::Key::W)) {
position.z += sin(camera_rot_y);
}
else if (input->IsKeyDown(sf::Key::S)) {
position.z -= sin(camera_rot_y);
}
if (input->IsKeyDown(sf::Key::A)) {
position.x -= cos(camera_rot_y);
}
else if (input->IsKeyDown(sf::Key::D)) {
position.x += cos(camera_rot_y);
}
你也可以使用“else if”,因为你不能同时前进和后退。^^
此外,为什么要在以下代码中使用 std::min() 和 max()?
GLfloat mouse_x_delta = std::max(mouse_x, old_mouse_x) - std::min(mouse_x, old_mouse_x);
GLfloat mouse_y_delta = std::max(mouse_y, old_mouse_y) - std::min(mouse_y, old_mouse_y);
如果您想朝相反的方向旋转,您需要获得一个负增量,这对您的代码来说是不可能的。去掉 min() 和 max() 函数。
顺便说一句:我的相机代码不是最有效的(使用旋转而不是直接计算),但它可以工作。
关于c++ - 在 Open GL 2.0 和 glm 中创建第一人称相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12126882/
如何将 glm::vec4 转换为 glm::vec3? 仅需要 x、y、z - 可以删除 w 组件。 在 GLSL 中,这可以通过 .xyz[1] 完成,但在 glm 中,这会导致编译错误: err
我正在使用 R 中的 h2o 包通过 h2o.glm() 函数来适应 GLM。在具有 l1 正则化惩罚的 GLM 中评估特征重要性的一种合理方法是监视参数进入线性预测器(即模型)的顺序,因为 l1 惩
我在从最新版本的 R 中的 glm 计算 OR 置信区间时遇到问题,但我以前没有遇到过这个问题。与任何 glm 在哪里 family="binomial" ,无论模型多么简单,我都可以轻松提取摘要和
我需要在我的 glm 图上添加 95% 的置信区间。 当我执行 multiline=T 时(因为我需要在同一个地 block 上进行 2 次处理,所以我这样做了),间隔消失了。我怎样才能让他们回来?
对于上下文,我一直在学习 OpenGL,并且学习了透视投影。花了整整一个小时试图弄清楚为什么我的三角金字塔没有正确显示,直到我注意到我的一个矩阵乘法(使用 operator* for glm::mat
你能告诉我 返回了什么吗? glm$残差和 残留物(glm)其中 glm 是一个准泊松对象。例如我将如何使用 glm$y 和 glm$linear.predictors 创建它们。 glm$残差
这是关于理解 glm 源码的。我想知道 glm 是否对它的类进行了零初始化并进行了尝试。是的,glm::vec3 和 glm::mat4 被初始化,即使没有提供构造函数值。然后我想了解它是如何完成的并
我正在尝试使用 probit 模型重现其他人的工作。不幸的是,我没有太多关于他们方法的信息,只有他们的起始数据和他们的模型图。 当我在 ggplot 中绘制数据并使用 geom_smooth(meth
我有一个 ModelMatrix 类,其中我有一个 glm::vec3 定义为 glm::vec3 *position = nullptr; 然后我得到了一个setter方法 void ModelMa
我想标准化生物数据集的变量。我需要使用不同的响应变量运行 glm、glm.nb 和 lm。 该数据集包含按地块划分的给定树种的数量(所有地块大小相同)和一系列定性变量:植被类型、土壤类型和牛的存在/不
我正在使用著名的 OpenGL 框架创建一个漂亮的图形引擎,但突然遇到了一个意想不到的问题(就像所有问题一样)。 我必须创建一个函数来修改 glm::mat3 中的特定值。为此,我创建了一个简单的函数
其他答案似乎有一些相关信息,但没有解决我遇到的问题。 从我在 Xcode 中的项目中,我选择了“将文件添加到项目”并选择了整个 glm 子文件夹(我被告知这是使用 glm 所需的全部)。然后,在我的
像下面这样计算四元数时, 你能解释一下 1.0f means.. 是什么意思吗? 我认为它可能是 x 轴的 90 度。 glm::angleAxis(3.141592... / 2, glm::dv
这可能真的很简单,但我已经下载了 glm 的正确软件包 glm-0.9.9-a2,但我不知道如何安装。说明要么太复杂,要么太简单。 我无法通过使用包管理器或将存储库添加到 aptitude 或 Ana
我无法理解使用 glm 的效果编码。举个例子: data('mpg') mpg$trans = as.factor(mpg$trans) levels(mpg$trans) [1] "auto(av)
我正在尝试在 R 中重现二项式 glm 的结果。 考虑来自此处的数据 http://www.ats.ucla.edu/stat/r/dae/logit.htm mydata <- read.csv("
ChatGPT已经火了一段时间了,国内也出现了一些平替,其中比较容易使用的是ChatGLM-6B: https://github.com/THUDM/ChatGLM-6B ,主要是能够让我们基于
我一直在阅读code R 使用它来拟合广义线性模型 (GLM),因为 R 的源代码是免费提供的。使用的算法称为迭代重新加权最小二乘法 (IRLS),这是一种有据可查的算法。对于每次迭代,都会调用 Fo
我读过 glmnet 可以在没有正则化的情况下使用,即它可以用作常规 glm。我正在写一篇论文并试图避免使用许多不同的包,因此使用 glmnet 进行常规的 glm 逻辑回归拟合会很方便。谁能帮我?
我正在尝试使用 glm( family='binomial') 来拟合逻辑回归. 这是模型: model<-glm(f_ocur~altitud+UTM_X+UTM_Y+j_sin+j_cos+tem
我是一名优秀的程序员,十分优秀!