0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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? \$\endgroup\$ – DMGregory Aug 20 '17 at 13:56
  • \$\begingroup\$ You can specify locations in pixels. Why not? Then use an orthographic projection matrix to transform them to NDC. \$\endgroup\$ – HolyBlackCat Aug 20 '17 at 14:56
2
\$\begingroup\$

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

enter image description here

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.

\$\endgroup\$
  • \$\begingroup\$ 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? \$\endgroup\$ – MahanGM Aug 21 '17 at 10:24
  • \$\begingroup\$ @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 \$\endgroup\$ – Bálint Aug 21 '17 at 11:24
  • \$\begingroup\$ 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? \$\endgroup\$ – MahanGM Aug 21 '17 at 14:53
  • \$\begingroup\$ @MahanGM the arguments in the glm functions correspond to the parameters above glm.g-truc.net/0.9.2/api/a00245.html \$\endgroup\$ – Bálint Aug 21 '17 at 14:58
  • \$\begingroup\$ can you give me an example with glm::perspective? \$\endgroup\$ – MahanGM Aug 21 '17 at 16:22
0
\$\begingroup\$

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[3]=1.0/out[3];   

world_coord[0] = out[0]*out[3];
world_coord[1] = out[1]*out[3];
world_coord[2] = out[2]*out[3];
 }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.