while my question may be pretty application-specific, I think I stems from the fact that I’m not yet fully grasping the programming paradigms of OpenGL. So bear with me.
So here’s my problem:
I have a grid of 3d sample points given in world space, at which some value V has been computed. Now, I want to visualize the V’s as a colored rectangle, one rectangle for each “slice”. While I can directly assign a color to the pixels belonging to the sample points, I need to interpolate V’s for the remaining pixels, which belong to world points that lie somewhere in the grid (but not at the sample points).
I know that OpenGL is able to interpolate fragment shader inputs according to their location on the primitive. This is, however, only an interpolation that takes place in 2d (on triangles, lines). I, on the other hand, need to trilinearly interpolate each V from the 8 surrounding neighbors.
In an attempt to determine these neighbors, I have tried to unproject the window coordinates of each fragment back to world coordinates. This is however, where I get stuck. I don’t know how to reliably find the 8 neighboring sample points (and therefore the V’s associated with them).
(In a second attempt, I have briefly looked into 3d textures. As I understand it, I would need to use buffered textures, which are only available for 1d.)
My question: My approach seems like a quirky backwards solution. Am I missing out on a more straightforward approach that could somehow associate the 8 V’s (of the neighbors) with each fragment, or better yet, give me the interpolated V’s?
Thank you for your answers. :o
Also, feel free to ask if something is unclear.
Yes, I want to render slices/planes that are aligned to the grid, but can lie between sample points. I don’t understand why bilinear interpolation would suffice. Also, assuming bilinear interpolation would suffice, how would I do the actual interpolation? If I just draw triangles, I get automatic interpolation but it considers only 3 (triangle) not 4 values (quadrilateral).
If the slices are between sample points (by “aligned to the grid”, I mean that slices actually pass through all the points in a “plane”), then you need trilinear filtering.
Texture lookups use bilinear (2D) or trilinear (3D) interpolation if you set the filter to GL_LINEAR.
So one way to approach this is to store the colours in a 3D texture, set the filters to GL_LINEAR, then draw each slice as a single rectangle with the appropriate texture coordinates. In order to have the corners of the rectangle use the values from the corner points directly, the texture coordinates should be combinations of 1/2N and 1-1/2N, where N is the number of points in that dimension. E.g. for a 64x64x64 texture, the texture coordinates should be 1/128=0.0078125 or 127/128=0.9921875).
Also: quads are typically implemented as pairs of triangles, with vertex attributes (e.g. colour) interpolated linearly across each triangle (i.e. you don’t get bilinear interpolation of vertex attributes for quads; you get a separate linear interpolation for each triangle, typically with a visible crease along the diagonal).
For the sake of closure… I found a pretty straightforward solution:
I created a 2d texture for whose color entries I do the trilinear interpolation myself. This texture then gets mapped onto a rectangle. This keeps the shader code at a minimum.