| Table of Contents |
|---|
First Part
In this part we will see how to create and render a custom node.
...
| Code Block | ||
|---|---|---|
| ||
#include <nel/misc/path.h>
#include <nel/misc/file.h>
#include <nel/misc/common.h>
#include <nel/3d/u_scene.h>
#include <nel/3d/texture.h>
#include <nel/3d/texture_mem.h>
#include <nel/3d/u_light.h>
#include <nel/3d/u_camera.h>
#include <nel/3d/u_driver.h>
#include <nel/3d/u_instance.h>
#include "../include/m_driver.h"
using namespace NLMISC;
using namespace NL3D;
//We create an MDriver instance here because fonctions displayCube and main will need it.
MDriver *Driver = MDriver::createDriver();
//Create and render the cube
void displayCube()
{
//Material needed by the fonction of render triangles
CMaterial mat;
mat.initUnlit();
mat.setSrcBlend(CMaterial::srcalpha);
mat.setDstBlend(CMaterial::invsrcalpha);
mat.setColor(CRGBA(50,255,255,150)); //Set a color to the material
CVertexBuffer vb;
//Must determinate the format for vertex
//Later we'll see that we could add other informations as the color for exemple
vb.setVertexFormat (CVertexBuffer::PositionFlag);
vb.setNumVertices (8);
{
CVertexBufferReadWrite vba;
vb.lock(vba);
vba.setVertexCoord (0, CVector (0.0, 0.0, 0.0));
vba.setVertexCoord (1, CVector (0.2, 0.00, 0.0));
vba.setVertexCoord (2, CVector (0.2, 0.00, 0.2));
vba.setVertexCoord (3, CVector (0, 0.0, 0.2));
vba.setVertexCoord (4, CVector (0.0, 0.2, 0));
vba.setVertexCoord (5, CVector (0.20, 0.2, 0.0));
vba.setVertexCoord (6, CVector (0.2, 0.2, 0.20));
vba.setVertexCoord (7, CVector (0, 0.2, 0.20));
}
//Now we can set to active our vertex buffer
Driver->activeVertexBuffer(vb);
CIndexBuffer pbQuad1;
pbQuad1.setNumIndexes (36);
{
CIndexBufferReadWrite iba1;
pbQuad1.lock(iba1);
//With vertex we can now make some triangles for cube
iba1.setTri (0, 0, 1, 2); //front
iba1.setTri (3, 2, 3, 0); //front
iba1.setTri (6, 2, 1, 5); //right
iba1.setTri (9, 5, 6, 2); //right
iba1.setTri (12, 2, 7, 3); //top
iba1.setTri (15, 2, 6, 7); //top
iba1.setTri (18, 0, 4, 1); //bottom
iba1.setTri (21, 1, 4, 5); //bottom
iba1.setTri (24, 3, 4, 0); //left
iba1.setTri (27, 3, 7, 4); //left
iba1.setTri (30, 6, 5, 4); //behind
iba1.setTri (33, 4, 7, 6); //behind
}
//Active index buffer
Driver->activeIndexBuffer(pbQuad1);
//Render of all triangles of the cube
Driver->renderTriangles(mat, 0, pbQuad1.getNumIndexes()/3);
}
sint main(int argc, char **argv)
{
try
{
if (!Driver) throw 2;
// create a window in 800x600
Driver->setDisplay(NL3D::UDriver::CMode(800, 600, 32, true),true, false);
// set the title
Driver->setWindowTitle(ucstring("NeL shape viewer"));
// can use both dds and tga textures for shapes
CPath::remapExtension ("dds", "tga", true);
// Create a scene
NL3D::UScene *Scene = Driver->createScene(true);
if (!Scene) throw 4;
// create the camera
UCamera Camera = Scene->getCam();
if (Camera.empty()) throw 5;
Camera.setTransformMode (UTransformable::DirectMatrix);
Camera.setPerspective ((float)Pi/2.f, 1.33f, 0.1f, 1000);
// camera will look at entities
Camera.lookAt (CVector(0, -10.f, 0.f), CVector(0.f, 0.f, 0.f));
// main loop
while (Driver->isActive())
{
Driver->EventServer.pump();
// the background is black
Driver->clearBuffers(CRGBA(0, 0, 0));
//Display our cube
displayCube();
// show the scene
Driver->swapBuffers();
// escape will leave the program
if (Driver->AsyncListener.isKeyPushed(KeyESCAPE))
{
break;
}
// F3 will change the render mode
else if (Driver->AsyncListener.isKeyPushed(KeyF3))
{
NL3D::UDriver::TPolygonMode p = Driver->getPolygonMode();
p = NL3D::UDriver::TPolygonMode(((int)p+1)%3);
Driver->setPolygonMode(p);
}
}
// we are leaving the program
// delete the scene
Driver->deleteScene(Scene);
// release all textures and others elements
Driver->release();
// delete the driver
delete Driver;
}
catch(int a)
{
return a;
}
catch(...)
{
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
} |
Second Part
In this part you learn how to move the object and change the color of vertex.
...