# Thread: Show function on the surface of a sphere

1. ## 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?

AeRoOo

2. 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?

3. Originally Posted by Carmine
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?

4. 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.

Attachment 1238

5. 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.

6. 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.

Originally Posted by Brokenmind
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.
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.

7. 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
Originally Posted by AeRoOo
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.
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?

8. Finally i would like to have something like:

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?

9. Originally Posted by Brokenmind
The problem with a texture is that you have to know beforehand what function you would like to plot and cannot change it afterwards.
My textures are computed on the fly. As the satellites orbit the coverage swaths are updated. Also, the swath widths can be changed interactively. So I am constantly changing the texture.[/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.

10. 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 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•