obs-convenience.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. obs_enter_graphics();
  21. gs_vertbuffer_t *tmp = NULL;
  22. struct gs_vb_data *vrect = NULL;
  23. vrect = gs_vbdata_create();
  24. vrect->num = num_verts;
  25. vrect->points = (struct vec3 *)bmalloc(sizeof(struct vec3) * num_verts);
  26. vrect->num_tex = 1;
  27. vrect->tvarray =
  28. (struct gs_tvertarray *)bmalloc(sizeof(struct gs_tvertarray));
  29. vrect->tvarray[0].width = 2;
  30. vrect->tvarray[0].array = bmalloc(sizeof(struct vec2) * num_verts);
  31. if (add_color)
  32. vrect->colors = (uint32_t *)bmalloc
  33. (sizeof(uint32_t)* num_verts);
  34. memset(vrect->points, 0, sizeof(struct vec3) * num_verts);
  35. memset(vrect->tvarray[0].array, 0, sizeof(struct vec2) * num_verts);
  36. if (add_color)
  37. memset(vrect->colors, 0, sizeof(uint32_t)* num_verts);
  38. tmp = gs_vertexbuffer_create(vrect, GS_DYNAMIC);
  39. if (tmp == NULL) {
  40. blog(LOG_WARNING, "Couldn't create UV vertex buffer.");
  41. }
  42. obs_leave_graphics();
  43. return tmp;
  44. }
  45. void draw_uv_vbuffer(gs_vertbuffer_t *vbuf, gs_texture_t *tex,
  46. gs_effect_t *effect, uint32_t num_verts)
  47. {
  48. gs_texture_t *texture = tex;
  49. gs_technique_t *tech = gs_effect_get_technique(effect, "Draw");
  50. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  51. size_t passes;
  52. if (vbuf == NULL || tex == NULL) return;
  53. gs_vertexbuffer_flush(vbuf);
  54. gs_load_vertexbuffer(vbuf);
  55. gs_load_indexbuffer(NULL);
  56. passes = gs_technique_begin(tech);
  57. for (size_t i = 0; i < passes; i++) {
  58. if (gs_technique_begin_pass(tech, i)) {
  59. gs_effect_set_texture(image, texture);
  60. gs_draw(GS_TRIS, 0, num_verts);
  61. gs_technique_end_pass(tech);
  62. }
  63. }
  64. gs_technique_end(tech);
  65. }