How to save and redraw screen contents in OpenGL ES

I am working on a kind of game on the iPhone where the player travels through a programmable wormhole breed. To draw a wormhole, I selected a pixel width for arrays of textured vertical lines to implement the upper and lower walls of the wormhole. Each frame, all lines must be shifted to the left in order to realize the player’s movement, and new lines should be drowned in free space on the right. But when drawing 1000 textured rectangles every frame kills my FPS. And Im looking for a solution to save all the lines that drowned in the previous frame, and completely redraw them to a new offset position. It would be great if there was a way to draw textured rectangles in some kind of buffer that is larger than the screen, and then display this buffer on the screen.

I am a guest, these are newbies, because I am completely new in OpenGL. I spent hours trying to figure it out, but Haven succeeded. So any help is appreciated.

+4
source share
2 answers

To extend the answer to @Jerry, I will walk you through the steps since you are a beginner. First we will create a frame buffer object:

GLuint framebuffer; glGenFramebuffersOES(1, &framebuffer); glBindFramebufferOES(GL_FRAMEBUFFER_OES, framebuffer); 

Then we will create an empty texture to hold our snapshot. This is common material for creating OpenGL textures, and you can modify it to suit your needs, of course. The only line that should be noted is the glTexImage2D line - note that instead of the pixel data, you can pass NULL as the last coordinate, which creates an empty texture.

 GLuint texture; glGenTextures(1, &texture); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glBindTexture(GL_TEXTURE_2D, 0); glDisable(GL_TEXTURE_2D); 

Now we associate the texture with the frame buffer:

 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, texture, 0); 

and check if everything is ok:

 if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) return false; // uh oh, something went wrong 

Now we are set to draw!

 glBindFramebufferOES(GL_FRAMEBUFFER_OES, framebuffer); // do drawing here glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0); 

Finally, clear the frame buffer if you no longer need it:

 glDeleteFramebuffersOES(1, &framebuffer); 

Some reservations:

  • The frame buffer size must be strong.
  • You can go up to 1024x1024 on the latest iPhone, but perhaps this level of detail is not necessary.
+3
source

Offline I don’t know the exact size that will be available on a particular iPhone model, but the general idea would be to use a frame buffer object (FBO) to render the texture, then you can break pieces from that texture to the screen buffer.

+1
source

Source: https://habr.com/ru/post/1339574/


All Articles