PDA

View Full Version : Draw image from matrix of colors (CFD Visualization)



torben
03-01-2017, 03:56 AM
Hi,

I am trying to write some OpenGL code in C for 2D fluid flow visualization. I have a matrix of data, such at the pressure in the fluid at x and y position. I would like to draw an image where the color at each position of the image represents the pressure in the fluid at that point (E.g red for high pressure, blue for low pressure etc). It would also be nice if the colors could be interpolated between the discrete positions of the matrix.

What is the correct and most efficient way to do this in OpenGL?

Dark Photon
03-01-2017, 05:10 AM
There's all kinds of ways to do this. One would be to feed in your pressure state at a timestep into a shader via 2D texture. For each pixel, the shader would lookup the 2D pressure value under that pixel by looking up into the texture with interpolation, figure out where it is between a min and max pressure value (i.e. map to a 0..1 value), and then use that 0..1 value to lookup into a 1D color ramp texture with interpolation which you populate with colors you see fit (e.g. 0 = blue, 1 = red, rainbow in between).

torben
03-01-2017, 06:21 AM
There's all kinds of ways to do this. One would be to feed in your pressure state at a timestep into a shader via 2D texture. For each pixel, the shader would lookup the 2D pressure value under that pixel by looking up into the texture with interpolation, figure out where it is between a min and max pressure value (i.e. map to a 0..1 value), and then use that 0..1 value to lookup into a 1D color ramp texture with interpolation which you populate with colors you see fit (e.g. 0 = blue, 1 = red, rainbow in between).

Thanks a lot for your reply.

I have actually never user OpenGL before, so I am not very familiar with terms like texture and shader. Could you please point me in the right direction of which functions to use for this purpose?

Dark Photon
03-01-2017, 05:01 PM
I have actually never user OpenGL before, so I am not very familiar with terms like texture and shader. Could you please point me in the right direction of which functions to use for this purpose?

There's quite a few.

If you're using this project as an excuse to learn OpenGL, I'd highly recommend picking up a good book on OpenGL programming such as the OpenGL Programming Guide or the OpenGL Superbible.

If you're not so much interested in learning OpenGL, I'd suggest instead using one of the many scientific visualization or image analysis packages or libraries already out there (some open source). I haven't been in the sci vis industry for years, but you might websearch "scientific visualization packages vtk" and see where it takes you.

torben
03-02-2017, 03:04 AM
There's quite a few.

If you're using this project as an excuse to learn OpenGL, I'd highly recommend picking up a good book on OpenGL programming such as the OpenGL Programming Guide or the OpenGL Superbible.

If you're not so much interested in learning OpenGL, I'd suggest instead using one of the many scientific visualization or image analysis packages or libraries already out there (some open source). I haven't been in the sci vis industry for years, but you might websearch "scientific visualization packages vtk" and see where it takes you.

The code is for a project at school, where the main goal is to learn to implement scientific methods in C. The professor is not very flexible on which tools to use, so I guess I'm stuck with pure C and OpenGL.

The professor suggested using and array of GL_QUADS, but I think this sounds like a very ineffective solution, as I would need 4 vertices for each point on the grid (exept for the boundaries). It also looks pretty bad, since the colors are only interpolated within each quad, not between them. This is why I'm looking for an alternative solution.

Dark Photon
03-03-2017, 05:04 AM
The professor suggested using and array of GL_QUADS, but I think this sounds like a very ineffective solution, as I would need 4 vertices for each point on the grid (exept for the boundaries). It also looks pretty bad, since the colors are only interpolated within each quad, not between them. This is why I'm looking for an alternative solution.

It sounds like you're considering the option where the colors are passed down as vertex attributes with each of the 4 vertices of the quad. That's one way to do it, but it has the disadvantage you mention.

Another way to get colors onto your quad is to do texture lookups in the fragment shader. With this, you can have different colors for every pixel that lies within your quad. I think this is probably what you're looking for.

torben
03-10-2017, 12:50 AM
It sounds like you're considering the option where the colors are passed down as vertex attributes with each of the 4 vertices of the quad. That's one way to do it, but it has the disadvantage you mention.

Another way to get colors onto your quad is to do texture lookups in the fragment shader. With this, you can have different colors for every pixel that lies within your quad. I think this is probably what you're looking for.

Okey, it seems like I need to learn some of the basics of OpenGL before I can solve this problem. Thanks a lot for pointing me in the right direction :)