Jump to content

Set camera to a camera in the scene/level/map


Azure_Zero
 Share

Recommended Posts

Hi

I'm trying to set the camera to one I have in the scene

 

Currently I'm using one that is created, but I would like to switch to one that already is in the scene/level/map.

 

I'm curious as to how to do this?

 

Thank you for your time and replies

AZ

Link to comment
Share on other sites

due to my lack of knowledge about c++ (and in general!)...

i can't give you a solution.

 

if you place a hook on the Load function, you may store map entities for future use. But entities are not strictly speaking a camera. i don't know how to cast an entity to a camera

 

for instance in app.cpp you may:

 

//Store entities
vector<Entity*> entities;
//Store all entities when map is loaded
void StoreWorldObjects(Entity* entity, Object* extra)
{
System::Print("Loaded an entity and stored it: " + entity->GetKeyValue("name"));
entities.push_back(entity);
}

 

and to load and invoke this function:

 

//Load the map
std::string mapname = System::GetProperty("map", "Maps/start.map");
//Map::Load(mapname);
Map::Load(mapname, StoreWorldObjects);
//Store position of every entity
vector<Entity*>::iterator iter = entities.begin();
int id = 0;
for (iter; iter != entities.end(); iter++)
{
 Entity* entity = *iter;
 if (entity->script != NULL) {
  System::Print(entity->GetKeyValue("name"));
 }
 // 2 IntelliSense: a value of type "Leadwerks::Entity *" cannot be assigned to an entity of type "Leadwerks::Camera *" c:\Users\usuario\Documents\Leadwerks\Projects\GetMapEntity\Source\App.cpp 60 61 GetMapEntity
 //if (entity->GetKeyValue("name") == "EditorCamera") camera = entity;
}

 

but, the camera=entity isn't legal

 

if it works, then , you may test ic camera==nil and if so (no camera from map) you can create the camera by code..

 

any idea?

 

(i started a thread about lua-cpp iterations, should be good to have a simple way of connecting both)

Paren el mundo!, me quiero bajar.

Link to comment
Share on other sites

A camera as far as I know is extended from the base entity class. So you can iterate through the entity list and grab the camera based on the name - which is what I am doing in lua.

 

As for the actual editor's viewport camera - from what i can see its not being saved in your map, so you have to add your own camera and position/rotate it as needed.

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

in the code above, there are two cameras, the one created in code, the one created in the editor. The one on the editor is over the first one, so if you run the code, you will see what the editor camera see. But the freelook is working on the code created camera who's viewport is overlapped by the editor camera viewport.

 

if i, inside the Vector items loop i write:

 

if (entity->GetKeyValue("name") == "EditorCamera") entity->Release();

 

then the editor camera is released and the code created camera is seen and work as supposed

 

So i get the camera from the editor, but can't assign it to the Camera type variable camera defined in app.h

the sentence camera=entity is not legal, the error message is:

a value of type "Leadwerks::Entity *" cannot be assigned to an entity of type "Leadwerks::Camera *"

 

does any one know if it is possible to do that?

Paren el mundo!, me quiero bajar.

Link to comment
Share on other sites

I think a camera is an entity so just change the Leadwerks::Camera* to Leadwerks::Entity* and give it a try. Or you can try casting it before you assign it.

 

I find this very odd that it has the editor camera though. I wouldn't think it should have that entity in the map file itself. I wonder if this is a bug that it's there.

Link to comment
Share on other sites

it works! :) thanks

 

declaring camear as entity in app.h was enough.

 

//Camera* camera;
Entity* camera;

 

in App::Start() :

 

//Load the map
std::string mapname = System::GetProperty("map", "Maps/start.map");
Map::Load(mapname, StoreWorldObjects);
vector<Entity*>::iterator iter = entities.begin();
int id = 0;
for (iter; iter != entities.end(); iter++)
{
 Entity* entity = *iter;
 System::Print(entity->GetKeyValue("name"));
 if (entity->GetKeyValue("name") == "EditorCamera"){
  camera = entity;
  System::Print("found a camera in the editor");
 }
}
if (camera == NULL) {
 System::Print("no camera from editor");
 //Create a camera
 camera = Camera::Create();
 camera->Move(0, 2, -5);
}

 

now i got the Editor Camera, it's position and orientation

works with freelookmode if i set it to true

 

Why use the editor camera and not the one created in c++?

 

For me is just a matter of connecting lua and c++

 

Perhaps editor is a better place to set the camera, position it etc

I'm not concerned only about cameras.

 

Probably in a near future, i will only use c++, but at this moment i'm using a combination and want to see how can i define one thing in the editor and have a reference from c++

 

thxs again

Paren el mundo!, me quiero bajar.

Link to comment
Share on other sites

I find this very odd that it has the editor camera though. I wouldn't think it should have that entity in the map file itself. I wonder if this is a bug that it's there.

