变换后获取对象在opengl中的位置

发布于 2024-12-23 09:57:48 字数 461 浏览 4 评论 0原文

我想获取 OpenGL 中对象的坐标。我画了一个四边形。之后我做了一些转换,例如 GL11.glTranslatef() 和 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(); 

是否可以得到变换后顶点的位置?

I would like to get the coordinates of an object in OpenGL. I draw a quad. And after that I do some transforms like 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酷炫老祖宗 2024-12-30 09:57:48

当然。将相同的变换(平移和旋转)应用于保存对象位置的位置向量。结果将是变换后对象的位置。如果您想将 3d 坐标转换为 2d 屏幕坐标,您可能需要在某些时候进行一些缩放。但这是非常可行的。它涉及基于对象的 z 深度的缩放。

编辑:

 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 );
   }

但您可能只想使用比 LWJGL 更完整的东西。谷歌vecmath,处理,统一。 3D 很棘手,似乎没有真正的捷径,所以只要继续尝试,你就会得到它。

Sure. Apply the same transforms (translations and rotations) to a position vector, that holds the position of your object. The result will be the position of your object after the transform. You may need to do some scaling at some point if you want to convert the 3d co-ords to 2d screen co-ords. But this is very much doable. It involves 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 may want to simply use something more complete than LWJGL. Google vecmath, processing, unity. 3D is tricky and it seems there are no real short cuts, so just keep trying, you will get it.

归属感 2024-12-30 09:57:48

这是不可能的... opengl 将您的顶点数据发送到 GPU,并且只有在 GPU 上您才能在转换后获取它们。

要获得变换后的顶点,您必须将它们乘以矩阵,

for each vertex:
   vertex_transformed = matrix * vertex

一些更高级的方法是使用变换反馈并将这些变换后的顶点存储到缓冲区中(但该缓冲区仍将存储在 GPU 上)。

this is not possible... opengl sends your vertex data to the GPU and only on GPU you can get them after the transformation.

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

for each vertex:
   vertex_transformed = matrix * vertex

some more advanced ways is to use transform feedback and store those transformed vertices into a buffer (but still this buffer will be stored on the GPU).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文