OpenGL 粒子系统,投影矩阵问题

OpenGL particle-system, problem with projection matrix

我正在尝试使用 Kotlin 和 Joml 在 Opengl 中实现粒子系统。按照教程的建议,我只需为顶点着色器创建一个投影矩阵并使用 ortho() 进行透视除法。这是我的矩阵代码

    var projection = Matrix4f()
    projection.ortho(0.0f, 1280.0f, 0.0f, 720.0f, -1.0f, 1.0f)
    partShader.setMat4("projection", projection, false )

和我的顶点着色器:

layout (location = 0) in vec4 vertex;

out vec2 TexCoords;
    
uniform mat4 projection;
uniform vec2 offset; //position vector    

void main() {

    float scale = 10.0f;
    TexCoords = vertex.zw;
    gl_Position = projection * vec4((vertex.xy * scale) + offset, 0.0, 1.0);

}

结果四边形位于屏幕的左下角,现在是正方形,大小正确。在它以偏移向量 (0,0) 和左下角位于屏幕中心之前。此外,不可能再通过偏移向量移动它。我真的不明白为什么。这是我的初始化和渲染方法

class ParticleGenerator (var shader : ShaderProgram, var texture : Texture2D, var amount : Int) {

var particleList = mutableListOf<Particle>()
var lastUsedParticle = 0
var unusedParticles = 0
var firstUnusedParticle = 0

lateinit var partTex: Texture2D

private var vao = 0
private var vbo = 0
private var ibo = 0

private var indexcount = 0

init {

    var particle_quad : FloatArray = floatArrayOf(

            0.0f, 1.0f, 0.0f, 1.0f,
            1.0f, 0.0f, 1.0f, 0.0f,
            0.0f, 0.0f, 0.0f, 0.0f,

            0.0f, 1.0f, 0.0f, 1.0f,
            1.0f, 1.0f, 1.0f, 1.0f,
            1.0f, 0.0f, 1.0f, 0.0f

    )

    var index_quad = intArrayOf(
            0, 1, 2,
            2, 3, 0
    )

    indexcount = index_quad.size

    this.vao = ARBVertexArrayObject.glGenVertexArrays()
    ARBVertexArrayObject.glBindVertexArray(vao)

    this.vbo = GL15.glGenBuffers()
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo)
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, particle_quad, GL15.GL_STATIC_DRAW)

    this.ibo = GL15.glGenBuffers()
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo)
    GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, index_quad, GL15.GL_STATIC_DRAW)

    glEnableVertexAttribArray(0)
    glVertexAttribPointer(0, 4, GL_FLOAT, false, 16, 0)
    glBindVertexArray(0)

}

fun render() {

    glEnable(GL_BLEND)
    glBlendFunc(GL_SRC_ALPHA, GL11.GL_ONE)
    glBindVertexArray(vao)

    glDrawArrays(GL_TRIANGLE_STRIP, 0, indexcount)
    glDisable(GL_BLEND)
    glBindVertexArray(0)

}

Moreover it is not possible to move it by the offset vector any longer. I don't really understand why.

当然可以通过更改 offset 来移动您的对象。您只需将其设置为 offset 现在以像素为单位(相对于假定的 1280x720 视口大小,这可能是也可能不是您视口的确切像素大小)。因此,如果没有投影矩阵,偏移量 1 意味着移动视口宽度的一半(在 NDC 中为 2),而偏移量 1 现在仅意味着视口宽度的 1/1280