OpenGL ES Dai a textura
-
06-07-2019 - |
Pergunta
Eu tenho tido dificuldade em encontrar código simples para renderizar uma cena com uma textura em OpenGL ES (especificamente para o iPhone, se o que importa). Estou interessado em saber o seguinte:
- Como você renderizar uma cena com uma textura em OpenGL ES?
- Quais parâmetros você deve usar para criar uma textura que é capaz de ser um destino de processamento em OpenGL ES?
- Existem quaisquer implicações com a aplicação desta textura prestados a outras primitivas?
Solução
Isto é como eu estou fazendo isso.
Eu definir uma variável de textura (eu uso classe Texture2D
da Apple, mas você pode usar um ID de textura OpenGL se você quiser), e um buffer de quadros:
Texture2d * texture;
GLuint textureFrameBuffer;
Então, em algum ponto, eu criar a textura, frame buffer e anexar o renderbuffer. Isso você só precisa fazer isso uma vez:
texture = [[Texture2D alloc] initWithData:0
pixelFormat:kTexture2DPixelFormat_RGB888
pixelsWide:32
pixelsHigh:32
contentSize:CGSizeMake(width, height)];
// create framebuffer
glGenFramebuffersOES(1, &textureFrameBuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, textureFrameBuffer);
// attach renderbuffer
glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, texture.name, 0);
// unbind frame buffer
glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Toda vez que eu quero processar a textura, eu faço:
glBindFramebufferOES(GL_FRAMEBUFFER_OES, textureFrameBuffer);
...
// GL commands
...
glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Sobre a pergunta 3, é isso, você pode usar a textura como se fosse qualquer outra textura.
Outras dicas
Para tornar a cena para uma textura você deve usar um framebuffer associado com uma textura. Aqui está um método que eu criei para simplificá-lo:
void glGenTextureFromFramebuffer(GLuint *t, GLuint *f, GLsizei w, GLsizei h)
{
glGenFramebuffers(1, f);
glGenTextures(1, t);
glBindFramebuffer(GL_FRAMEBUFFER, *f);
glBindTexture(GL_TEXTURE_2D, *t);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, *t, 0);
GLuint depthbuffer;
glGenRenderbuffers(1, &depthbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, w, h);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthbuffer);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(status != GL_FRAMEBUFFER_COMPLETE)
NSLog(@"Framebuffer status: %x", (int)status);
}
Você pode criar o frame buffer e a textura facilmente:
GLuint _texture, _framebuffer;
GLsizei w,h;
float scale = [UIScreen mainScreen].scale;
w = self.view.bounds.size.width * scale;
h = self.view.bounds.size.height * scale;
glGenTextureFromFramebuffer(&_texture, &_framebuffer, w, h);
Você pode usar mais tarde _framebuffer para renderizar a cena em _texture no seu método de sorteio:
glBindFramebuffer(GL_FRAMEBUFFER, _framebuffer);
//draw here the content you want in the texture
//_texture is now a texture with the drawn content
//bind the base framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);
//or if you use GLKit
[view bindDrawable];
//draw normaly
Agora você pode fazer o que quiser com a textura. Se você quiser fazer alguma pós-processamento (borrão, flor, sombra, etc ...) você pode!