OpenGL-ES 3.2 -> OpenGL-ES 2.0

Started by Aisaaax, August 18, 2022, 04:41:04

Previous topic - Next topic

Aisaaax

Hello.
Since our hardware on my project got downgraded, I need to adapt my previous program writtem with GLES 3.2 to an older GLES 2.0 version.
Thankfully, I'm not using a lot of 3.0+ features, so the transition should be rather smooth.
AFAIK I will only need to modify my shaders and also get rid of VAO mechanics, and here's the point that is confusing to me.

My mesh creation was done something like this:
public Mesh(float[] vertexes, int[] indexes, int lengthStep, int vertexSize, int vertexLayoutSlot)
            throws Exception
    {
            int vboID;
 
            vaoID = glGenVertexArrays();
            vboIDList = new ArrayList<>();
            glBindVertexArray(vaoID);
 
            // Vertices VBO generation
            vboID = glGenBuffers();
            vboIDList.add(vboID);
 
            vertexCount = vertexes.length;
            this.vertexSize = vertexSize;
            vertexBufferLength = calculateBufferLength(vertexCount, vertexSize);
 
            FloatBuffer vertexesBuffer = getFloatBuffer(vertexes, vertexBufferLength);
 
            glBindBuffer(GL_ARRAY_BUFFER, vboID);
            glBufferData(GL_ARRAY_BUFFER, vertexesBuffer, GL_STATIC_DRAW);
            glVertexAttribPointer(vertexLayoutSlot, vertexSize, GL_FLOAT, false, 0, 0);
            vertexAttrArrCount++;
 
            // Indices VBO generation
            vboID = glGenBuffers();
            vboIDList.add(vboID);
            indexCount = indexes.length;
            indexBufferLength = calculateBufferLength(indexCount, 3);       // every one vertex adds 3 indexes, so 3 indexes per node is standard
                                                                                    // may not be applicable to meshes with complex geometry
 
            IntBuffer indexesBuffer = getIntBuffer(indexes, indexBufferLength);
 
            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboID);
            glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexesBuffer, GL_STATIC_DRAW);
 
            // unbinding
            glBindBuffer(GL_ARRAY_BUFFER, 0);
            glBindVertexArray(0);
    }


And then for rendering, something like this:
public void render(ShaderProgram sp)
    {
            // draw the mesh
            glBindVertexArray(getVAO());
            for (int i = 0; i < vertexAttrArrCount; i++)
            {
                // enable the right number of vertex attribute arrays
                glEnableVertexAttribArray(i);
            }
 
            glDrawElements(GL_TRIANGLES, getIndexCount(), GL_UNSIGNED_INT, 0);
 
            // restore state
            glDisableVertexAttribArray(0);
            glDisableVertexAttribArray(1);
            glDisableVertexAttribArray(2);
            glBindVertexArray(0);
    }


As far as I understand, I need to get rid of VAO entirely, and then manually bind the VBO's each time I want to render something. So my constructor should be somehting like this, basically just remove any mention of VAO but when it comes to VBO's I still generate them in the same manner and store them in the array of VBO ID's:

public Mesh(float[] vertexes, int[] indexes, int lengthStep, int vertexSize, int vertexLayoutSlot)
            throws Exception
    {
            int vboID;
            vboIDList = new ArrayList<>();
 
            // Vertices VBO generation
            vboID = glGenBuffers();
            vboIDList.add(vboID);
 
            vertexCount = vertexes.length;
            this.vertexSize = vertexSize;
            vertexBufferLength = calculateBufferLength(vertexCount, vertexSize);
 
            FloatBuffer vertexesBuffer = getFloatBuffer(vertexes, vertexBufferLength);
 
            glBindBuffer(GL_ARRAY_BUFFER, vboID);
            glBufferData(GL_ARRAY_BUFFER, vertexesBuffer, GL_STATIC_DRAW);
            glVertexAttribPointer(vertexLayoutSlot, vertexSize, GL_FLOAT, false, 0, 0);
            vertexAttrArrCount++;
 
            // Indices VBO generation
            vboID = glGenBuffers();
            vboIDList.add(vboID);
            indexCount = indexes.length;
            indexBufferLength = calculateBufferLength(indexCount, 3);       // every one vertex adds 3 indexes, so 3 indexes per node is standard
                                                                                    // may not be applicable to meshes with complex geometry
 
            IntBuffer indexesBuffer = getIntBuffer(indexes, indexBufferLength);
 
            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboID);
            glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexesBuffer, GL_STATIC_DRAW);
 
            // unbinding
            glBindBuffer(GL_ARRAY_BUFFER, 0);
    }


And then to render I need to just bind all my VBO's associated with this mesh and then unbind them after. So again, basically what I was already doing, just take out the VAO mentions from the code.
public void render(ShaderProgram sp)
    {
            // draw the mesh
            for (int i = 0; i < vertexAttrArrCount; i++)
            {
                // enable the right number of vertex attribute arrays
                glEnableVertexAttribArray(i);
            }

            glDrawElements(GL_TRIANGLES, getIndexCount(), GL_UNSIGNED_INT, 0);

            // restore state
            glDisableVertexAttribArray(0);
            glDisableVertexAttribArray(1);
            glDisableVertexAttribArray(2);
    }


Is this correct? Or am I missing something?

Aisaaax

It was something like that.
For mesh constructor, I indeed need to just take out everything to do with VAO and just save my VBO's into array.

For rendering, it goes something like this:
            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbi);           

            glBindBuffer(GL_ARRAY_BUFFER, vboIDList.get(0));         //These 3 lines for each VBO
            glEnableVertexAttribArray(index);
            glVertexAttribPointer(index, size, type, normalized, stride, offset);

            glDrawElements(GL_TRIANGLES, getIndexCount(), GL_UNSIGNED_INT, 0);

            glDisableVertexAttribArray(0);
            glDisableVertexAttribArray(1);

            glBindBuffer(GL_ARRAY_BUFFER, 0);
            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);