Skyscraper 2.0
cameratexture.cpp
Go to the documentation of this file.
1/*
2 Scalable Building Simulator - Camera Texture Object
3 The Skyscraper Project - Version 2.0
4 Copyright (C)2004-2024 Ryan Thoryk
5 https://www.skyscrapersim.net
6 https://sourceforge.net/projects/skyscraper/
7 Contact - ryan@skyscrapersim.net
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22*/
23
24#include <OgreCamera.h>
25#include <OgreSceneManager.h>
26#include <OgreTextureManager.h>
27#include <OgreTechnique.h>
28#include <OgreMaterialManager.h>
29#include <OgreRenderTexture.h>
30#include <OgreViewport.h>
31#include <OgreHardwarePixelBuffer.h>
32#include <OgreImage.h>
33#include "globals.h"
34#include "sbs.h"
35#include "texture.h"
36#include "scenenode.h"
37#include "elevatorcar.h"
38#include "floor.h"
39#include "shaft.h"
40#include "stairs.h"
41#include "map.h"
42#include "cameratexture.h"
43
44namespace SBS {
45
46CameraTexture::CameraTexture(Object *parent, const std::string &name, int quality, Real fov, const Vector3 &position, bool use_rotation, const Vector3 &rotation, bool permanent) : Object(parent)
47{
48 //creates a CameraTexture object
49
50 //if use_rotation is true, the rotation vector is a standard rotation, otherwise that vector represents a point in space to look at
51 //texture quality is 1 for 256x256, 2 for 512x512, and 3 for 1024x1024.
52
53 //set up SBS object
54 SetValues("CameraTexture", name, permanent);
55
56 FOV = fov;
57 camera = 0;
58 renderTexture = 0;
59 ortho = false;
60 zoom = 1.0;
61
62 unsigned int texture_size = 256;
63 if (quality == 2)
64 texture_size = 512;
65 if (quality == 3)
66 texture_size = 1024;
67
68 try
69 {
70 if (sbs->GetTextureManager()->GetTextureByName(name, "General") || sbs->GetTextureManager()->GetMaterialByName(name, "General"))
71 {
72 ReportError("Error creating camera texture:\nTexture with the name '" + name + "' already exists.");
73 return;
74 }
75
76 //create a new render texture
77 texturename = ToString(sbs->InstanceNumber) + ":" + name;
78 texture = Ogre::TextureManager::getSingleton().createManual(texturename, "General", Ogre::TEX_TYPE_2D, texture_size, texture_size, 0, Ogre::PF_R8G8B8, Ogre::TU_RENDERTARGET);
80 renderTexture = texture->getBuffer()->getRenderTarget();
81
82 //create and set up camera
83 camera = sbs->mSceneManager->createCamera(GetSceneNode()->GetFullName());
84 camera->setNearClipDistance(0.1);
85 camera->setFarClipDistance(0.0);
86 camera->setAspectRatio(1.0);
87
88 SetFOVAngle(fov);
89
90 //attach camera to scene node
92
93 //set camera position and rotation
94 Move(position);
95 if (use_rotation == true)
96 SetRotation(rotation);
97 else
98 GetSceneNode()->LookAt(rotation);
99
100 //attach camera viewport to texture
101 renderTexture->addViewport(camera);
102 renderTexture->getViewport(0)->setClearEveryFrame(true);
103 renderTexture->getViewport(0)->setBackgroundColour(Ogre::ColourValue::Black);
104 Enabled(true);
105
106 //create a new material
107 Ogre::MaterialPtr material = sbs->GetTextureManager()->CreateMaterial(name, "General");
109 material->setLightingEnabled(false);
110
111 //add texture multipliers
112 sbs->GetTextureManager()->RegisterTextureInfo(name, "", "", 1.0f, 1.0f, false, false, texture->getSize(), material->getSize());
113
114 //register with system
116
117 //disable by default
118 Enabled(false);
119
120 Report("Created camera texture '" + GetName() + "'");
121 }
122 catch (Ogre::Exception &e)
123 {
124 ReportError("Error creating camera texture:\n" + e.getDescription());
125 }
126}
127
129{
130 renderTexture->removeAllViewports();
131 if (camera)
132 {
134 sbs->mSceneManager->destroyCamera(camera);
135 }
136
139
140 if (sbs->FastDelete == false)
141 {
144
145 //unregister from parent
146 if (parent_deleting == false)
147 {
148 std::string type = GetParent()->GetType();
149
150 if (type == "ElevatorCar")
151 static_cast<ElevatorCar*>(GetParent())->RemoveCameraTexture(this);
152 else if (type == "Floor")
153 static_cast<Floor*>(GetParent())->RemoveCameraTexture(this);
154 else if (type == "Shaft Level")
155 static_cast<Shaft::Level*>(GetParent())->RemoveCameraTexture(this);
156 else if (type == "Stairwell Level")
157 static_cast<Stairwell::Level*>(GetParent())->RemoveCameraTexture(this);
158 }
159 }
160}
161
163{
164 if (renderTexture)
165 renderTexture->setActive(value);
166}
167
169{
170 if (renderTexture)
171 return renderTexture->isActive();
172
173 return false;
174}
175
177{
178 //set camera FOV angle
179
180 if (!camera)
181 return;
182
183 if (angle > 0 && angle < 179.63)
184 {
185 Real ratio = (float)camera->getAspectRatio();
186 if (ratio > 0)
187 camera->setFOVy(Degree(angle / ratio));
188 }
189}
190
192{
193 if (!camera)
194 return 0.0;
195 return (float)(camera->getFOVy().valueDegrees() * camera->getAspectRatio());
196}
197
199{
200 //set to default FOV angle value
202}
203
204void CameraTexture::LookAt(const Vector3 &position)
205{
206 GetSceneNode()->LookAt(position);
207}
208
210{
211 ortho = value;
212
213 if (!camera)
214 return;
215
216 if (value == true)
217 camera->setProjectionType(Ogre::PT_ORTHOGRAPHIC);
218 else
219 camera->setProjectionType(Ogre::PT_PERSPECTIVE);
220}
221
222void CameraTexture::GetImage(Ogre::Image &image)
223{
224 //copy texture to image
225
226 if (texture)
227 texture->convertToImage(image, false);
228}
229
231{
232 //zoom camera in orthographic mode
233
234 if (!camera || value <= 0)
235 return;
236
237 //get current camera view matrix
238 Ogre::Affine3 vmatrix = camera->getViewMatrix();
239
240 //remove old zoom factor
241 vmatrix[0][0] /= zoom;
242 vmatrix[1][2] /= zoom;
243
244 //apply new zoom factor
245 zoom = value;
246 vmatrix[0][0] *= zoom;
247 vmatrix[1][2] *= zoom;
248
249 //set camera view matrix with zoom applied
250 camera->setCustomViewMatrix(true, vmatrix);
251}
252
254{
255 return zoom;
256}
257
258}
Ogre::RenderTexture * renderTexture
std::string texturename
void EnableOrthographic(bool value)
void Enabled(bool value)
void GetImage(Ogre::Image &image)
Ogre::TexturePtr texture
void LookAt(const Vector3 &position)
void SetZoom(Real value)
CameraTexture(Object *parent, const std::string &name, int quality, Real fov, const Vector3 &position, bool use_rotation, const Vector3 &rotation, bool permanent=false)
Ogre::Camera * camera
void SetFOVAngle(Real angle)
const std::string & GetName()
Definition object.cpp:53
virtual bool ReportError(const std::string &message)
Definition object.cpp:84
Object * GetParent()
Definition object.cpp:42
virtual void Report(const std::string &message)
Definition object.cpp:78
bool parent_deleting
Definition object.h:64
virtual void SetRotation(const Vector3 &rotation)
Definition object.cpp:332
virtual void Move(const Vector3 &vector, Real speed=1.0)
Definition object.cpp:253
SceneNode * GetSceneNode()
Definition object.cpp:240
void SetValues(const std::string &type, const std::string &name, bool is_permanent, bool is_movable=true)
Definition object.cpp:144
const std::string & GetType()
Definition object.cpp:177
Ogre::SceneManager * mSceneManager
Definition sbs.h:138
void UnregisterCameraTexture(CameraTexture *camtex)
Definition sbs.cpp:4585
void RegisterCameraTexture(CameraTexture *camtex)
Definition sbs.cpp:4578
bool FastDelete
Definition sbs.h:188
TextureManager * GetTextureManager()
Definition sbs.cpp:4558
int InstanceNumber
Definition sbs.h:197
void DetachObject(Ogre::MovableObject *object)
void AttachObject(Ogre::MovableObject *object)
void LookAt(const Vector3 &point)
bool UnloadMaterial(const std::string &name, const std::string &group)
Definition texture.cpp:359
Ogre::MaterialPtr CreateMaterial(const std::string &name, const std::string &path)
Definition texture.cpp:2178
Ogre::TexturePtr GetTextureByName(const std::string &name, const std::string &group="General")
Definition texture.cpp:2268
bool UnloadTexture(const std::string &name, const std::string &group)
Definition texture.cpp:344
void RegisterTextureInfo(const std::string &name, const std::string &material_name, const std::string &filename, Real widthmult, Real heightmult, bool enable_force, bool force_mode, size_t tex_size, size_t mat_size)
Definition texture.cpp:303
bool UnregisterTextureInfo(std::string name, std::string material_name="")
Definition texture.cpp:328
void IncrementTextureCount()
Definition texture.cpp:2078
Ogre::TextureUnitState * BindTextureToMaterial(Ogre::MaterialPtr mMat, std::string texture_name, bool has_alpha)
Definition texture.cpp:2235
Ogre::MaterialPtr GetMaterialByName(const std::string &name, const std::string &group="General")
Definition texture.cpp:2224
Ogre::Vector3 Vector3
Definition globals.h:58
Ogre::Real Real
Definition globals.h:57
Ogre::Degree Degree
Definition globals.h:61
std::string ToString(int number)
Definition globals.cpp:279