LWJGL Forum

Please login or register.

Login with username, password and session length

Author Topic: Transformations from pixels to NDC  (Read 645 times)

guatto

  • Newbie
  • *
  • Offline Offline
  • Posts: 4
Transformations from pixels to NDC
« on: March 12, 2017, 17:48:11 »

let's say that my screen is (800 * 600) and i have a Quad (2D) drawn with the following vertices positions using Triangle_Strip (in NDC) :

Code: [Select]
float[] vertices = {-0.2f,0.2f,-0.2f,-0.2f,0.2f,0.2f,0.2f,-0.2f};
And I set up my Transformation Matrix in this way :

Code: [Select]
Matrix4f tranMatrix = new Matrix4f();
tranMatrix.setIdentity();
Matrix4f.translate(position, tranMatrix, tranMatrix);
Matrix4f.scale(new Vector3f(size.x, size.y, 1f), tranMatrix, tranMatrix);

And my vertex Shader :

Code: [Select]
#version 150 core
 
in vec2 in_Position;
 
uniform mat4 transMatrix;
 
void main(void) {
 
    gl_Position = transMatrix * vec4(in_Position,0,1.0);
 
}

My question is, which formula should I use to modify the transformations of my quad with coordinates (in Pixels) ?

For example :
-set scale (50px width, 50px height)  => NDC Vector2f(width,height)
-set position (100px posX, 100px posY) => NDC Vector2f(x,y)

To better understand, I would create a function to convert my Pixels data to NDCs to send them next to the shader. Thank you !
Logged

Kai

  • Nerdus Imperius
  • *****
  • Offline Offline
  • Posts: 903
Re: Transformations from pixels to NDC
« Reply #1 on: March 12, 2017, 17:57:36 »

You were already given an answer in your previous post, which you deleted.
The answer was: http://www.songho.ca/opengl/gl_projectionmatrix.html
Logged

guatto

  • Newbie
  • *
  • Offline Offline
  • Posts: 4
Re: Transformations from pixels to NDC
« Reply #2 on: March 12, 2017, 18:16:35 »

First thank you for your answers, concerning my previous problem it was about the Orthogonal projection that's why I remove it, can you just tell me what formula should I follow, I am still a beginner , thanks again !
Logged

Kai

  • Nerdus Imperius
  • *****
  • Offline Offline
  • Posts: 903
Re: Transformations from pixels to NDC
« Reply #3 on: March 12, 2017, 18:21:09 »

Please do read the "Orthographic Projection" section of that article.
Logged

guatto

  • Newbie
  • *
  • Offline Offline
  • Posts: 4
Re: Transformations from pixels to NDC
« Reply #4 on: March 12, 2017, 23:03:40 »

I followed the link you gave me and I tried to create an orthographic projection but the result is disappointing, my project contains multiple classes so I uploaded it to a host if you want to help me, thank you !

https://drive.google.com/file/d/0B_GWyJmcwrpYeGdaQ0dZZnpKZms/view?usp=sharing
Logged

Kai

  • Nerdus Imperius
  • *****
  • Offline Offline
  • Posts: 903
Re: Transformations from pixels to NDC
« Reply #5 on: March 12, 2017, 23:38:43 »

In Matrix4f.mXY the X is the column index!
Logged

guatto

  • Newbie
  • *
  • Offline Offline
  • Posts: 4
Re: Transformations from pixels to NDC
« Reply #6 on: March 13, 2017, 08:55:03 »

I modified my Matrix4f as you told me but now nothing appears, even if I remove the transformation matrix :

Code: [Select]
            matrix.m00 = 2.0f / (right - left);
    matrix.m01 = 0;
    matrix.m02 = 0;
    matrix.m03 = 0;
   
    matrix.m10 = 0;
    matrix.m11 = 2.0f / (top - bottom);
    matrix.m12 = 0;
    matrix.m13 = 0;
   
    matrix.m20 = 0;
    matrix.m21 = 0;
    matrix.m22 = -2.0f / (far - near);
    matrix.m23 = 0;
   
    matrix.m30 = -(right+left)/(right-left);
    matrix.m31 = -(top+bottom)/(top-bottom);
    matrix.m32 = -(far+near)/(far-near);
    matrix.m33 = 1;

I entered the following coordinates for the parameters:

Code: [Select]
  //left      right   bottom    top     near   far
    (0.0f,   800,    600,       0.0f,   0.0f,   1.0f)
Logged

Kai

  • Nerdus Imperius
  • *****
  • Offline Offline
  • Posts: 903
Re: Transformations from pixels to NDC
« Reply #7 on: March 14, 2017, 16:58:05 »

Some points:
- that matrix computation is 100% correct. You shouldn't change anything in it anymore
- make sure that your vertices are actually now defined in window coordinates, such as 300 or 600 and not in NDC space, such as 0.5 or -0.2
- I'd use a different near plane distance than 0.0, such as -1.0. The danger when using 0.0 is that your vertices which are currently defined as 2-dimentional vectors also by default use 0.0 as their Z value. This can cause clipping because of rounding errors. So, please use -1.0 as znear.
- if it still won't work, then please build a very very simple and minimal single-file example which also does not work, and post that
Logged