Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do draw to a texture in OpenGL

Now that my OpenGL application is getting larger and more complex, I am noticing that it's also getting a little slow on very low-end systems such as Netbooks. In Java, I am able to get around this by drawing to a BufferedImage then drawing that to the screen and updating the cached render every one in a while. How would I go about doing this in OpenGL with C++?

I found a few guides but they seem to only work on newer hardware/specific Nvidia cards. Since the cached rendering operations will only be updated every once in a while, i can sacrifice speed for compatability.

glBegin(GL_QUADS);
       setColor(DARK_BLUE); 
       glVertex2f(0, 0);                  //TL
       glVertex2f(appWidth, 0);           //TR
       setColor(LIGHT_BLUE);
       glVertex2f(appWidth, appHeight);   //BR
       glVertex2f(0, appHeight);          //BR
glEnd();

This is something that I am especially concerned about. A gradient that takes up the entire screen is being re-drawn many times per second. How can I cache it to a texture then just draw that texture to increase performance?

Also, a trick I use in Java is to render it to a 1 X height texture then scale that to width x height to increase the performance and lower memory usage. Is there such a trick with openGL?

like image 291
Alexander Avatar asked Dec 05 '22 06:12

Alexander


1 Answers

If you don't want to use Framebuffer Objects for compatibility reasons (but they are pretty widely available), you don't want to use the legacy (and non portable) Pbuffers either. That leaves you with the simple possibility of reading the contents of the framebuffer with glReadPixels and creating a new texture with that data using glTexImage2D.

Let me add that I don't really think that in your case you are going to gain much. Drawing a texture onscreen requires at least texel access per pixel, that's not really a huge saving if the alternative is just interpolating a color as you are doing now!

like image 143
UncleZeiv Avatar answered Dec 07 '22 22:12

UncleZeiv