GLSL map gl_FragCoord.xy to coordinates in orthographic projection
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Hello I have an opengl Program that renders 2d polygons in an orthographic projection. At the start of the porgram, or when the window size changes, the function reshape is called. Here is the code for the reshape function:
/* Call back when the windows is re-sized */
void reshape(GLsizei width, GLsizei height) {
// Compute aspect ratio of the new window
if (height == 0) height = 1;
// To prevent divide by 0
GLfloat aspect = (GLfloat)width /
(GLfloat)height;
// Set the viewport to cover the new window
glViewport(0, 0, width, height);
// Set the aspect ratio of the clipping area to match the viewport
glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix
glLoadIdentity(); // Reset the projection matrix
if (width >= height) {
clipAreaXLeft = -1.0 * aspect;
clipAreaXRight = 1.0 * aspect;
clipAreaYBottom = -1.0;
clipAreaYTop = 1.0;
}
else {
clipAreaXLeft = -1.0;
clipAreaXRight = 1.0;
clipAreaYBottom = -1.0 / aspect;
clipAreaYTop = 1.0 / aspect;
}
clipAreaXLeft *= 600;
clipAreaYBottom *= 600;
clipAreaXRight *= 600;
clipAreaYTop *= 600;
gluOrtho2D(clipAreaXLeft, clipAreaXRight,
clipAreaYBottom, clipAreaYTop);
glScissor(0, 0, width, height);
glEnable(GL_SCISSOR_TEST);
}
Here is some code for a GLSL fragment shader:
#version 420 core
out vec4 color
void main(){
vec2 orthoXY =... need help here, should
convert window-space to ortho-space,
maybe use projection matrix from fixed
pipeline?
color=vec4{1,1,1,1}
}
c++ opengl glsl fragment-shader orthographic
add a comment |
Hello I have an opengl Program that renders 2d polygons in an orthographic projection. At the start of the porgram, or when the window size changes, the function reshape is called. Here is the code for the reshape function:
/* Call back when the windows is re-sized */
void reshape(GLsizei width, GLsizei height) {
// Compute aspect ratio of the new window
if (height == 0) height = 1;
// To prevent divide by 0
GLfloat aspect = (GLfloat)width /
(GLfloat)height;
// Set the viewport to cover the new window
glViewport(0, 0, width, height);
// Set the aspect ratio of the clipping area to match the viewport
glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix
glLoadIdentity(); // Reset the projection matrix
if (width >= height) {
clipAreaXLeft = -1.0 * aspect;
clipAreaXRight = 1.0 * aspect;
clipAreaYBottom = -1.0;
clipAreaYTop = 1.0;
}
else {
clipAreaXLeft = -1.0;
clipAreaXRight = 1.0;
clipAreaYBottom = -1.0 / aspect;
clipAreaYTop = 1.0 / aspect;
}
clipAreaXLeft *= 600;
clipAreaYBottom *= 600;
clipAreaXRight *= 600;
clipAreaYTop *= 600;
gluOrtho2D(clipAreaXLeft, clipAreaXRight,
clipAreaYBottom, clipAreaYTop);
glScissor(0, 0, width, height);
glEnable(GL_SCISSOR_TEST);
}
Here is some code for a GLSL fragment shader:
#version 420 core
out vec4 color
void main(){
vec2 orthoXY =... need help here, should
convert window-space to ortho-space,
maybe use projection matrix from fixed
pipeline?
color=vec4{1,1,1,1}
}
c++ opengl glsl fragment-shader orthographic
Same way as usual - create projection matrix and transform vertex position in vertex shader.
– Michael Nastenko
Nov 17 '18 at 1:00
add a comment |
Hello I have an opengl Program that renders 2d polygons in an orthographic projection. At the start of the porgram, or when the window size changes, the function reshape is called. Here is the code for the reshape function:
/* Call back when the windows is re-sized */
void reshape(GLsizei width, GLsizei height) {
// Compute aspect ratio of the new window
if (height == 0) height = 1;
// To prevent divide by 0
GLfloat aspect = (GLfloat)width /
(GLfloat)height;
// Set the viewport to cover the new window
glViewport(0, 0, width, height);
// Set the aspect ratio of the clipping area to match the viewport
glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix
glLoadIdentity(); // Reset the projection matrix
if (width >= height) {
clipAreaXLeft = -1.0 * aspect;
clipAreaXRight = 1.0 * aspect;
clipAreaYBottom = -1.0;
clipAreaYTop = 1.0;
}
else {
clipAreaXLeft = -1.0;
clipAreaXRight = 1.0;
clipAreaYBottom = -1.0 / aspect;
clipAreaYTop = 1.0 / aspect;
}
clipAreaXLeft *= 600;
clipAreaYBottom *= 600;
clipAreaXRight *= 600;
clipAreaYTop *= 600;
gluOrtho2D(clipAreaXLeft, clipAreaXRight,
clipAreaYBottom, clipAreaYTop);
glScissor(0, 0, width, height);
glEnable(GL_SCISSOR_TEST);
}
Here is some code for a GLSL fragment shader:
#version 420 core
out vec4 color
void main(){
vec2 orthoXY =... need help here, should
convert window-space to ortho-space,
maybe use projection matrix from fixed
pipeline?
color=vec4{1,1,1,1}
}
c++ opengl glsl fragment-shader orthographic
Hello I have an opengl Program that renders 2d polygons in an orthographic projection. At the start of the porgram, or when the window size changes, the function reshape is called. Here is the code for the reshape function:
/* Call back when the windows is re-sized */
void reshape(GLsizei width, GLsizei height) {
// Compute aspect ratio of the new window
if (height == 0) height = 1;
// To prevent divide by 0
GLfloat aspect = (GLfloat)width /
(GLfloat)height;
// Set the viewport to cover the new window
glViewport(0, 0, width, height);
// Set the aspect ratio of the clipping area to match the viewport
glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix
glLoadIdentity(); // Reset the projection matrix
if (width >= height) {
clipAreaXLeft = -1.0 * aspect;
clipAreaXRight = 1.0 * aspect;
clipAreaYBottom = -1.0;
clipAreaYTop = 1.0;
}
else {
clipAreaXLeft = -1.0;
clipAreaXRight = 1.0;
clipAreaYBottom = -1.0 / aspect;
clipAreaYTop = 1.0 / aspect;
}
clipAreaXLeft *= 600;
clipAreaYBottom *= 600;
clipAreaXRight *= 600;
clipAreaYTop *= 600;
gluOrtho2D(clipAreaXLeft, clipAreaXRight,
clipAreaYBottom, clipAreaYTop);
glScissor(0, 0, width, height);
glEnable(GL_SCISSOR_TEST);
}
Here is some code for a GLSL fragment shader:
#version 420 core
out vec4 color
void main(){
vec2 orthoXY =... need help here, should
convert window-space to ortho-space,
maybe use projection matrix from fixed
pipeline?
color=vec4{1,1,1,1}
}
c++ opengl glsl fragment-shader orthographic
c++ opengl glsl fragment-shader orthographic
asked Nov 16 '18 at 23:13
Michael SohnenMichael Sohnen
2413
2413
Same way as usual - create projection matrix and transform vertex position in vertex shader.
– Michael Nastenko
Nov 17 '18 at 1:00
add a comment |
Same way as usual - create projection matrix and transform vertex position in vertex shader.
– Michael Nastenko
Nov 17 '18 at 1:00
Same way as usual - create projection matrix and transform vertex position in vertex shader.
– Michael Nastenko
Nov 17 '18 at 1:00
Same way as usual - create projection matrix and transform vertex position in vertex shader.
– Michael Nastenko
Nov 17 '18 at 1:00
add a comment |
1 Answer
1
active
oldest
votes
If you want to transform gl_FragCoord
to normalize device space, then I recommend to create uniform, which contains the size of the viewport:
uniform vec2 u_resolution; // with and height of the viewport
gl_FragCoord.xy
contains the "window" coordinates of the fragment, gl_FragCoord.z
contains the depth in the depth range, which is [0, 1], if you didn't change it by glDepthRange
.
The normalized device space is a cube with a left, lower, front coordnate (-1, -1, -1) and a right, top, back coordinate of (1, 1, 1):
So the transformation is:
vec3 ndc = -1.0 + 2.0 * gl_FragCoord.xyz/vec3(u_resolution.xy, 1.0);
Or the following, if you want to transform the x
and y
component only:
vec2 ndc_xy = -1.0 + 2.0 * gl_FragCoord.xy/u_resolution.xy;
This answer helped a lot, thanks!
– Michael Sohnen
Nov 17 '18 at 19:05
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53346578%2fglsl-map-gl-fragcoord-xy-to-coordinates-in-orthographic-projection%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you want to transform gl_FragCoord
to normalize device space, then I recommend to create uniform, which contains the size of the viewport:
uniform vec2 u_resolution; // with and height of the viewport
gl_FragCoord.xy
contains the "window" coordinates of the fragment, gl_FragCoord.z
contains the depth in the depth range, which is [0, 1], if you didn't change it by glDepthRange
.
The normalized device space is a cube with a left, lower, front coordnate (-1, -1, -1) and a right, top, back coordinate of (1, 1, 1):
So the transformation is:
vec3 ndc = -1.0 + 2.0 * gl_FragCoord.xyz/vec3(u_resolution.xy, 1.0);
Or the following, if you want to transform the x
and y
component only:
vec2 ndc_xy = -1.0 + 2.0 * gl_FragCoord.xy/u_resolution.xy;
This answer helped a lot, thanks!
– Michael Sohnen
Nov 17 '18 at 19:05
add a comment |
If you want to transform gl_FragCoord
to normalize device space, then I recommend to create uniform, which contains the size of the viewport:
uniform vec2 u_resolution; // with and height of the viewport
gl_FragCoord.xy
contains the "window" coordinates of the fragment, gl_FragCoord.z
contains the depth in the depth range, which is [0, 1], if you didn't change it by glDepthRange
.
The normalized device space is a cube with a left, lower, front coordnate (-1, -1, -1) and a right, top, back coordinate of (1, 1, 1):
So the transformation is:
vec3 ndc = -1.0 + 2.0 * gl_FragCoord.xyz/vec3(u_resolution.xy, 1.0);
Or the following, if you want to transform the x
and y
component only:
vec2 ndc_xy = -1.0 + 2.0 * gl_FragCoord.xy/u_resolution.xy;
This answer helped a lot, thanks!
– Michael Sohnen
Nov 17 '18 at 19:05
add a comment |
If you want to transform gl_FragCoord
to normalize device space, then I recommend to create uniform, which contains the size of the viewport:
uniform vec2 u_resolution; // with and height of the viewport
gl_FragCoord.xy
contains the "window" coordinates of the fragment, gl_FragCoord.z
contains the depth in the depth range, which is [0, 1], if you didn't change it by glDepthRange
.
The normalized device space is a cube with a left, lower, front coordnate (-1, -1, -1) and a right, top, back coordinate of (1, 1, 1):
So the transformation is:
vec3 ndc = -1.0 + 2.0 * gl_FragCoord.xyz/vec3(u_resolution.xy, 1.0);
Or the following, if you want to transform the x
and y
component only:
vec2 ndc_xy = -1.0 + 2.0 * gl_FragCoord.xy/u_resolution.xy;
If you want to transform gl_FragCoord
to normalize device space, then I recommend to create uniform, which contains the size of the viewport:
uniform vec2 u_resolution; // with and height of the viewport
gl_FragCoord.xy
contains the "window" coordinates of the fragment, gl_FragCoord.z
contains the depth in the depth range, which is [0, 1], if you didn't change it by glDepthRange
.
The normalized device space is a cube with a left, lower, front coordnate (-1, -1, -1) and a right, top, back coordinate of (1, 1, 1):
So the transformation is:
vec3 ndc = -1.0 + 2.0 * gl_FragCoord.xyz/vec3(u_resolution.xy, 1.0);
Or the following, if you want to transform the x
and y
component only:
vec2 ndc_xy = -1.0 + 2.0 * gl_FragCoord.xy/u_resolution.xy;
edited Nov 17 '18 at 7:33
answered Nov 17 '18 at 7:11
Rabbid76Rabbid76
44.7k123354
44.7k123354
This answer helped a lot, thanks!
– Michael Sohnen
Nov 17 '18 at 19:05
add a comment |
This answer helped a lot, thanks!
– Michael Sohnen
Nov 17 '18 at 19:05
This answer helped a lot, thanks!
– Michael Sohnen
Nov 17 '18 at 19:05
This answer helped a lot, thanks!
– Michael Sohnen
Nov 17 '18 at 19:05
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53346578%2fglsl-map-gl-fragcoord-xy-to-coordinates-in-orthographic-projection%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Same way as usual - create projection matrix and transform vertex position in vertex shader.
– Michael Nastenko
Nov 17 '18 at 1:00