Game Development

c++ – Digicam (View) in Euler angles

That is code (beneath) from unique recreation undertaking (90s years), I it research now. Used mounted level math.

typedef struct PHD_3DPOS {
    int32_t x;
    int32_t y;
    int32_t z;
    int16_t x_rot;
    int16_t y_rot;
    int16_t z_rot;
} PHD_3DPOS;

#outline W2V_SHIFT 14

#outline TRIGMULT2(A, B) (((A) * (B)) >> W2V_SHIFT)
#outline TRIGMULT3(A, B, C) (TRIGMULT2((TRIGMULT2(A, B)), C))

typedef struct PHD_MATRIX {
    int32_t _00;
    int32_t _01;
    int32_t _02;
    int32_t _03;
    int32_t _10;
    int32_t _11;
    int32_t _12;
    int32_t _13;
    int32_t _20;
    int32_t _21;
    int32_t _22;
    int32_t _23;
} PHD_MATRIX;

#outline MAX_MATRICES 40

PHD_MATRIX m_MatrixStack[MAX_MATRICES] = { 0 };

PHD_MATRIX g_W2VMatrix = { 0 };

void phd_GenerateW2V(PHD_3DPOS *viewpos)
{
    int32_t sx = phd_sin(viewpos->x_rot);
    int32_t cx = phd_cos(viewpos->x_rot);
    int32_t sy = phd_sin(viewpos->y_rot);
    int32_t cy = phd_cos(viewpos->y_rot);
    int32_t sz = phd_sin(viewpos->z_rot);
    int32_t cz = phd_cos(viewpos->z_rot);

    m_MatrixStack[0]._00 = TRIGMULT3(sx, sy, sz) + TRIGMULT2(cy, cz);
    m_MatrixStack[0]._01 = TRIGMULT2(cx, sz);
    m_MatrixStack[0]._02 = TRIGMULT3(sx, cy, sz) - TRIGMULT2(sy, cz);
    m_MatrixStack[0]._10 = TRIGMULT3(sx, sy, cz) - TRIGMULT2(cy, sz);
    m_MatrixStack[0]._11 = TRIGMULT2(cx, cz);
    m_MatrixStack[0]._12 = TRIGMULT3(sx, cy, cz) + TRIGMULT2(sy, sz);
    m_MatrixStack[0]._20 = TRIGMULT2(cx, sy);
    m_MatrixStack[0]._21 = -sx;
    m_MatrixStack[0]._22 = TRIGMULT2(cx, cy);
    m_MatrixStack[0]._03 = viewpos->x;
    m_MatrixStack[0]._13 = viewpos->y;
    m_MatrixStack[0]._23 = viewpos->z;
    g_W2VMatrix = m_MatrixStack[0];
}

That is my code, I transferred it with out mounted level math.

void Draw_Cube()
{
    RECT Rc;
    GetClientRect(g_hWnd, &Rc);

    //begin compose view matrix

    vector3 VecLook = {0.0f, 0.0f, 1.0f};
    vector3 VecCamPos = {0.0f, 10.0f, -18.0f};

    vector3 VecCamPosInv = { VecCamPos.x * -1.0f,
        VecCamPos.y * -1.0f, 
        VecCamPos.z * -1.0f };

    vector3 VecCamPosInvNorm = Vec3_Normalize(VecCamPosInv);

    float Dot = Vec3_Dot(VecLook, VecCamPosInvNorm);

    float x_rot = acosf(Dot);
    
    float y_rot = 0.0f;

    float z_rot = 0.0f;

    float sx = sinf(x_rot);
    float cx = cosf(x_rot);
    
    float sy = sinf(y_rot);
    float cy = cosf(y_rot);
    
    float sz = sinf(z_rot);
    float cz = cosf(z_rot);

    matrix4x4 MatView;
    
    MatView[0][0] = sx * sy * sz + cy * cz;
    MatView[1][0] = cx * sz;
    MatView[2][0] = sx * cy * sz - sy * cz;
    
    MatView[0][1] = sx * sy * cz - cy * sz;
    MatView[1][1] = cx * cz;
    MatView[2][1] = sx * cy * cz + sy * sz;

    MatView[0][2] = cx * sy;
    MatView[1][2] = -sx;
    MatView[2][2] = cx * cy;

    MatView[3][0] = VecCamPos.x;
    MatView[3][1] = VecCamPos.y;
    MatView[3][2] = VecCamPos.z;

    MatView[0][3] = 0.0f;
    MatView[1][3] = 0.0f;
    MatView[2][3] = 0.0f;
    MatView[3][3] = 1.0f;

    //finish compose view matrix
    
    //begin vertex transformations
    //dice mannequin has 8 vertices
    for ( int i = 0; i < 8; i++ )
    {

        vector3 Vec = Vec3_Mat4x4_Mul(g_VertBuff[i], MatView);

        Vec.x = Vec.x / Vec.z;
        Vec.y = Vec.y / Vec.z;
        Vec.x = Vec.x / ((float)Rc.proper / Rc.backside);

        Vec.x = Vec.x * Rc.proper / 2.0f + Rc.proper / 2.0f;
        Vec.y =-Vec.y * Rc.backside / 2.0f + Rc.backside / 2.0f;

        g_VertBuffTransformed[i] = Vec;
    }

This my code attracts dice mannequin (mannequin has 8 vertices). This my code works with bugs. I.e. once I change viewpos->x_rot in unique code – digicam strikes appropriately extra to prime at display. However once I change x_rot in my code – digicam not transferring in any respect, simply rotating my dice by X axis. Why my code not change dice place like unique code? Appears I transferred code appropriately.

About the author

Theme control panel

Leave a Comment