View Full Version : Show function on the surface of a sphere

AeRoOo

03-11-2014, 01:22 AM

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

Carmine

03-11-2014, 09:52 AM

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?

AeRoOo

03-11-2014, 11:19 AM

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?

Carmine

03-12-2014, 10:05 AM

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.

1238

Brokenmind

03-13-2014, 09:47 AM

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.

AeRoOo

03-13-2014, 04:16 PM

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.

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.

Brokenmind

03-13-2014, 04:53 PM

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

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? ;)

AeRoOo

03-14-2014, 03:18 AM

Finally i would like to have something like:

1239

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?

Carmine

03-14-2014, 09:13 AM

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.

Brokenmind

03-14-2014, 09:14 AM

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 (http://tpfto.files.wordpress.com/2012/02/marbles.png), 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.

Carmine

03-14-2014, 11:47 AM

If the creation of the texture is computationally efficient, then this is no problem.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.

AeRoOo

03-14-2014, 12:58 PM

Thank you two for your constructive discussion.

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.

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 (http://people.sc.fsu.edu/~jburkardt/datasets/sphere_grid/sphere_grid_icos1_f7_triangles.png), 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).

Brokenmind

03-14-2014, 01:03 PM

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:

http://upload.wikimedia.org/wikibooks/en/a/a8/Openscad-sphere.jpg

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

AeRoOo

03-14-2014, 03:37 PM

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

Brokenmind

03-14-2014, 06:23 PM

A test image like the above one? I'm afraid it's from Google ;) But there's an image for everything: Here, for example (http://brainimaging.waisman.wisc.edu/~chung/lb/sphere.jpg). 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 (http://www.3dbuzz.com/forum/threads/118279-Quick-solution-for-making-a-sphere-in-OpenGL?p=961840#post961840).

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 (http://3.bp.blogspot.com/-tQWFkqW_nIY/URghlJ-t17I/AAAAAAAAAdU/Tl8-zkPAY34/s1600/_SHADING.GIF)).

By the way, this (http://forums.odforce.net/uploads/monthly_01_2013/post-5178-0-17079600-1357751174.png) image illustrates what I mean by "projecting on the sphere", only not a vertical projection, but one to the center of the sphere.

Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.