How to draw grids in OpenGL ES Android?

I want to draw a 10 by 10 grid that defines the plane of the earth so that the center is the source of the world’s coordinates. This is the code that is called for each row defined in the grid.

gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glLoadIdentity(); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVerticesBuffer); gl.glTranslatef(x, y, z); gl.glRotatef(rz, 0, 0, 1); gl.glRotatef(rx, 1, 0, 0); gl.glRotatef(ry, 0, 1, 0); gl.glDrawArrays(GL10.GL_LINES, 0, 2); gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 

The problem is that I see only one horizontal line. Therefore, I think something is wrong.

This is the code that defines the lines:

 Line line; for (int i = 0; i <= 10; i++) { // horizontal lines line = new Line(-50, 0, 0, 50, 0, 0, 0, 0, 1, 1); // blue line line.z = (i * 100) - 50; lines.add(line); // draw perspective lines line = new Line(-50, 0, 0, 50, 0, 0, 0, 0, 1, 1); // blue line line.x = (i * 100) - 50; line.ry = 90; lines.add(line); } 

For each row in the row collection, I call the drawing code in onDrawFrame.

+4
source share
2 answers

The reason is that you only draw one line. glDrawArrays basically draws opengl primitives from data. This way, the coordinates in your mVerticesBuffer buffer are drawn once by glDrawArrays.

An easy way to do what you want is:

  • Rotate / Return to starting position
  • Draw the first line with glDrawArrays ();
  • Use gl.glTranslatef (somenumber, 0, 0);
  • Draw again with the same call to glDrawArrays ();
  • Use gl.glRotatef (90, 0, 1, 0); to rotate around the y axis (or any axis you are in 0)
  • (Possibly move backward along the axis to go to the same starting position)
  • Repeat steps 2, 3, and 4 of the point.

A tidier and more efficient way to do this would be with a push and pop matrix, but for simplicity this should work if you're new to opengl.

+3
source

The solution given to you seems wonderful and should work to solve your problems. Probably the best solution is to create vertices once and save it in a file, you can read the file once and make a grid at a time, which will be much better in terms of performance and speed.

+1
source

Source: https://habr.com/ru/post/1342462/


All Articles