The N3D Mesh Sample shows how to:
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 a texture // - convert a mesh file into N3D format (see makefile) // - convert a texture into N3D format (see makefile) // - render a textured mesh // // // 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 "texture_n3dtex_bin.h" // Prototypes void SetupMatrices(); void RenderScene(const N3DFLOAT _timeStep); // Defines the video memory offset to that the texture is loaded. #define MY_TEXTURE_VRAMOFFSET 0 // The N3DDEVICE represents the interface to the Nintendo DS 3D hardware. N3DDEVICE g_device; // Program entry point int main(void) { // Initialize the hardware N3DSampleFrameworkInit(); // Load texture data to video memory N3DSampleFrameworkLoadTexture(MY_TEXTURE_VRAMOFFSET, texture_n3dtex_bin, texture_n3dtex_bin_size); // 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(2), N3DFLOAT(0)); N3DVECTOR3 at(N3DFLOAT(0), N3DFLOAT(0), N3DFLOAT(3)); 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.0f, 0.0f, 0.7f), 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.5f); material.diffuse.g = N3DFLOAT(0.5f); material.diffuse.b = N3DFLOAT(0.7f); material.specular.r = N3DFLOAT(1.0f); material.specular.g = N3DFLOAT(0.0f); material.specular.b = N3DFLOAT(0.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 various render states g_device.SetRenderState(N3DRS_ANTIALIASEENABLE, TRUE); // Set the texture g_device.SetTexture(MY_TEXTURE_VRAMOFFSET, 128, 128, N3DFMT_A1B5G5R5); g_device.SetTextureState(N3DTST_ADDRESSU, N3DTADDRESS_WRAP); g_device.SetTextureState(N3DTST_ADDRESSV, N3DTADDRESS_WRAP); g_device.SetTextureState(N3DTST_COLOROP, N3DTOP_MODULATE); // Transform and draw geometry. // Let the object rotate around its own Y-axis and move it // 3 units into the scene. N3DMATRIX transMatrix; N3DMATRIX rotationMatrix; N3DMATRIX worldMatrix; static N3DFLOAT angle(0); angle += _timeStep * N3DFLOAT(22.5f); N3DMatrixRotationY(rotationMatrix, DegreeToRadian(angle)); N3DMatrixTranslation(transMatrix, N3DFLOAT(0), N3DFLOAT(0), N3DFLOAT(3)); N3DMatrixMultiply(worldMatrix, rotationMatrix, transMatrix); g_device.SetTransform(N3DTS_WORLD, worldMatrix); N3DCMDSTREAM* pCmdStream = (N3DCMDSTREAM*)cube_n3dmesh_bin; g_device.DrawPrimitive(*pCmdStream); // End the scene and present it. g_device.EndScene(); g_device.Present(); }