obs-convenience.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /******************************************************************************
  2. Copyright (C) 2014 by Nibbles
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <obs-module.h>
  15. #include <graphics/vec2.h>
  16. #include <graphics/vec3.h>
  17. #include <graphics/vec4.h>
  18. #include "obs-convenience.h"
  19. gs_vertbuffer_t *create_uv_vbuffer(uint32_t num_verts, bool add_color)
  20. {
  21. obs_enter_graphics();
  22. gs_vertbuffer_t *tmp = NULL;
  23. struct gs_vb_data *vrect = NULL;
  24. vrect = gs_vbdata_create();
  25. vrect->num = num_verts;
  26. vrect->points = (struct vec3 *)bmalloc(sizeof(struct vec3) * num_verts);
  27. vrect->num_tex = 1;
  28. vrect->tvarray =
  29. (struct gs_tvertarray *)bmalloc(sizeof(struct gs_tvertarray));
  30. vrect->tvarray[0].width = 2;
  31. vrect->tvarray[0].array = bmalloc(sizeof(struct vec2) * num_verts);
  32. if (add_color)
  33. vrect->colors =
  34. (uint32_t *)bmalloc(sizeof(uint32_t) * num_verts);
  35. memset(vrect->points, 0, sizeof(struct vec3) * num_verts);
  36. memset(vrect->tvarray[0].array, 0, sizeof(struct vec2) * num_verts);
  37. if (add_color)
  38. memset(vrect->colors, 0, sizeof(uint32_t) * num_verts);
  39. tmp = gs_vertexbuffer_create(vrect, GS_DYNAMIC);
  40. if (tmp == NULL) {
  41. blog(LOG_WARNING, "Couldn't create UV vertex buffer.");
  42. }
  43. obs_leave_graphics();
  44. return tmp;
  45. }
  46. void draw_uv_vbuffer(gs_vertbuffer_t *vbuf, gs_texture_t *tex,
  47. gs_effect_t *effect, uint32_t num_verts)
  48. {
  49. gs_texture_t *texture = tex;
  50. gs_technique_t *tech = gs_effect_get_technique(effect, "Draw");
  51. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  52. size_t passes;
  53. if (vbuf == NULL || tex == NULL)
  54. return;
  55. const bool linear_srgb = gs_get_linear_srgb();
  56. const bool previous = gs_framebuffer_srgb_enabled();
  57. gs_enable_framebuffer_srgb(linear_srgb);
  58. gs_vertexbuffer_flush(vbuf);
  59. gs_load_vertexbuffer(vbuf);
  60. gs_load_indexbuffer(NULL);
  61. passes = gs_technique_begin(tech);
  62. for (size_t i = 0; i < passes; i++) {
  63. if (gs_technique_begin_pass(tech, i)) {
  64. if (linear_srgb)
  65. gs_effect_set_texture_srgb(image, texture);
  66. else
  67. gs_effect_set_texture(image, texture);
  68. gs_draw(GS_TRIS, 0, num_verts);
  69. gs_technique_end_pass(tech);
  70. }
  71. }
  72. gs_technique_end(tech);
  73. gs_enable_framebuffer_srgb(previous);
  74. }