0

I'm trying to get a blur working using a glsl shader. I have looked for it on the internet but found only found fragment shaders with a texture input. What if I want to blur things without a texture though? How would I do that?

Should look something like that:

img

Spektre
  • 49,595
  • 11
  • 110
  • 380
RobinCirex
  • 11
  • 4
  • 1
    Can you be more specific about the desired effect? I picture would help. – Hymns For Disco Aug 31 '21 at 00:53
  • edited the post – RobinCirex Aug 31 '21 at 00:59
  • It looks like you may want a general post-processing blur. This may be a good starting resource: https://learnopengl.com/In-Practice/2D-Game/Postprocessing – Hymns For Disco Aug 31 '21 at 03:48
  • looks like simple blur ... google convolution blur/smooth filters in case its Gaussian blur see https://stackoverflow.com/a/64845819/2521214 – Spektre Aug 31 '21 at 06:59
  • @Spektre the problem is that is has a uniform sampler2D texture and I don't know what to put in there as I don't have any texture... what would be the fix for that? – RobinCirex Aug 31 '21 at 13:27
  • 1
    @RobinCirex render to textute or use `glReadPixels` in first pass and then in second pass blur it using the texture obtained from first pass – Spektre Aug 31 '21 at 14:26
  • 1
    @RobinCirex where/how is your image located/stored? – Yakov Galka Aug 31 '21 at 17:04
  • @YakovGalka i don't have an image – RobinCirex Aug 31 '21 at 19:36
  • @Spektre okay, could you give an example of how to use that? – RobinCirex Aug 31 '21 at 19:36
  • 1
    @RobinCirex huh? 'blur' is an operation that's applied to images. In fact you just attached a sample image to your question above. This image must be stored somewhere in your system's memory -- my question is where. If it's not in a texture, is it in a framebuffer? system RAM? file? – Yakov Galka Aug 31 '21 at 19:46
  • @YakovGalka Oh, sorry, I misunderstood that. It's in a framebuffer, yes – RobinCirex Aug 31 '21 at 20:33
  • 2
    @RobinCirex see [OpenGL Scale Single Pixel Line](https://stackoverflow.com/a/43654398/2521214) for 2 pass rendering example using `glReadPixels` if you want speed and have capable HW use [FBO and render to texture](https://stackoverflow.com/a/43930271/2521214) instead ... – Spektre Aug 31 '21 at 21:13

1 Answers1

1

A shader cannot read from a framebuffer directly. Textures are the primary way for reading images from within a shader (there's also SSBO but I wouldn't suggest using it here).

Thus, put it short, the answer to "how to blur an image without a texture" is: create a texture! You can either copy your image from the framebuffer to the texture with glCopyTextureSubImage2D; or, even better, render to an off-screen FBO backed by the texture from the start.

EDIT: example of copying from current framebuffer to a texture (in C):

GLuint tex;
glCreateTextures(GL_TEXTURE_2D, 1, &tex);
glTextureStorage2D(tex, 1 /*levels*/, GL_SRGB8_ALPHA8, width, height);
glCopyTextureSubImage2D(tex, 0, 0, 0, 0, 0, width, height);
// tex now contains the content of framebuffer
Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
  • could you give an example of how to use glCopyTexSubImage2D and store the data? Because it doesn't return anything. Btw I'm doing this in Java, probably should've mentioned that before. – RobinCirex Sep 02 '21 at 15:45
  • Sorry to necro this but I saw an unanswered comment and couldn't resist. glCreateTextures in C takes a pointer to the tex value (hence the ampersand) and will just write into that value in-place. This mitigates the need for return values, though I can see how it would be confusing to non-C programmers, seeing as it looks like an input parameter, and the function has no return type. I haven't touched Java in years so can't say how it would work in that. It also depends on the library. If your version of glCreateTextures returns a value, you'll want to capture it. – Logix Feb 02 '23 at 07:55
  • @Logix OP was asking about an example of using glCopyTexSubImage2D, not glCreateTextures. And their comment was answered in the the EDIT to the answer above. – Yakov Galka Feb 02 '23 at 15:17
  • I think you've missed the point. I was answering @RobinCirex' query in the comment above. – Logix Feb 09 '23 at 18:30