游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1904|回复: 3

opengl和d3d的代码区别问题?

[复制链接]

36

主题

135

帖子

135

积分

注册会员

Rank: 2

积分
135
发表于 2004-5-7 13:52:00 | 显示全部楼层 |阅读模式
opengl的代码:

float   proj[16];
float   modl[16];
glGetFloatv( GL_PROJECTION_MATRIX, proj );
glGetFloatv( GL_MODELVIEW_MATRIX, modl );

这里得到的矩阵proj[16]和modl[16]是否对应在d3d中的projection matrix和view matrix,他们内部的每个元素的排列也是相同的吗?

20

主题

473

帖子

502

积分

高级会员

Rank: 4

积分
502
发表于 2004-5-7 16:55:00 | 显示全部楼层

Re:opengl和d3d的代码区别问题?

缺省情况下
ogl使用右手坐标系
d3d使用左手坐标系
你取得的矩阵,需要相互转换一下

36

主题

135

帖子

135

积分

注册会员

Rank: 2

积分
135
 楼主| 发表于 2004-5-7 23:09:00 | 显示全部楼层

Re:opengl和d3d的代码区别问题?

右手坐标系和左手坐标系的转换怎么做?

36

主题

135

帖子

135

积分

注册会员

Rank: 2

积分
135
 楼主| 发表于 2004-5-7 23:25:00 | 显示全部楼层

Re:opengl和d3d的代码区别问题?

