【发布时间】:2021-06-14 07:01:52
【问题描述】:
这是我的问题,我将它们列出以使其清楚:
- 我正在编写一个使用实例化绘制 2D 正方形的程序。
- 我的相机方向是(0,0,-1),相机向上是(0,1,0),相机位置是(0,0,3),当我按下一些键时相机位置会改变。
- 我想要的是,当我放大(相机靠近正方形)时,正方形的大小(在屏幕上)不会改变。所以在我的着色器中:
#version 330 core
layout(location = 0) in vec2 squareVertices;
layout(location = 1) in vec4 xysc;
out vec4 particlecolor;
uniform mat4 VP;
void main()
{
float particleSize = xysc.z;
float color = xysc.w;
gl_Position = VP* vec4(xysc.x, xysc.y, 2.0, 1.0) + vec4(squareVertices.x*particleSize,squareVertices.y*particleSize,0,0);
particlecolor = vec4(1.0f * color , 1.0f * (1-color), 0.0f, 0.5f);
}
请注意,为了保持正方形的大小不变,我所做的是:
1. transform the center of the square first
VP * vec4(xysc.x, xysc.y, 2.0, 1.0)
2. then compute one of the four corners (x,y,z,1) of the square
+ vec4(squareVertices.x*particleSize,squareVertices.y*particleSize,0,0);
代替:
gl_Position = VP* (vec4(xysc.x, xysc.y, 2.0, 1.0) + vec4(squareVertices.x*particleSize,squareVertices.y*particleSize,0,0));
但是,当我将相机移近 z=0 平面时。正方形的大小出乎意料地增长。哪里有问题?如有需要,我可以提供演示代码。
【问题讨论】: