- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个绘制 100x100 网格并允许用户单击单元格并更改颜色的程序。
点击目前也有效,但只有在查看网格面(即 camPos.z
等于 camLook.z
)并且网格位于屏幕中心。
过去几天我遇到的问题是从不同的相机位置或屏幕上的不同区域查看网格时选择正确的单元格。
我唯一的猜测是深度缓冲区无法反射(reflect)相机的当前位置,或者缓冲区深度范围与相机的近距和远距值之间存在一些不一致。或者我应用投影/ View 矩阵的方式可以显示图像,但是在通过管道返回时出现问题。但我不太明白。
(自最初发布以来代码已更新/重构)
顶点着色器:
#version 330
layout(location = 0) in vec4 position;
smooth out vec4 theColor;
uniform vec4 color;
uniform mat4 pv;
void main() {
gl_Position = pv * position;
theColor = color;
}
Camera 类(projectionViewMatrix()
的结果是上面的 pv
uniform):
Camera::Camera()
{
camPos = glm::vec3(1.0f, 5.0f, 2.0f);
camLook = glm::vec3(1.0f, 0.0f, 0.0f);
fovy = 90.0f;
aspect = 1.0f;
near = 0.1f;
far = 1000.0f;
}
glm::mat4 Camera::projectionMatrix()
{
return glm::perspective(fovy, aspect, near, far);
}
glm::mat4 Camera::viewMatrix()
{
return glm::lookAt(
camPos,
camLook,
glm::vec3(0.0f, 1.0f, 0.0f)
);
}
glm::mat4 Camera::projectionViewMatrix()
{
return projectionMatrix() * viewMatrix();
}
// view controls
void Camera::moveForward()
{
camPos.z -= 1.0f;
camLook.z -= 1.0f;
}
void Camera::moveBack()
{
camPos.z += 1.0f;
camLook.z += 1.0f;
}
void Camera::moveLeft()
{
camPos.x -= 1.0f;
camLook.x -= 1.0f;
}
void Camera::moveRight()
{
camPos.x += 1.0f;
camLook.x += 1.0f;
}
void Camera::zoomIn()
{
camPos.y -= 1.0f;
}
void Camera::zoomOut()
{
camPos.y += 1.0f;
}
void Camera::lookDown()
{
camLook.z += 0.1f;
}
void Camera::lookAtAngle()
{
if (camLook.z != 0.0f)
camLook.z -= 0.1f;
}
我试图获取世界坐标的相机类中的特定函数(x
和 y
是屏幕坐标):
glm::vec3 Camera::experiment(int x, int y)
{
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
GLfloat winZ;
glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
printf("DEPTH: %f\n", winZ);
glm::vec3 pos = glm::unProject(
glm::vec3(x, viewport[3] - y, winZ),
viewMatrix(),
projectionMatrix(),
glm::vec4(0.0f, 0.0f, viewport[2], viewport[3])
);
printf("POS: (%f, %f, %f)\n", pos.x, pos.y, pos.z);
return pos;
}
初始化和显示:
void init(void)
{
glewExperimental = GL_TRUE;
glewInit();
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LESS);
glDepthRange(0.0f, 1.0f);
InitializeProgram();
InitializeVAO();
InitializeGrid();
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CW);
}
void display(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(theProgram);
glBindVertexArray(vao);
glUniformMatrix4fv(projectionViewMatrixUnif, 1, GL_FALSE, glm::value_ptr(camera.projectionViewMatrix()));
DrawGrid();
glBindVertexArray(0);
glUseProgram(0);
glutSwapBuffers();
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH);
glutInitContextVersion(3, 2);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutInitWindowSize(500, 500);
glutInitWindowPosition(300, 200);
glutCreateWindow("testing");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
最佳答案
在光标下转换光线实现拾取其实很简单。它总是适用于几乎任何投影和模型 View 矩阵(除了一些无效的奇异情况,这些情况将整个场景转换为无穷大等)。
为了简单起见,我编写了一个使用已弃用的固定功能管道的小型演示,但该代码也适用于着色器。它首先从 OpenGL 读取矩阵:
glm::mat4 proj, mv;
glGetFloatv(GL_PROJECTION_MATRIX, &proj[0][0]);
glGetFloatv(GL_MODELVIEW_MATRIX, &mv[0][0]);
glm::mat4 mvp = proj * mv;
此处 mvp
是您要传递给顶点着色器的内容。然后我们定义两个点:
glm::vec4 nearc(f_mouse_x, f_mouse_y, 0, 1);
glm::vec4 farc(f_mouse_x, f_mouse_y, 1, 1);
这些是归一化空间中的远近光标坐标(因此 f_mouse_x
和 f_mouse_y
在 [-1, 1]
区间内) .请注意,z
坐标不必是 0 和 1,它们只需要是两个不同的任意数字即可。现在我们可以使用 mvp
将它们取消投影到世界空间:
nearc = glm::inverse(mvp) * nearc;
nearc /= nearc.w; // dehomog
farc = glm::inverse(mvp) * farc;
farc /= farc.w; // dehomog
请注意,同质划分在这里很重要。这为我们提供了光标在定义对象的世界空间中的位置(除非它们有自己的模型矩阵,但这很容易合并)。
最后,该演示计算了 nearc
和 farc
之间的光线与具有纹理的平面(您的 100x100 网格)的交点:
glm::vec3 plane_normal(0, 0, 1); // plane normal
float plane_d = 0; // plane distance from origin
// this is the plane with the grid
glm::vec3 ray_org(nearc), ray_dir(farc - nearc);
ray_dir = glm::normalize(ray_dir);
// this is the ray under the mouse cursor
float t = glm::dot(ray_dir, plane_normal);
if(fabs(t) > 1e-5f)
t = -(glm::dot(ray_org, plane_normal) + plane_d) / t;
else
t = 0; // no intersection, the plane and ray is collinear
glm::vec3 isect = ray_org + t * ray_dir;
// calculate ray-plane intersection
float grid_x = N * (isect.x + 1) / 2;
float grid_y = N * (isect.y + 1) / 2;
if(t && grid_x >= 0 && grid_x < N && grid_y >= 0 && grid_y < N) {
int x = int(grid_x), y = int(grid_y);
// calculate integer coordinates
tex_data[x + N * y] = 0xff0000ff; // red
glBindTexture(GL_TEXTURE_2D, n_texture);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, N, N, GL_RGBA, GL_UNSIGNED_BYTE, &tex_data[0]);
// change the texture to see
}
// calculate grid position in pixels
输出相当不错:
这只是一个 20x20 的纹理,但增加到 100x100 是微不足道的。您可以获得完整的演示源和预编译的 win32 二进制文件 here .它依赖于 glm。您可以使用鼠标转动或使用 WASD
移动。
比平面更复杂的物体是可能的,它本质上是raytracing .使用光标下的深度分量(窗口 z
)同样简单 - 只需注意归一化坐标([0, 1]
与 [-1, 1]
)。另请注意,回读 z
值可能会降低性能,因为它需要 CPU/GPU 同步。
关于opengl - 从窗口转换的深度分量 -> 世界坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23486230/
我想使用错误组件显示我的错误消息,但不想在 中加载组件对于经过身份验证的用户,导航菜单也不应显示。 我有这样的应用程序组件.. 我有错误处理程序,它使用 router.navigate 路由
我正在尝试获取 RGB 图像,将其转换为 LAB(又名 CIE L* a* b*)色彩空间,然后提取 L* 分量。 这是我的代码: from skimage import io, color from
我在我的一个模型中定义了以下常量。 export const NEWS_TYPE_TEXT = { News: 'News', Interview: 'Intervie
我有一个Electron(6)/Angular(8)应用程序。 在正面( Angular ),我通过IPCRenderer向背面发送一条消息。 在背面,IPCMain接收消息并执行所需的操作,例如,获
我正在尝试在我的应用程序中创建一个可重用的 quickView 模式,以使用 ng-bootstrap modal library 动态加载任何组件。 就我加载文档中所示的相同示例组件而言,它工作正常
我需要将一个名为“photos”的数组从我的 component.ts 传递到 component.html。这是我的 component.ts 文件 export class PhotosCompo
我有一个按钮,单击该按钮会转到新路线并打开附加到该路线的另一个组件。 有没有一种方法可以从 DOM 中删除我们单击以转到不同组件的组件?示例:当单击“单击我返回主页”按钮时,它会打开另一个组件。在这种
这个问题在这里已经有了答案: Detect click outside Angular component (12 个答案) 5天前关闭。 我知道这方面有无数的问题,我尝试了每一个解决方案,但没有一个
我想将显示值的格式传递给 Angular 分量。 例如: format="'(utc, offset) location (tz)'" === '(UTC -04:00) New York (EDT)
我正在使用 Angular 组件将动态图表加载到我的小部件中: 这是我的组件的示例: angular.module("generalApp").component("chartPie", { temp
假设我有一个组件在被点击时发出一个事件,即 @Component({ selector: 'component-checkout-payment', template:
我有一个问题。 我正在处理另一个人的代码,有一个 JFrame 有很多 JSeparators(他用它们作为“面板”的边框)现在我将它们替换为 JBorderedPanel 类,该类遵循与整体相同的边
所以我在这里想做的是制作一个 Angular 组件并将其注入(inject)到我的 Angular 应用程序中。这是 Angular 分量的代码: (function(angular) { 'use
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 4 年前。 Improve this ques
我正在创建一个像这样的可重用组件: submit 我想在属性 isDisabled 为 true 时禁用点击事件,我尝试了类似的操作,但它不起作用。 packages/component/my-b
一种简单的说法是,当 RGB 分量相等时,它们形成灰色。然而,这还不是全部,因为如果它们只有细微的差别,它们看起来仍然是灰色的。 假设观看者具有健康的色彩视觉,我如何确定给定值是否会被视为灰色(大概具
您好我正在尝试使用带 Angular Electron 构建桌面应用程序,主要问题是在用户登录后我找不到正确加载主要组件的方法。正如您在 main.js 中看到的,这是我创建两个窗口(1 个用于登录的
new AngularJS 1.5 中似乎没有“替换”选项组件概念(就像指令一样)。 如果我想要表格行,你有什么建议元素作为组件?就有效的 HTML 而言是不可能的吗? 真实示例:mailBox组件内
我有颜色=#12FFFF。这是这种格式的颜色,其中 12FFFF 是十六进制数。现在我想获取每个独立的 R、G、B 分量的十进制。我该如何在java中做到这一点? 最佳答案 目前尚不清楚你的问题是什么
我需要一些关于 Java 的 ImageIO API 的帮助。我似乎迷失在 ComponentColorModel 类中。我需要逐像素检查 *.png 文件来检测它是灰度图像还是彩色图像。但是,我不知
我是一名优秀的程序员,十分优秀!