husker-dev / openglfx

OpenGL node for JavaFX
Apache License 2.0
88 stars 12 forks source link

Color Attachments #36

Closed yetyman closed 1 year ago

yetyman commented 1 year ago

Hello,

What framebuffer is used in this library to render to the screen? is it the default Framebuffer? or is it a custom framebuffer? Can i give it color attachments? If not, how might i set my own Framebuffer for this library to use? Or am totally on the wrong track for how to go about this?

I want to get a texture of unsigned integers representing the instance ids each pixel is drawn from.


int[] renderTargets = new int[1];
int[] renderTextures = new int[DRAW_BUFFERS.length];
glGenFramebuffers(renderTargets);
FRAMEBUFFER_INDEX = renderTargets[0];
CheckGPUErrors("Error generating framebuffers:");

glGenTextures(renderTextures);
FRAMEBUFFER_COLOR_TEX_INDEX = renderTextures[0];
FRAMEBUFFER_POINT_INDEX_TEX_INDEX = renderTextures[1];
CheckGPUErrors("Error generating texture buffers:");

glBindFramebuffer(GL_FRAMEBUFFER, FRAMEBUFFER_INDEX);
glBindTexture(GL_TEXTURE_2D, FRAMEBUFFER_COLOR_TEX_INDEX);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16, (int)viewportSize.getX(), (int)viewportSize.getY(), 0, GL_RGBA, GL_UNSIGNED_BYTE, (ByteBuffer) null);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, FRAMEBUFFER_COLOR_TEX_INDEX, 0);
CheckGPUErrors("Error generating color framebuffer:");

glBindFramebuffer(GL_FRAMEBUFFER, FRAMEBUFFER_INDEX);
glBindTexture(GL_TEXTURE_2D, FRAMEBUFFER_POINT_INDEX_TEX_INDEX);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32UI, (int)viewportSize.getX(), (int)viewportSize.getY(), 0, GL_RED_INTEGER, GL_UNSIGNED_INT, (IntBuffer) null);
CheckGPUErrors("Error generating pointIndex framebuffer3:");
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, FRAMEBUFFER_POINT_INDEX_TEX_INDEX, 0);
CheckGPUErrors("Error generating pointIndex framebuffer4:");

glBindFramebuffer(GL_FRAMEBUFFER, 0);
CheckGPUErrors("Error creating framebuffers:");

and in the Render call i am using

private static final int[] DRAW_BUFFERS = new int[]{ GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 };

private void resizeColorAttachments() {
    glBindFramebuffer(GL_FRAMEBUFFER, FRAMEBUFFER_INDEX);
    glBindTexture(GL_TEXTURE_2D, FRAMEBUFFER_COLOR_TEX_INDEX);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16, (int) viewportSize.getX(), (int) viewportSize.getY(), 0, GL_RGBA, GL_UNSIGNED_BYTE, (ByteBuffer) null);
    //glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, FRAMEBUFFER_COLOR_TEX_INDEX, 0);
    CheckGPUErrors("Error generating color framebuffer:");

    glBindFramebuffer(GL_FRAMEBUFFER, FRAMEBUFFER_INDEX);
    glBindTexture(GL_TEXTURE_2D, FRAMEBUFFER_POINT_INDEX_TEX_INDEX);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_R32UI, (int) viewportSize.getX(), (int) viewportSize.getY(), 0, GL_RED_INTEGER, GL_UNSIGNED_INT, (IntBuffer) null);
    CheckGPUErrors("Error generating pointIndex framebuffer3:");
    //glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, FRAMEBUFFER_POINT_INDEX_TEX_INDEX, 0);
    CheckGPUErrors("Error generating pointIndex framebuffer4:");

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    CheckGPUErrors("Error creating framebuffers:");
}

public void render(GLRenderEvent event){
    ...
    if(resized)
        resizeColorAttachments();

    ...

    glBindFramebuffer(GL_FRAMEBUFFER, FRAMEBUFFER_INDEX);
    glDrawBuffers(DRAW_BUFFERS);
    CheckGPUErrors("Error setting draw buffers:");
    glDrawElementsInstanced(...);

 }

everything works until i replace the framebuffer. How can i achieve a texture of instance ids with this library?

husker-dev commented 1 year ago

This library cannot guarantee the same configured framebufer because it uses 3 different rendering methods depending on the system. But this framebuffer always has 4 color channels.

If you want to use unusual framebuffers, I recommend you to use your own framebuffer and then render it to the existing one using shaders.

yetyman commented 1 year ago

Thank you!