提交 cdf36cf8 编写于 作者: J jp9000

Add helper functions for drawing sources

If you look at the previous commits, you'll see I had added
obs_source_draw before.  For custom drawn sources in particular, each
time obs_source_draw was called, it would restart the effect and its
passes for each draw call, which was not optimal.  It should really use
the effect functions for that.  I'll have to add a function to simplify
effect usage.

I also realized that including the color matrix parameters in
obs_source_draw made the function kind of messy to use; instead,
separating the color matrix stuff out to
obs_source_draw_set_color_matrix feels a lot more clean.

On top of that, having the ability to set the position would be nice to
have as well, rather than having to mess with the matrix stuff each
time, so I also added that for the sake of convenience.

obs_source_draw will draw a texture sprite, optionally of a specific
size and/or at a specific position, as well as optionally inverted.  The
texture used will be set to the 'image' parameter of whatever effect is
currently active.

obs_source_draw_set_color_matrix will set the color matrix value if the
drawing requires color matrices.  It will set the 'color_matrix',
'color_range_min', and 'color_range_max' parameters of whatever effect
is currently active.

Overall, these feel much more clean to use than the previous iteration.
上级 ed2d9936
......@@ -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();
}
......@@ -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);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册