textures.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #ifdef _arch_dreamcast
  2. #include "GL/glkos.h"
  3. #include "GL/glu.h"
  4. #include "GL/glext.h"
  5. #include "kos.h"
  6. #else
  7. #include "GL/gl.h"
  8. #endif
  9. #include <stdio.h>
  10. #include "../../include/base.h"
  11. #define STB_IMAGE_IMPLEMENTATION
  12. #include "stb/stb_image.h"
  13. #include "../../include/textures.h"
  14. Texture LoadTexture(const char* filename) {
  15. Texture texture = {0};
  16. stbi_set_flip_vertically_on_load(1);
  17. unsigned char* data = stbi_load(filename, &texture.width, &texture.height, &texture.channels, 0);
  18. GLenum format;
  19. if (texture.channels == 4) {
  20. format = GL_RGBA;
  21. } else if (texture.channels == 3) {
  22. format = GL_RGB;
  23. } else {
  24. stbi_image_free(data);
  25. fprintf(stderr, "unsupported number of channels %d in texture\n", texture.channels);
  26. return texture;
  27. }
  28. glGenTextures(1, &texture.id);
  29. glBindTexture(GL_TEXTURE_2D, texture.id);
  30. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  31. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  32. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  33. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  34. glTexImage2D(GL_TEXTURE_2D, 0, format, texture.width, texture.height,
  35. 0, format, GL_UNSIGNED_BYTE, data);
  36. stbi_image_free(data);
  37. texture.list_id = glGenLists(1);
  38. glNewList(texture.list_id, GL_COMPILE);
  39. glEnable(GL_TEXTURE_2D);
  40. glBindTexture(GL_TEXTURE_2D, texture.id);
  41. if (texture.channels == 4) {
  42. glEnable(GL_BLEND);
  43. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  44. }
  45. glBegin(GL_QUADS);
  46. glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
  47. glTexCoord2f(1.0f, 0.0f); glVertex2f(1.0f, 0.0f);
  48. glTexCoord2f(1.0f, 1.0f); glVertex2f(1.0f, 1.0f);
  49. glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, 1.0f);
  50. glEnd();
  51. if (texture.channels == 4) {
  52. glDisable(GL_BLEND);
  53. }
  54. glDisable(GL_TEXTURE_2D);
  55. glEndList();
  56. return texture;
  57. }
  58. void DrawTexture(Texture texture, float x, float y, float scale, Color tint) {
  59. glPushMatrix();
  60. glTranslatef(x, y, 0.0f);
  61. glScalef(texture.width * scale, texture.height * scale, 1.0f);
  62. glTranslatef(-0.5f, -0.5f, 0.0f);
  63. glColor4f(tint.r / 255.0f, tint.g / 255.0f, tint.b / 255.0f, tint.a / 255.0f);
  64. glCallList(texture.list_id);
  65. glColor4f(1.0, 1.0, 1.0, 1.0);
  66. glPopMatrix();
  67. }
  68. // i do not recommend using this because of immediate mode.
  69. void DrawTexturePro(Texture texture, Rectangle source, Rectangle dest, Vector2 origin, float rotation, Color tint) {
  70. float texWidth = (float)texture.width;
  71. float texHeight = (float)texture.height;
  72. float texLeft = source.x / texWidth;
  73. float texRight = (source.x + source.width) / texWidth;
  74. float texTop = source.y / texHeight;
  75. float texBottom = (source.y + source.height) / texHeight;
  76. texTop = 1.0f - texTop; // for inverting
  77. texBottom = 1.0f - texBottom; // too
  78. glPushMatrix();
  79. glTranslatef(dest.x, dest.y, 0.0f);
  80. if (rotation != 0.0f) {
  81. glTranslatef(origin.x, origin.y, 0.0f);
  82. glRotatef(rotation, 0.0f, 0.0f, 1.0f);
  83. glTranslatef(-origin.x, -origin.y, 0.0f);
  84. }
  85. glScalef(dest.width, dest.height, 1.0f);
  86. glColor4f(tint.r / 255.0f, tint.g / 255.0f, tint.b / 255.0f, tint.a / 255.0f);
  87. glEnable(GL_TEXTURE_2D);
  88. glBindTexture(GL_TEXTURE_2D, texture.id);
  89. if (texture.channels == 4) {
  90. glEnable(GL_BLEND);
  91. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  92. }
  93. glBegin(GL_QUADS);
  94. glTexCoord2f(texLeft, texTop);
  95. glVertex2f(0.0f, 0.0f);
  96. glTexCoord2f(texRight, texTop);
  97. glVertex2f(1.0f, 0.0f);
  98. glTexCoord2f(texRight, texBottom);
  99. glVertex2f(1.0f, 1.0f);
  100. glTexCoord2f(texLeft, texBottom);
  101. glVertex2f(0.0f, 1.0f);
  102. glEnd();
  103. if (texture.channels == 4) {
  104. glDisable(GL_BLEND);
  105. }
  106. glDisable(GL_TEXTURE_2D);
  107. glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  108. glPopMatrix();
  109. }
  110. void UnloadTexture(Texture texture) {
  111. if (texture.id != 0) {
  112. glDeleteTextures(1, &texture.id);
  113. }
  114. if (texture.list_id != 0) {
  115. glDeleteLists(texture.list_id, 1);
  116. }
  117. }