Using OpenGL shaders to plot 4D points in 2D in C++ [duplicate]
Possible Duplicate:
Visualising 4D objects in OpenGL
I have a set of 4 dimensional data points (lets call the dimensions X,Y,A, and B) that I'd like to plot in 6 different 2d plots (plot XxY, XxA, XxB, YxA, etc...).
In a previous question I asked about the proper way to store and plot this data. The solution was to use a VBO that stored the 4 dimensional vertices. Then using different vertex shaders I could select which dimensions are plotted in each of the 2d plots.
I've spent a few weeks looking around for openGL tutorials on how to do this but I 开发者_C百科haven't yet found something that makes sense.
My question is this: In c++, how can I define a shader that would allow me to plot only 2 dimensions of a 4 dimensional point in a VBO, and do I need to define a single shader for all 6 2d plots or do I need a shader for each of 6 different plots?
Finally how do I incorporate the shader into the plotting code so it gets used by openGL?
Since shaders can work with 4 dimensional vectors and 4x4 matrix, we can be smart and use only one vertex shader to do the trick. This shader will take three inputs:
- the point data (a 4 floating vector),
- a selection 4x4 matrix,
- a projection 4x4 matrix.
All the magic is done by the selection matrix, which will map you 4 coordinates vector to planar coordinates. Let's call the point data v
, the selection matrix S
and P
the projection matrix. The output of the vertex shader will be:
P * S * v
By setting correctly the coefficients of S
, you will be able to select which component in v
should appear in the result. For example, if you want to display YxB
, then S
will be:
So we have:
P is a standard projection matrix, which will help you to place your graph correctly (offsets and scale).
Following is an example of vertex shader implementation (not tested, may contain mistakes) for OpenGl3:
#version 130
in vec4 vin;
uniform mat4 s;
uniform mat4 p;
out vec4 vout;
void main()
{
vec4 tmp = s * vin;
// Depending on your projection matrix,
// you may force the 4th component to 1.
tmp[3] = 1;
vout = p * tmp;
}
Notes:
glUniformMatrix4fv
is the function to use to define S
and P
.
You have this input:
in vec4 Points;
You can access to vec4 components by indexing them:
Point[0]; // Equals Point.x
You can supply the following uniforms to specify which components to use for plotting:
uniform int XCoordIndex;
uniform int YCoordIndex;
gl_Position = vec4(Point[XCoordIndex], Point[YCoordIndex], 0.0, 1.0)
精彩评论