开发者

Use index as coordinate in OpenGL

开发者 https://www.devze.com 2023-02-09 19:06 出处:网络
I want to implement a timeseries viewer that allows a user to zoom and smoothly pan. I\'ve done some immediate mode opengl before, but that\'s now deprecated in favor of VBOs.All the examples of VBOs

I want to implement a timeseries viewer that allows a user to zoom and smoothly pan.

I've done some immediate mode opengl before, but that's now deprecated in favor of VBOs. All the examples of VBOs I can find store XYZ coordinates of each and every point.

I suspect that I need to keep all my data in VRAM in order to get a framerate during pan that can be called "smooth", but I have only Y data (the dependent variable). X is an independent variable which can be calculated from the index, and Z is constant. If I have to store X and Z then my memory requirements (both buffer size and CPU->GPU block transfer) are tripled. And I have tens of millions of data points through which the user can pan, so the memory usage will be non-trivial.

Is there some technique for either drawing a 1-D vertex array, where the index is used as the other coordinate, or storing a 1-D array (probably in a texture?) and using a shader program to generate the XYZ? I'm under the impression that I need a simple shader anyway under the new fixed-feature-less pipeline model to implement scaling and translation, so if I could combine the generation of X and Z coordinates and scaling/translation of Y that would be ideal.

Is this even possible? Do you know of any sample code that does this? Or can you at least give me some pseudocode saying what GL functions to开发者_StackOverflow call in what order?

Thanks!

EDIT: To make sure this is clear, here's the equivalent immediate-mode code, and vertex array code:

// immediate
glBegin(GL_LINE_STRIP);
for( int i = 0; i < N; ++i )
    glVertex2(i, y[i]);
glEnd();

// vertex array
struct { float x, y; } v[N];
for( int i = 0; i < N; ++i ) {
    v[i].x = i;
    v[i].y = y[i];
}
glVertexPointer(2, GL_FLOAT, 0, v);
glDrawArrays(GL_LINE_STRIP, 0, N);

note that v[] is twice the size of y[].


That's perfectly fine for OpenGL.

Vertex Buffer Objects (VBO) can store any information you want in one of GL supported format. You can fill a VBO with just a single coordinate:

glGenBuffers( 1, &buf_id);
glBindBuffer( GL_ARRAY_BUFFER, buf_id );
glBufferData( GL_ARRAY_BUFFER, N*sizeof(float), data_ptr, GL_STATIC_DRAW );

And then bind the proper vertex attribute format for a draw:

glBindBuffer( GL_ARRAY_BUFFER, buf_id );
glEnableVertexAttribArray(0);  // hard-coded here for the sake of example
glVertexAttribPointer(0, 1, GL_FLOAT, false, 0, NULL);

In order to use it you'll need a simple shader program. The vertex shader can look like:

#version 130
in float at_coord_Y;

void main() {
    float coord_X = float(gl_VertexID);
    gl_Position = vec4(coord_X,at_coord_Y,0.0,1.0);
}

Before linking the shader program, you should bind it's at_coord_Y to the attribute index you'll use (=0 in my code):

glBindAttribLocation(program_id,0,"at_coord_Y");

Alternatively, you can ask the program after linking for the index to which this attribute was automatically assigned and then use it:

const int attrib_pos = glGetAttribLocation(program_id,"at_coord_Y");

Good luck!


Would you store ten millions of XY coordinates, in VRAM?

I would you suggest to compute those coordinates on CPU, and pass them to the shader pipeline as uniforms (since coordinates are fixed to the panned image).

Keep it simple.

0

精彩评论

暂无评论...
验证码 换一张
取 消