diff options
author | BastiaanOlij <mux213@gmail.com> | 2017-08-21 00:17:24 +1000 |
---|---|---|
committer | Bastiaan Olij <mux213@gmail.com> | 2019-06-15 21:30:32 +1000 |
commit | 02ea99129e8f3882914431374c60a7d80c5146e1 (patch) | |
tree | 6e41adaa70a2d5a3441ba6c1d455d9b63ea8c1de /servers | |
parent | 0a3c21d999559617cc9cdfe261d631e6d1267374 (diff) |
Adding a new Camera Server implementation to Godot.
This is a new singleton where camera sources such as webcams or cameras on a mobile phone can register themselves with the Server.
Other parts of Godot can interact with this to obtain images from the camera as textures.
This work includes additions to the Visual Server to use this functionality to present the camera image in the background. This is specifically targetted at AR applications.
Diffstat (limited to 'servers')
-rw-r--r-- | servers/SCsub | 1 | ||||
-rw-r--r-- | servers/arvr/arvr_interface.cpp | 7 | ||||
-rw-r--r-- | servers/arvr/arvr_interface.h | 1 | ||||
-rw-r--r-- | servers/camera/SCsub | 7 | ||||
-rw-r--r-- | servers/camera/camera_feed.cpp | 266 | ||||
-rw-r--r-- | servers/camera/camera_feed.h | 115 | ||||
-rw-r--r-- | servers/camera_server.cpp | 169 | ||||
-rw-r--r-- | servers/camera_server.h | 96 | ||||
-rw-r--r-- | servers/register_server_types.cpp | 6 | ||||
-rw-r--r-- | servers/visual/rasterizer.h | 2 | ||||
-rw-r--r-- | servers/visual/visual_server_raster.h | 2 | ||||
-rw-r--r-- | servers/visual/visual_server_wrap_mt.h | 2 | ||||
-rw-r--r-- | servers/visual_server.cpp | 1 | ||||
-rw-r--r-- | servers/visual_server.h | 3 |
14 files changed, 678 insertions, 0 deletions
diff --git a/servers/SCsub b/servers/SCsub index f4af347fe6..34ba70b8cb 100644 --- a/servers/SCsub +++ b/servers/SCsub @@ -6,6 +6,7 @@ env.servers_sources = [] env.add_source_files(env.servers_sources, "*.cpp") SConscript('arvr/SCsub') +SConscript('camera/SCsub') SConscript('physics/SCsub') SConscript('physics_2d/SCsub') SConscript('visual/SCsub') diff --git a/servers/arvr/arvr_interface.cpp b/servers/arvr/arvr_interface.cpp index 686ad0ba9b..e1b7611354 100644 --- a/servers/arvr/arvr_interface.cpp +++ b/servers/arvr/arvr_interface.cpp @@ -56,6 +56,7 @@ void ARVRInterface::_bind_methods() { // but we do have properties specific to AR.... ClassDB::bind_method(D_METHOD("get_anchor_detection_is_enabled"), &ARVRInterface::get_anchor_detection_is_enabled); ClassDB::bind_method(D_METHOD("set_anchor_detection_is_enabled", "enable"), &ARVRInterface::set_anchor_detection_is_enabled); + ClassDB::bind_method(D_METHOD("get_camera_feed_id"), &ARVRInterface::get_camera_feed_id); ADD_GROUP("AR", "ar_"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ar_is_anchor_detection_enabled"), "set_anchor_detection_is_enabled", "get_anchor_detection_is_enabled"); @@ -136,3 +137,9 @@ bool ARVRInterface::get_anchor_detection_is_enabled() const { void ARVRInterface::set_anchor_detection_is_enabled(bool p_enable){ // don't do anything here, this needs to be implemented on AR interface to enable/disable things like plane detection etc. }; + +int ARVRInterface::get_camera_feed_id() { + // don't do anything here, this needs to be implemented on AR interface to enable/disable things like plane detection etc. + + return 0; +}; diff --git a/servers/arvr/arvr_interface.h b/servers/arvr/arvr_interface.h index 9ea59a3961..ffafa4fcf5 100644 --- a/servers/arvr/arvr_interface.h +++ b/servers/arvr/arvr_interface.h @@ -101,6 +101,7 @@ public: /** specific to AR **/ virtual bool get_anchor_detection_is_enabled() const; virtual void set_anchor_detection_is_enabled(bool p_enable); + virtual int get_camera_feed_id(); /** rendering and internal **/ diff --git a/servers/camera/SCsub b/servers/camera/SCsub new file mode 100644 index 0000000000..ccc76e823f --- /dev/null +++ b/servers/camera/SCsub @@ -0,0 +1,7 @@ +#!/usr/bin/env python + +Import('env') + +env.add_source_files(env.servers_sources, "*.cpp") + +Export('env') diff --git a/servers/camera/camera_feed.cpp b/servers/camera/camera_feed.cpp new file mode 100644 index 0000000000..b96e9b749f --- /dev/null +++ b/servers/camera/camera_feed.cpp @@ -0,0 +1,266 @@ +/*************************************************************************/ +/* camera_feed.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "camera_feed.h" +#include "servers/visual_server.h" + +void CameraFeed::_bind_methods() { + // The setters prefixed with _ are only exposed so we can have feeds through GDNative! + // They should not be called by the end user. + + ClassDB::bind_method(D_METHOD("get_id"), &CameraFeed::get_id); + ClassDB::bind_method(D_METHOD("get_name"), &CameraFeed::get_name); + ClassDB::bind_method(D_METHOD("_set_name", "name"), &CameraFeed::set_name); + + ClassDB::bind_method(D_METHOD("is_active"), &CameraFeed::is_active); + ClassDB::bind_method(D_METHOD("set_active", "active"), &CameraFeed::set_active); + + ClassDB::bind_method(D_METHOD("get_position"), &CameraFeed::get_position); + ClassDB::bind_method(D_METHOD("_set_position", "position"), &CameraFeed::set_position); + + // Note, for transform some feeds may override what the user sets (such as ARKit) + ClassDB::bind_method(D_METHOD("get_transform"), &CameraFeed::get_transform); + ClassDB::bind_method(D_METHOD("set_transform", "transform"), &CameraFeed::set_transform); + + ClassDB::bind_method(D_METHOD("_set_RGB_img", "rgb_img"), &CameraFeed::set_RGB_img); + ClassDB::bind_method(D_METHOD("_set_YCbCr_img", "ycbcr_img"), &CameraFeed::set_YCbCr_img); + ClassDB::bind_method(D_METHOD("_set_YCbCr_imgs", "y_img", "cbcr_img"), &CameraFeed::set_YCbCr_imgs); + ClassDB::bind_method(D_METHOD("_allocate_texture", "width", "height", "format", "texture_type", "data_type"), &CameraFeed::allocate_texture); + + ADD_GROUP("Feed", "feed_"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "feed_is_active"), "set_active", "is_active"); + ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "feed_transform"), "set_transform", "get_transform"); + + BIND_ENUM_CONSTANT(FEED_NOIMAGE); + BIND_ENUM_CONSTANT(FEED_RGB); + BIND_ENUM_CONSTANT(FEED_YCbCr); + BIND_ENUM_CONSTANT(FEED_YCbCr_Sep); + + BIND_ENUM_CONSTANT(FEED_UNSPECIFIED); + BIND_ENUM_CONSTANT(FEED_FRONT); + BIND_ENUM_CONSTANT(FEED_BACK); +} + +int CameraFeed::get_id() const { + return id; +} + +bool CameraFeed::is_active() const { + return active; +} + +void CameraFeed::set_active(bool p_is_active) { + if (p_is_active == active) { + // all good + } else if (p_is_active) { + // attempt to activate this feed + if (activate_feed()) { + print_line("Activate " + name); + active = true; + } + } else { + // just deactivate it + deactivate_feed(); + print_line("Deactivate " + name); + active = false; + } +} + +String CameraFeed::get_name() const { + return name; +} + +void CameraFeed::set_name(String p_name) { + name = p_name; +} + +int CameraFeed::get_base_width() const { + return base_width; +} + +int CameraFeed::get_base_height() const { + return base_height; +} + +CameraFeed::FeedDataType CameraFeed::get_datatype() const { + return datatype; +} + +CameraFeed::FeedPosition CameraFeed::get_position() const { + return position; +} + +void CameraFeed::set_position(CameraFeed::FeedPosition p_position) { + position = p_position; +} + +Transform2D CameraFeed::get_transform() const { + return transform; +} + +void CameraFeed::set_transform(const Transform2D &p_transform) { + transform = p_transform; +} + +RID CameraFeed::get_texture(CameraServer::FeedImage p_which) { + return texture[p_which]; +} + +CameraFeed::CameraFeed() { + // initialize our feed + id = CameraServer::get_singleton()->get_free_id(); + name = "???"; + active = false; + datatype = CameraFeed::FEED_RGB; + position = CameraFeed::FEED_UNSPECIFIED; + transform = Transform2D(1.0, 0.0, 0.0, -1.0, 0.0, 1.0); + + // create a texture object + VisualServer *vs = VisualServer::get_singleton(); + texture[CameraServer::FEED_Y_IMAGE] = vs->texture_create(); // also used for RGBA + texture[CameraServer::FEED_CbCr_IMAGE] = vs->texture_create(); +} + +CameraFeed::CameraFeed(String p_name, FeedPosition p_position) { + // initialize our feed + id = CameraServer::get_singleton()->get_free_id(); + base_width = 0; + base_height = 0; + name = p_name; + active = false; + datatype = CameraFeed::FEED_NOIMAGE; + position = p_position; + transform = Transform2D(1.0, 0.0, 0.0, -1.0, 0.0, 1.0); + + // create a texture object + VisualServer *vs = VisualServer::get_singleton(); + texture[CameraServer::FEED_Y_IMAGE] = vs->texture_create(); // also used for RGBA + texture[CameraServer::FEED_CbCr_IMAGE] = vs->texture_create(); +} + +CameraFeed::~CameraFeed() { + // Free our textures + VisualServer *vs = VisualServer::get_singleton(); + vs->free(texture[CameraServer::FEED_Y_IMAGE]); + vs->free(texture[CameraServer::FEED_CbCr_IMAGE]); +} + +void CameraFeed::set_RGB_img(Ref<Image> p_rgb_img) { + if (active) { + VisualServer *vs = VisualServer::get_singleton(); + + int new_width = p_rgb_img->get_width(); + int new_height = p_rgb_img->get_height(); + + if ((base_width != new_width) || (base_height != new_height)) { + // We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot... + base_width = new_width; + base_height = new_height; + + vs->texture_allocate(texture[CameraServer::FEED_RGBA_IMAGE], new_width, new_height, 0, Image::FORMAT_RGB8, VS::TEXTURE_TYPE_2D, VS::TEXTURE_FLAGS_DEFAULT); + } + + vs->texture_set_data(texture[CameraServer::FEED_RGBA_IMAGE], p_rgb_img); + datatype = CameraFeed::FEED_RGB; + } +} + +void CameraFeed::set_YCbCr_img(Ref<Image> p_ycbcr_img) { + if (active) { + VisualServer *vs = VisualServer::get_singleton(); + + int new_width = p_ycbcr_img->get_width(); + int new_height = p_ycbcr_img->get_height(); + + if ((base_width != new_width) || (base_height != new_height)) { + // We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot... + base_width = new_width; + base_height = new_height; + + vs->texture_allocate(texture[CameraServer::FEED_RGBA_IMAGE], new_width, new_height, 0, Image::FORMAT_RGB8, VS::TEXTURE_TYPE_2D, VS::TEXTURE_FLAGS_DEFAULT); + } + + vs->texture_set_data(texture[CameraServer::FEED_RGBA_IMAGE], p_ycbcr_img); + datatype = CameraFeed::FEED_YCbCr; + } +} + +void CameraFeed::set_YCbCr_imgs(Ref<Image> p_y_img, Ref<Image> p_cbcr_img) { + if (active) { + VisualServer *vs = VisualServer::get_singleton(); + + ///@TODO investigate whether we can use thirdparty/misc/yuv2rgb.h here to convert our YUV data to RGB, our shader approach is potentially faster though.. + // Wondering about including that into multiple projects, may cause issues. + // That said, if we convert to RGB, we could enable using texture resources again... + + int new_y_width = p_y_img->get_width(); + int new_y_height = p_y_img->get_height(); + int new_cbcr_width = p_cbcr_img->get_width(); + int new_cbcr_height = p_cbcr_img->get_height(); + + if ((base_width != new_y_width) || (base_height != new_y_height)) { + // We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot... + base_width = new_y_width; + base_height = new_y_height; + + vs->texture_allocate(texture[CameraServer::FEED_Y_IMAGE], new_y_width, new_y_height, 0, Image::FORMAT_R8, VS::TEXTURE_TYPE_2D, VS::TEXTURE_FLAG_USED_FOR_STREAMING); + + ///@TODO GLES2 doesn't support FORMAT_RG8, need to do some form of conversion + vs->texture_allocate(texture[CameraServer::FEED_CbCr_IMAGE], new_cbcr_width, new_cbcr_height, 0, Image::FORMAT_RG8, VS::TEXTURE_TYPE_2D, VS::TEXTURE_FLAG_USED_FOR_STREAMING); + } + + vs->texture_set_data(texture[CameraServer::FEED_Y_IMAGE], p_y_img); + vs->texture_set_data(texture[CameraServer::FEED_CbCr_IMAGE], p_cbcr_img); + datatype = CameraFeed::FEED_YCbCr_Sep; + } +} + +void CameraFeed::allocate_texture(int p_width, int p_height, Image::Format p_format, VisualServer::TextureType p_texture_type, FeedDataType p_data_type) { + VisualServer *vs = VisualServer::get_singleton(); + + if ((base_width != p_width) || (base_height != p_height)) { + // We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot... + base_width = p_width; + base_height = p_height; + + vs->texture_allocate(texture[0], p_width, p_height, 0, p_format, p_texture_type, VS::TEXTURE_FLAGS_DEFAULT); + } + + datatype = p_data_type; +} + +bool CameraFeed::activate_feed() { + // nothing to do here + return true; +} + +void CameraFeed::deactivate_feed() { + // nothing to do here +} diff --git a/servers/camera/camera_feed.h b/servers/camera/camera_feed.h new file mode 100644 index 0000000000..aa8a78b2f6 --- /dev/null +++ b/servers/camera/camera_feed.h @@ -0,0 +1,115 @@ +/*************************************************************************/ +/* camera_feed.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef CAMERA_FEED_H +#define CAMERA_FEED_H + +#include "core/image.h" +#include "core/math/transform_2d.h" +#include "servers/camera_server.h" +#include "servers/visual_server.h" + +/** + @author Bastiaan Olij <mux213@gmail.com> + + The camera server is a singleton object that gives access to the various + camera feeds that can be used as the background for our environment. +**/ + +class CameraFeed : public Reference { + GDCLASS(CameraFeed, Reference); + +public: + enum FeedDataType { + FEED_NOIMAGE, // we don't have an image yet + FEED_RGB, // our texture will contain a normal RGB texture that can be used directly + FEED_YCbCr, // our texture will contain a YCbCr texture that needs to be converted to RGB before output + FEED_YCbCr_Sep // our camera is split into two textures, first plane contains Y data, second plane contains CbCr data + }; + + enum FeedPosition { + FEED_UNSPECIFIED, // we have no idea + FEED_FRONT, // this is a camera on the front of the device + FEED_BACK // this is a camera on the back of the device + }; + +private: + int id; // unique id for this, for internal use in case feeds are removed + int base_width; + int base_height; + +protected: + String name; // name of our camera feed + FeedDataType datatype; // type of texture data stored + FeedPosition position; // position of camera on the device + Transform2D transform; // display transform + + bool active; // only when active do we actually update the camera texture each frame + RID texture[CameraServer::FEED_IMAGES]; // texture images needed for this + + static void _bind_methods(); + +public: + int get_id() const; + bool is_active() const; + void set_active(bool p_is_active); + + String get_name() const; + void set_name(String p_name); + + int get_base_width() const; + int get_base_height() const; + + FeedPosition get_position() const; + void set_position(FeedPosition p_position); + + Transform2D get_transform() const; + void set_transform(const Transform2D &p_transform); + + RID get_texture(CameraServer::FeedImage p_which); + + CameraFeed(); + CameraFeed(String p_name, FeedPosition p_position = CameraFeed::FEED_UNSPECIFIED); + virtual ~CameraFeed(); + + FeedDataType get_datatype() const; + void set_RGB_img(Ref<Image> p_rgb_img); + void set_YCbCr_img(Ref<Image> p_ycbcr_img); + void set_YCbCr_imgs(Ref<Image> p_y_img, Ref<Image> p_cbcr_img); + void allocate_texture(int p_width, int p_height, Image::Format p_format, VisualServer::TextureType p_texture_type, FeedDataType p_data_type); + + virtual bool activate_feed(); + virtual void deactivate_feed(); +}; + +VARIANT_ENUM_CAST(CameraFeed::FeedDataType); +VARIANT_ENUM_CAST(CameraFeed::FeedPosition); + +#endif /* !CAMERA_FEED_H */ diff --git a/servers/camera_server.cpp b/servers/camera_server.cpp new file mode 100644 index 0000000000..f52d3decae --- /dev/null +++ b/servers/camera_server.cpp @@ -0,0 +1,169 @@ +/*************************************************************************/ +/* camera_server.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "camera_server.h" +#include "servers/camera/camera_feed.h" +#include "visual_server.h" + +//////////////////////////////////////////////////////// +// CameraServer + +void CameraServer::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_feed", "index"), &CameraServer::get_feed); + ClassDB::bind_method(D_METHOD("get_feed_count"), &CameraServer::get_feed_count); + ClassDB::bind_method(D_METHOD("feeds"), &CameraServer::get_feeds); + + ClassDB::bind_method(D_METHOD("add_feed", "feed"), &CameraServer::add_feed); + ClassDB::bind_method(D_METHOD("remove_feed", "feed"), &CameraServer::remove_feed); + + ADD_SIGNAL(MethodInfo("camera_feed_added", PropertyInfo(Variant::INT, "id"))); + ADD_SIGNAL(MethodInfo("camera_feed_removed", PropertyInfo(Variant::INT, "id"))); + + BIND_ENUM_CONSTANT(FEED_RGBA_IMAGE); + BIND_ENUM_CONSTANT(FEED_YCbCr_IMAGE); + BIND_ENUM_CONSTANT(FEED_Y_IMAGE); + BIND_ENUM_CONSTANT(FEED_CbCr_IMAGE); +}; + +CameraServer *CameraServer::singleton = NULL; + +CameraServer *CameraServer::get_singleton() { + return singleton; +}; + +int CameraServer::get_free_id() { + bool id_exists = true; + int newid = 0; + + // find a free id + while (id_exists) { + newid++; + id_exists = false; + for (int i = 0; i < feeds.size() && !id_exists; i++) { + if (feeds[i]->get_id() == newid) { + id_exists = true; + }; + }; + }; + + return newid; +}; + +int CameraServer::get_feed_index(int p_id) { + for (int i = 0; i < feeds.size(); i++) { + if (feeds[i]->get_id() == p_id) { + return i; + }; + }; + + return -1; +}; + +Ref<CameraFeed> CameraServer::get_feed_by_id(int p_id) { + int index = get_feed_index(p_id); + + if (index == -1) { + return NULL; + } else { + return feeds[index]; + } +}; + +void CameraServer::add_feed(const Ref<CameraFeed> &p_feed) { + // add our feed + feeds.push_back(p_feed); + +// record for debugging +#ifdef DEBUG_ENABLED + print_line("Registered camera " + p_feed->get_name() + " with id " + itos(p_feed->get_id()) + " position " + itos(p_feed->get_position()) + " at index " + itos(feeds.size() - 1)); +#endif + + // let whomever is interested know + emit_signal("camera_feed_added", p_feed->get_id()); +}; + +void CameraServer::remove_feed(const Ref<CameraFeed> &p_feed) { + for (int i = 0; i < feeds.size(); i++) { + if (feeds[i] == p_feed) { + int feed_id = p_feed->get_id(); + +// record for debugging +#ifdef DEBUG_ENABLED + print_line("Removed camera " + p_feed->get_name() + " with id " + itos(feed_id) + " position " + itos(p_feed->get_position())); +#endif + + // remove it from our array, if this results in our feed being unreferenced it will be destroyed + feeds.remove(i); + + // let whomever is interested know + emit_signal("camera_feed_removed", feed_id); + return; + }; + }; +}; + +Ref<CameraFeed> CameraServer::get_feed(int p_index) { + ERR_FAIL_INDEX_V(p_index, feeds.size(), NULL); + + return feeds[p_index]; +}; + +int CameraServer::get_feed_count() { + return feeds.size(); +}; + +Array CameraServer::get_feeds() { + Array return_feeds; + int cc = get_feed_count(); + return_feeds.resize(cc); + + for (int i = 0; i < feeds.size(); i++) { + return_feeds[i] = get_feed(i); + }; + + return return_feeds; +}; + +RID CameraServer::feed_texture(int p_id, CameraServer::FeedImage p_texture) { + int index = get_feed_index(p_id); + ERR_FAIL_COND_V(index == -1, RID()); + + Ref<CameraFeed> feed = get_feed(index); + + return feed->get_texture(p_texture); +}; + +CameraServer::CameraServer() { + singleton = this; +}; + +CameraServer::~CameraServer() { + singleton = NULL; +}; diff --git a/servers/camera_server.h b/servers/camera_server.h new file mode 100644 index 0000000000..c7716a3cff --- /dev/null +++ b/servers/camera_server.h @@ -0,0 +1,96 @@ +/*************************************************************************/ +/* camera_server.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef CAMERA_SERVER_H +#define CAMERA_SERVER_H + +#include "core/object.h" +#include "core/os/thread_safe.h" +#include "core/reference.h" +#include "core/rid.h" +#include "core/variant.h" + +/** + @author Bastiaan Olij <mux213@gmail.com> + + The camera server is a singleton object that gives access to the various + camera feeds that can be used as the background for our environment. +**/ + +class CameraFeed; + +class CameraServer : public Object { + GDCLASS(CameraServer, Object); + _THREAD_SAFE_CLASS_ + +public: + enum FeedImage { + FEED_RGBA_IMAGE = 0, + FEED_YCbCr_IMAGE = 0, + FEED_Y_IMAGE = 0, + FEED_CbCr_IMAGE = 1, + FEED_IMAGES = 2 + }; + +private: +protected: + Vector<Ref<CameraFeed> > feeds; + + static CameraServer *singleton; + + static void _bind_methods(); + +public: + static CameraServer *get_singleton(); + + // Right now we identify our feed by it's ID when it's used in the background. + // May see if we can change this to purely relying on CameraFeed objects or by name. + int get_free_id(); + int get_feed_index(int p_id); + Ref<CameraFeed> get_feed_by_id(int p_id); + + // add and remove feeds + void add_feed(const Ref<CameraFeed> &p_feed); + void remove_feed(const Ref<CameraFeed> &p_feed); + + // get our feeds + Ref<CameraFeed> get_feed(int p_idx); + int get_feed_count(); + Array get_feeds(); + + RID feed_texture(int p_id, FeedImage p_texture); + + CameraServer(); + ~CameraServer(); +}; + +VARIANT_ENUM_CAST(CameraServer::FeedImage); + +#endif /* CAMERA_SERVER_H */ diff --git a/servers/register_server_types.cpp b/servers/register_server_types.cpp index f3394019f5..f7cec6a378 100644 --- a/servers/register_server_types.cpp +++ b/servers/register_server_types.cpp @@ -54,6 +54,8 @@ #include "audio/effects/audio_effect_stereo_enhance.h" #include "audio/effects/audio_stream_generator.h" #include "audio_server.h" +#include "camera/camera_feed.h" +#include "camera_server.h" #include "core/script_debugger_remote.h" #include "physics/physics_server_sw.h" #include "physics_2d/physics_2d_server_sw.h" @@ -114,6 +116,7 @@ void register_server_types() { ClassDB::register_virtual_class<PhysicsServer>(); ClassDB::register_virtual_class<Physics2DServer>(); ClassDB::register_class<ARVRServer>(); + ClassDB::register_class<CameraServer>(); shader_types = memnew(ShaderTypes); @@ -169,6 +172,8 @@ void register_server_types() { ClassDB::register_virtual_class<AudioEffectSpectrumAnalyzerInstance>(); } + ClassDB::register_class<CameraFeed>(); + ClassDB::register_virtual_class<Physics2DDirectBodyState>(); ClassDB::register_virtual_class<Physics2DDirectSpaceState>(); ClassDB::register_virtual_class<Physics2DShapeQueryResult>(); @@ -208,4 +213,5 @@ void register_server_singletons() { Engine::get_singleton()->add_singleton(Engine::Singleton("PhysicsServer", PhysicsServer::get_singleton())); Engine::get_singleton()->add_singleton(Engine::Singleton("Physics2DServer", Physics2DServer::get_singleton())); Engine::get_singleton()->add_singleton(Engine::Singleton("ARVRServer", ARVRServer::get_singleton())); + Engine::get_singleton()->add_singleton(Engine::Singleton("CameraServer", CameraServer::get_singleton())); } diff --git a/servers/visual/rasterizer.h b/servers/visual/rasterizer.h index 31888261ec..47929b1a77 100644 --- a/servers/visual/rasterizer.h +++ b/servers/visual/rasterizer.h @@ -60,6 +60,7 @@ public: virtual void environment_set_bg_energy(RID p_env, float p_energy) = 0; virtual void environment_set_canvas_max_layer(RID p_env, int p_max_layer) = 0; virtual void environment_set_ambient_light(RID p_env, const Color &p_color, float p_energy = 1.0, float p_sky_contribution = 0.0) = 0; + virtual void environment_set_camera_feed_id(RID p_env, int p_camera_feed_id) = 0; virtual void environment_set_dof_blur_near(RID p_env, bool p_enable, float p_distance, float p_transition, float p_far_amount, VS::EnvironmentDOFBlurQuality p_quality) = 0; virtual void environment_set_dof_blur_far(RID p_env, bool p_enable, float p_distance, float p_transition, float p_far_amount, VS::EnvironmentDOFBlurQuality p_quality) = 0; @@ -204,6 +205,7 @@ public: virtual uint32_t texture_get_height(RID p_texture) const = 0; virtual uint32_t texture_get_depth(RID p_texture) const = 0; virtual void texture_set_size_override(RID p_texture, int p_width, int p_height, int p_depth_3d) = 0; + virtual void texture_bind(RID p_texture, uint32_t p_texture_no) = 0; virtual void texture_set_path(RID p_texture, const String &p_path) = 0; virtual String texture_get_path(RID p_texture) const = 0; diff --git a/servers/visual/visual_server_raster.h b/servers/visual/visual_server_raster.h index 921d55556d..f6d26c07d4 100644 --- a/servers/visual/visual_server_raster.h +++ b/servers/visual/visual_server_raster.h @@ -159,6 +159,7 @@ public: BIND1RC(uint32_t, texture_get_height, RID) BIND1RC(uint32_t, texture_get_depth, RID) BIND4(texture_set_size_override, RID, int, int, int) + BIND2(texture_bind, RID, uint32_t) BIND3(texture_set_detect_3d_callback, RID, TextureDetectCallback, void *) BIND3(texture_set_detect_srgb_callback, RID, TextureDetectCallback, void *) @@ -503,6 +504,7 @@ public: BIND2(environment_set_bg_energy, RID, float) BIND2(environment_set_canvas_max_layer, RID, int) BIND4(environment_set_ambient_light, RID, const Color &, float, float) + BIND2(environment_set_camera_feed_id, RID, int) BIND7(environment_set_ssr, RID, bool, int, float, float, float, bool) BIND13(environment_set_ssao, RID, bool, float, float, float, float, float, float, float, const Color &, EnvironmentSSAOQuality, EnvironmentSSAOBlur, float) diff --git a/servers/visual/visual_server_wrap_mt.h b/servers/visual/visual_server_wrap_mt.h index cd24deb60c..3fdb4acd3c 100644 --- a/servers/visual/visual_server_wrap_mt.h +++ b/servers/visual/visual_server_wrap_mt.h @@ -95,6 +95,7 @@ public: FUNC1RC(uint32_t, texture_get_height, RID) FUNC1RC(uint32_t, texture_get_depth, RID) FUNC4(texture_set_size_override, RID, int, int, int) + FUNC2(texture_bind, RID, uint32_t) FUNC3(texture_set_detect_3d_callback, RID, TextureDetectCallback, void *) FUNC3(texture_set_detect_srgb_callback, RID, TextureDetectCallback, void *) @@ -430,6 +431,7 @@ public: FUNC2(environment_set_bg_energy, RID, float) FUNC2(environment_set_canvas_max_layer, RID, int) FUNC4(environment_set_ambient_light, RID, const Color &, float, float) + FUNC2(environment_set_camera_feed_id, RID, int) FUNC7(environment_set_ssr, RID, bool, int, float, float, float, bool) FUNC13(environment_set_ssao, RID, bool, float, float, float, float, float, float, float, const Color &, EnvironmentSSAOQuality, EnvironmentSSAOBlur, float) diff --git a/servers/visual_server.cpp b/servers/visual_server.cpp index 0fe00ad61a..a9ea983cc7 100644 --- a/servers/visual_server.cpp +++ b/servers/visual_server.cpp @@ -1676,6 +1676,7 @@ void VisualServer::_bind_methods() { ClassDB::bind_method(D_METHOD("texture_set_path", "texture", "path"), &VisualServer::texture_set_path); ClassDB::bind_method(D_METHOD("texture_get_path", "texture"), &VisualServer::texture_get_path); ClassDB::bind_method(D_METHOD("texture_set_shrink_all_x2_on_set_data", "shrink"), &VisualServer::texture_set_shrink_all_x2_on_set_data); + ClassDB::bind_method(D_METHOD("texture_bind", "texture", "number"), &VisualServer::texture_bind); ClassDB::bind_method(D_METHOD("texture_debug_usage"), &VisualServer::_texture_debug_usage_bind); ClassDB::bind_method(D_METHOD("textures_keep_original", "enable"), &VisualServer::textures_keep_original); diff --git a/servers/visual_server.h b/servers/visual_server.h index 01be996bfc..70222d09b1 100644 --- a/servers/visual_server.h +++ b/servers/visual_server.h @@ -140,6 +140,7 @@ public: virtual uint32_t texture_get_height(RID p_texture) const = 0; virtual uint32_t texture_get_depth(RID p_texture) const = 0; virtual void texture_set_size_override(RID p_texture, int p_width, int p_height, int p_depth_3d) = 0; + virtual void texture_bind(RID p_texture, uint32_t p_texture_no) = 0; virtual void texture_set_path(RID p_texture, const String &p_path) = 0; virtual String texture_get_path(RID p_texture) const = 0; @@ -707,6 +708,7 @@ public: ENV_BG_COLOR_SKY, ENV_BG_CANVAS, ENV_BG_KEEP, + ENV_BG_CAMERA_FEED, ENV_BG_MAX }; @@ -718,6 +720,7 @@ public: virtual void environment_set_bg_energy(RID p_env, float p_energy) = 0; virtual void environment_set_canvas_max_layer(RID p_env, int p_max_layer) = 0; virtual void environment_set_ambient_light(RID p_env, const Color &p_color, float p_energy = 1.0, float p_sky_contribution = 0.0) = 0; + virtual void environment_set_camera_feed_id(RID p_env, int p_camera_feed_id) = 0; //set default SSAO options //set default SSR options |