Sample Project - All in one

N3D all in one Sample

The N3D Mesh Sample shows how to:

Screenshot:
sampleproject_allinone.png

Sourcecode:

//
// N3D Mesh Sample (http:www.console-dev.de)
//
// Shows how to:
//      - setup the project and view matrices.
//      - setup a light
//      - setup a material
//      - transform geometry
//      - set render states
//      - render a mesh
//      - how to convert a mesh file into N3D format. See makefile.
//
//
// Date..: 2007/08/26
// Author: Peter Schraut
//
// Notes.: You can build the content with "make content"
//
#include <n3d.h>
#include <n3dsampleframework.h>
#include "cube_n3dmesh_bin.h"
#include "knot_n3dmesh_bin.h"
#include "texture_n3dtex_bin.h"


#define MY_TEXTURE_VRAMOFFSET 0

// Prototypes
void SetupMatrices();
void RenderScene(const N3DFLOAT _timeStep);

// The N3DDEVICE represents the interface to the Nintendo DS 3D hardware.
N3DDEVICE g_device;



// Program entry point
int main(void)
{
    // Initialize the hardware
    N3DSampleFrameworkInit();

    // Initialize the 3D device. This method must be called before using
    // anything else from the N3DDEVICE.
    g_device.Init();

    // Setup the projection and view matrix only once
    // at the beginning of the application.
    SetupMatrices();


    // Load texture to video memory
    N3DSampleFrameworkLoadTexture(MY_TEXTURE_VRAMOFFSET, texture_n3dtex_bin, texture_n3dtex_bin_size);


    // Infinite loop to keep the application running.
    for(;;)
    {
        N3DFLOAT timeStep(1.0f / 60.0f);

        RenderScene(timeStep);

        N3DSampleFrameworkWaitForVBlank();
    }

    return 0;
}






void SetupMatrices()
{   
    // Build the projection matrix
    N3DMATRIX projMatrix;

    N3DMatrixPerspectiveFovLH(projMatrix, N3DFLOAT(N3DPI / 4.0f), N3DFLOAT(4.0f / 3.0f), N3DFLOAT(1.0f), N3DFLOAT(100.0f));
    g_device.SetTransform(N3DTS_PROJECTION, projMatrix);


    // Build the view matrix
    N3DMATRIX   viewMatrix;
    N3DVECTOR3  eye(N3DFLOAT(0), N3DFLOAT(1), N3DFLOAT(0));
    N3DVECTOR3  at(N3DFLOAT(0), N3DFLOAT(0), N3DFLOAT(4));
    N3DVECTOR3  up(N3DFLOAT(0), N3DFLOAT(1), N3DFLOAT(0));

    N3DMatrixLookAtLH(viewMatrix, eye, at, up);
    g_device.SetTransform(N3DTS_VIEW, viewMatrix);
}




void ProcessGeometry(const N3DCMDSTREAM& _stream, const N3DFLOAT _angle)
{
    N3DMATRIX mat[9];

    // not the best code, but will do for now
    N3DMatrixRotationX(mat[0], _angle);
    N3DMatrixRotationZ(mat[1], _angle);
    N3DMatrixTranslation(mat[2], N3DFLOAT(0), N3DFLOAT(0), N3DFLOAT(1.3));
    N3DMatrixRotationY(mat[3], _angle);
    N3DMatrixTranslation(mat[4], N3DFLOAT(0), N3DFLOAT(0), N3DFLOAT(4.0));

    N3DMatrixMultiply(mat[5], mat[0], mat[1]);
    N3DMatrixMultiply(mat[6], mat[5], mat[2]);
    N3DMatrixMultiply(mat[7], mat[6], mat[3]);
    N3DMatrixMultiply(mat[8], mat[7], mat[4]);

    g_device.SetTransform(N3DTS_WORLD, mat[8]);
    g_device.DrawPrimitive(_stream);
}





void RenderScene(const N3DFLOAT _timeStep)
{
    // Set the clear color and new depth value, then start the new scene.
    g_device.Clear(N3DCLEAR_TARGET | N3DCLEAR_DEPTH, N3DCOLOR(0, 0, 0.8f), N3DFLOAT(1.0f));
    g_device.BeginScene();

    // Setup a light source. Lights in N3D are always directional
    // lights with an infinite distance.
    N3DLIGHT light;

    // Set the light color to white
    light.color.r= N3DFLOAT(1.0f);
    light.color.g= N3DFLOAT(1.0f);
    light.color.b= N3DFLOAT(1.0f);

    // Point into the scene
    light.direction.x = N3DFLOAT(0);
    light.direction.y = N3DFLOAT(0);
    light.direction.z = N3DFLOAT(1);

    g_device.SetLight(0, light);
    g_device.LightEnable(0, true);


    // Setup a material
    N3DMATERIAL material;

    material.ambient.r = N3DFLOAT(0.2f);
    material.ambient.g = N3DFLOAT(0.2f);
    material.ambient.b = N3DFLOAT(0.2f);

    material.diffuse.r = N3DFLOAT(0.4f);
    material.diffuse.g = N3DFLOAT(0.4f);
    material.diffuse.b = N3DFLOAT(0.6f);

    material.specular.r = N3DFLOAT(0.7f);
    material.specular.g = N3DFLOAT(0.7f);
    material.specular.b = N3DFLOAT(1.0f);

    material.emissive.r = N3DFLOAT(0.0f);
    material.emissive.g = N3DFLOAT(0.0f);
    material.emissive.b = N3DFLOAT(0.0f);

    // completely opaque
    material.alpha = N3DFLOAT(1);

    g_device.SetMaterial(material);


    // Transform and draw geometry.
    static N3DFLOAT angle(0);

    angle += _timeStep * N3DFLOAT(30.0f); // 30 degree / second

    g_device.SetTextureState(N3DTST_COLOROP, N3DTOP_DISABLE);
    g_device.SetRenderState(N3DRS_FILLMODE, N3DFILL_SOLID);
    ProcessGeometry(*(N3DCMDSTREAM*)knot_n3dmesh_bin, DegreeToRadian(angle));

    g_device.SetRenderState(N3DRS_FILLMODE, N3DFILL_SOLID);
    ProcessGeometry(*(N3DCMDSTREAM*)cube_n3dmesh_bin, DegreeToRadian(N3DFLOAT(90) + angle));

    g_device.SetRenderState(N3DRS_FILLMODE, N3DFILL_WIRE);
    ProcessGeometry(*(N3DCMDSTREAM*)knot_n3dmesh_bin, DegreeToRadian(N3DFLOAT(180) + angle));

    g_device.SetRenderState(N3DRS_FILLMODE, N3DFILL_SOLID);
    g_device.SetTexture(MY_TEXTURE_VRAMOFFSET, 128, 128, N3DFMT_A1B5G5R5);
    g_device.SetTextureState(N3DTST_COLOROP, N3DTOP_MODULATE);
    ProcessGeometry(*(N3DCMDSTREAM*)cube_n3dmesh_bin, DegreeToRadian(N3DFLOAT(270) + angle));



    // End the scene and present it.
    g_device.EndScene();
    g_device.Present();
}

Generated on Wed Aug 29 19:48:04 2007 for N3D by  doxygen 1.5.3