# OpenGL NDC initial values

I have a set of vertex locations to create a plane:

this->Vertices = {
// position         // color          // texture
-0.5f,  0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
0.5f,  0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f
};


Everything is fine, but I can't grasp how I should input these values at the very beginning. I used to give exact locations for vertices in DirectX in pixel unit, but here in OpenGL I should give vertex locations in normalized coordinates.

If I wanted to create a plane say 200px by 200px how should I deal with that when I set vertex data. I know I can transform my plane with coordinate matrices through model space and view space and etc. I just don't know what I should be giving for the initial vertex location values.

• What size is your screen, and where do you want this plane on it? Is it safe to assume you want this quad billboarded to the screen (ie. not tilted or showing any perspective)? Does the depth of the plane matter for your application? – DMGregory Aug 20 '17 at 13:56
• You can specify locations in pixels. Why not? Then use an orthographic projection matrix to transform them to NDC. – HolyBlackCat Aug 20 '17 at 14:56

In whatever coordinate system you want. It doesn't matter for OpenGL, it only needs to receive them in NDC at the end. For example, if you want an ortographic projection, where 1 unit equals 1 pixel on the screen, then you can use the matrix with the arguments r = 0, l = width, t = 0, b = height, f = 100, n = -100. You need to pass this matrix to the shaders and multiply the vertex coordinates with it.

• If I use an orthographic matrix I should input vertex locations in screen space which is pixels, but if I use perspective matrix I have to give normalized vertex locations? – MahanGM Aug 21 '17 at 10:24
• @MahanGM Again, no. The projection matrices project from one coordinate system to the other, if you use the arguments l = -0.5, r = 0.5, t = 0.5, b = -0.5, f = 100, n = -100, then you need to input the coordinates in normalized device coordinates – Bálint Aug 21 '17 at 11:24
• I'm using glm. I'm not feeding the matrix myself. The problem I have right now is that when I switch from orthographic to perspective projection, my plane gets very huge due to having pixel based vertices. Am I doing something wrong or is it just the way it is? – MahanGM Aug 21 '17 at 14:53
• @MahanGM the arguments in the glm functions correspond to the parameters above glm.g-truc.net/0.9.2/api/a00245.html – Bálint Aug 21 '17 at 14:58
• can you give me an example with glm::perspective? – MahanGM Aug 21 '17 at 16:22

I think you can keep your original co-ordinates as they are . Because I see they are always in NDC space or OpenGL i.e [-1.0,1.]. So just pass this vertices as it is to shader. So you Vertex Shader should look like this

VS

layout(location = 0) in vec3 pos;

void main(void) { gl_Position = vec3(pos,1.0); }

FS

out vec4 color;

void main(void) { color = vec4(1.0,0.0,0.0,1.0); }

This should draw your plane in red color. If you want to transform this plane some how then

with using following function transform your NDC space co-ordinates to world space w.r.t model and view and projection matrix you want.

You can do this in shader also as you will just need to pass model view projection matrices to shaders along with NDC co-ordinates of plane. Convert them to world co-ordinates apply model view projection to them to get transformed coordinates in NDC. and pass them along.

void unproject(float x, float y, float z)
{
vec4 in;
vec4 out;
mat4 m;
mat4 A;
vec4 world_coord;
vec3 object_coordinate;

A = mvp_matrix;

m = inverse(A);

//in the sample i was following, theres a bit of code to convert screen coords to NDC
//i just set in equal to my parameters which are already in NDC
in = vec4(x,y,z,1.0);

out = m * in;

out=1.0/out;

world_coord = out*out;
world_coord = out*out;
world_coord = out*out;
}