当前位置: 首页 > article >正文

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

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

代码流程

// 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);
}

http://www.kler.cn/a/455448.html

相关文章:

  • Linux shell脚本用于常见图片png、jpg、jpeg、webp、tiff格式批量转PDF文件
  • ElasticSearch - 深入解析 Elasticsearch Composite Aggregation 的分页与去重机制
  • 将多个 k8s yaml 配置文件合并为一个文件
  • 显示 Windows 任务栏
  • AES 与 SM4 加密算法:深度解析与对比
  • 【QED】爱丽丝与混沌的无尽海
  • AndroidStudio Ladybug中编译完成apk之后定制名字kts复制到指定目录
  • SpringCloudAlibaba升级手册-nacos问题记录
  • C# Winform简单的俄罗斯方块小游戏源码2
  • 深入理解Composer自动加载机制
  • Linux服务器pm2 运行chatgpt-on-wechat,搭建微信群ai机器人
  • 云图库平台(三)——后端用户模块开发
  • 查看php已安装扩展命令
  • 《CS2》报错dxgi.dll缺失怎么办?《CS2》游戏提示dxgi.dll缺失要怎么解决?
  • SpringBoot3——核心原理
  • 基于STM32的智能温湿度监控与自动调节系统设计
  • 嵌入式硬件杂谈(七)IGBT MOS管 三极管应用场景与区别
  • linux 中 Vi 和 Vim 的使用
  • openssh9.9P1-CentOS7升级包
  • 自适应滤波算法分类及详细介绍
  • 绿葆自助取袋机:以科技之力,共筑绿色医疗新风尚
  • 跟着 8.6k Star 的开源数据库,搞 RAG!
  • springcloud2023集成 knife4j 4.4.0 如何关闭
  • 说说 DinoGrid Open Edition 算法生成艺术背后的故事
  • 修改 ssh 默认访问端口
  • springai报错记录