Transformations from pixels to NDC

Started by guatto, March 12, 2017, 17:48:11

Previous topic - Next topic

guatto

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) :

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 :

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 :

#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 !

Kai

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

guatto

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 !

Kai


guatto

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

Kai

In Matrix4f.mXY the X is the column index!

guatto

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

            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:

  //left      right   bottom    top     near   far
    (0.0f,   800,    600,       0.0f,   0.0f,   1.0f)

Kai

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