Get object position in opengl after transformations

I would like to get the coordinates of an object in OpenGL. I draw a quad. And after that I do some conversions, such as GL11.glTranslatef () and GL11.glRotatef ().

GL11.glBegin(GL11.GL_QUADS); // draw independent triangles GL11.glColor3f(0.0f, 1.0f, 0.0f); GL11.glVertex3f(-0.2f, 0.0f, -2.0f); // lower left vertex GL11.glVertex3f( 0.2f, 0.0f, -2.0f); // upper vertex GL11.glVertex3f( 0.2f, 0.4f, -2.0f); // lower right verte GL11.glVertex3f( -0.2f, 0.4f, -2.0f); GL11.glEnd(); 

Is it possible to get the position of the vertices after the transformation?

+4
source share
2 answers

Sure. Apply the same transformations (translations and rotations) to the position vector that holds the position of your object. The result will be the position of your object after conversion. At some point, you may need some scaling if you want to convert 3d coordinates to 2d screen coordinates. But it is very convenient. It includes scaling based on the z-depth of the object.

EDIT:

  private final FloatBuffer buffer = EngineUtil.createBuffer( 16 ); /** * @return Current modelview matrix (column-major) */ public Matrix getModelviewMatrix() { return getMatrix( GL11.GL_MODELVIEW_MATRIX ); } /** * @return Current projection matrix (column-major) */ public Matrix getProjectionMatrix() { return getMatrix( GL11.GL_PROJECTION_MATRIX ); } /** * Retrieves the specified matrix. * @param name Matrix name */ private Matrix getMatrix( int name ) { // Retrieve specified matrix buffer buffer.rewind(); GL11.glGetFloat( name, buffer ); // Convert to array final float[] array = new float[ 16 ]; buffer.get( array ); // Convert to matrix return new Matrix( array ); } 

But you can just use something more complete than LWJGL. Google vecmath, processing, unity. 3D is complicated, and it seems that there are no real short cuts, so just keep trying, you will get it.

+2
source

this is not possible ... opengl sends your vertex data to the GPU, and only to the GPU can you get it after conversion.

to get the transformed vertices you have to multiply them by the matrix

 for each vertex: vertex_transformed = matrix * vertex 

A few more advanced ways are to use conversion feedback and store these converted vertices in a buffer (but this buffer will be stored on the GPU).

0
source

Source: https://habr.com/ru/post/1388634/


All Articles