How do you do nonlinear shading in OpenGL?
I am developing a visualization tool in OpenGL to visualize the output of a 3d finite element modeling application. The application uses a tetrahedral mesh (but I am only viewing the exterior facets, which are triangles). The output is a scalar variable, which I want to map to a color map (I already know how to do that). The tricky part is that the value of the variable in each cell is given by a polynomial function (I think it's of degree 3, but that hasn't been finalized yet) of the coordinates in that cell.
In OpenGL, if I use the "smooth" shading model, then if I create a polygon and give each vertex a different value, then it will automatically interpolate (linearly) between the values at the vertices in order to get the color values at the points in the interior. But that just gives a linear function in each cell, and I want it to be a nonlinear function that I specify. Is there a way to do this?
(Of course, one sol开发者_StackOverflowution would be to interpolate "manually" by drawing each cell as a composite of much smaller OpenGL polygons that are small enough that the color doesn't change much in each of them. But I want to know if OpenGL itself has a solution.)
You could either use a pixel shader if you have experience in GLSL (or the time to learn it), or render your scalar values to a texture and texture-map your triangles with it.
If you use a shader, you should be able to read the color values from your triangle's vertices and perform the interpolation yourself as you see fit.
Edit
I found a paper dealing with that exact problem: http://mgarland.org/files/papers/perpixel.pdf
精彩评论