- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试使用带有 Shader 的 OpenGL 显示三角形,但在移动相机时出现问题。我的三角形的方向似乎没问题,但 x 和 y 缩放比例看起来很有趣...
以下两张图片说明了我的问题:图片的左侧是我使用着色器的输出,右侧是使用 JOGL 函数(例如“glLookAt”/“glFrustum”)对同一场景的表示).这两个三角形应该是一样的,但我旋转相机时却不是这样。
这是我的顶点着色器的“主要部分”:
void main(void) {
varying_Color = attribute_Color;
gl_Position = projectionMatrix * viewMatrix * vec4(attribute_Position,1.0);
}
这是我计算投影矩阵的方法:
final int height = getSurfaceHeight();
final double aspect = getSurfaceWidth() / height;
final double maxDim = getMaxEnvDim();
final double zNear = maxDim / 1000;
final double zFar = maxDim*10;
final double frustum_length = zFar - zNear;
double fW, fH;
final double fovY = 45;
if (aspect > 1.0) {
fH = FastMath.tan(fovY / 360 * Math.PI) * zNear;
fW = fH * aspect;
} else {
fW = FastMath.tan(fovY / 360 * Math.PI) * zNear;
fH = fW / aspect;
}
projectionMatrix = new Matrix4f();
projectionMatrix.m00 = (float) (zNear / fW);
projectionMatrix.m11 = (float) (zNear / fH);
projectionMatrix.m22 = (float) -((zFar + zNear) / frustum_length);
projectionMatrix.m23 = -1;
projectionMatrix.m32 = (float) -((2 * zNear * zFar) / frustum_length);
projectionMatrix.m33 = 0;
这是我计算 View 矩阵的方法:
public static double[] CrossProduct(final double[] vect1, final double[] vect2) {
final double[] result = new double[3];
result[0] = vect1[1] * vect2[2] - vect1[2] * vect2[1];
result[1] = vect1[2] * vect2[0] - vect1[0] * vect2[2];
result[2] = vect1[0] * vect2[1] - vect1[1] * vect2[0];
return result;
}
public static double ScalarProduct(final double[] vect1, final double[] vect2) {
return vect1[0]*vect2[0]+vect1[1]*vect2[1]+vect1[2]*vect2[2];
}
public static double[] Normalize(final double[] vect) {
double[] result = new double[vect.length];
double sum = 0;
for (int i = 0; i < vect.length ; i++) {
sum += Math.abs(vect[i]);
}
for (int i = 0; i < vect.length ; i++) {
result[i] = vect[i] / sum;
}
return result;
}
public static Matrix4f createViewMatrix(ICamera camera) {
// see http://in2gpu.com/2015/05/17/view-matrix/
Matrix4f viewMatrix = new Matrix4f();
double[] fVect = new double[3]; // forward vector : direction vector of the camera.
double[] sVect = new double[3]; // orthogonal vector : "right" or "sideways" vector.
double[] vVect = new double[3]; // cross product between f and s.
double[] pVect = new double[3]; // camera position.
double sum = Math.abs(camera.getTarget().x - camera.getPosition().x)
+ Math.abs(camera.getTarget().y - camera.getPosition().y)
+ Math.abs(camera.getTarget().z - camera.getPosition().z);
fVect[0] = -(camera.getTarget().x - camera.getPosition().x) / sum;
fVect[1] = (camera.getTarget().y - camera.getPosition().y) / sum;
fVect[2] = -(camera.getTarget().z - camera.getPosition().z) / sum;
double[] crossProduct = CrossProduct(fVect,new double[]{camera.getOrientation().x,
-camera.getOrientation().y,camera.getOrientation().z});
sVect = Normalize(crossProduct);
vVect = CrossProduct(sVect,fVect);
pVect = new double[]{camera.getPosition().x,
-camera.getPosition().y,camera.getPosition().z};
viewMatrix.m00 = (float) sVect[0];
viewMatrix.m01 = (float) sVect[1];
viewMatrix.m02 = (float) sVect[2];
viewMatrix.m03 = (float) -ScalarProduct(sVect,pVect);
viewMatrix.m10 = (float) vVect[0];
viewMatrix.m11 = (float) vVect[1];
viewMatrix.m12 = (float) vVect[2];
viewMatrix.m13 = (float) -ScalarProduct(vVect,pVect);
viewMatrix.m20 = (float) fVect[0];
viewMatrix.m21 = (float) fVect[1];
viewMatrix.m22 = (float) fVect[2];
viewMatrix.m23 = (float) -ScalarProduct(fVect,pVect);
viewMatrix.m30 = (float) 0;
viewMatrix.m31 = (float) 0;
viewMatrix.m32 = (float) 0;
viewMatrix.m33 = (float) 1;
viewMatrix.transpose();
return viewMatrix;
}
请注意,当我使用简单的变换矩阵而不是 View 矩阵时,如果我对三角形应用旋转,它看起来是正确的。这让我知道问题出在我的 View 矩阵上,但我找不到我的错误......另请注意,我有一些奇怪的“-y”来获取 View 矩阵,因为我的 openGL 世界是一个间接坐标系。
提前致谢!
最佳答案
您的Normalize
函数很奇怪。您不是在计算 vector 的欧几里德长度,而是在计算其统一范数。
尝试将其更改为:
double[] result = new double[vect.length];
double sum = 0;
for (int i = 0; i < vect.length ; i++) {
sum += Math.pow(vect[i], 2);
}
for (int i = 0; i < vect.length ; i++) {
result[i] = vect[i] / Math.sqrt(sum);
}
return result;
您在 createViewMatrix
函数的这一行也有完全相同的问题:
double sum = Math.abs(camera.getTarget().x - camera.getPosition().x) +
Math.abs(camera.getTarget().y - camera.getPosition().y) +
Math.abs(camera.getTarget().z - camera.getPosition().z);
将其更改为:
double sum = Math.sqrt(Math.pow(camera.getTarget().x - camera.getPosition().x, 2) +
Math.pow(camera.getTarget().y - camera.getPosition().y, 2) +
Math.pow(camera.getTarget().z - camera.getPosition().z, 2));
关于java - OpenGL/着色器/矩阵 : Weird scaling of the shape after a rotation of the view matrix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37852969/
您好,我很确定我的问题很愚蠢,但我无法弄清楚它对我的生活有何影响。我有这个家庭作业,它基本上是为了加强我们在类里面学到的关于多态性的知识(顺便说一下,这是 C++)。该程序的基础是一个名为 shape
我是新手,所以需要任何帮助,当我要求一个例子时,我的教授给我了这段代码,我希望有一个工作模型...... from numpy import loadtxt import numpy as np fr
CSS 形状边距 和 外型不适用于我的系统。我正在使用最新版本的 Chrome。我唯一能想到的是我的操作系统是 Windows 7。这应该是一个问题吗? 这是JSFiddle .但是,由于在您的系统上
#tf.shape(tensor)和tensor.shape()的区别 ?
我要求提示以下问题。如何从事件表添加到指定的单元格形状?当我知道名称但不知道如何为...中的每个形状实现论坛时,我可以添加形状 目前我有这样的事情: Sub loop() Dim a As Integ
我在 Excel 中有一个流程设计(使用形状、连接器等)。 我需要的是有一个矩阵,每个形状都有所有的前辈和所有的后继者。 在 VBA 中,为此我正在尝试执行以下操作: - 我列出了所有的连接器(Sha
我正在使用 JavaFX 编写一个教育应用程序,用户可以在其中绘制和操作贝塞尔曲线 Line、QuadCurve 和 CubicCurve。这些曲线应该能够用鼠标拖动。我有两种选择: 1- 使用类 L
我正在尝试绘制 pandas 系列中列的直方图 ('df_plot')。因为我希望 y 轴是百分比(而不是计数),所以我使用权重选项来实现这一点。正如您在下面的堆栈跟踪中发现的那样,权重数组和数据系列
我尝试在 opencv dnn 中实现一个 tensorflow 模型。这是我遇到的错误: OpenCV: Can't create layer "flatten_1/Shape" of type "
我目前正在用 Java 开发一款游戏,我一直在尝试弄清楚如何在 Canvas 上绘制一个形状(例如圆形),在不同的形状(例如正方形)之上,但是只绘制与正方形相交的圆的部分,类似于 Photoshop
import cv2 import numpy as np import sys import time import os cap = cv2.VideoCa
我已经成功创建了 Keras 序列模型并对其进行了一段时间的训练。现在我试图做出一些预测,但即使使用与训练阶段相同的数据,它也会失败。 我收到此错误:{ValueError}检查输入时出错:预期 em
我正在尝试逐行分解程序。 Y 是一个数据矩阵,但我找不到任何关于 .shape[0] 究竟做了什么的具体数据。 for i in range(Y.shape[0]): if Y[i] == -
我正在尝试运行代码,但它给了我这个错误: 行,列,_ = frame.shape AttributeError:“tuple”对象没有属性“shape” 我正在使用OpenCV和python 3.6,
我想在 JavaFx 中的 Pane 上显示形状。我正在使用从空间数据库中选择的 Oracle JGeometry 对象,它有一个方法 createShape() 但它返回 java.awt.Shap
在此代码中: import pandas as pd myj='{"columns":["tablename","alias_tablename","real_tablename","
我正在尝试将 API 结果应用于两列。 下面是我的虚拟数据框。不幸的是,这不是很容易重现,因为我使用的是带有 key 和密码的 API...这只是为了让您了解尺寸。 但我希望也许有人能发现一个明显的问
我的代码是: final String json = getObjectMapper().writeValueAsString(JsonView.with(graph) .onClas
a=np.arange(240).reshape(3,4,20) b=np.arange(12).reshape(3,4) c=np.zeros((3,4),dtype=int) x=np.arang
我正在尝试从张量中提取某些数据,但出现了奇怪的错误。在这里,我将尝试生成错误: a=np.random.randn(5, 10, 5, 5) a[:, [1, 6], np.triu_indices(
我是一名优秀的程序员,十分优秀!