- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在 Android 平台上使用 OpenGL 绘制一个地球仪,并试图在球体上找到用户点击的点。我是 OpenGL 的新手,我正在使用开源库 Rajawali如示例中所示 Touch & Drag但如果有人知道更好的方法,请建议我,因为 Rajawali 的代码完全没有评论,而且大部分文档都已过时:(
double[] np4 = new double[4]; //near plane
double[] fp4 = new double[4]; // far plane
GLU.gluUnProject(
x, mViewportHeight - y, 0.0f,
mViewMatrix.getDoubleValues(), 0,
mProjectionMatrix.getDoubleValues(), 0,
mViewport, 0,
np4, 0
);
GLU.gluUnProject(
x, mViewportHeight - y, 1.0f,
mViewMatrix.getDoubleValues(), 0,
mProjectionMatrix.getDoubleValues(), 0,
mViewport, 0,
fp4, 0
);
// transform 4D coordinates (x, y, z, w) to 3D (x, y, z) by dividing each coordinate (x, y, z) by w.
return new Ray(
new Vector3(np4[0] / np4[3], np4[1] / np4[3], np4[2] / np4[3]),
new Vector3(fp4[0] / fp4[3], fp4[1] / fp4[3], fp4[2] / fp4[3])
);
然后按所述找到与球体的交点(半径 = 1,中心 (0,0,0))here
/**
* Calculate point of intersection of line and sphere.
*
* @param p1 Point the line goes throw
* @param p2 Point the line goes throw
* @param center Center of the sphere
* @param r Radius of the sphere
* @return <code>Vector3</code> Coordinates of intersection point
*/
private Vector3 getIntersectionPoint(Vector3 p1, Vector3 p2, Vector3 center, double r){
if (p1 == null || p2 == null || center == null)
return null;
double a = in2(p2.x - p1.x) + in2(p2.y - p1.y) + in2(p2.z - p1.z);
double b = 2*( (p2.x - p1.x)*(p1.x - center.x) + (p2.y - p1.y)*(p1.y - center.y) + (p2.z - p1.z)*(p1.z - center.z) );
double c = in2(center.x) + in2(center.y) + in2(center.z) + in2(p1.x) + in2(p1.y) + in2(p1.z) - 2*(center.x*p1.x + center.y*p1.y + center.z*p1.z) - in2(r);
//simplified equations for Sphere coordinates are (0, 0, 0) r=1 and cam.x = cam.y = 0
// double a = in2(p2.x) + in2(p2.y) + in2(p2.z - p1.z);
// double b = 2*( (p2.z - p1.z)*p1.z );
// double c = in2(p1.z) - 1;
for (Vector3 poi : solveQuadraticEquation(a, b, c, p1, p2)){
if (poi.z > 0)
return poi;
}
return null;
}
/**
* Solve quadratic equation.
*
* @param a Parameter A in equation Ax^2 + Bx + C = 0
* @param b Parameter B in equation Ax^2 + Bx + C = 0
* @param c Parameter C in equation Ax^2 + Bx + C = 0
* @param p1 <code>Vector3</code> Start point for the line
* @param p2 <code>Vector3</code> End point for the line
*
* @return <code>List<Vector3></code> of results. Empty if no results found.
*/
private List<Vector3> solveQuadraticEquation(double a, double b, double c, Vector3 p1, Vector3 p2){
double D = in2(b) - 4*a*c;
List<Vector3> rez = new ArrayList<Vector3>();
if (D < 0) {
return rez;
} else if (D == 0) {
double t = (-b) / (2*a);
rez.add(new Vector3(p1.x + (p2.x - p1.x)*t, p1.y + (p2.y - p1.y)*t, p1.z + (p2.z - p1.z)*t));
return rez;
} else {
double t = (Math.sqrt(D)-b) / (2*a);
rez.add(new Vector3(p1.x + (p2.x - p1.x)*t, p1.y + (p2.y - p1.y)*t, p1.z + (p2.z - p1.z)*t));
t = (-Math.sqrt(D)-b) / (2*a);
rez.add(new Vector3(p1.x + (p2.x - p1.x)*t, p1.y + (p2.y - p1.y)*t, p1.z + (p2.z - p1.z)*t));
return rez;
}
}
结果如下:
这里:蓝点是 (-1, -1, -1), (1, -1, -1), (1, 1, -1) 和 (-1, 1, -1)绿点是 (-1, -1, 1), (1, -1, 1), (1, 1, 1) 和 (-1, 1, 1)黄色点是 (1, 0, 0), (0, 1, 0), (-1, 0, 0) 和 (0, -1, 0)
小红点是我点击顶部黄色圆圈时的交点:x: 405.10202 y: 430.5059 (event.getX(), evet.getY() in touch listener)这是一个未预测的点:附近:Vector3:<0.0038529188490706075,0.08910624125329661,4.999999999999999>远:Vector3:<0.46235026188847467,10.692748950395632,-114.00000000000048>交叉点:Vector3:<0.019687997272589165,0.45532322467387265,0.8901085011592542>
相机位置是(0,0,-6)
谁能帮我把这个小红点放在手指下,好吗?
附言我需要一个交点,而不仅仅是一个对象拾取,因为稍后我将在此时获取国家/地区代码...
最佳答案
问题是 ViewPort 对应于 View 尺寸(在本例中为 GLSurfaceView),但点具有绝对坐标,因此应将其转换为相对坐标,例如:
final float x = e.getX() - getLeft();
final float y = e.getY() - getTop();
关于android - Android OpenGL ES 上的光线对象拾取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21528359/
在 OpenGL/ES 中,在实现渲染到纹理功能时,您必须小心,不要引起反馈循环(从正在写入的同一纹理中读取像素)。由于显而易见的原因,当您读取和写入纹理的相同像素时,行为是未定义的。但是,如果您正在
正如我们最终都知道的那样,规范是一回事,实现是另一回事。大多数错误是我们自己造成的,但有时情况并非如此。 我相信列出以下内容会很有用: GPU 驱动程序中当前已知的与最新版本的 OpenGL 和 GL
很难说出这里问的是什么。这个问题是模棱两可的、模糊的、不完整的、过于宽泛的或修辞的,无法以目前的形式得到合理的回答。为了帮助澄清这个问题以便可以重新打开它,visit the help center
我正在学习 OpenGL,非常想知道与显卡的交互如何。 我觉得了解它是如何在图形驱动程序中实现的,会让我了解 opengl 的完整内部结构(通过这个我可以知道哪些阶段/因素影响我对 opengl 性能
我正在尝试绘制到大于屏幕尺寸(即 320x480)的渲染缓冲区 (512x512)。 执行 glReadPixels 后,图像看起来是正确的,除非图像的尺寸超过屏幕尺寸——在本例中,超过 320 水平
我正在 Windows 中制作一个 3D 小行星游戏(使用 OpenGL 和 GLUT),您可以在其中穿过一堆障碍物在太空中移动并生存下来。我正在寻找一种方法来针对无聊的 bg 颜色选项设置图像背景。
如果我想要一个包含 100 个 10*10 像素 Sprite 的 Sprite 表,是否可以将它们全部排成一排来制作 1,000*10 像素纹理?还是 GPU 对不那么窄的纹理表现更好?这对性能有什
这个问题在这里已经有了答案: Rendering 2D sprites in a 3D world? (7 个答案) 关闭 6 年前。 我如何概念化让图像始终面对相机。我尝试将三角函数与 arcta
是否可以在 OpenGL 中增加缓冲区? 假设我想使用实例化渲染。每次在世界上生成一个新对象时,我都必须用实例化数据更新缓冲区。 在这种情况下,我有一个 3 个 float 的缓冲区 std::v
有人可以向我解释为什么下面的代码没有绘制任何东西,但如果我使用 GL_LINE_LOOP 它确实形成了一个闭环吗? glBegin(GL_POLYGON); for(int i = 0; i <= N
正如标题所说,OpenGL 中的渲染目标是什么?我对 OpenGL 很陌生,我看到的所有网站都让我很困惑。 它只是一个缓冲区,我在其中放置稍后将用于渲染的东西吗? 如果您能提供一个很好的引用来阅读它,
当使用 OpenGL 1.4 固定功能多纹理时,每个纹理阶段的输出在传递到下一个阶段之前是否都固定在 [0, 1]? spec说(第 153 页): If the value of TEXTURE_E
我比较了 2 个函数 openGL ES 和 openGL gvec4 texelFetchOffset(gsampler2DArray sampler, ivec3 P, int lod, ivec
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 6年前关闭。 Improve this qu
那么当你调用opengl函数时,比如glDraw或者gLBufferData,是否会导致程序线程停止等待GL完成调用呢? 如果不是,那么 GL 如何处理调用像 glDraw 这样的重要函数,然后立即更
我正在尝试实现级联阴影贴图,当我想访问我的视锥体的每个分区的相应深度纹理时,我遇到了一个错误。 更具体地说,当我想选择正确的阴影纹理时会出现我的问题,如果我尝试下面的代码,我会得到一个像 this 中
我想为OpenGL ES和OpenGL(Windows)使用相同的着色器源。为此,我想定义自定义数据类型并仅使用OpenGL ES函数。 一种方法是定义: #define highp #define
我尝试用 6 个位图映射立方体以实现天空盒效果。我的问题是一个纹理映射到立方体的每个面。我已经检查了 gDEBugger,在立方体纹理内存中我只有一个 图像(因为我尝试加载六个图像)。 代码准备纹理:
在 OpenGL 中偏移深度的最佳方法是什么?我目前每个多边形都有索引顶点属性,我将其传递给 OpenGL 中的顶点着色器。我的目标是在深度上偏移多边形,其中最高索引始终位于较低索引的前面。我目前有这
我是一名优秀的程序员,十分优秀!