1
votes

I have a task to prepare a hipsometric globe in OpenGL. Besides of the colours of the vertices, I need some vertices to be higher (mountains), some to be lower.

Currently, I have a program which generates a globe, only with points (every 1 degree), all on the same height, I am doing that by passing latitude ang longitude to my program in GLSL, and it calculates translation matrix for each vertex.

Now, I can easily imagine how to move vertices higher and lower - I will just add another uniform variable "height", and in GLSL I will do the rest. The same easy thing is with passing color - no problem.

However, I have no idea, how to create a whole map using triangles (well, quadrangles composed from two triangles). The best solution I have, is that when I have four vertices (x1, x2, x3, x4) which create a quadrangle, I could just transform a quadrangle (which was previously in the buffer) in that way, to produce a quadrangle which has corners in my four vertices (x1, x2, x3, x4). Then, I just pass it to GLSL and render it.

However, I don't know how to transform a quadrangle to create a new quadrangle with corners in my vertices. Is it possible? Or maybe another idea how to realize this quadrangles?

Additional info:

I am using OpenGL 3.3.

This is how I render points:

for (double lng = 0; lng < 360; lng++) {
    for (double lat = 0; lat < 180; lat++) {
        draw_dots(MVP, lat, lng);
    }
}

// draw_dots function:
glUniform1f(latID, lat);
glUniform1f(lngID, lng);
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
glDrawElements(GL_POINTS, 1, GL_UNSIGNED_INT, field1_location);

This is my vertex shader:

gl_Position = MVP * transl_mtx * vec4(vertexPosition_modelspace, 1);

where transl_mtx is translation matrix calculated from uniform variables "lat" and "lng".

1

1 Answers

3
votes

Nope, nope, nope.

Thing about GPU rendering is usually the biggest performance hit is the CPU-GPU sync point. Thus you typically want to reduce the number of commands per frame to absolute minimum. The one and only function we can't remove is a draw call; typically glDrawElements or gDrawArrays.

However, you're calling this function thousands of times each frame. Considering the fact that your GPU (most probably) has a few gigabytes a memory, here's a better solution:

  1. Generate the whole set of vertices offline to a regular float array.
  2. Copy that to a GPU via VBO.
  3. Use in variable instead of a uniform, and only pass MVP matrix via uniform.
  4. Use just one drawcall to render the whole model.

That way you can manipulate and prepare the data conveniently on a CPU once, and then use the absurd speed of GPU to draw it.