Skip to content

Commit

Permalink
CameraServer class
Browse files Browse the repository at this point in the history
  • Loading branch information
BastiaanOlij committed Jan 22, 2018
1 parent 511742e commit 0eb5d3a
Show file tree
Hide file tree
Showing 45 changed files with 1,761 additions and 3 deletions.
3 changes: 3 additions & 0 deletions doc/classes/@GlobalScope.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
<member name="AudioServer" type="AudioServer" setter="" getter="">
[AudioServer] singleton
</member>
<member name="CameraServer" type="CameraServer" setter="" getter="">
[CameraServer] singleton
</member>
<member name="ClassDB" type="ClassDB" setter="" getter="">
[ClassDB] singleton
</member>
Expand Down
58 changes: 58 additions & 0 deletions doc/classes/CameraServer.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="CameraServer" inherits="Object" category="Core" version="3.0.alpha.custom_build">
<brief_description>
</brief_description>
<description>
</description>
<tutorials>
</tutorials>
<demos>
</demos>
<methods>
<method name="feed_is_active" qualifiers="const">
<return type="bool">
</return>
<argument index="0" name="id" type="int">
</argument>
<description>
Returns true if the feed with this id is active.
</description>
</method>
<method name="feed_set_active">
<return type="void">
</return>
<argument index="0" name="id" type="int">
</argument>
<argument index="1" name="active" type="bool">
</argument>
<description>
Activates or deactivates the feed with the given id.
</description>
</method>
<method name="feeds" qualifiers="const">
<return type="Array">
</return>
<description>
Returns an array of available feeds.
</description>
</method>
</methods>
<signals>
<signal name="camera_feed_added">
<argument index="0" name="id" type="int">
</argument>
<description>
Signal to alert a new feed is available
</description>
</signal>
<signal name="camera_feed_removed">
<argument index="0" name="id" type="int">
</argument>
<description>
Signal to alert a feed was removed (say USB camera was unplugged or another program took ownership of a camera)
</description>
</signal>
</signals>
<constants>
</constants>
</class>
14 changes: 13 additions & 1 deletion doc/classes/Environment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@
<member name="auto_exposure_speed" type="float" setter="set_tonemap_auto_exposure_speed" getter="get_tonemap_auto_exposure_speed">
Speed of the auto exposure effect. Affects the time needed for the camera to perform auto exposure.
</member>
<member name="background_camera_feed_h_flip" type="bool" setter="set_camera_feed_h_flip" getter="get_camera_feed_h_flip">
Horizontally flip the camera feed
</member>
<member name="background_camera_feed_id" type="int" setter="set_camera_feed_id" getter="get_camera_feed_id">
The id of the camera feed to show in the background
</member>
<member name="background_camera_feed_v_flip" type="bool" setter="set_camera_feed_v_flip" getter="get_camera_feed_v_flip">
Vertically flip the camera feed
</member>
<member name="background_canvas_max_layer" type="int" setter="set_canvas_max_layer" getter="get_canvas_max_layer">
Maximum layer id (if using Layer background mode).
</member>
Expand Down Expand Up @@ -253,7 +262,10 @@
<constant name="BG_CANVAS" value="4" enum="BGMode">
Display a [CanvasLayer] in the background.
</constant>
<constant name="BG_MAX" value="6" enum="BGMode">
<constant name="BG_CAMERA_FEED" value="6">
Display a camera feed in the background
</constant>
<constant name="BG_MAX" value="7">
Helper constant keeping track of the enum's size, has no direct usage in API calls.
</constant>
<constant name="GLOW_BLEND_MODE_ADDITIVE" value="0" enum="GlowBlendMode">
Expand Down
78 changes: 78 additions & 0 deletions drivers/gles3/rasterizer_scene_gles3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "os/os.h"
#include "project_settings.h"
#include "rasterizer_canvas_gles3.h"
#include "servers/camera_server.h"
#include "servers/visual/visual_server_raster.h"

#ifndef GLES_OVER_GL
Expand Down Expand Up @@ -841,6 +842,24 @@ void RasterizerSceneGLES3::environment_set_ambient_light(RID p_env, const Color
env->ambient_energy = p_energy;
env->ambient_sky_contribution = p_sky_contribution;
}
void RasterizerSceneGLES3::environment_set_camera_feed_id(RID p_env, int p_camera_feed_id) {
Environment *env = environment_owner.getornull(p_env);
ERR_FAIL_COND(!env);

env->camera_feed_id = p_camera_feed_id;
}
void RasterizerSceneGLES3::environment_set_camera_feed_h_flip(RID p_env, bool p_camera_feed_h_flip) {
Environment *env = environment_owner.getornull(p_env);
ERR_FAIL_COND(!env);

env->camera_feed_h_flip = p_camera_feed_h_flip;
}
void RasterizerSceneGLES3::environment_set_camera_feed_v_flip(RID p_env, bool p_camera_feed_v_flip) {
Environment *env = environment_owner.getornull(p_env);
ERR_FAIL_COND(!env);

env->camera_feed_v_flip = p_camera_feed_v_flip;
}