考虑这个代码:
void CFrustum::CalculateFrustum()
{   
        float   proj[16];                                                                // This will hold our projection matrix
        float   modl[16];                                                                // This will hold our modelview matrix
        float   clip[16];                                                                // This will hold the clipping planes

        // glGetFloatv() is used to extract information about our OpenGL world.
        // Below, we pass in GL_PROJECTION_MATRIX to abstract our projection matrix.
        // It then stores the matrix into an array of [16].
        glGetFloatv( GL_PROJECTION_MATRIX, proj );

        // By passing in GL_MODELVIEW_MATRIX, we can abstract our model view matrix.
        // This also stores it in an array of [16].
        glGetFloatv( GL_MODELVIEW_MATRIX, modl );

        // Now that we have our modelview and projection matrix, if we combine these 2 matrices,
        // it will give us our clipping planes.  To combine 2 matrices, we multiply them.

        clip[ 0] = modl[ 0] * proj[ 0] + modl[ 1] * proj[ 4] + modl[ 2] * proj[ 8] + modl[ 3] * proj[12];
        clip[ 1] = modl[ 0] * proj[ 1] + modl[ 1] * proj[ 5] + modl[ 2] * proj[ 9] + modl[ 3] * proj[13];
        clip[ 2] = modl[ 0] * proj[ 2] + modl[ 1] * proj[ 6] + modl[ 2] * proj[10] + modl[ 3] * proj[14];
        clip[ 3] = modl[ 0] * proj[ 3] + modl[ 1] * proj[ 7] + modl[ 2] * proj[11] + modl[ 3] * proj[15];

        clip[ 4] = modl[ 4] * proj[ 0] + modl[ 5] * proj[ 4] + modl[ 6] * proj[ 8] + modl[ 7] * proj[12];
        clip[ 5] = modl[ 4] * proj[ 1] + modl[ 5] * proj[ 5] + modl[ 6] * proj[ 9] + modl[ 7] * proj[13];
        clip[ 6] = modl[ 4] * proj[ 2] + modl[ 5] * proj[ 6] + modl[ 6] * proj[10] + modl[ 7] * proj[14];
        clip[ 7] = modl[ 4] * proj[ 3] + modl[ 5] * proj[ 7] + modl[ 6] * proj[11] + modl[ 7] * proj[15];

        clip[ 8] = modl[ 8] * proj[ 0] + modl[ 9] * proj[ 4] + modl[10] * proj[ 8] + modl[11] * proj[12];
        clip[ 9] = modl[ 8] * proj[ 1] + modl[ 9] * proj[ 5] + modl[10] * proj[ 9] + modl[11] * proj[13];
        clip[10] = modl[ 8] * proj[ 2] + modl[ 9] * proj[ 6] + modl[10] * proj[10] + modl[11] * proj[14];
        clip[11] = modl[ 8] * proj[ 3] + modl[ 9] * proj[ 7] + modl[10] * proj[11] + modl[11] * proj[15];

        clip[12] = modl[12] * proj[ 0] + modl[13] * proj[ 4] + modl[14] * proj[ 8] + modl[15] * proj[12];
        clip[13] = modl[12] * proj[ 1] + modl[13] * proj[ 5] + modl[14] * proj[ 9] + modl[15] * proj[13];
        clip[14] = modl[12] * proj[ 2] + modl[13] * proj[ 6] + modl[14] * proj[10] + modl[15] * proj[14];
        clip[15] = modl[12] * proj[ 3] + modl[13] * proj[ 7] + modl[14] * proj[11] + modl[15] * proj[15];
       
        // Now we actually want to get the sides of the frustum.  To do this we take
        // the clipping planes we received above and extract the sides from them.

        // This will extract the RIGHT side of the frustum
        m_Frustum[RIGHT][A] = clip[ 3] - clip[ 0];
        m_Frustum[RIGHT][B] = clip[ 7] - clip[ 4];
        m_Frustum[RIGHT][C] = clip[11] - clip[ 8];
        m_Frustum[RIGHT][D] = clip[15] - clip[12];

        // Now that we have a normal (A,B,C) and a distance (D) to the plane,
        // we want to normalize that normal and distance.

        // Normalize the RIGHT side
        NormalizePlane(m_Frustum, RIGHT);

        // This will extract the LEFT side of the frustum
        m_Frustum[LEFT][A] = clip[ 3] + clip[ 0];
        m_Frustum[LEFT][B] = clip[ 7] + clip[ 4];
        m_Frustum[LEFT][C] = clip[11] + clip[ 8];
        m_Frustum[LEFT][D] = clip[15] + clip[12];

        // Normalize the LEFT side
        NormalizePlane(m_Frustum, LEFT);

        // This will extract the BOTTOM side of the frustum
        m_Frustum[BOTTOM][A] = clip[ 3] + clip[ 1];
        m_Frustum[BOTTOM][B] = clip[ 7] + clip[ 5];
        m_Frustum[BOTTOM][C] = clip[11] + clip[ 9];
        m_Frustum[BOTTOM][D] = clip[15] + clip[13];

        // Normalize the BOTTOM side
        NormalizePlane(m_Frustum, BOTTOM);

        // This will extract the TOP side of the frustum
        m_Frustum[TOP][A] = clip[ 3] - clip[ 1];
        m_Frustum[TOP][B] = clip[ 7] - clip[ 5];
        m_Frustum[TOP][C] = clip[11] - clip[ 9];
        m_Frustum[TOP][D] = clip[15] - clip[13];

        // Normalize the TOP side
        NormalizePlane(m_Frustum, TOP);

        // This will extract the BACK side of the frustum
        m_Frustum[BACK][A] = clip[ 3] - clip[ 2];
        m_Frustum[BACK][B] = clip[ 7] - clip[ 6];
        m_Frustum[BACK][C] = clip[11] - clip[10];
        m_Frustum[BACK][D] = clip[15] - clip[14];

        // Normalize the BACK side
        NormalizePlane(m_Frustum, BACK);

        // This will extract the FRONT side of the frustum
        m_Frustum[FRONT][A] = clip[ 3] + clip[ 2];
        m_Frustum[FRONT][B] = clip[ 7] + clip[ 6];
        m_Frustum[FRONT][C] = clip[11] + clip[10];
        m_Frustum[FRONT][D] = clip[15] + clip[14];

        // Normalize the FRONT side
        NormalizePlane(m_Frustum, FRONT);
}

这就是opengl实现一个view frustum的代码,我不知道用d3d应该怎么写?或者说做这种view frustum的数学推导过程是怎样的?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-6-30 18:42

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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