OpenGL: draw a line between two elements

I need to draw a line between the two grids that I created. Each grid is associated with a different model matrix. I was thinking about how to do this, and I was thinking about this:

glMatrixMode(GL_MODELVIEW); glLoadMatrixf(first_object_model_matrix); glBegin(GL_LINES); glVertex3f(0, 0, 0); // object coord glMatrixMode(GL_MODELVIEW); glLoadMatrixf(first_object_model_matrix); glVertex3f(0, 0, 0); // ending point of the line glEnd( ); 

But the problem is that I cannot name glMatrixMode and glLoadMatrixf between glBegin and glEnd . I also use shaders and a programmable pipeline, so the idea of ​​returning to a fixed pipeline with a rendered scene does not bother.

Can you:

  • Tell me exactly how to draw a line between two grids (I have their model matrix) with shaders.

or

  • Invite me to write code similar to the one above to draw a row that has two grid model matrices.
+4
source share
2 answers

Calculate the line by two points, multiplying each by one of your model matrices. Below is the pseudo code. Since you use Qt, you can use the built-in math libraries to achieve this effect.

 vec3 line_point_1 = model_matrix_object1 * vec4(0, 0, 0, 1); vec3 line_point_2 = model_matrix_object2 * vec4(0, 0, 0, 1); // Draw Lines 
+4
source

The position of the second point can simply be taken from the w-vector model_matrix_object2. No need to multiply with (0,0,0,1).

This is due to the fact that the 4x4 matrix in OpenGL is usually an ortho matrix consisting of the 3x3 rotational part and the translation vector. The last line is then filled with 0,0,0,1. If you want to know where the 4x4 matrix is ​​translated, just translate the vector into the right column.

See Given a homogeneous 4x4 matrix, how can I get the 3D coordinates of the world? for more information.

+1
source

All Articles