PDA

View Full Version : problem while calculating NDC of a square

debonair
03-02-2016, 10:57 PM
Can anyone explain me what what are steps to be performed in order to generate NDC from object space coordinates with below example.

float vertices[] = {
-2.0f, -12.0f,
2.0f, -12.0f,
-2.0f, 12.0f,
2.0f, 12.0f
};

here is my mvp matrix:

float MVP[16] = {

0.993, 0.054, -0.102, -0.102,
0.007, 0.852, 0.524, 0.524,
-0.115, 0.521, -0.846, -0.846,
0.575, -2.604, 4.061, 4.260 };
By my calculation I get:

X,Y,Z,W:

0.824918 7.111959 1.110172 1.000000
-1.111003 5.714332 1.090060 1.000000
-0.123152 0.698949 0.981588 1.000000
0.256286 0.747339 0.980855 1.000000

But those are wrong. By wrong I mean, if I do:

vec4 vert = mvp * vec4(inPos.x,inPos.y,0,1);
vert.xyzw/=vert.w;
gl_Position = vert;

in VS, i get different o/p as that of:

vec4 vert = mvp * vec4(inPos.x,inPos.y,0,1);
gl_Position = vert;

mamannon
03-03-2016, 02:26 AM
Can anyone explain me what what are steps to be performed in order to generate NDC from object space coordinates with below example.

If you mean NDC a coordinate system with [-1,1] range x, y, z coordinates, you should multiply your vertexes by a projection matrix. But normally you first transform your vertexes from object space to world space ie. place the objects where they should stay.

Spoops
03-03-2016, 06:17 PM
But those are wrong. By wrong I mean, if I do:

vec4 vert = mvp * vec4(inPos.x,inPos.y,0,1);
vert.xyzw/=vert.w;
gl_Position = vert;

in VS, i get different o/p as that of:

vec4 vert = mvp * vec4(inPos.x,inPos.y,0,1);
gl_Position = vert;

Strange. I get the same results when doing the perspective divide in the vertex shader, it should be done automatically.

debonair
03-04-2016, 10:50 AM
The problem is that for first two vertices, w is negative that means they lie behind the camera. Now question is how GPU does clipping i.e. how it generates new vertices in clipping box. So I tried to do basic math, finding a line equation and putting y=-1 as it is going down to find out x-coordinate. but it turned out that the coordinate which i got from this does not match with one generated by GPU. so this is unclear.

Spoops
03-04-2016, 11:33 AM