Show function on the surface of a sphere

Hey Guys!

I’m a absolute beginner in OpenGL. I have created a sphere with the help of tutorials. Now i would like to display a function on the surface of the sphere. This means for every point (x,y,z) on the surface of the sphere i compute a scalar value f(xi,yi,zi) = si and assign a color to this value.

Do someone of you know a tutorial that shows how to achieve this or can explain it to me?

Thanks in advance,

AeRoOo

I have created a sphere with the help of tutorials. Now i would like to display a function on the surface of the sphere. This means for every point (x,y,z) on the surface of the sphere i compute a scalar value f(xi,yi,zi) = si and assign a color to this value.
Depends on what you mean by ‘create’. Did you use gluSphere, or did you define surface polygons by computing the coordinates of the vertices?

I created it with gluSphere. Is this good / bad?

I created it with gluSphere. Is this good / bad?
Neither good or bad. What I would try to do in this situation is texture map the sphere with an image onto which you plot your function. gluSpheres have texture coordinates assigned, so you don’t have to worry about that. You will have to convert (xi, yi, zi) into 2D image coordinates, generate the image, then map it onto the sphere. As a first step, try mapping any image onto the surface of the sphere. If you can do that, it will be very easy to map images you generate onto the sphere.

Note - that I use the older, classic version of GL. Modern GL users might have other suggestions for you.

An example is shown below. I compute the image in my C code, then map it onto a rectangle (flat map) and a sphere (earth globe) using texture mapping.

[ATTACH=CONFIG]1238[/ATTACH]

Do you want to assign the colors to each vertex individually and interpolate between them (so that the colors fade), or do you want pixel-precise color transitions showing accurate curves?

The problem with a texture is that you have to know beforehand what function you would like to plot and cannot change it afterwards. Plus, you have to distort it at the poles of the sphere. A more dynamic approach could be achieved with shaders, where you can change the parameters at runtime according to your function or even recompute the shader into something new.

If possible, you should refrain from drawing the gluSphere() and create a VBO with all the vertices and normals you need. This gives you a much greater freedom in achieving what you want.

at Carmine: Thank you for your suggestion. I have implemented this idea in Matlab and it is a good starting point for my implementation in C++.

at Brokenmind: Sorry i’m a newbie in OpenGL. I have scattered data points on the surface of the sphere and i interpolate between this points (this is the function i want to show). So i can compute a value for an arbitrary point on the sphere.

[QUOTE=Brokenmind;1258365]A more dynamic approach could be achieved with shaders, where you can change the parameters at runtime according to your function or even recompute the shader into something new.

If possible, you should refrain from drawing the gluSphere() and create a VBO with all the vertices and normals you need. This gives you a much greater freedom in achieving what you want.[/QUOTE]

That sounds very interesting. Could you please guide me to some good tutorials to create a VBO and the more dynamic approach with the shader?

Thank you two for your help.

If you only have scattered points on the sphere’s surface, there is no need for shaders. They work best if you have a mathematical description for the function you want to show, which I thought you meant by saying

With scattered points, this might be a little more difficult, as they cannot be assigned to the default sphere coordinates given by gluSphere() or any custom-made regular sphere representation, e.g. icosahedron. You could of course interpolate your points and imitate a smooth curve, but that depends on the values you want to plot. You dont have some graphical example of what you want to do, by chance? :wink:

Finally i would like to have something like:

[ATTACH=CONFIG]594[/ATTACH]

So my idea is to interpolate between my scattered data points (= position + e.g. temperature). This gives me a function for e.g. the temperature over the whole sphere and i can determine for an arbitrary point on the sphere the interpolated temperature. Now i’m not sure how to visualize this function like it is shown in the example image. As last step i will map the outlines of Earth’s continents on the sphere.

Does this help?

Plus, you have to distort it at the poles of the sphere.
This is done with a very straightforward lat-lon to spherical coordinates conversion.[/QUOTE]

