Sample Project - Wireframe

N3D Wireframe Sample

The N3D Mesh Sample shows how to:

Screenshot:
sampleproject_wireframe.png

Sourcecode:

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

// 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();


    // 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(0), N3DFLOAT(0));
    N3DVECTOR3  at(N3DFLOAT(0), N3DFLOAT(0), N3DFLOAT(1.5));
    N3DVECTOR3  up(N3DFLOAT(0), N3DFLOAT(1), N3DFLOAT(0));

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






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);


    // Set fillmode renderstate to enable wireframe
    g_device.SetRenderState(N3DRS_FILLMODE, N3DFILL_WIRE);
    //g_device.SetRenderState(N3DRS_CULLMODE, N3DCULL_NONE);
    g_device.SetRenderState(N3DRS_ANTIALIASEENABLE, TRUE);


    // Transform and draw geometry.
    N3DMATRIX transMatrix;
    N3DMATRIX rotationMatrix;
    N3DMATRIX rotationYMatrix;
    N3DMATRIX rotationXMatrix;
    N3DMATRIX worldMatrix;
    static N3DFLOAT angle(0);

    angle += _timeStep * N3DFLOAT(22.5f);
    N3DMatrixRotationY(rotationYMatrix, DegreeToRadian(angle));
    N3DMatrixRotationX(rotationXMatrix, N3DFLOAT(2.0)*DegreeToRadian(angle));
    N3DMatrixMultiply(rotationMatrix, rotationYMatrix, rotationXMatrix);
    
    N3DMatrixTranslation(transMatrix, N3DFLOAT(0), N3DFLOAT(0), N3DFLOAT(1.5));
    N3DMatrixMultiply(worldMatrix, rotationMatrix, transMatrix);

    g_device.SetTransform(N3DTS_WORLD, worldMatrix);

    N3DCMDSTREAM* pCmdStream = (N3DCMDSTREAM*)knot_n3dmesh_bin;
    g_device.DrawPrimitive(*pCmdStream);


    // 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