サンプル

ライト

ライト - MascotCapsule eruption

ここではライトについて解説します。




※ 開発環境によってダウンロードするサンプルデータが異なります。

目次




モデルの凹凸や材質の質感、また存在を自然に見せることが可能な機能です。


1. ライトの種類

ライトの種類は、ライト生成時に用いる mceLight_create( type, exception ) の引数 type で設定します。
ライトのタイプ設定には以下の 5 種類が用意されています。



2. 各ライトの設定

環境光源と平行光源

環境光源、平行光源ともにライトの種類を生成後、mceLight_setColor( This, bgr ) で光源カラーの設定、mceLight_setIntensity( This, i ) で光源強度の設定を行い、平行光源の場合は、光源方向も設定する必要があります。
以上の処理と合わせて、ライトを有効化することで任意のライティングが可能となっています。

下記のサンプルプログラムでは環境光源と平行光源をワールド全体に反映するように mceGraphics3D に設定しています。

#define ANGLE        (360)          /* 角度 */

    (中略)

typedef struct tagWork
{
    (中略)

    mceGraphics3D *G3d;             /* Graphics3D */

    (中略)

    mceFigure       *Figure;        /* Figure       */

    (中略)

    mceAppearance   *Appe;          /* Appearance   */

/*------ 光源 ------*/

hi_uint8 LightFlag;                 /* ライトフラグ */

mceLight *AmbientLight;             /* AmbientLight   */
mceLight *DirectionLight;           /* DirectionLight */

hi_coord LightRx;                   /* 光源X軸回転 */

    (中略)
} WORK;
hi_exception exception;

    (中略)

/* mceFigureの持つmceAppearanceを取得 */
work->Appe = (mceAppearance *)mceObject3D_findObject3D (
                    (mceObject3D *)work->Figure,
                    ClassId_Appearance,
                    0);
if (work->Appe == NULL)
{
    return 1;
}

    (中略)

work->LightFlag = 1;

/* 環境光源を生成 */
work->AmbientLight = mceLight_create (mceLight_Type_AMBIENT,
                             &exception);
if (exception != hi_NoException)
{
    return 1;
}

/* 平行光源を生成 */
work->DirectionLight = mceLight_create (mceLight_Type_DIRECTION,
                             &exception);
if (exception != hi_NoException)
{
    return 1;
}
    (中略)

hi_exception exception;

mceTransform matrix;
mceTransform matrix2;
mceVector3D  rotate;

hi_sint32 data;

    (中略)

/* mceAppearanceのプロパティを取得 */
data = mceAppearance_getProperties (work->Appe);

if (work->LightFlag == 0)
{
/* ライトを無効化 */
    exception = mceAppearance_setProperties (work->Appe,
                    data & ~MCE_APPEARANCE_LIGHTING);
    if (exception != hi_NoException)
    {
        return 1;
    }
}
else
{
/* カラータイプを設定 */
exception = mceAppearance_setColor (work->Appe,
            mceAppearance_MaterialType_AMBIENT,
            0x00ffffff);
    if (exception != hi_NoException)
    {
        return 1;
    }

/* ライトを有効化*/
    exception = mceAppearance_setProperties (work->Appe,
                    data | MCE_APPEARANCE_LIGHTING);
    if (exception != hi_NoException)
    {
        return 1;
    }

/* テクスチャブレンドモードを設定 */
    exception = mceAppearance_setTextureBlendMode (work->Appe, 0,
                                                   mceAppearance_Texture_FUNC_MODULATE);
    if (exception != hi_NoException)
    {
        return 1;
    }

/* 環境光源の色を設定 */
    mceLight_setColor      (work->AmbientLight, 0x00ffffff);
/* 環境光源の強度を設定 */
    mceLight_setIntensity  (work->AmbientLight, MCE_F2C (0.4f));

/* 環境光源をmceGraphics3Dに設定 */
    exception = mceGraphics3D_setLight (work->G3d, 0,
                                        work->AmbientLight, NULL);
    if (exception != hi_NoException)
    {
        return 1;
    }

/* 平行光源の方向を設定 */
    mceVector3D_set        (&rotate,
                            MCE_F2C (0.0f), MCE_F2C(0.0f), MCE_F2C (1.0f));
    mceTransform_setRotate (&matrix2, &rotate,
                            MCE_F2C (-45.0f) / ANGLE);
    mceTransform_multiply  (&matrix, &matrix2);

    mceVector3D_set        (&rotate,
                            MCE_F2C (1.0f), MCE_F2C (0.0f), MCE_F2C (0.0f));
    mceTransform_setRotate (&matrix2, &rotate,
                            work->LightRx / ANGLE);
    mceTransform_multiply  (&matrix, &matrix2);

/* 平行光源の色を設定 */
    mceLight_setColor     (work->DirectionLight, 0x00ffffff);
/* 平行光源の強度を設定 */
    mceLight_setIntensity (work->DirectionLight, MCE_F2C (1.0f));

/* 平行光源をmceGraphics3Dに設定 */
exception = mceGraphics3D_setLight (work->G3d, 1,
                                    work->DirectionLight, &matrix);
    if (exception != hi_NoException)
    {
        return 1;
    }
}
描画結果 環境光源のみ 描画結果 環境光源 + 平行光源
環境光源のみ 環境光源 + 平行光源
描画結果

点光源

ライトの種類を点光源で生成後、他のライトと同様に mceLight_setColor( This, bgr )、 mceLight_setIntensity( This, i ) でそれぞれ光源カラーと光源強度の設定を行い、 mceLight_setAttenuation( This, c, l, q ) で光源減衰率、そして光源位置を設定する必要があります。
以上の処理と合わせて、ライトを有効化することで任意のライティングが可能となっています。


