Android Java 版本的 MSAA OpenGL ES 多重采样

最近多次被小伙伴问到 OpenGL 多重采样,其实前面文章里多次讲过了,就是构建2个缓冲区,多重采样缓冲区和目标解析缓冲区。

代码流程

c 复制代码
// Framebuffer IDs
private int msaaFBO;
private int msaaColorBuffer;
private int msaaDepthBuffer;

private int resolveFBO;
private int resolveTexture;

public void initFramebuffers(int width, int height) {
    // Step 1: Create MSAA FBO
    int[] fbo = new int[1];
    GLES30.glGenFramebuffers(1, fbo, 0);
    msaaFBO = fbo[0];
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, msaaFBO);

    // Create MSAA color buffer
    int[] renderbuffer = new int[1];
    GLES30.glGenRenderbuffers(1, renderbuffer, 0);
    msaaColorBuffer = renderbuffer[0];
    GLES30.glBindRenderbuffer(GLES30.GL_RENDERBUFFER, msaaColorBuffer);
    GLES30.glRenderbufferStorageMultisample(GLES30.GL_RENDERBUFFER, 4, GLES30.GL_RGBA8, width, height); // 4x MSAA
    GLES30.glFramebufferRenderbuffer(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_RENDERBUFFER, msaaColorBuffer);

    // Create MSAA depth buffer
    GLES30.glGenRenderbuffers(1, renderbuffer, 0);
    msaaDepthBuffer = renderbuffer[0];
    GLES30.glBindRenderbuffer(GLES30.GL_RENDERBUFFER, msaaDepthBuffer);
    GLES30.glRenderbufferStorageMultisample(GLES30.GL_RENDERBUFFER, 4, GLES30.GL_DEPTH_COMPONENT16, width, height);
    GLES30.glFramebufferRenderbuffer(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_ATTACHMENT, GLES30.GL_RENDERBUFFER, msaaDepthBuffer);

    if (GLES30.glCheckFramebufferStatus(GLES30.GL_FRAMEBUFFER) != GLES30.GL_FRAMEBUFFER_COMPLETE) {
        throw new RuntimeException("MSAA Framebuffer is not complete!");
    }

    // Step 2: Create Resolve FBO
    GLES30.glGenFramebuffers(1, fbo, 0);
    resolveFBO = fbo[0];
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, resolveFBO);

    // Create texture for resolved result
    int[] texture = new int[1];
    GLES30.glGenTextures(1, texture, 0);
    resolveTexture = texture[0];
    GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, resolveTexture);
    GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA, width, height, 0, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null);
    GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_LINEAR);
    GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR);
    GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, resolveTexture, 0);

    if (GLES30.glCheckFramebufferStatus(GLES30.GL_FRAMEBUFFER) != GLES30.GL_FRAMEBUFFER_COMPLETE) {
        throw new RuntimeException("Resolve Framebuffer is not complete!");
    }

    // Unbind framebuffer
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0);
}

public void renderScene() {
    // Step 3: Render to MSAA FBO
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, msaaFBO);
    GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT | GLES30.GL_DEPTH_BUFFER_BIT);

    // Render your scene here...

    // Step 4: Resolve to texture
    GLES30.glBindFramebuffer(GLES30.GL_READ_FRAMEBUFFER, msaaFBO);
    GLES30.glBindFramebuffer(GLES30.GL_DRAW_FRAMEBUFFER, resolveFBO);
    GLES30.glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GLES30.GL_COLOR_BUFFER_BIT, GLES30.GL_NEAREST);

    // Step 5: Unbind framebuffers
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0);

    // Step 6: Use resolveTexture for further processing (e.g., render to screen)
    renderQuadWithTexture(resolveTexture);
}
相关推荐
咖啡教室3 小时前
java日常开发笔记和开发问题记录
java
咖啡教室3 小时前
java练习项目记录笔记
java
鱼樱前端4 小时前
maven的基础安装和使用--mac/window版本
java·后端
RainbowSea4 小时前
6. RabbitMQ 死信队列的详细操作编写
java·消息队列·rabbitmq
RainbowSea4 小时前
5. RabbitMQ 消息队列中 Exchanges(交换机) 的详细说明
java·消息队列·rabbitmq
张风捷特烈4 小时前
Flutter 伪3D绘制#03 | 轴测投影原理分析
android·flutter·canvas
李少兄6 小时前
Unirest:优雅的Java HTTP客户端库
java·开发语言·http
此木|西贝6 小时前
【设计模式】原型模式
java·设计模式·原型模式
可乐加.糖6 小时前
一篇关于Netty相关的梳理总结
java·后端·网络协议·netty·信息与通信
s9123601016 小时前
rust 同时处理多个异步任务
java·数据库·rust