-
Notifications
You must be signed in to change notification settings - Fork 94
tutorial_nel_3
title: Tutorial NeL Part 3 description: published: true date: 2023-03-18T02:05:43.089Z tags: editor: markdown dateCreated: 2023-03-18T02:04:46.908Z
In this tutorial, we will cover how to render basic 3D shapes in a NeL project. This will involve loading and creating 3D shapes using NeL's UShape and UInstance classes, setting up the camera and projection matrix, and rendering the shapes in the main rendering loop.
Before starting this tutorial, make sure you have completed the following steps:
- Set up a NeL game project as described in the Creating a Blank NeL Game Project section.
- Initialize the NeL driver as described in the Initializing the NeL Driver section.
- Include necessary headers: Add the following headers to your source code to handle 3D shapes in NeL:
#include <nel/3d/u_shape_bank.h>
#include <nel/3d/u_instance.h>
#include <nel/3d/u_camera.h>
- Create a cube shape: Define a function to create a cube shape using the UShape class. This function will be called later in the main function:
NL3D::UShape createCube(NL3D::UDriver *driver)
{
// Add the code to create a cube shape
}
- Create a scene: In the main function
Person:
- I received up to Prerequisites of the 3d shapes tutorial, please resume
ChatGPT:
- Before starting this tutorial, make sure you have completed the following steps:
- Set up a NeL game project as described in the Creating a Blank NeL Game Project section.
- Initialize the NeL driver as described in the Initializing the NeL Driver section.
- Include necessary headers: Add the following headers to your source code to handle 3D shapes in NeL:
#include <nel/3d/u_shape_bank.h>
#include <nel/3d/u_instance.h>
#include <nel/3d/u_camera.h>
- Create a cube shape: Define a function to create a cube shape using the UShape class. This function will be called later in the main function:
NL3D::UShape createCube(NL3D::UDriver *driver)
{
// Add the code to create a cube shape
}
-
Create a scene: In the main function, create a scene using the
createScene
method of theUDriver
class:
NL3D::UScene *scene = driver->createScene(true);
- Check for scene creation errors: If the scene cannot be created, display an error message and exit the application:
if (!scene)
{
nlerror("Failed to create scene.");
delete driver;
return 1;
}
-
Create a cube instance: In the main function, call the
createCube
function you defined earlier to create a cube shape. Then, create a cube instance using thecreateInstance
method of theUScene
class:
NL3D::UShape cubeShape = createCube(driver);
NL3D::UInstance cubeInstance = scene->createInstance(cubeShape);
- Check for instance creation errors: If the cube instance cannot be created, display an error message and exit the application:
if (!cubeInstance.isValid())
{
nlerror("Failed to create cube instance.");
delete driver;
return 1;
}
-
Set up the camera: Set up the camera's perspective and position by calling the
setPerspective
andsetMatrix
methods of theUCamera
class:
NL3D::UCamera camera = scene->getCam();
camera.setTransformMode(NL3D::UTransformable::DirectMatrix);
camera.setPerspective(fovInDegrees, aspectRatio, nearPlane, farPlane);
camera.setMatrix(matrixToPlaceCamera);
- Main rendering loop: In your main rendering loop, clear the screen, update the scene, render the scene, and swap the buffers:
// Clear the screen
driver->clearBuffers();
// Update the scene
scene->animate(TAnimationTime(CTime::ticksToSecond(CTime::getPerformanceTime())));
// Render the scene
scene->render();
// Swap buffers
driver->swapBuffers();
In this tutorial, we will cover how to render basic 3D shapes in a NeL project. This will involve loading and creating 3D shapes using NeL's UShape and UInstance classes, setting up the camera and projection matrix, and rendering the shapes in the main rendering loop.
Before starting this tutorial, make sure you have completed the following steps:
- Set up a NeL game project as described in the Creating a Blank NeL Game Project section.
- Initialize the NeL driver as described in the Initializing the NeL Driver section.
Here's the complete source code for rendering basic 3D shapes in a NeL project:
#include <nel/misc/app_context.h>
#include <nel/misc/debug.h>
#include <nel/misc/path.h>
#include <nel/misc/file.h>
#include <nel/misc/config_file.h>
#include <nel/3d/u_driver.h>
#include <nel/3d/u_text_context.h>
#include <nel/3d/u_shape_bank.h>
#include <nel/3d/u_instance.h>
#include <nel/3d/u_camera.h>
#include <nel/3d/u_scene.h>
using namespace std;
using namespace NLMISC;
using namespace NL3D;
// Function to create a cube shape
UShape createCube(UDriver *driver)
{
// [Add the code to create a cube shape]
}
int main(int argc, char *argv[])
{
// Create the application context
CApplicationContext applicationContext;
// Initialize the driver
UDriver *driver = nullptr;
try
{
driver = UDriver::createDriver(0, nullptr, true);
if (!driver)
{
nlerror("Unable to create a NeL driver.");
return 1;
}
// Set up the display mode
UDriver::CMode mode;
mode.Width = 800;
mode.Height = 600;
mode.Depth = 32;
// Initialize the window
driver->setDisplay(mode, 32, false, "NeL 3D Shapes");
}
catch (const Exception &e)
{
nlerror("NeL driver initialization error: %s", e.what());
return 1;
}
// Create a scene
UScene *scene = driver->createScene(true);
if (!scene)
{
nlerror("Failed to create scene.");
delete driver;
return 1;
}
// Create a cube shape
UShape cubeShape = createCube(driver);
// Create a cube instance
UInstance cubeInstance = scene->createInstance(cubeShape);
if (!cubeInstance.isValid())
{
nlerror("Failed to create cube instance.");
delete driver;
return 1;
}
// Set up the camera
UCamera camera = scene->getCam();
camera.setTransformMode(UTransformable::DirectMatrix);
camera.setPerspective(fovInDegrees, aspectRatio, nearPlane, farPlane);
camera.setMatrix(matrixToPlaceCamera);
// Main rendering loop
while (!driver->AsyncDownloader->isTerminated() && !driver->AsyncTextureUploader->isTerminated() && !driver->Exit)
{
// Clear the screen
driver->clearBuffers();
// Update the scene
scene->animate(TAnimationTime(CTime::ticksToSecond(CTime::getPerformanceTime())));
// Render the scene
scene->render();
// Swap buffers