- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 OpenGL ES 2.0 开发 Android 应用程序。它由叠加在 3D 场景上的 2D HUD 组成。 3D 部分一切正常,但在 2D 中绘制时,我无法弄清楚如何将原点定位在屏幕的左上角(y 轴指向下方)。现在我的原点在屏幕的中心,我只是在绘制时从坐标中减去一半的宽度和高度。 (我知道这是一个 hacky 解决方案,我想解决这个问题!)
相关代码如下:
GLES20.glViewport(0, 0, surfaceWidth, surfaceHeight);
Matrix.orthoM(projectionMatrix, 0, -width / 2, width / 2, height / 2, -height / 2, -1, 1);
Matrix.setLookAtM(viewMatrix, 0, 0, 0, 2f, 0, 0, 0, 0, -1, 0);
Matrix.multiplyMM(mvpMatrix, 0, projectionMatrix, 0, viewMatrix, 0);
以下是相关的着色器:
private final static String vertexShaderCode =
"uniform mat4 uMVPMatrix;" +
"attribute vec2 aTexCoordinate;" +
"varying vec2 vTexCoordinate;" +
"attribute vec4 aPosition;" +
"attribute vec4 aColor;" +
"varying vec4 vColor;" +
"void main() {" +
" vColor = aColor;" +
" vTexCoordinate = aTexCoordinate;" +
" gl_Position = aPosition * uMVPMatrix;" +
"}";
private final static String fragmentShaderCode =
"precision mediump float;" +
"uniform sampler2D uTexture;" +
"varying vec2 vTexCoordinate;" +
"varying vec4 vColor;" +
"void main() {" +
" gl_FragColor = vColor * texture2D(uTexture, vTexCoordinate);" +
"}";
我尝试过的:
我试过使用这条线:
Matrix.orthoM(projectionMatrix, 0, 0, width, height, 0, -1, 1);
我试过将原点定位在左下角,而不是左上角(我不应该这样做,但我还是试过了)。
我尝试在顶点着色器的倒数第二行中切换 aPosition
和 uMVPMatrix
的顺序。
这三个更改的组合没有解决我的问题(2D 对象不渲染)。
我尝试的另一件事是将原点设置为偏离中心一个像素,然后是两个像素,然后是三个像素,依此类推。我越是偏离中心,图像就会变得越歪斜,最终变得太薄以至于看不见——但是仍然没有告诉我如何解决这个问题。
那么,问题又来了:如何在 OpenGL ES 2.0 中将原点定位在左上角?
附言我的契约(Contract)(第一份工作!)将于 7 月 29 日到期,所以我需要尽快得到答复;我不想让下一个人留下这样的 hacky 修复程序。
最佳答案
在 OpenGL 中不是只有一个 mvpMatrix 的规则。这很好,因为您可以自定义很多 View 。
如果您的所有应用程序都基于您在此处显示的 mvpMatrix,我建议您为您提出的建议创建另一个 mvpMatrix:使 View 的中心位于左上角。
所以,答案很简单,让我们重新创建每个矩阵。首先,viewMatrix:
Matrix.setLookAtM(viewMatrix, 0,
0, 0, 6, //eye
0, 0, 0, //center
0, 1, 0); //UP *remove your -1
魔术在下面。你必须反转底部和顶部:
float screenRatio = (float) width / height;
Matrix.orthoM(projectionMatrix, 0,
0, screenRatio, // left, right
1, 0, // bottom, top <----Solution is invert bottom with top
-1, 10); // near, far
最后:
Matrix.multiplyMM(
viewProjectionMatrix, 0,
projectionMatrix, 0,
viewMatrix, 0);
附言:不需要更改 GLES20.glViewport(0, 0, surfaceWidth, surfaceHeight);
OpenGL 有很多解决这个问题的方法,因为你可以尽可能地处理所有矩阵。
另一种解决方案是自定义您自己的矩阵,而不使用 Matrix.orthoM(..)
或 Matrix.frustum(..)
:
public static final float[] topLeftMatrix(int width, int height){
float matrix[] = new float[16];
// This is inverse of viewPort matrix
matrix[0] = 2.0f/width;
matrix[1] = 0;
matrix[2] = 0;
matrix[3] = 0;
matrix[4] = 0;
matrix[5] = -2.0f/height;
matrix[6] = 0;
matrix[7] = 0;
matrix[8] = 0;
matrix[9] = 0;
matrix[10] = 1;
matrix[11] = 0;
matrix[12] = -1;
matrix[13] = 1;
matrix[14] = 0;
matrix[15] = 1;
return matrix;
}
上面的这个矩阵非常适合处理像素上的对象(比如用户界面)。
为了全面理解,我建议您阅读这篇文章:3D Graphics with OpenGL .它对我理解 opengl 魔法有很大帮助。
P.S:您的着色器有错误。将矩阵 4x4 乘以向量 4x1 的正确方法是:
gl_Position = uMVPMatrix * aPosition;
关于android - 在 OpenGL ES 2 中将原点设置为屏幕的左上角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25416820/
我正在尝试使用 Google map 的 iframe 在我的网站上列出来自 Google map 的所有仓库位置: 目前,我一直在研究如何删除位置框(左上角),因为我将其用作 iframe。有没有
我正在设计数据表的样式,但我不知道如何设计数据网格左上角的样式。就是这张图中的灰色区域: 你知道怎么做吗? 到目前为止,这是我的风格:
我正在尝试在导航栏的左上角添加一些文本。单击后,我希望页面自动滚动回页面顶部 HTML: LOGO HERE Services
默认情况下,SVG 中文本元素的 anchor 在左下角,但我希望它在左上角,因为我还创建了一个矩形作为文本的背景,但它显示不正确,因为文本高于矩形(因为矩形 anchor /偏移位于左上角)。有没有
我需要的是将 DIV 的内容溢出(如果这是正确的词,我不会溢出)到左侧和顶部,并保持 DIV 大小始终固定。这是我制作的图像: 正常结果是 DIV 变为 70x76(其内容的大小),但我需要保持 DI
我们如何在纯 CSS 中创建这个形状? 最佳答案 你可以像下面这样: html and the css div { height: 300px; background:
无法诊断 div 左上角 float 像素的原因。它只在我添加了一个 border-radius 后出现。 仔细观察每个 Angular ,您会看到一些碎片,因为它在应用半径之前应用了直线。也许需要一
有很多问题需要从左上角(例如:编辑距离)和从右下角开始(例如:回文子串)填表。有什么时候使用哪个的直观解释吗?引用资料: http://www.geeksforgeeks.org/dynamic-pr
我正在开发一个 FabricJS 应用程序,我希望用户通过简单的单击即可将对象移动到 Canvas 原点,即使在旋转或缩放对象或一组对象之后也是如此。为此,当用户单击时,我会执行以下代码: obj.l
我在定位元素时遇到错误。浏览器通过查看元素的左上角来计算元素的位置。 让我们说那个点(左上角)“基础”(抱歉这个术语)当我使用对齐或中心对象时,它工作正常。但是,如果我用其他属性更改元素的位置,比如说
是否可以禁用/删除对话框左上角的QDialogs默认关闭按钮? 最佳答案 至少在(嵌入式)Linux 中,您可以这样做: setWindowFlags(Qt::Window | Qt::WindowT
我如何让应用程序的名称出现在此处(就像普通应用程序一样)... alt text http://snapplr.com/snap/6sgc ...当应用程序是 LSUIElement 时。 我的问题是
我有一个 div,我想在 div 的右下角放一张图片,在左下角放一些图片。它们的高度不同,所以我想确保它们都与 div 的底部对齐。 首先,我使用以下方法使右下角对齐: position: ab
如何在使用 border-width 时添加边框半径?我想添加 border-top-left-radius 为 15px。 我有这段代码可以创建一个三 Angular 形对象,它看起来不错,但我希望
我是一名优秀的程序员,十分优秀!