Search code examples
openglparticle-system

OpenGL particle-system, problem with projection matrix


I am trying to implement a particle system in Opengl with Kotlin and Joml. Following the advices of a tutorial I only have to create a projection matrix for the vertex shader and to do perspective division with ortho(). This is my code for the matrix

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

and my vertex shader:

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);

}

The result is that the quad is positioned in the bottom left corner of the screen, it is a square now and it's size is correct. Before it has been at screen center with offset vector (0,0) and with it's left bottom corner. Moreover it is not possible to move it by the offset vector any longer. I don't really understand why. This is my init and render method

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)

}

Solution

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

    It is certainly possible to move your object by changing offset. You just set it up in a way that offset is now in pixels (relative to an assumed 1280x720 viewport size, which might or might not be the exact pixel size of your viewport). So if without the projection matrix, an offset of 1 meant to move half the viewport width (which is 2 in NDC), and offset of 1 now means just 1/1280 of the viewport width.