Jump to content

Sample code without script


photo

Recommended Posts

Hello,
I have searched in the doc and the forum but haven't found anything about that.
For the moment, I initialize the engine in QT viewport without problem but,
I would like to know if there is a way to :

Initialize engine
Load a mesh and his material
Render the scene

Without any script or .world or .usc file ?
I think I don't understand how this engine work...
thanks.

Link to comment

Hi Cedric,

A script is optional, but the point is that you actually need a world.

You can check out the ViewportQt sample included in the SDK (In the SDK Browser open Samples -> 3rd party -> ViewportQt ) to familiarize yourself with the workflow.

See the SystemLogic::init() method in the AppSystemLogic.cpp file of this sample:

int AppSystemLogic::init()
{
  	// here we load a world-file named "viewport_world.world" (you can replace it with an empty world if you wish)
	Unigine::Console::run("world_load data/viewport_world");

	return 1;
}

In order to manage the logic of your world you'll need an instance of an AppWorldLogic class inherited from the  Unigine::WorldLogic.

As for loading meshes, creating objects, etc., you can replace the contents of the ViewportQt.cpp file in the sample with the following code (with comments):

// Qt
#include "MainWindow.h"
#include <QApplication>
#include <QDesktopWidget>

// Unigine
#include "AppSystemLogic.h"
#include <UnigineEngine.h>
// including necessary headers
#include <UnigineWorld.h>
#include <UnigineLights.h>
#include <UnigineGame.h>
#include <UnigineMaterials.h>
#include <UnigineObjects.h>
// Common
#include "GLAppQt.h"
#ifdef _WIN32
#include "D3D11AppQt.h"
#endif


enum
{
	RENDER_OPENGL,
	RENDER_D3D11,
};

int get_render_api()
{
#ifdef _WIN32
	const int DEFAULT = RENDER_D3D11;
#else
	const int DEFAULT = RENDER_OPENGL;
#endif

	QStringList arguments = qApp->arguments();
	int index = arguments.indexOf("-video_app");
	if (index == -1)
		return DEFAULT;

	if (index + 1 >= arguments.size())
	{
		arguments.removeAt(index);
		return DEFAULT;
	}

	if (arguments[index + 1] == "opengl")
		return RENDER_OPENGL;
#ifdef _WIN32
	if (arguments[index + 1] == "direct3d11")
		return RENDER_D3D11;
#endif
	return DEFAULT;
}

// injecting namespaces
using namespace Unigine;
using namespace Unigine::Math;

// declaring our World Logic class implementing the logic of our world
class AppWorldLogic : public Unigine::WorldLogic
{
public:
	AppWorldLogic() {}
	 virtual  ~AppWorldLogic() {}

	int init() override
	{
		// creating a world light source an setting its color to white
		LightWorldPtr	thesun = LightWorld::create(Math::vec4(1.0f, 1.0f, 1.0f, 1.0f));

		// setting light source's parameters (intensity, disable angle, scattering type, name and rotation)
		thesun->setName("Sun");
		thesun->setDisableAngle(90.0f);
		thesun->setIntensity(1.0f);
		thesun->setScattering(LightWorld::SCATTERING_SUN);
		thesun->setWorldRotation(Math::quat(86.0f, 30.0f, 300.0f));
		
		// creating a box (ObjectMeshDynamic node)
		MeshPtr mesh = Mesh::create();
		mesh->addBoxSurface("box_surface", Math::vec3(1.5f, 1.5f, 1.5f));
		ObjectMeshDynamicPtr my_mesh = ObjectMeshDynamic::create(mesh);

		MeshPtr mesh2 = Mesh::create();
		// loading a mesh from an fbx model imported to the data/fbx/ folder using the Editor
		mesh2->load("data/some_mesh.mesh");

		//create another ObjectMeshStatic node from the loaded mesh
		ObjectMeshStaticPtr my_mesh2 = ObjectMeshStatic::create(mesh2);
      
		// getting the base mesh_base material to inherit from
		MaterialPtr mesh_base = Materials::findMaterial("mesh_base");
		// creating a new child material of the mesh_base named "my_mesh_base0"
		MaterialPtr my_mesh_base = mesh_base->inherit("my_mesh_base0");

		// setting the albedo color of the material to red
		my_mesh_base->setParameterFloat4("albedo_color", Math::vec4(255, 0, 0, 255));

		// assigning a "my_mesh_base0" material to the surface 0 of the my_mesh ObjectMeshDynamic node
		my_mesh->setMaterial("my_mesh_base0", 0);
		// assigning a "my_mesh_base0" material to all surfaces of the my_mesh2 ObjectMeshDynamic node
		my_mesh2->setMaterial("my_mesh_base0", *);
		
		// creating a new PlayerSpectator instance
		PlayerSpectatorPtr playerSpectator = PlayerSpectator::create();

		// setting necessary parameters: FOV, ZNear, ZFar, view direction vector and position.
		playerSpectator->setFov(90.0f);
		playerSpectator->setZNear(0.1f);
		playerSpectator->setZFar(10000.0f);
		playerSpectator->setViewDirection(Math::vec3(0.0f, 1.0f, 0.0f));
		playerSpectator->setWorldPosition(Math::Vec3(-1.6f, -1.7f, 1.7f));

		// setting the player as a default one via the Game singleton instance
		Game::setPlayer(playerSpectator);
		//Unigine::World::loadNode("data/material_ball.node");
		Unigine::World::loadNode("data/some_node.node");
		return 1;
	}

	int shutdown() override
	{

		return 1;
	}

	int update() override
	{


		return 1;
	}



};
int main(int argc, char *argv[])
{
	// Qt part
	QApplication app(argc, argv);
	MainWindow window;

	// UnigineApp
	AppQt *widget = NULL;
	if (get_render_api() == RENDER_OPENGL)
		widget = new GLAppQt(&window);
	else
	{
#ifdef _WIN32
		widget = new D3D11AppQt(&window);
#endif
	}

	// UnigineLogic
	AppSystemLogic system_logic;
	AppWorldLogic world_logic;
	int unigine_argc = argc + 2;
	char **unigine_argv = new char *[unigine_argc];
	for (int i = 0; i < argc; i++)
	{
		unigine_argv[i] = new char[strlen(argv[i])];
		strcpy(unigine_argv[i], argv[i]);
	}
	unigine_argv[argc] = strdup("-data_path");
	unigine_argv[argc + 1] = strdup("./"); // local path

	Unigine::EnginePtr engine(UNIGINE_VERSION, widget, unigine_argc, unigine_argv);
	engine->addSystemLogic(&system_logic);
	// adding our world logic	
	engine->addWorldLogic(&world_logic);

	window.setWindowTitle("main_qt");
	window.setCentralWidget(widget);
	window.move((QApplication::desktop()->screen()->width() - window.width()) / 2, (QApplication::desktop()->screen()->height() - window.height()) / 2);
	window.show();

	return app.exec();
}

You can learn other things from our Quick Start Tutorial.

Hope this helps!

Thanks!

Link to comment

Yes, in meantime I have found the solution and my problem came from the initialization of QT on the main window that appear always black with Unigine evaluation logo.
In the second and third viewport there is no problem, my scene appear correctly.

I'm really surprised by the render quality.
That's really incredible !
Thanks for your answer.

Link to comment

Maybe have you got some idea why the main window stay black ?
I can draw the profiler with Fps.
The profiler draw :
Triangles All : 0
Triangles Shadows : 0
Triangles viewport : 0
If I open a new viewport the render appear OK in the viewport.
I use the sample code for QT integration.
Maybe a flag or other ?
Thanks.

Link to comment
×
×
  • Create New...