Hi, I'm working on implementing a deferred renderer in my SFML project.
However, there are some things that I am confused about. Specifically, I don't understand how to translate some OpenGL code I found on a tutorial (
http://ogldev.atspace.co.uk/www/tutorial35/tutorial35.html) to SFML.
I think the main thing is the interaction between the draw buffer and the fragment shader.
In the tutorial, the init() function of the GBuffer is as follows:
bool GBuffer
::Init(unsigned int WindowWidth
, unsigned int WindowHeight
){ // Create the FBO glGenFramebuffers
(1, &m_fbo
); glBindFramebuffer
(GL_DRAW_FRAMEBUFFER
, m_fbo
); // Create the gbuffer textures glGenTextures
(ARRAY_SIZE_IN_ELEMENTS
(m_textures
), m_textures
); glGenTextures
(1, &m_depthTexture
); for (unsigned int i
= 0 ; i
< ARRAY_SIZE_IN_ELEMENTS
(m_textures
) ; i
++) { glBindTexture
(GL_TEXTURE_2D
, m_textures
[i
]); glTexImage2D
(GL_TEXTURE_2D
, 0, GL_RGB32F
, WindowWidth
, WindowHeight
, 0, GL_RGB
, GL_FLOAT
, NULL
); glFramebufferTexture2D
(GL_DRAW_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
+ i
, GL_TEXTURE_2D
, m_textures
[i
], 0); } // depth glBindTexture
(GL_TEXTURE_2D
, m_depthTexture
); glTexImage2D
(GL_TEXTURE_2D
, 0, GL_DEPTH_COMPONENT32F
, WindowWidth
, WindowHeight
, 0, GL_DEPTH_COMPONENT
, GL_FLOAT
, NULL
); glFramebufferTexture2D
(GL_DRAW_FRAMEBUFFER
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, m_depthTexture
, 0); GLenum DrawBuffers
[] = { GL_COLOR_ATTACHMENT0
, GL_COLOR_ATTACHMENT1
, GL_COLOR_ATTACHMENT2
, GL_COLOR_ATTACHMENT3
}; glDrawBuffers
(ARRAY_SIZE_IN_ELEMENTS
(DrawBuffers
), DrawBuffers
); GLenum Status
= glCheckFramebufferStatus
(GL_FRAMEBUFFER
); if (Status
!= GL_FRAMEBUFFER_COMPLETE
) { printf("FB error, status: 0x%x\n", Status
); return false; } // restore default FBO glBindFramebuffer
(GL_DRAW_FRAMEBUFFER
, 0); return true;}Now, I think I have most of that down, except for the DrawBuffer thing (where it attaches the draw buffers so that the fragment shader can output those textures to the buffer.)
Here is my implementation in JSFML, where fbo is a RenderTexture, and the other variables are Textures:
public boolean init
(int windowWidth,
int windowHeight
) { // create the FBO try { fbo.
create(windowWidth, windowHeight
); } catch (TextureCreationException ex
) { System.
err.
println("Error creating F!"); return false; } // create gbuffer textures for (int i
= 0; i
< textures.
length; i
++) { try { textures
[i
].
create(windowWidth, windowHeight
); } catch (TextureCreationException ex
) { System.
err.
println("Error creating GBuffer textures!"); return false; } } // depth try { depthTexture.
create(windowWidth, windowHeight
); } catch (TextureCreationException ex
) { System.
err.
println("Error creating depth texture!"); return false; } return true; } As you can see, I stop at the depth texture creation, because I do not understand how to initialize the draw buffer. Also, I feel like I need to properly attach those textures to the framebuffer. How would I do that?
Can anyone help me translate that code, or point out any errors I have already?