mceLight OmniLight;        /*WORK構造体内に追加*/

hi_exception exception;

    (中略)

/* mceFigureの持つmceAppearanceを取得 */
work->Appe = (mceAppearance *)mceObject3D_findObject3D (
                    (mceObject3D *)work->Figure,
                    ClassId_Appearance,
                    0);
if (work->Appe == NULL)
{
    return 1;
}

    (中略)

/* 点光源を生成 */
work->OmniLight = mceLight_create (mceLight_Type_OMNI, &exception);
if (exception != hi_NoException)
{
    return 1;
}

    (中略)

hi_exception exception;

mceTransform matrix;

    (中略)

hi_sint32      data;

    (中略)

/* mceAppearanceのプロパティを取得 */
data = mceAppearance_getProperties (work->Appe);

/* 点光源を有効化 */
exception = mceAppearance_setProperties (work->Appe,
                                         data | MCE_APPEARANCE_LIGHTING);
if (exception != hi_NoException)
{
    return 1;
}

/* テクスチャブレンドモードを設定 */
exception = mceAppearance_setTextureBlendMode (work->Appe, 0,
                mceAppearance_Texture_FUNC_MODULATE);
if (exception != hi_NoException)
{
    return 1;
}

/* 点光源の位置を設定 */
mceTransform_setTranslate (&matrix,
                           MCE_F2C (0.0f), MCE_F2C (5.0f), MCE_F2C (3.0f));

/* 点光源の色を設定 */
mceLight_setColor      (work->OmniLight, 0x00ffffff);
/* 点光源の強度を設定 */
mceLight_setIntensity (work->OmniLight, MCE_F2C (0.4f));

/* 減衰設定 */
exception = mceLight_setAttenuation (work->OmniLight,
                                     MCE_F2C (1.0f), MCE_F2C (0.0f), MCE_F2C (0.0f));
if (exception != hi_NoException)
{
    return 1;
}

/* 点光源をmceGraphics3Dに設定 */
exception = mceGraphics3D_setLight (work->G3d, 1,
                                    work->OmniLight, &matrix);
if (exception != hi_NoException)
{
    return 1;
}

スポット光源

ライトの種類をスポット光源で生成後、他のライトと同様に mceLight_setColor( This, bgr )、mceLight_setIntensity( This, i ) でそれぞれ光源カラーと光源強度の設定を行い、 mceLight_setAttenuation( This, c, l, q ) で光源減衰率、 mceLight_setSpot( This, coff, exp ) でスポットアングルやスポット指数、 そして光源位置と光源方向を設定する必要があります。
以上の処理と合わせて、 ライトを有効化することで任意のライティングが可能となっています。


mceLight SpotLight;        /* WORK構造体内に追加 */

hi_exception exception;

    (中略)

/* mceFigureの持つmceAppearanceを取得 */
work->Appe = (mceAppearance *)mceObject3D_findObject3D (
                    (mceObject3D *)work->Figure,
                    ClassId_Appearance,
                    0);
if (work->Appe == NULL)
{
    return 1;
}

    (中略)

/* スポット光源を生成 */
work->SpotLight = mceLight_create (mceLight_Type_SPOT, &exception);
if (exception != hi_NoException)
{
    return 1;
}

    (中略)

hi_exception  exception;

mceTransform matrix;
mceTransform matrix2;
mceVector3D  rotate;

hi_sint32 data;

    (中略)

/* mceAppearanceのプロパティを取得 */
data = mceAppearance_getProperties (work->Appe);

/* スポット光源を有効化 */
exception = mceAppearance_setProperties (work->Appe,
                                         data | MCE_APPEARANCE_LIGHTING);
if (exception != hi_NoException)
{
    return 1;
}

/* テクスチャブレンドモードを設定 */
exception = mceAppearance_setTextureBlendMode (work->Appe, 0,
                                               mceAppearance_Texture_FUNC_MODULATE);
if (exception != hi_NoException)
{
    return 1;
}

/* スポット光源の位置を設定 */
mceTransform_setTranslate (&matrix,
                           MCE_F2C (0.0f), MCE_F2C (1.0f), MCE_F2C (5.0f));

/* スポット光源の方向を設定 */
mceVector3D_set           (&rotate,
                           MCE_F2C (0.0f), MCE_F2C(0.0f), MCE_F2C (1.0f));
mceTransform_setRotate    (&matrix2, &rotate, MCE_F2C (-45.0f) / ANGLE);
mceTransform_multiply     (&matrix, &matrix2);

mceVector3D_set           (&rotate,
                           MCE_F2C (1.0f), MCE_F2C (0.0f), MCE_F2C (0.0f));
mceTransform_setRotate    (&matrix2, &rotate, work->LightRx / ANGLE);
mceTransform_multiply     (&matrix, &matrix2);

/* スポット光源の色を設定 */
mceLight_setColor      (work->SpotLight, 0x00ffffff);
/* スポット光源の強度を設定 */
mceLight_setIntensity  (work->SpotLight, MCE_F2C (1.0f));

/* スポット光源のスポットアングルを設定 */
exception = mceLight_setSpot (work->SpotLight,
                              MCE_F2C (0.25f), MCE_F2C (1.0f));
if (exception != hi_NoException)
{
    return 1;
}

/* 減衰設定 */
exception = mceLight_setAttenuation (work->SpotLight,
            MCE_F2C (1.0f), MCE_F2C (0.0f), MCE_F2C (0.0f));
if (exception != hi_NoException)
{
    return 1;
}

/* スポット光源をmceGraphics3Dに設定 */
exception = mceGraphics3D_setLight (work->G3d, 1,
                                    work->SpotLight, &matrix);
if (exception != hi_NoException)
{
    return 1;
}



このページの先頭へ戻る