2

Following is the part of code that I am using to draw a rectangle. And I can see the rectangle on the display but confused with the quadrants and co-ordinates on display plane.

int position_loc = glGetAttribLocation(ProgramObject, "vertex");
int color_loc = glGetAttribLocation(ProgramObject, "color_a");
GLfloat Vertices[4][4] = {
             -0.8f,  0.6f, 0.0f, 1.0f,
             -0.1f,  0.6, 0.0f, 1.0f,
             -0.8f,  0.8f, 0.0f, 1.0f,
             -0.1f,  0.8f, 0.0f, 1.0f
        };
GLfloat red[4] = {1, 0, 1, 1};
glUniform4fv(glGetUniformLocation(ProgramObject, "color"), 1, red);
PrintGlError();
glEnableVertexAttribArray(position_loc);
PrintGlError();
printf("\nAfter Enable Vertex Attrib Array");
glBindBuffer(GL_ARRAY_BUFFER, VBO);
PrintGlError();
glVertexAttribPointer(position_loc, 4, GL_FLOAT, GL_FALSE, 0, 0);
PrintGlError();
glBufferData(GL_ARRAY_BUFFER, sizeof Vertices, Vertices, GL_DYNAMIC_DRAW);
PrintGlError();
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
PrintGlError();

So keeping in mind the above vertices

GLfloat Vertices[4][4] = {
x,  y, p, q,
x1,  y1, p1, q1,
x2,  y2, p2, q2,
x3,  y3, p3, q3,
};

what is p,q .. p1,q1.. ? on what basis are these last two points determined? And how does it effect x,y or x1,y1 .. and so on?

moizh
  • 55
  • 6
  • Is this set of vertices in the form of a `4x4` matrix being used in `3D` rendering or `2D` rendering? I ask this because the use and application of the `4x4` matrix is different between 2D and 3D. – Francis Cugler Nov 07 '18 at 10:34
  • Possible duplicate of: https://stackoverflow.com/questions/29079685/how-does-4x4-matrix-work-in-3d-graphic – Francis Cugler Nov 07 '18 at 11:18
  • @FrancisCugler 4x4 is being used in 2D rendering. – moizh Nov 07 '18 at 11:34

1 Answers1

2

OpenGL works with a 3-dimensional coordinate system with a homogeneous coordinate. Usually the values are donated [x,y,z,w] with w being the homogeneous part. Before any projection, [x,y,z] describe the position of the point in 3D space. w will usually be 1 for positions and 0 for directions.

During rendering, OpenGL handles transformations (vertex shader) resulting in a new point [x', y', z', w']. The w component is needed here because it allows us to describe all transformations, especially translations and (perspective) projections as 4x4 matrices. Have a look at 1 and 2 for details about transformations.

Afterwards clipping happens and the resulting vectors gets divided by the w component giving so-called Normalized device coordinates [x'/w', y'/w', z'/w', 1]. This NDC coordinates is what is actually used to draw to the screen. The first and second component (x'/w' and y'/w') are multiplied by the viewport size to get to the final pixel coordinates. The third component (z'/w', aka depth) is used to determine which points are in front during depth-testing. The last coordinate has no purpose here anymore.

In your case, without using any transformations or projections, you are drawing directly in NDC space, thus z can be used to order triangles in depth and w always has to be 1.

BDL
  • 21,052
  • 22
  • 49
  • 55
  • That is good if he's rendering in a 3D Scene in a 3D world. However if he's rendering 2D in a 2D world without any Z axis, no concept of depth or a camera and everything is orthographic projection instead of perspective projection, then the 4x4 matrix may not represent the same exact information and can be used to represent other information and calculations. However, typically in rendering 3D you are correct! – Francis Cugler Nov 07 '18 at 11:16
  • Thanks for the explanation (bdl and Francis). My goal is to render 2D image and fonts in 2D space, So, As I understand Z will be always 0 for me and w should always be 1. Am I Correct? Also, does x,y represents value between -1,1 and the co-ordinate (0,0) is at the center of the display? – moizh Nov 07 '18 at 11:31
  • If you don't draw any overlapping content, then yes, depth doesn't matter. If rectangles overlap, then depth can be used to define which one is "on top". w will always be 1 in your case. Also note, that there is no real need to have the w (and z) coordinate in your vertex buffer. You can also append that in the vertex shader. – BDL Nov 07 '18 at 12:23