in the code above, there are two cameras, the one created in code, the one created in the editor. The one on the editor is over the first one, so if you run the code, you will see what the editor camera see. But the freelook is working on the code created camera who's viewport is overlapped by the editor camera viewport.

 

thats odd - unless i am misunderstanding what you are saying, if i do not create an actual camera entity via the editor or via lua code, then there is no camera when I run the program (ie nothing is rendered). Also when iterating through the loaded entities when loading the map file, i am also checking the entity class/name and it does not show a editor viewport camera listed.

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

Perhaps editor is a better place to set the camera, position it etc

I'm not concerned only about cameras.

 

Wait a min. When you say editor camera you mean that you created a camera entity in your scene using the editor? I thought you were talking about the viewport camera the editor uses behind the scenes for the 3D viewport (not listed in your scene tab). I'm confused as to what you mean now Charrua.

Link to comment
Share on other sites

AZ wrote:

Hi

I'm trying to set the camera to one I have in the scene

 

Currently I'm using one that is created, but I would like to switch to one that already is in the scene/level/map.

 

the situation, and i guess the needs of AZ was to get a camera created in the editor from c++

 

so, i created a camera in the editor, name it EditorCamera and then inside c++ i scan the list of objects and get's the "EditorCamera"entity

 

i create a camera only if i didn't found the one i created on the editor, because, as you said, having no camera is no sense.

 

in App.h, the variable camera is declared as Camera

if we change the declaration from Camera to Entity, then there are no problem and we can get the EditorCamera from the map and assign it to the global variable camera from c++

 

i'm just trying to solve the AZ trouble, and by te way, learning a bit more.

Paren el mundo!, me quiero bajar.

Link to comment
Share on other sites

i create a camera only if i didn't found the one i created on the editor, because, as you said, having no camera is no sense.

 

ah i see - i was misunderstanding - someone had mentioned the viewport camera and from my testing it showed that the editor's built-in cameras were not saved into the mapfile - but i see now that you are referring to a camera created in the editor.

 