void RasterizerSceneGLES3::environment_set_dof_blur_far(RID p_env, bool p_enable, float p_distance, float p_transition, float p_amount, VS::EnvironmentDOFBlurQuality p_quality) {

Expand Down Expand Up @@ -4209,6 +4228,7 @@ void RasterizerSceneGLES3::render_scene(const Transform &p_cam_transform, const
Color clear_color(0, 0, 0, 0);

RasterizerStorageGLES3::Sky *sky = NULL;
CameraFeed *feed = NULL;
GLuint env_radiance_tex = 0;

if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_OVERDRAW) {
Expand Down Expand Up @@ -4246,6 +4266,9 @@ void RasterizerSceneGLES3::render_scene(const Transform &p_cam_transform, const
clear_color = env->bg_color.to_linear();
}

} else if (env->bg_mode == VS::ENV_BG_CAMERA_FEED) {
feed = CameraServer::get_singleton()->get_feed(env->camera_feed_id);
storage->frame.clear_request = false;
} else {
storage->frame.clear_request = false;
}
Expand Down Expand Up @@ -4282,6 +4305,61 @@ void RasterizerSceneGLES3::render_scene(const Transform &p_cam_transform, const
glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
} else if (env && env->bg_mode == VS::ENV_BG_CAMERA_FEED) {
if (feed == NULL) {
// don't have a feed, just show greenscreen :)
clear_color = Color(0.0, 1.0, 0.0, 1.0);
} else {
// copy our camera feed to our background

glDisable(GL_BLEND);
glDepthMask(GL_FALSE);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);

if (feed->get_datatype() == CameraFeed::FEED_RGB) {
RID camera_texture = feed->get_texture(0);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, VS::get_singleton()->texture_get_texid(camera_texture));

storage->shaders.copy.set_conditional(CopyShaderGLES3::DISABLE_ALPHA, true);
storage->shaders.copy.set_conditional(CopyShaderGLES3::SRGB_TO_LINEAR, true);

///@TODO implement h and v flip, we don't have any RGB based cameras yet so some day....

storage->shaders.copy.bind();

_copy_screen(true, true);

//turn off everything used
storage->shaders.copy.set_conditional(CopyShaderGLES3::SRGB_TO_LINEAR, false);
storage->shaders.copy.set_conditional(CopyShaderGLES3::DISABLE_ALPHA, false);
} else if (feed->get_datatype() == CameraFeed::FEED_YCbCr) {
RID camera_Y = feed->get_texture(0);
RID camera_CbCr = feed->get_texture(1);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, VS::get_singleton()->texture_get_texid(camera_Y));
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, VS::get_singleton()->texture_get_texid(camera_CbCr));

storage->shaders.ycbcr.set_conditional(YcbcrShaderGLES3::H_FLIP, env->camera_feed_h_flip);
storage->shaders.ycbcr.set_conditional(YcbcrShaderGLES3::V_FLIP, env->camera_feed_v_flip);
storage->shaders.ycbcr.bind();

_copy_screen(true, true);

storage->shaders.ycbcr.set_conditional(YcbcrShaderGLES3::H_FLIP, false);
storage->shaders.ycbcr.set_conditional(YcbcrShaderGLES3::V_FLIP, false);
};

//restore
glEnable(GL_BLEND);
glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
}
}

