obs-convenience.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. gs_vertexbuffer_flush(vbuf);
  56. gs_load_vertexbuffer(vbuf);
  57. gs_load_indexbuffer(NULL);
  58. passes = gs_technique_begin(tech);
  59. for (size_t i = 0; i < passes; i++) {
  60. if (gs_technique_begin_pass(tech, i)) {
  61. gs_effect_set_texture(image, texture);
  62. gs_draw(GS_TRIS, 0, num_verts);
  63. gs_technique_end_pass(tech);
  64. }
  65. }
  66. gs_technique_end(tech);
  67. }