obs-convenience.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 = (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(sizeof(uint32_t) * num_verts);
  33. memset(vrect->points, 0, sizeof(struct vec3) * num_verts);
  34. memset(vrect->tvarray[0].array, 0, sizeof(struct vec2) * num_verts);
  35. if (add_color)
  36. memset(vrect->colors, 0, sizeof(uint32_t) * num_verts);
  37. tmp = gs_vertexbuffer_create(vrect, GS_DYNAMIC);
  38. if (tmp == NULL) {
  39. blog(LOG_WARNING, "Couldn't create UV vertex buffer.");
  40. }
  41. obs_leave_graphics();
  42. return tmp;
  43. }
  44. void draw_uv_vbuffer(gs_vertbuffer_t *vbuf, gs_texture_t *tex, gs_effect_t *effect, uint32_t num_verts, bool use_color)
  45. {
  46. gs_texture_t *texture = tex;
  47. gs_technique_t *tech = gs_effect_get_technique(effect, "Draw");
  48. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  49. size_t passes;
  50. if (vbuf == NULL || tex == NULL)
  51. return;
  52. const bool linear_srgb = gs_get_linear_srgb();
  53. const bool previous = gs_framebuffer_srgb_enabled();
  54. gs_enable_framebuffer_srgb(linear_srgb);
  55. gs_load_vertexbuffer(vbuf);
  56. gs_load_indexbuffer(NULL);
  57. passes = gs_technique_begin(tech);
  58. for (size_t i = 0; i < passes; i++) {
  59. if (gs_technique_begin_pass(tech, i)) {
  60. if (linear_srgb)
  61. gs_effect_set_texture_srgb(image, texture);
  62. else
  63. gs_effect_set_texture(image, texture);
  64. gs_effect_set_bool(gs_effect_get_param_by_name(effect, "use_color"), use_color);
  65. gs_draw(GS_TRIS, 0, num_verts);
  66. gs_technique_end_pass(tech);
  67. }
  68. }
  69. gs_technique_end(tech);
  70. gs_enable_framebuffer_srgb(previous);
  71. }