state.texscreen_copied = false;
Expand Down
10 changes: 10 additions & 0 deletions drivers/gles3/rasterizer_scene_gles3.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,10 @@ class RasterizerSceneGLES3 : public RasterizerScene {
float bg_energy;
float sky_ambient;

int camera_feed_id;
bool camera_feed_h_flip;
bool camera_feed_v_flip;

Color ambient_color;
float ambient_energy;
float ambient_sky_contribution;
Expand Down Expand Up @@ -450,6 +454,9 @@ class RasterizerSceneGLES3 : public RasterizerScene {
ambient_energy = 1.0;
ambient_sky_contribution = 0.0;
canvas_max_layer = 0;
camera_feed_id = 0;
camera_feed_h_flip = false;
camera_feed_v_flip = true;

ssr_enabled = false;
ssr_max_steps = 64;
Expand Down Expand Up @@ -536,6 +543,9 @@ class RasterizerSceneGLES3 : public RasterizerScene {
virtual void environment_set_bg_energy(RID p_env, float p_energy);
virtual void environment_set_canvas_max_layer(RID p_env, int p_max_layer);
virtual void environment_set_ambient_light(RID p_env, const Color &p_color, float p_energy = 1.0, float p_sky_contribution = 0.0);
virtual void environment_set_camera_feed_id(RID p_env, int p_camera_feed_id);
virtual void environment_set_camera_feed_h_flip(RID p_env, bool p_camera_feed_h_flip);
virtual void environment_set_camera_feed_v_flip(RID p_env, bool p_camera_feed_v_flip);

virtual void environment_set_dof_blur_near(RID p_env, bool p_enable, float p_distance, float p_transition, float p_amount, VS::EnvironmentDOFBlurQuality p_quality);
virtual void environment_set_dof_blur_far(RID p_env, bool p_enable, float p_distance, float p_transition, float p_amount, VS::EnvironmentDOFBlurQuality p_quality);
Expand Down
1 change: 1 addition & 0 deletions drivers/gles3/rasterizer_storage_gles3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7145,6 +7145,7 @@ void RasterizerStorageGLES3::initialize() {
frame.clear_request = false;

shaders.copy.init();
shaders.ycbcr.init();

{
//default textures
Expand Down
2 changes: 2 additions & 0 deletions drivers/gles3/rasterizer_storage_gles3.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "shaders/copy.glsl.gen.h"
#include "shaders/cubemap_filter.glsl.gen.h"
#include "shaders/particles.glsl.gen.h"
#include "shaders/ycbcr.glsl.gen.h"

class RasterizerCanvasGLES3;
class RasterizerSceneGLES3;
Expand Down Expand Up @@ -102,6 +103,7 @@ class RasterizerStorageGLES3 : public RasterizerStorage {
mutable struct Shaders {

CopyShaderGLES3 copy;
YcbcrShaderGLES3 ycbcr;

ShaderCompilerGLES3 compiler;

Expand Down
1 change: 1 addition & 0 deletions drivers/gles3/shaders/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ if 'GLES3_GLSL' in env['BUILDERS']:
env.GLES3_GLSL('exposure.glsl');
env.GLES3_GLSL('tonemap.glsl');
env.GLES3_GLSL('particles.glsl');
env.GLES3_GLSL('ycbcr.glsl');
59 changes: 59 additions & 0 deletions drivers/gles3/shaders/ycbcr.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[vertex]


layout(location=0) in highp vec4 vertex_attrib;
layout(location=4) in vec2 uv_in;

out vec2 uv_interp;

void main() {

uv_interp = uv_in;
#ifdef H_FLIP
uv_interp.x = 1.0 - uv_interp.x;
#endif
#ifdef V_FLIP
uv_interp.y = 1.0 - uv_interp.y;
#endif

gl_Position = vertex_attrib;

}

[fragment]

in vec2 uv_interp;

// seems without atleast one settable uniform we can't compile our shader because our enum won't be setup...
uniform float i_seem_to_need_one_of_these;

uniform sampler2D color_y; //texunit:0
uniform sampler2D color_cbcr; //texunit:1

layout(location = 0) out vec4 frag_color;

void main() {
vec3 yuv;
vec3 rgb;

yuv.x = texture(color_y, uv_interp).r;
yuv.yz = texture(color_cbcr, uv_interp).rg - vec2(0.5, 0.5);

// BT.601, which is the standard for SDTV is provided as a reference
/*
rgb = mat3(
vec3( 1.00000, 1.00000, 1.00000),
vec3( 0.00000,-0.34413, 1.77200),
vec3( 1.40200,-0.71414, 0.00000)
) * yuv;
*/

// Using BT.709 which is the standard for HDTV
rgb = mat3(
vec3( 1.00000, 1.00000, 1.00000),
vec3( 0.00000,-0.18732, 1.85560),
vec3( 1.57481,-0.46813, 0.00000)
) * yuv;

frag_color = vec4(rgb, 1.0);
}
6 changes: 6 additions & 0 deletions platform/android/os_android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ Error OS_Android::initialize(const VideoMode &p_desired, int p_video_driver, int
input = memnew(InputDefault);
input->set_fallback_mapping("Default Android Gamepad");

///@TODO implement a subclass for Android and instantiate that instead
camera_server = memnew(CameraServer);

//power_manager = memnew(power_android);

return OK;
Expand All @@ -165,6 +168,9 @@ void OS_Android::delete_main_loop() {
}

void OS_Android::finalize() {

memdelete(camera_server);

memdelete(input);
}

Expand Down
3 changes: 3 additions & 0 deletions platform/android/os_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "os/main_loop.h"
//#include "power_android.h"
#include "servers/audio_server.h"
#include "servers/camera_server.h"
#include "servers/visual/rasterizer.h"

#ifdef ANDROID_NATIVE_ACTIVITY
Expand Down Expand Up @@ -105,6 +106,8 @@ class OS_Android : public OS_Unix {

VisualServer *visual_server;

CameraServer *camera_server;

mutable String data_dir_cache;

//AudioDriverAndroid audio_driver_android;
Expand Down
4 changes: 4 additions & 0 deletions platform/haiku/os_haiku.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ Error OS_Haiku::initialize(const VideoMode &p_desired, int p_video_driver, int p
window->Show();
visual_server->init();

camera_server = memnew(CameraServer);

AudioDriverManager::initialize(p_audio_driver);

power_manager = memnew(PowerHaiku);
Expand All @@ -148,6 +150,8 @@ void OS_Haiku::finalize() {
memdelete(visual_server);
memdelete(rasterizer);

memdelete(camera_server);

memdelete(input);

#if defined(OPENGL_ENABLED)
Expand Down
Loading

0 comments on commit 0eb5d3a

Please sign in to comment.