Plus, you have to distort it at the poles of the sphere.
This is a very straightforward lat-lon to spherical conversion.

Would this run faster with shaders and VBO’s? Probably - but I’m satisfied with what I have now.

You got me; I’ve never done sphere mapping before and thus am not familiar with the coordinate conversion.

If the creation of the texture is computationally efficient, then this is no problem. My experience with procedural textures is that the creation can take quite some time, so the process either takes quite long or the result needs to be fairly small. Do you have a basic texture on which you only highlight certain areas? Again, I didn’t try this before.

Anyway, I think your solution is much more suited for the OP than mine :wink: I was expecting a more noise-like surface like this, but for a reality-based globe, this is hardly an option.

€dit: This might be faster with VBOs and shaders and might give you more freedom in doing fancy things, but you’re right, it might not be necessary.

[QUOTE=Brokenmind;1258386]If the creation of the texture is computationally efficient, then this is no problem.[/QUOTE]The sim runs ~40 fps without computing map textures. With texture computations it’s ~2 fps, which is on the slow side. For this application I don’t need speed. The texture image is 1080x540 pixels.

Do you have a basic texture on which you only highlight certain areas?
No - every pixel in the image is recomputed every time. The resulting texture is applied to both the flat map and the sphere. ‘Computing’ a pixel means converting it from pixel to lat-lon to spherical coordinates, checking for satellite visibility, and coloring the pixel accordingly. This conversion is necessary for me to get a value for each pixel.

OP would not have to do this conversion. He already has values at various lat lons. What he could do is plot his points in a rectangular viewport (with lat-lon dimensions) and triangulate those points to get polygons. GL will automatically do the color interpolation for him, assuming he applies colors to each point. He would then need to overlay his map of the continents. The hardest part here is the triangulation. If there aren’t many points and they don’t change location with time, he could do it by hand. Otherwise he’s going to have to apply an algorithm such as Delaunay Triangulation.

Thank you two for your constructive discussion.

In fact i have very few points. Worst case: On one side clusters of points and on the other side large gaps.

If i have understood you correctly i can create a triangulation, assign a color value on every vertex and OpenGL do the interpolation for me inside the triangle? I will look for a good tutorial to test this for a single triangle.

Because of the fact that i have only a few points on the sphere i don’t want to triangulate only the points and let OpenGL do a linear interpolation inside the triangles. I want to use the information from my own interpolation. Would it be a good idea to use a good triangulation something like this, compute for every vertex the value with my own interpolation and then let OpenGL interpolate it. Or is this a bad idea?

An idea for the future would be to blend between functions (like i have a visualization for the temperature in 2013 and blend to the temperature in 2014).

If you have few points which are unevenly distributed and form a convex geometry around the center of the imaginary sphere, you could extrapolate the values to map a sphere and assign the color values resulting this calculation to a sphere.
Whether you use an icosahedron sphere or the gluSphere doesn’t matter. I personally prefer the icosahedron sphere because of the even distribution of triangles (the gluSphere is very dense at the poles and very sparse at the equator), but it is much more complex to build up.

With the extrapolation, I mean something like this:

only that the points in the left image correspond to your data points and get colors assigned.

@ Brokenmind: Your idea seems very interesting to me. I think it is like subdivision? Could you do me a favor and make an test image with more clusters (distance between some points very small). I would like to see how this influence the result.

A test image like the above one? I’m afraid it’s from Google :wink: But there’s an image for everything: Here, for example. I do have selfwritten code for such a sphere, but no project to display it atm. You could test it yourself, just take the template from here.

The issue you face is assigning to the sphere points the colors from your “object consisting of the coordinates that have colors”. How detailed you draw this sphere does not really matter, since you have a texture and only little light influence (in the yellow image, the lighting role is much stronger than with a texture). Shading also greatly reduces the amount of faces needed to display a smooth surface (see here).

By the way, this image illustrates what I mean by “projecting on the sphere”, only not a vertical projection, but one to the center of the sphere.