- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个场景,我正在用 openGL 在其中渲染几个立方体(程序结构没有使用 GLUT,它在 win32 程序结构中,但我只是用 glutSolidCube
绘制立方体)现在我想通过鼠标选择这些立方体。这就是我正在做的:首先,当用户在场景上单击鼠标按钮时,我得到鼠标位置并尝试在场景坐标中找到它的坐标(templateSkeletons 是我用立方体创建的骨架,仅此而已):
if (mouse.buttonPressed(Mouse::BUTTON_LEFT))
{
mouse.update();
templateSkeletons[0].selectionMode = true;
Vector3* points;
points = GetOGLPos();
templateSkeletons[0].setIntersectionPoints(points[0],points[1]);
}else
templateSkeletons[0].selectionMode = false;
这是我在场景中检索坐标的 GerOGLPos
函数(注意我有自己的 camrea 和它自己的投影矩阵,但我只是通过调用在这个函数中获取投影矩阵glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
这是错误的吗,我应该得到我自己的 camrea 投影矩阵?):
Vector3* GetOGLPos()
{Vector3 pointsOnLine[2];
double mvmatrix[16];
double projmatrix[16];
int viewport[4];
double dX, dY, dZ, dClickY,zz;
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
dClickY = double (viewport[3] - mouse.yPos());
// OpenGL renders with (0,0) on bottom, mouse reports with (0,0) on top
//glReadPixels( mouse.xPos(), int(dClickY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &zz );
gluUnProject ((double) mouse.xPos(), dClickY, 0.0, mvmatrix, projmatrix, viewport, &dX, &dY, &dZ);
pointsOnLine[0] = Vector3( (float) dX, (float) dY, (float) dZ );
gluUnProject ((double) mouse.xPos(), dClickY, 1.0, mvmatrix, projmatrix, viewport, &dX, &dY, &dZ);
pointsOnLine[1] = Vector3( (float) dX, (float) dY, (float) dZ );
return pointsOnLine;
}
现在我想我有两个点指示场景中的拾取光线。现在,当我渲染立方体时,我尝试计算光线和立方体创建的线的距离,如果它小于某个值,我更改立方体的颜色以知道我选择了它(jointsOfSkeleton 指示每个立方体仅创建骨架,在这里我只测试数组中的第 6 个立方体):
if(selectionMode)
{
distToLine = Vector3::PointToLineDistance3D(rayPoints[0],rayPoints[1],Vector3::Vector3(jointsOfSkeleton[6].x,
jointsOfSkeleton[6].y,jointsOfSkeleton[6].z));
//distToLine = sqrt(distToLine);
if(distToLine < 0.5)
glColor3f(1.0,0.0,0.0);
else
glColor3f(1.0,1.0,1.0);
}
当我点击窗口上不相关的位置时,我看到立方体的颜色发生了变化,它无法正常工作,我正在观察调试器上的距离,但距离看起来不正确。这是我用来查找线点距离的函数:
static float PointToLineDistance3D(Vector3 a, Vector3 b, Vector3 point)
{
Vector3 lineDirection = b - a;
float t = (Vector3::dot(point,lineDirection) - Vector3::dot(lineDirection,a))/(Vector3::dot(lineDirection,lineDirection));
Vector3 direction;
direction.x = a.x + (lineDirection.x *t) - point.x;
direction.y = a.y + (lineDirection.y *t) - point.y;
direction.z = a.z + (lineDirection.z *t) - point.z;
float ShortestDistance = sqrtf((direction.x*direction.x)+(direction.y*direction.y)+(direction.z*direction.z));
return ShortestDistance;
}
最佳答案
我会这样写 PointToLineDistance3D
:
static float PointToLineDistance3D(const Vector3 &a, const Vector3 &b, const Vector3 &point){
Vector3 lineDirection = Vector3::normalize(b - a), pointDirection = point - a;
float t = Vector3::dot(pointDirection,lineDirection);
Vector3 projection = a + (lineDirection * t);
float ShortestDistance = (projection - point).length();
return ShortestDistance;
}
我假设:
Vector3
类有一个 length
方法,意义明显,*
缩放 vector 的运算符,normalize
函数也返回...一个归一化 vector (这也可以成为一种避免构造额外对象的方法)。想法是计算 point
的投影在射线上,然后计算 projection
之间的距离和 point
.如您所见,该算法与您的实现略有不同,最显着的是计算t
。 .也许这就是您的问题所在。
为了测试我上面提供的代码,我用它编写了一个小程序,它在 XY 平面上构建了一个 3x3 的立方体墙,墙的中心是 <0,0,0>
.我设法让它正常工作,即使在移动相机时也是如此。唯一的问题是关于从上到下的鼠标坐标系(即 t_Y 鼠标坐标向下增加_),它与自然 OpenGL Y 轴相反。它需要 SDL 库才能编译和运行。
#include <iostream>
#include <GL/gl.h>
#include <GL/glu.h>
#include <SDL/SDL.h>
#include <unistd.h>
#include <Vector3.h>
#define WIDTH 800
#define HEIGHT 600
GLuint box;
int highlight[2]; // position of the cube in the wall
const float cube_width = 5.0;
Vector3 position(0,0,-25); // camera position
// build the cube display list
void setup_cube(){
const float w = cube_width;
float w0 = -w, h0 = -w, w1 = w, h1 = w;
box = glGenLists(1);
glNewList(box, GL_COMPILE);
glBegin(GL_QUAD_STRIP);
glVertex3f(w0, h1, w0);
glVertex3f(w0, h0, w0 );
glVertex3f(w1, h1, w0 );
glVertex3f(w1, h0, w0 );
glVertex3f(w1, h1, w1 );
glVertex3f(w1, h0, w1 );
glVertex3f(w0, h1, w1 );
glVertex3f(w0, h0, w1 );
glEnd();
glBegin(GL_QUAD_STRIP);
glVertex3f(w1, h1, w0 );
glVertex3f(w1, h1, w1 );
glVertex3f(w0, h1, w0 );
glVertex3f(w0, h1, w1 );
glVertex3f(w0, h0, w0 );
glVertex3f(w0, h0, w1 );
glVertex3f(w1, h0, w0 );
glVertex3f(w1, h0, w1 );
glEnd();
glEndList();
}
void setup_scene(){
float r = WIDTH / HEIGHT;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum( -r, r, -1, 1, 1, 1024);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(position[0],position[1],position[2]);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
}
void draw_scene(){
const float w = cube_width;
int i = 0, j = 0;
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
float x = w * 2 * i, y = w * 2 * j;
if (highlight[0] == i && highlight[1] == j)
glColor3f(0.0, 1.0, 0.0);
else
glColor3f(1.0, 0.0, 0.0);
glPushMatrix ();
glTranslatef(x,y,0);
glCallList(box);
glPopMatrix ();
}
}
}
void aim(float xm, float ym_){
const float w = cube_width;
float ym = HEIGHT - ym_;
GLdouble model[16];
GLdouble proj[16];
GLint view[16];
glGetDoublev(GL_MODELVIEW_MATRIX, model);
glGetDoublev(GL_PROJECTION_MATRIX, proj);
glGetIntegerv(GL_VIEWPORT, view);
highlight[0] = -5;
highlight[1] = -5;
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
float x = w * 2 * i, y = w * 2 * j;
double ox, oy, oz;
Vector3 centre(x,y,0);
gluUnProject(xm, ym, 0, model, proj, view, &ox, &oy, &oz);
Vector3 p0(ox,oy,oz);
gluUnProject(xm, ym, 1, model, proj, view, &ox, &oy, &oz);
Vector3 p1(ox,oy,oz);
float d = PointToLineDistance(p0,p1,centre);
if (d < w) {
highlight[0] = i;
highlight[1] = j;
return;
}
}
}
}
int main(){
SDL_Surface *screen;
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
if ( (screen=SDL_SetVideoMode( WIDTH, HEIGHT, 32, SDL_OPENGL )) == NULL ) {
SDL_Quit();
return -1;
}
setup_cube();
while (1) {
SDL_Event event;
setup_scene();
while(SDL_PollEvent(&event)){
switch(event.type){
case SDL_MOUSEMOTION:
aim(event.motion.x, event.motion.y);
break;
case SDL_KEYDOWN:
{
switch (event.key.keysym.sym){
case SDLK_ESCAPE:
SDL_Quit();
exit(1);
case SDLK_LEFT:
position.add(Vector3(1,0,0));
break;
case SDLK_RIGHT:
position.sub(Vector3(1,0,0));
break;
case SDLK_UP:
position.add(Vector3(0,0,1));
break;
case SDLK_DOWN:
position.sub(Vector3(0,0,1));
break;
case SDLK_PAGEDOWN:
position.add(Vector3(0,1,0));
break;
case SDLK_PAGEUP:
position.sub(Vector3(0,1,0));
break;
}
}
default:
break;
}
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw_scene();
SDL_GL_SwapBuffers();
usleep(10);
}
return 0;
}
上面列出的来源正确显示了鼠标指针瞄准的立方体。可以使用箭头和向上/向下翻页键四处移动。
关于c++ - 使用 `gluUnProject` 在 opengl 中进行鼠标拾取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16962891/
我在网上搜索但没有找到任何合适的文章解释如何使用 javascript 使用 WCF 服务,尤其是 WebScriptEndpoint。 任何人都可以对此给出任何指导吗? 谢谢 最佳答案 这是一篇关于
我正在编写一个将运行 Linux 命令的 C 程序,例如: cat/etc/passwd | grep 列表 |剪切-c 1-5 我没有任何结果 *这里 parent 等待第一个 child (chi
所以我正在尝试处理文件上传,然后将该文件作为二进制文件存储到数据库中。在我存储它之后,我尝试在给定的 URL 上提供文件。我似乎找不到适合这里的方法。我需要使用数据库,因为我使用 Google 应用引
我正在尝试制作一个宏,将下面的公式添加到单元格中,然后将其拖到整个列中并在 H 列中复制相同的公式 我想在 F 和 H 列中输入公式的数据 Range("F1").formula = "=IF(ISE
问题类似于this one ,但我想使用 OperatorPrecedenceParser 解析带有函数应用程序的表达式在 FParsec . 这是我的 AST: type Expression =
我想通过使用 sequelize 和 node.js 将这个查询更改为代码取决于在哪里 select COUNT(gender) as genderCount from customers where
我正在使用GNU bash,版本5.0.3(1)-发行版(x86_64-pc-linux-gnu),我想知道为什么简单的赋值语句会出现语法错误: #/bin/bash var1=/tmp
这里,为什么我的代码在 IE 中不起作用。我的代码适用于所有浏览器。没有问题。但是当我在 IE 上运行我的项目时,它发现错误。 而且我的 jquery 类和 insertadjacentHTMl 也不
我正在尝试更改标签的innerHTML。我无权访问该表单,因此无法编辑 HTML。标签具有的唯一标识符是“for”属性。 这是输入和标签的结构:
我有一个页面,我可以在其中返回用户帖子,可以使用一些 jquery 代码对这些帖子进行即时评论,在发布新评论后,我在帖子下插入新评论以及删除 按钮。问题是 Delete 按钮在新插入的元素上不起作用,
我有一个大约有 20 列的“管道分隔”文件。我只想使用 sha1sum 散列第一列,它是一个数字,如帐号,并按原样返回其余列。 使用 awk 或 sed 执行此操作的最佳方法是什么? Accounti
我需要将以下内容插入到我的表中...我的用户表有五列 id、用户名、密码、名称、条目。 (我还没有提交任何东西到条目中,我稍后会使用 php 来做)但由于某种原因我不断收到这个错误:#1054 - U
所以我试图有一个输入字段,我可以在其中输入任何字符,但然后将输入的值小写,删除任何非字母数字字符,留下“。”而不是空格。 例如,如果我输入: 地球的 70% 是水,-!*#$^^ & 30% 土地 输
我正在尝试做一些我认为非常简单的事情,但出于某种原因我没有得到想要的结果?我是 javascript 的新手,但对 java 有经验,所以我相信我没有使用某种正确的规则。 这是一个获取输入值、检查选择
我想使用 angularjs 从 mysql 数据库加载数据。 这就是应用程序的工作原理;用户登录,他们的用户名存储在 cookie 中。该用户名显示在主页上 我想获取这个值并通过 angularjs
我正在使用 autoLayout,我想在 UITableViewCell 上放置一个 UIlabel,它应该始终位于单元格的右侧和右侧的中心。 这就是我想要实现的目标 所以在这里你可以看到我正在谈论的
我需要与 MySql 等效的 elasticsearch 查询。我的 sql 查询: SELECT DISTINCT t.product_id AS id FROM tbl_sup_price t
我正在实现代码以使用 JSON。 func setup() { if let flickrURL = NSURL(string: "https://api.flickr.com/
我尝试使用for循环声明变量,然后测试cols和rols是否相同。如果是,它将运行递归函数。但是,我在 javascript 中执行 do 时遇到问题。有人可以帮忙吗? 现在,在比较 col.1 和
我举了一个我正在处理的问题的简短示例。 HTML代码: 1 2 3 CSS 代码: .BB a:hover{ color: #000; } .BB > li:after {
我是一名优秀的程序员,十分优秀!