几何尺寸与公差论坛

 找回密码
 注册
查看: 2826|回复: 0

How SolidWorks Xform relates to OpenGL transformation matrix

[复制链接]
发表于 2007-3-13 14:42:15 | 显示全部楼层 |阅读模式
/*
Description:
    How SolidWorks Xform relates to OpenGL transformation matrix


Problem:
    There are several SolidWorks APIs which return a transform:

        Body2::SetXform
        Component2::GetXform
        ModelView::Xform

    which all return an array of 16 doubles.  The meaning of each of the
    values is documented but it is not clear how these values correspond to
    the "standard" OpenGL transformation matrix.  Knowledge of this is
    typically needed when drawing directly into the SolidWorks OpenGL window.


Solution:
    The relationship between the SolidWorks and OpenGL transformations is
    shown in the following code fragments.

    Note that there is an AutoMatrix class which automatically pushes and pops
    the OpenGL matrices.
*/

//------------------------------------------------------------------------
void swViewEvents:oComponentDisplay(CComPtr <IComponent2> pComp)
{
    double                          ChildXform[16];
    GLdouble                        OpenGLmatrix[16];
    GLdouble                        nScaling;
    AutoMatrix                      Matrix;

    hr = pChildComp->IGetXform(ChildXform);

    // convert from SW matrix to OpenGL matrix
    nScaling         = ChildXform [12];

    OpenGLmatrix[ 0] = nScaling * ChildXform[ 0];
    OpenGLmatrix[ 1] = nScaling * ChildXform[ 1];
    OpenGLmatrix[ 2] = nScaling * ChildXform[ 2];
    OpenGLmatrix[ 3] = 0.0;

    OpenGLmatrix[ 4] = nScaling * ChildXform[ 3];
    OpenGLmatrix[ 5] = nScaling * ChildXform[ 4];
    OpenGLmatrix[ 6] = nScaling * ChildXform[ 5];
    OpenGLmatrix[ 7] = 0.0;

    OpenGLmatrix[ 8] = nScaling * ChildXform[ 6];
    OpenGLmatrix[ 9] = nScaling * ChildXform[ 7];
    OpenGLmatrix[10] = nScaling * ChildXform[ 8];
    OpenGLmatrix[11] = 0.0;

    OpenGLmatrix[12] = ChildXform[ 9];
    OpenGLmatrix[13] = ChildXform[10];
    OpenGLmatrix[14] = ChildXform[11];
    OpenGLmatrix[15] = 1.0;


    // apply component Xform to OpenGL matrix
    glMultMatrixd(OpenGLmatrix);

    // do some OpenGL drawing
}
//------------------------------------------------------------------------



//------------------------------------------------------------------------
// class AutoMatrix
// automatically pushes matrix on creation and pops matrix on destruction
class AutoMatrix
{
private:
    GLint           m_MatrixMode;

public:
     AutoMatrix();
    ~AutoMatrix();
};
//------------------------------------------------------------------------


//------------------------------------------------------------------------
// class AutoMatrix
// automatically pushes matrix on creation and pops matrix on destruction
AutoMatrix::AutoMatrix()
{
    glGetIntegerv(GL_MATRIX_MODE, &m_MatrixMode);

    glPushMatrix();

    glMatrixMode(GL_MODELVIEW);

    GLenum          glErrCode = glGetError();
}
//------------------------------------------------------------------------
AutoMatrix::~AutoMatrix()
{
    glMatrixMode(m_MatrixMode);

    glPopMatrix();

    GLenum          glErrCode = glGetError();
}
//------------------------------------------------------------------------
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|几何尺寸与公差论坛

GMT+8, 2024-12-22 22:35 , Processed in 0.035792 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表