yes, i think we are all in the same boat as far as the learning curve on LE3. i personally have to "unlearn" all the ways we did this in LE2 as alot does not apply anymore. For example, the previous SBX file (LE2's version of a map) used to save the actual Editor's perspective/3D camera's position and rotation values so you could easily set a camera to that if you desired.

 

--edit actually from just a quick test i believe the built-in cameras' positions/rotations are being saved in the mapfile, but offhand I don't know how to retrieve that information. Josh could maybe chime in and explain as the map file is ascii gibberish when opened in a text editor like we did with the old sbx files.

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

Hey there.

 

In one of my projects I keep the default camera in my project, however, during load-time, I search the map for key entities*, one of these key entities is a camera, if one is found, it'll delete the default camera, and use the one in the map. I don't seem to have any problems with this, and it works as expected.

 

If you want to simply store a location for the camera to move to (maybe a security camera, or cut-scene) I'd use a pivot, and lock the camera to that pivot

 

*It's useful to search the map for key entities all at one time so you can reference them later. Example: Spawn Points, HeightMap

I made a LoadMap function that wraps the Leadwerks LoadMap function to give all the extra functionality of referencing important entities, and in my case generating trees, and water.

Link to comment
Share on other sites

actually from just a quick test i believe the built-in cameras' positions/rotations are being saved in the mapfile, but offhand I don't know how to retrieve that information.

 

What was your test? Are you thinking it's just position/rotation and not a full entity object?

Link to comment
Share on other sites

AZ wrote:

 

 

the situation, and i guess the needs of AZ was to get a camera created in the editor from c++

 

so, i created a camera in the editor, name it EditorCamera and then inside c++ i scan the list of objects and get's the "EditorCamera"entity

 

i create a camera only if i didn't found the one i created on the editor, because, as you said, having no camera is no sense.

 

in App.h, the variable camera is declared as Camera

if we change the declaration from Camera to Entity, then there are no problem and we can get the EditorCamera from the map and assign it to the global variable camera from c++

 

i'm just trying to solve the AZ trouble, and by te way, learning a bit more.

 

We have a winner, who got what I was asking for,

I pasted the code in, but I'm getting a problem on

Map::Load(mapname, StoreWorldObjects);

I'm getting

Error 1 error C3867: 'State::StoreWorldObjects': function call missing argument list; use '&State::StoreWorldObjects' to create a pointer to member

Link to comment
Share on other sites

did you declare the entities vector and the function?

 

//Store entities
vector<Entity*> entities;
//Store all entities when map is loaded
void StoreWorldObjects(Entity* entity, Object* extra)
{
System::Print("Loaded an entity and stored it: " + entity->GetKeyValue("name"));
entities.push_back(entity);
}

Paren el mundo!, me quiero bajar.

Link to comment
Share on other sites

@shadoh

 

*It's useful to search the map for key entities all at one time so you can reference them later. Example: Spawn Points, HeightMap

I made a LoadMap function that wraps the Leadwerks LoadMap function to give all the extra functionality of referencing important entities, and in my case generating trees, and water

 

i use entities name's to embed csv's with extra info

i found a way in leadwerks to do so via SetKeyValue

 

with this simple script:

Script.AnInt=5--int "anInt"
Script.AString="Hola"--string "string"
function Script:Start()
self.entity:SetKeyValue("anInt", self.AnInt)
self.entity:SetKeyValue("aString", self.AString)
end

attached to an entity you can pass any value, so you may have a general name: "WayPoint" and pass as keyvalues other properties not handled by the editor

 

you only has to use GetKeyValue from C++

 

System::Print("Loaded an entity and stored it: " + entity->GetKeyValue("name") + " /" + entity->GetKeyValue("anInt") + "/" + entity->GetKeyValue("aString")+"/");

  • Upvote 1

Paren el mundo!, me quiero bajar.

Link to comment
Share on other sites

did you declare the entities vector and the function?

 

//Store entities
vector<Entity*> entities;
//Store all entities when map is loaded
void StoreWorldObjects(Entity* entity, Object* extra)
{
System::Print("Loaded an entity and stored it: " + entity->GetKeyValue("name"));
entities.push_back(entity);
}

 

Yep, I included that.

 

I'll move the function next to the class file and see if that fixes it.

Link to comment
Share on other sites

I filled in the MAP::Load(mapname, StoreWorldObjects,NULL,Map::LoadScripts)

and the error changedto

Error 1 error C3867: 'MainMenuState::StoreWorldObjects': function call missing argument list; use '&MainMenuState::StoreWorldObjects' to create a pointer to member

Link to comment
Share on other sites

this fragment must be in app.cpp before start() function

 

 

this is my complete App.cpp:

 

#include "App.h"

using namespace Leadwerks;

App::App() : window(NULL), context(NULL), world(NULL), camera(NULL) {}

App::~App() { delete world; delete window; }

Vec3 camerarotation;
#if defined (PLATFORM_WINDOWS) || defined (PLATFORM_MACOS)
bool freelookmode=true;
#else
bool freelookmode=false;
#endif

//Store entities
vector<Entity*> entities;

//Store all entities when map is loaded
void StoreWorldObjects(Entity* entity, Object* extra)
{
System::Print("Loaded an entity and stored it: " + entity->GetKeyValue("name") + " /" + entity->GetKeyValue("anInt") + "/" + entity->GetKeyValue("aString")+"/");
entities.push_back(entity);
}


bool App::Start()
{
//Initialize Steamworks (optional)
/*if (!Steamworks::Initialize())
{
System::Print("Error: Failed to initialize Steam.");
return false;
}*/

//Create a window
window = Leadwerks::Window::Create("GetMapEntity");

//Create a context
context = Context::Create(window);

//Create a world
world = World::Create();

//Load the map
std::string mapname = System::GetProperty("map", "Maps/start.map");

Map::Load(mapname, StoreWorldObjects);

vector<Entity*>::iterator iter = entities.begin();

int id = 0;
for (iter; iter != entities.end(); iter++)
{
Entity* entity = *iter;
System::Print(entity->GetKeyValue("name"));
if (entity->GetKeyValue("name") == "EditorCamera"){
camera = entity;
System::Print("found a camera in the editor");
}
}

if (camera == NULL) {
System::Print("no camera from editor");
//Create a camera
camera = Camera::Create();
camera->Move(0, 2, -5);
}

//Hide the mouse cursor
window->HideMouse();

//Move the mouse to the center of the screen
window->SetMousePosition(context->GetWidth()/2,context->GetHeight()/2);

freelookmode = false;

return true;
}

bool App::Loop()
{
//Close the window to end the program
if (window->Closed()) return false;

//Press escape to end freelook mode
if (window->KeyHit(Key::Escape))
{
if (!freelookmode) return false;
freelookmode=false;
window->ShowMouse();
}

if (freelookmode)
{
//Keyboard movement
float strafe = (window->KeyDown(Key:) - window->KeyDown(Key::A))*Leadwerks::Time::GetSpeed() * 0.05;
float move = (window->KeyDown(Key::W) - window->KeyDown(Key::S))*Leadwerks::Time::GetSpeed() * 0.05;
camera->Move(strafe,0,move);

//Get the mouse movement
float sx = context->GetWidth()/2;
float sy = context->GetHeight()/2;
Vec3 mouseposition = window->GetMousePosition();
float dx = mouseposition.x - sx;
float dy = mouseposition.y - sy;

//Adjust and set the camera rotation
camerarotation.x += dy / 10.0;
camerarotation.y += dx / 10.0;
camera->SetRotation(camerarotation);

//Move the mouse to the center of the screen
window->SetMousePosition(sx,sy);
}

Leadwerks::Time::Update();
world->Update();
world->Render();
context->Sync(false);

return true;
}

Paren el mundo!, me quiero bajar.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...