PDA

View Full Version : Scrollbar



guest
09-03-2003, 11:24 AM
I've made a scrollbar to scroll some picture, build with statements like glBegin(GL_LINE), etc. So it's not a texture image. If I scroll the bar, it's reaction time is too slow, so I want to speed up the scrollingfunction. Can I put the picture, during run time in a texture? If yes, how?

I hope anybody can help me.

virtualchetan
09-03-2003, 12:18 PM
I'll suggest u to use some openGL based GUI toolkit like GLUI (google for 'GLUI') for the scrollbar.

The toolkit will take care of managing the scrollbar, what u will need to do is just get a value from the scrollbar and change your camera accordingly.

- Chetan

dorbie
09-04-2003, 12:13 AM
Make sure you have an image with packed 8 bit data per component per pixel like rgbrgbrgbrgb etc. etc for each pixel, and make calls like the following ONLY ONCE AT THE START but make sure it's AFTER you have your OpenGL context active:

glBindTexture(GL_TEXTURE_2D, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, your_image_pointer);

Then when you need to use the texture call:

glBindTexture(GL_TEXTURE_2D, 1);
glEnable(GL_TEXTURE_2D);

To draw a quad with the texture on it use the following code, just alter the glVertex calls to suit your needs, you obviously know what these do now.

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(0.0f, 1.0f);
glEnd();

Then when you're done call

glDisable(GL_TEXTURE_2D);

If you want to use other images simply use glBindTexture(GL_TEXTURE_2D, 2); with a different integer NOTE I'VE CHANGED THE ONE TO A TWO, that tells OpenGL this is a different texture, the second argument is a unique handle identifying the texture, you can use any numbers you like unless you want to play nicely with other software libraries you might link to. You can initialize lots of textures once at the start and use them later with the bind calls, this will ensure your code is nice & fast when you move the slider.

If you only have one texture you don't need the bind calls, however you will need to use them later if you add images and want to stay as fast as possible.

This is very basic and so the code looks surprisingly simple, once you get this working you can improve it with features like texture filtering etc.

This is all untested so excuse any typos. One thing you may want to do depending on your image is change the formats and use ABGR to speed up load time marginally depending on your image source, but just try to get something working first, then you can get fancy later.

dorbie
09-04-2003, 12:17 AM
P.S. Bind reserves 0 so don't use that integer, although I think if you don't bind at the start it'll use zero for your first load. I wouldn't rely on this though. If you bother with bind calls use a non zero unsigned int to identify each texture you load.