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);
}
相关推荐
得寸进尺的鸡丝面15 分钟前
tomcat官网下载历史版本
java·tomcat
罗政18 分钟前
PDF书籍《手写调用链监控APM系统-Java版》第7章 插件与链路的结合:Tomcat插件实现
java·pdf·tomcat
程序猿毕设源码分享网26 分钟前
基于springboot校园招聘系统源码和论文
java·spring boot·后端
山山而川粤41 分钟前
美食推荐系统|Java|SSM|JSP|
java·开发语言·后端·学习·mysql
小小药43 分钟前
012-spring的注解开发
java·后端·spring
找了一圈尾巴1 小时前
Wend看源码-Java-集合学习(Queue)
java·学习
#sakura1 小时前
javaEE
java·开发语言·java-ee
m0_748240441 小时前
MySQL Workbench菜单汉化为中文
android·数据库·mysql
AI人H哥会Java2 小时前
【Spring】基于XML的Spring容器配置——Bean的作用域
java·开发语言·后端·spring·架构
紧跟先前的步伐2 小时前
java常用类(下)
java·开发语言·算法