开发者

What are good OpenGL ES 1.x practices?

开发者 https://www.devze.com 2023-03-26 05:55 出处:网络
I just started working with OpenGL ES within Android but I suppose this could also go for other mobile platforms that use OpenGL ES. I\'ve gone over a few tutorials to get me started but I feel like t

I just started working with OpenGL ES within Android but I suppose this could also go for other mobile platforms that use OpenGL ES. I've gone over a few tutorials to get me started but I feel like there are various things that I am missing.

For instance is it good practice to have objects draw themselves individually? I have a cube object which draws itself, then I have a "panel" object which is an array of 10x10 cubes, and finally another object which consist of panels to create a cube like object. I have 7500 cubes that need to be drawn, though each one being very "small". Here's what the drawing function looks like:

public void draw(GL10 gl) {
    gl.glFrontFace(GL10.GL_CCW);

    gl.glEnable(GL10.GL_CULL_FACE);
    gl.glCullFace(GL10.GL_BACK);

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, verticesBuffer);

    gl.glColor4f(rgba[0], rgba[1], rgba[2], rgba[3]);

    gl.glTranslatef(x, y, z);
    gl.glRotatef(rx, 1, 0, 0);
    gl.glRotatef(ry, 0, 1, 0);
    gl.glRotatef(rz, 0, 0, 1);

    gl.glDrawElements(GL10.GL_TRIANGLES, numOfIndices,
                    GL10.GL_UNSIGNED_SHORT, indicesBuffer);

    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisable(GL10.GL_CULL_FACE);
}

Are there any other things which one should avoid when dealing with OpenGL ES?

Edit: Another thing I have been wondering, is it good practice to reduce the amount of calls to certain methods, such as

gl.glFrontFace(GL10.GL_CCW);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glCullFace(GL10.GL_BACK);

gl.glDisable(GL10.GL_CULL_FACE);

This 开发者_如何转开发is called for every object when drawn, is it a necessity or can it be called once in my upper most object once?

Also:

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

I realize that enabling and disabling may be necessary depending on situation but if it can be avoided, will this not adversely affect anything?


You can use a batcher to batch all the vertices into one array and then draw, thus using only one draw call.

EDIT: Here is an example from my project. I have several NPCs that I need to draw and update individually so that they move independently from one another.

// update
    for (int i = 0; i < npcs.length; i++) {
        npcs[i].update(touchEvents);
    }

    // draw
    npcBatcher.beginBatch(Assets.atlas);
    for (int i = 0; i < npcs.length; i++) {
        npcs[i].draw(npcBatcher);
    }
    npcBatcher.endBatch();

In my draw method I begin the batch, add to the batch and finally end the batch (which does the actually drawing). In the update method, I loop through each NPC object and update EACH NPC. In the update method for NPC object I randomly generate a few numbers and move them according to the random number (do all the logic that makes them behave differently). Since each NPC has their own update with random generated numbers, they all act differently. All the drawing is done at once. The drawing all at once does not matter, it is the update for each object that matters and is what you are looking for.


your opengl es performance is highly depend upon your number of gl calls, so try to reduce the gl calls. If its your only place to draw, there is no need to disable cullface and vertex array every time and put your enabling calls of cull face in initialization method only so that it will enables only once.

0

精彩评论

暂无评论...
验证码 换一张
取 消