0
\$\begingroup\$

I am a beginner to drawing with OpenGL ES and I have a problem. I am drawing a figure consisting of triangles in orthographic projection, but when I run my app, I see a black screen and system buttons only (I am using a real device and not an emulator).

Why do I get a black screen, and how could I fix it?

I am using OpenGL ES 1.0 version.

Initialization before drawing:

float[] verticesArray = new float[] {
        180.0f, 80.0f, 10.0f,
        260.0f, 80.0f, 10.0f,
        318.0f, 138.0f, 10.0f,

        260.0f, 80.0f, 10.0f,
        318.0f, 138.0f, 10.0f,
        318.0f, 216.0f, 10.0f,

        318.0f, 138.0f, 10.0f,
        318.0f, 216.0f, 10.0f,
        260.0f, 264.0f, 10.0f,

        318.0f, 216.0f, 10.0f,
        260.0f, 264.0f, 10.0f,
        180.0f, 264.0f, 10.0f,

        260.0f, 264.0f, 10.0f,
        180.0f, 264.0f, 10.0f,
        122.0f, 216.0f, 10.0f,

        180.0f, 264.0f, 10.0f,
        122.0f, 216.0f, 10.0f,
        122.0f, 138.0f, 10.0f,

        122.0f, 216.0f, 10.0f,
        122.0f, 138.0f, 10.0f,
        180.0f, 80.0f, 10.0f,

        122.0f, 138.0f, 10.0f,
        180.0f, 80.0f, 10.0f,
        260.0f, 80.0f, 10.0f
};
verticesAmountTotal = verticesArray.length/3;
coordsOfVerticesAmountTotal = verticesArray.length;

ByteBuffer byteBuffer = ByteBuffer.allocateDirect(coordsOfVerticesAmountTotal * 4);
byteBuffer.order(ByteOrder.nativeOrder());
vertices = byteBuffer.asFloatBuffer();       
vertices.put(verticesArray);
vertices.flip();

Drawing method:

gl.glViewport(0, 0, glGraphics.getWidth(), glGraphics.getHeight());
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0, 480, 0, 320, 30, -1);
gl.glColor4f((float)Math.random(), (float)Math.random(), (float)Math.random(), opacity);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertices);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, verticesAmountTotal);
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I don't see anything obviously wrong with your code, but here are some possible things you can look at:

  1. Is your call to glViewport() getting the values you think it is?
  2. What is your clear color set to? If you set it to something like yellow, do you see anything else?
  3. What is opacity set to?
  4. Does Math.random() default to 0 if you don't seed it? If so, are you seeding it?
  5. Is the vertex array getting set up before drawing?

Have you tried adding any calls to glGetError() to see if anything is going wrong? That might give you a clue as to what's happening.

\$\endgroup\$
6
  • \$\begingroup\$ 1) Gonna check it. 2)Clear color is not set at all, but when i drew one triangle is was not a problem, there was a red triangle on a black screen. 3)opacity is 0.1 and it is incremented by 0.1 when i touch the screen(1 max value). 4) Don't know what is seeding, is the mistake here? 5) yes, and u can see the init code above)) \$\endgroup\$
    – Maxgmer
    Mar 20, 2017 at 6:47
  • \$\begingroup\$ Sorry if I wasn't clear on point 5. I see you've written the init code, but are you sure it's actually getting called before you call the render code? I've made mistakes like that before, so just thought I'd mention it. \$\endgroup\$ Mar 20, 2017 at 15:43
  • \$\begingroup\$ yes, im sure bcoz I drew one triangle before the same way and it was actually drawn. I will check glViewport values today, maybe the problem is hidden there as. \$\endgroup\$
    – Maxgmer
    Mar 20, 2017 at 16:02
  • \$\begingroup\$ I ve checked glViewPort values, they are correct ( \$\endgroup\$
    – Maxgmer
    Mar 20, 2017 at 16:31
  • \$\begingroup\$ Hi again. I have changed my z values if vertices to 0 and everything started to work. I dont know why, maybe you can tell me? This is strange. \$\endgroup\$
    – Maxgmer
    Mar 21, 2017 at 18:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .