Howto:Use a Camera View in an Instrument: Difference between revisions

Jump to navigation Jump to search
m
remove outdated proof-of-concept code that's predating the Canvas, since that'd would be the most sensible way forward these days
m (remove outdated proof-of-concept code that's predating the Canvas, since that'd would be the most sensible way forward these days)
 
Line 69: Line 69:
camera->attach(osg::Camera::COLOR_BUFFER, shot);
camera->attach(osg::Camera::COLOR_BUFFER, shot);
osgDB::writeImageFile(*shot,"image_file.png");
osgDB::writeImageFile(*shot,"image_file.png");
</syntaxhighlight>
==FlightGear code==
FlighGear already has an "Owner Drawn instrument" in flightgear/src/Instrumentation/od_gauge.cxx and .hxx (this is now in canvas/simgear). It already sets up a pre-render camera and a texture for it, and has a visitor to replace model's texture with the custom texture.
Using it would be something like this:
* Create a custom instrument which inherits od_gauge. class CameraInstrument : public FGODGauge { ... }
* Implement the init and update functions, whatever instrument manager requires.
* In init set up proper texture sizes and call allocRT().
* NOTE: od_gauge does NOT add camera to render the scenery, but a custom model. One needs to modify od_gauge so that allocRT calls globals()->get_renderer()->addCamera(camera.get(), '''true''');
* In update function update camera position according to viewer->getAbsolutePosition(...).
* Add the custom instrument to instrument_mgr.cxx.
=Creating the CamView as a hard coded instrument=
==Header File: cam_display.hxx==
The header file is used to simply initialize a new class that contains all functions related to this instrument. The Functions are just constructed here, the contents are put in the ''cam_display.cxx'' file.
<syntaxhighlight lang="cpp">
// cam_display.hxx - simple function to render view to texture
#ifndef __INSTRUMENTATION_CAMVIEW_HXX
#define __INSTRUMENTATION_CAMVIEW_HXX
#include <simgear/props/props.hxx>
#include "od_gauge.hxx"
class CamView : public SGPropertyChangeListener, public FGODGauge
{
public:
static const int TextureHalfSize = 256;
CamView(SGPropertyNode* node);
virtual ~CamView();
void updateTexture();
virtual void valueChanged(SGPropertyNode*);
protected:
  void createTexture(const char* texture_name);
};
#endif // __INSTRUMENTATION_CAMVIEW_HXX
</syntaxhighlight>
=Adding the Instrument to 'instrument_mgr.cxx'=
Your Instrument's header file, ''cam_display.hxx'' is a constructor for the main instrument class, and the functions are further defined in the ''cam_display.cxx'' function. Include the ''cam_display.hxx'' function in the ''instrument_mgr.cxx'' file so it knows where your function is.
<syntaxhighlight lang="cpp">
#include "cam_display.hxx"
</syntaxhighlight>
In the ''FGInstrumentMgr::build'' function, add an '''else if''' statement so that it checks whether your instrument too is called.
<syntaxhighlight lang="cpp">
else if ( name == "cam_display" ) {
            set_subsystem( id, new CamView( node ),1);
        }
</syntaxhighlight>
=Part 2: Getting that texture into an instrument=
This part doesn't require any C++ coding, just a simple XML script to use that texture in an instrument.
<syntaxhighlight lang="xml">
<?xml version="1.0"?>
<PropertyList>
    <name>Camera Instrument</name>
    <w-base>512</w-base>
    <h-base>512</h-base>
    <layers>
<layer>
        <name>camera</name>
            <texture>
                <path>image_file.png</path> <!-- Path to the file we just rendered -->
                <x1>0</x1>
                <y1>0</y1>
                <x2>1</x2>
                <y2>1</y2>
            </texture>
<emissive>1</emissive>
        <w>512</w>
        <h>512</h>
</layer>
    </layers>
</PropertyList>
</syntaxhighlight>
''You could create more layers for more cameras/different views or even have a sprite or something on the view. Or we could also add transformations or conditions to the layer to give some function to the instrument other than just show a camera view.''
<syntaxhighlight lang="xml">
<transformation>
    <type>y-shift</type>
    <property>/instrumentation/camera/y_shift</property>
    <scale>2</scale>
    </transformation>
</syntaxhighlight>
</syntaxhighlight>


Navigation menu