# gl\_FragCoord is larger than the window size

**URL:** <https://discourse.libcinder.org/t/gl-fragcoord-is-larger-than-the-window-size/1733>\
**Category:** Using Cinder\
**Created:** [October 7, 2020, 4:47pm UTC](https://discourse.libcinder.org/t/gl-fragcoord-is-larger-than-the-window-size/1733 "2020-10-07T16:47:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![skmhrk1209](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@skmhrk1209](https://discourse.libcinder.org/u/skmhrk1209)\
**Post date:** [October 7, 2020, 4:47pm UTC](https://discourse.libcinder.org/t/gl-fragcoord-is-larger-than-the-window-size/1733/1 "2020-10-07T16:47:00Z")

</div>

Hello,

I wrote a simple GLSL vertex and fragment shader as follows:

```cpp
#version 150

uniform mat4 ciModelViewProjection;
in vec4 ciPosition;

void main()
{
    gl_Position = ciModelViewProjection * ciPosition;
}

```

```cpp
#version 150

uniform vec2 uResolution;
out vec4 oFragColor;

void main()
{
    oFragColor = vec4(vec3(gl_FragCoord.x < uResolution.x ? 0 : 1), 1.0);
}

```

and draw() and resize() as follows:

```cpp
void ExampleApp::draw()
{
	ci::gl::clear(ci::Color(0, 0, 0));
	ci::gl::ScopedGlslProg scopedGlslProg(mGlslProg);
	{
		ci::gl::drawSolidRect(ci::Rectf(ci::vec2(0, 0), ci::vec2(getWindowSize())));
	}
}

void ExampleApp::resize()
{
	if (mGlslProg)
	{
		mGlslProg->uniform("uResolution", ci::vec2(getWindowSize()));
	}
}

```

So, I think the result would be a white rectangle, but I got:

 ![result](https://canada1.discourse-cdn.com/flex030/uploads/libcinder/original/1X/b196824cc6d42b2608b4e0ba06627fe37151b10f.jpeg)

Does anyone know why the gl\_FragCoord is larger than the window size?

---

<div class="post-metadata">

**Author:** ![lithium](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/lithium/32/38_2.png) [@lithium](https://discourse.libcinder.org/u/lithium)\
**Post date:** [October 7, 2020, 11:30pm UTC](https://discourse.libcinder.org/t/gl-fragcoord-is-larger-than-the-window-size/1733/2 "2020-10-07T23:30:27Z")

</div>

I would expect that to be all black as opposed to all white since `gl_FragCoord.x` would always be ` < uResolution.x` (give or take 0.5 for pixel centres), but either way what you’re seeing is weird.

The code as you’ve pasted it works for me, are you certain the black (or white) you’re seeing is coming from that draw call? A few things:

1. Can you change your clear colour to make sure?
2. Do you have any errant transforms that might be shifting where the rectangle is drawn?
3. Try resetting the matrices with `gl::setMatricesWindow ( getWindowSize() )` before your rect draw.

The other unfortunate thing is that mac’s OpenGL driver is old and shit (a few years ago it was just shit) so it’s possible they aren’t respecting the spec’s idea of `gl_FragCoord`. As a general rule I avoid as many intrinsics as possible to remove these kinds of problems, I make enough mistakes on my own, I don’t need the driver’s help at all with that.

I would also suggest getting comfortable working in UV space where possible since it removes the requirement to know about the window size at all. In your example, your shaders would become:

```auto
uniform mat4 ciModelViewProjection;
in vec4 ciPosition;
in vec2 ciTexCoord0;

out vec2 UV;

void main()
{
    gl_Position = ciModelViewProjection * ciPosition;
    UV = ciTexCoord0;
}

```

```auto
in vec2 UV;
out vec4 oFragColor;
 
void main()
{
    oFragColor = vec4(vec3(UV.x < 1.0 ? 0 : 1), 1.0);
}

```

And you could remove all the resize code and passing any uniforms to the shader since everything is now in the `0..1` range, regardless of window size.

---

<div class="post-metadata">

**Author:** ![paul.houx](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/paul.houx/32/31_2.png) [@paul.houx](https://discourse.libcinder.org/u/paul.houx)\
**Post date:** [October 8, 2020, 7:55am UTC](https://discourse.libcinder.org/t/gl-fragcoord-is-larger-than-the-window-size/1733/3 "2020-10-08T07:55:51Z")

</div>

> [@lithium](#):
>
> Try resetting the matrices with `gl::setMatricesWindow ( getWindowSize() )` before your rect draw.

That’s probably it.

-Paul

---

<div class="post-metadata">

**Author:** ![skmhrk1209](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@skmhrk1209](https://discourse.libcinder.org/u/skmhrk1209)\
**Post date:** [October 8, 2020, 1:43pm UTC](https://discourse.libcinder.org/t/gl-fragcoord-is-larger-than-the-window-size/1733/4 "2020-10-08T13:43:17Z")

</div>

@lithium @paul.houx Thank you everyone!

First, I changed the clear color like `ci::gl::clear(ci::Color(1, 0, 0))`, but nothing has changed. So, I’m sure that the window is completely covered with the rectangle.

Next, I called `ci::gl::setMatricesWindow(getWindowSize())`, but nothing has changed. This is natural because any geometrical transformation has not been applied.

Using `ciTexCoord0` instead of `gl_FragCoord` works well.  
However, `gl_FragCoord` is generally different from `ciTexCoord0`, so I think this is not a fundamental solution.

I’m using high density display. Is this related to this issue?

---

<div class="post-metadata">

**Author:** ![lithium](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/lithium/32/38_2.png) [@lithium](https://discourse.libcinder.org/u/lithium)\
**Post date:** [October 8, 2020, 1:57pm UTC](https://discourse.libcinder.org/t/gl-fragcoord-is-larger-than-the-window-size/1733/5 "2020-10-08T13:57:12Z")

</div>

> [@skmhrk1209](#):
>
> I’m using high density display. Is this related to this issue?

Aaaand there it goes. 😉

In cinder when you’re in high density mode you need to convert your units from points to pixels. Basically any time you use any of the `getWindow*` functions you’ll want to wrap it in `app::toPixels()` to multiply it by the window’s content scale.

e.g

```auto
mGlslProg->uniform ( "uResolution", ci::vec2 ( app::toPixels ( getWindowSize() ) ) );

```

---

<div class="post-metadata">

**Author:** ![skmhrk1209](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@skmhrk1209](https://discourse.libcinder.org/u/skmhrk1209)\
**Post date:** [October 8, 2020, 3:50pm UTC](https://discourse.libcinder.org/t/gl-fragcoord-is-larger-than-the-window-size/1733/6 "2020-10-08T15:50:17Z")

</div>

`ci::app::toPixels` works well.  
Got it! Thank you!

---

<div class="post-metadata">

**Author:** ![seph](https://avatars.discourse-cdn.com/v4/letter/s/87869e/32.png) [@seph](https://discourse.libcinder.org/u/seph)\
**Post date:** [October 8, 2020, 7:08pm UTC](https://discourse.libcinder.org/t/gl-fragcoord-is-larger-than-the-window-size/1733/7 "2020-10-08T19:08:12Z")

</div>

fyi I think there is a default ciWindowSize uniform that Cinder provides.
