another_commander wrote: ↑Tue Jul 26, 2022 5:02 pm
Unfortunately, it requires render to texture support in the engine and currently we don't have that. RTT is certainly doable (and would allow for many more graphics and post processing improvements too), but it needs either someone with plenty of OpenGL experience to code it in or someone with enough time to learn how to do it.
I think it is quite easy to render to a texture. At least if I'm not misunderstanding the problem.
Basically before drawing, one can call
glBindFramebuffer(GL_FRAMEBUFFER, 0)
to draw to the screen, or
glBindFramebuffer(GL_FRAMEBUFFER, some_framebuffer_id)
to render into a texture that has been previously attached to
some_framebuffer_id
. It's a bit verbose to create this framebuffer though (see below).
In case this is indeed what you wanted, how could it be implemented such that it is useful? Have
- (void) drawUniverse
optionally render into a texture?
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
Create a framebuffer with texture (also found a
YouTube video that explains this, because I don't know anymore what's actually happening here, I just took this from an earlier project I had):
Code: Select all
// window width and height
int width = 1000, height = 500;
// creating texture that should be rendered into
GLuint texture_id;
glGenTextures(1, &texture_id);
glBindTexture(GL_TEXTURE_2D, texture_id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_WRAP_T, GL_REPEAT);
// create necessary render buffer for depth
GLuint depth_buffer_id;
glGenRenderbuffers(1, &depth_buffer_id);
glBindRenderbuffer(GL_RENDERBUFFER, depth_buffer_id);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT32F, width, height);
// create framebuffer and attach texture and depth buffer
GLuint target_framebuffer_id;
glGenFramebuffers(1, &target_framebuffer_id);
glBindFramebuffer(GL_FRAMEBUFFER, target_framebuffer_id);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth_buffer_id);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture_id, 0);
const GLenum att_type = GL_COLOR_ATTACHMENT0;
glDrawBuffers(1, &att_type);
// draw to screen
glBindFramebuffer(GL_FRAMEBUFFER, 0);
...
glDrawArrays(...)
// next, draw to texture (texure_id)
glBindFramebuffer(GL_FRAMEBUFFER, target_framebuffer_id);
...
glDrawArrays(...)