Jump to content

Recommended Posts

Posted

Outputting vec4(gl_FragCoord.xy / DrawViewport.zw, 0.f, 1.f) as outColor[0] for all the scene geometry fragments into viewport directly:
image.png.b657b59de9e071f6c78a751bb8c81502.png

Outputting vec4(gl_FragCoord.xy / DrawViewport.zw, 0.f, 1.f) as outColor[0] for all the scene geometry fragments into texture buffer
and then assigning this texture values to the surface.basecolor:

image.png.d87441066cd1afc1d8dae9c33f0fae86.png

I had to reorient gl_FragCoord to (gl_FragCoord.x, DrawViewport.w-gl_FragCoord.y) for one of the cameras in order to achieve the pixel-perfect dithering effect between two cameras.

Posted

You might want to try getting the camera projection matrix instead. I believe that will account for the flipping of the Y coordinate that happens, because OpenGL considers +1 to face up in screen coordinates.

If it's an ortho camera, just use the CameraProjectionViewMatrix variable.

If it's a perspective camera, I believe you can still get the ortho projection with this function: ExtractCameraOrthoMatrix(in uint cameraID)

My job is to make tools you love, with the features you want, and performance you can't live without.

Posted

Also see Shaders/Utilities/ReconstructPosition.glsl, as you might find some of those functions useful.

My job is to make tools you love, with the features you want, and performance you can't live without.

Posted
6 hours ago, Josh said:

You might want to try getting the camera projection matrix instead. I believe that will account for the flipping of the Y coordinate that happens, because OpenGL considers +1 to face up in screen coordinates.
If it's an ortho camera, just use the CameraProjectionViewMatrix variable.
If it's a perspective camera, I believe you can still get the ortho projection with this function: ExtractCameraOrthoMatrix(in uint cameraID)

Ok, not sure how it should help, but I will see if I can utilize projection view matrix for it. Currently I fix this problem for myself with the following trick:

if (CameraRange.x == 0.125f)
{
  // NOTE: gl_FragCoord has different orientation when drawn into texture buffer
  ivec2    FragCoord_Reoriented = ivec2(gl_FragCoord.x, DrawViewport.w - gl_FragCoord.y);
  ...
}

 

2 hours ago, Josh said:

Also see Shaders/Utilities/ReconstructPosition.glsl, as you might find some of those functions useful.

I saw these utility methods while thinking on how to reconstruct position from a depth cubemap. They work as transform from the screen to the world, but in the case stated what I need is just a consistency of the screen coordinates between drawing to the viewport and texture buffer render target.

I also have some guts feeling that depth pre-pass doesn't work while rendering to a texture buffer. Maybe I will make some minimal samples to reproduce both issues, but as for now it doesn't stop me moving towards more difficult scenarios for my visibility filter.
image.png.b314d1d659d880e907ce1df56f2b4d17.png

Posted

Okay, I think this problem is solved then. Thank you for reporting.

My job is to make tools you love, with the features you want, and performance you can't live without.

  • 3 months later...
Posted

Hello, I would like to revive this topic, because I've stumbled on the same issue again and this time I can provide a good small sample for it.

main.cpp:

#include "Leadwerks.h"

namespace lwe = Leadwerks;

int main(int argc, const char* argv[])
{
  auto displays    = lwe::GetDisplays();
  auto window      = lwe::CreateWindow("gl_FragCoord", 0, 0, 720, 720, displays[0], lwe::WINDOW_DEFAULT);
  auto framebuffer = lwe::CreateFramebuffer(window);
  auto world       = lwe::CreateWorld();

  auto light  = lwe::CreateDirectionalLight(world);
  auto cone_0 = lwe::CreateCone(world, 0.5f, 1.f, 64);
  auto cube_f = lwe::CreateBox(world);

  cube_f->SetScale(100.f, 1.f, 100.f);
  cube_f->SetPosition(0.f, -3.001f, 0.f);
  cone_0->SetPosition(0.0f, +1.f, +2.f);

  light->SetRotation(60, 0, 0);
  light->SetColor(0.5f);
  world->SetAmbientLight(0.1f, 0.1f, 0.1f);

  auto camera_0 = lwe::CreateCamera(world);
  auto camera_1 = lwe::CreateCamera(world);

  auto camera_0_texture_0 = lwe::CreateTexture(lwe::TEXTURE_2D, 720, 720);
  auto camera_0_texbuff   = lwe::CreateTextureBuffer(720, 720, 1, true);
  camera_0_texbuff->SetColorAttachment(camera_0_texture_0, 0);

  camera_0->SetRenderTarget(camera_0_texbuff);
  camera_0->SetOrder(0);

  camera_1->SetLighting(false);
  camera_1->SetRenderLayers(0);
  camera_1->SetOrder(1);

  auto camera_1_effect    = lwe::LoadPostEffect("Shaders/BaseColor.fx");
  auto camera_1_effect_id = camera_1->AddPostEffect(camera_1_effect);
  camera_1->EnablePostEffect(camera_1_effect_id);
  camera_1->SetUniform(camera_1_effect_id, "ColorBuffer", camera_0_texture_0);

  auto texturebuff_rt = lwe::CreateTextureBuffer(2, 2, 1, false);
  auto framebuffer_rt = nullptr;

  while (window->Closed() == false &&
         window->KeyDown(lwe::KEY_ESCAPE) == false)
  {
#if 1
    if (window->KeyHit(lwe::KEY_D1))
    { // Output to framebuffer from camera_1 (post effect Shaders/BaseColor.fx)
      camera_0->SetRenderTarget(camera_0_texbuff);
      camera_1->SetRenderTarget(framebuffer_rt);
    }
    if (window->KeyHit(lwe::KEY_D2))
    { // Output to framebuffer from camera_0 (post effect is ignored)
      camera_0->SetRenderTarget(framebuffer_rt);
      camera_1->SetRenderTarget(texturebuff_rt);
    }
#endif

    world->Update();
    world->Render(framebuffer);
  }

  return 0;
}

BaseColor.fx:

{
  "posteffect": {
    "subpasses": [
      {
        "shader": {
          "float32": {
            "vertex":   "Shaders/PostEffects/PostEffect.vert",
            "fragment": "Shaders/PostEffects/BaseColor.frag"
          }
        }
      }
    ]
  }
}


 lwe::KEY_D1: 

image.thumb.png.684b475d6c06e65bab60ae2a46066adb.png

lwe::KEY_D2:

image.thumb.png.9292ab057088ca49894b4ea01e0b4527.png

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...