diff --git a/libobs/obs-source.c b/libobs/obs-source.c index a2c8fe5af..7230ef48e 100644 --- a/libobs/obs-source.c +++ b/libobs/obs-source.c @@ -2115,3 +2115,71 @@ uint32_t obs_source_get_flags(const obs_source_t *source) { return source ? source->flags : 0; } + +void obs_source_draw_set_color_matrix(const struct matrix4 *color_matrix, + const struct vec3 *color_range_min, + const struct vec3 *color_range_max) +{ + static const struct vec3 color_range_min_def = {0.0f, 0.0f, 0.0f}; + static const struct vec3 color_range_max_def = {1.0f, 1.0f, 1.0f}; + gs_effect_t *effect = gs_get_effect(); + gs_eparam_t *matrix; + gs_eparam_t *range_min; + gs_eparam_t *range_max; + + if (!effect) { + blog(LOG_WARNING, "obs_source_draw_set_color_matrix: NULL " + "effect"); + return; + } + + if (!color_matrix) { + blog(LOG_WARNING, "obs_source_draw_set_color_matrix: NULL " + "color_matrix"); + return; + } + + if (!color_range_min) + color_range_min = &color_range_min_def; + if (!color_range_max) + color_range_max = &color_range_max_def; + + matrix = gs_effect_get_param_by_name(effect, "color_matrix"); + range_min = gs_effect_get_param_by_name(effect, "color_range_min"); + range_max = gs_effect_get_param_by_name(effect, "color_range_max"); + + gs_effect_set_matrix4(matrix, color_matrix); + gs_effect_set_val(range_min, color_range_min, sizeof(float)*3); + gs_effect_set_val(range_max, color_range_max, sizeof(float)*3); +} + +void obs_source_draw(gs_texture_t *texture, int x, int y, uint32_t cx, + uint32_t cy, bool flip) +{ + gs_effect_t *effect = gs_get_effect(); + bool change_pos = (x != 0 || y != 0); + gs_eparam_t *image; + + if (!effect) { + blog(LOG_WARNING, "obs_source_draw: NULL effect"); + return; + } + + if (!texture) { + blog(LOG_WARNING, "obs_source_draw: NULL texture"); + return; + } + + image = gs_effect_get_param_by_name(effect, "image"); + gs_effect_set_texture(image, texture); + + if (change_pos) { + gs_matrix_push(); + gs_matrix_translate3f((float)x, (float)y, 0.0f); + } + + gs_draw_sprite(texture, flip ? GS_FLIP_V : 0, cx, cy); + + if (change_pos) + gs_matrix_pop(); +} diff --git a/libobs/obs.h b/libobs/obs.h index af4a90a5d..f5d88aa1d 100644 --- a/libobs/obs.h +++ b/libobs/obs.h @@ -742,6 +742,37 @@ EXPORT uint32_t obs_source_get_flags(const obs_source_t *source); /* ------------------------------------------------------------------------- */ /* Functions used by sources */ +/** + * Helper function to set the color matrix information when drawing the source. + * + * @param color_matrix The color matrix. Assigns to the 'color_matrix' + * effect variable. + * @param color_range_min The minimum color range. Assigns to the + * 'color_range_min' effect variable. If NULL, + * {0.0f, 0.0f, 0.0f} is used. + * @param color_range_max The maximum color range. Assigns to the + * 'color_range_max' effect variable. If NULL, + * {1.0f, 1.0f, 1.0f} is used. + */ +EXPORT void obs_source_draw_set_color_matrix( + const struct matrix4 *color_matrix, + const struct vec3 *color_range_min, + const struct vec3 *color_range_max); + +/** + * Helper function to draw sprites for a source (synchronous video). + * + * @param image The sprite texture to draw. Assigns to the 'image' variable + * of the current effect. + * @param x X position of the sprite. + * @param y Y position of the sprite. + * @param cx Width of the sprite. If 0, uses the texture width. + * @param cy Height of the sprite. If 0, uses the texture height. + * @param flip Specifies whether to flip the image vertically. + */ +EXPORT void obs_source_draw(gs_texture_t *image, int x, int y, + uint32_t cx, uint32_t cy, bool flip); + /** Outputs asynchronous video data */ EXPORT void obs_source_output_video(obs_source_t *source, const struct obs_source_frame *frame);