GL.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. #include <GL/glew.h>
  2. #include <type_traits>
  3. #include "utils/Array.h"
  4. #include "utils/Logger.h"
  5. #include "wrapper/GL.h"
  6. static_assert(std::is_same<GL::Shader, GLuint>::value,
  7. "shader has invalid type");
  8. static_assert(std::is_same<GL::Program, GLuint>::value, "p has invalid type");
  9. static_assert(std::is_same<char, GLchar>::value, "char has invalid type");
  10. static_assert(std::is_same<int, GLint>::value, "int has invalid type");
  11. static_assert(std::is_same<float, GLfloat>::value, "float has invalid type");
  12. static_assert(std::is_same<GL::ShaderType, GLenum>::value,
  13. "shader type has invalid type");
  14. static_assert(std::is_same<GL::Texture, GLuint>::value,
  15. "texture has invalid type");
  16. static_assert(std::is_same<GL::Framebuffer, GLuint>::value,
  17. "framebuffer has invalid type");
  18. static_assert(std::is_same<GL::ColorAttachment, GLenum>::value,
  19. "color attachment has invalid type");
  20. static_assert(std::is_same<GL::VertexArray, GLuint>::value,
  21. "vertex array has invalid type");
  22. static_assert(std::is_same<GL::Buffer, GLuint>::value,
  23. "buffer has invalid type");
  24. GL::ShaderType GL::VERTEX_SHADER = GL_VERTEX_SHADER;
  25. GL::ShaderType GL::FRAGMENT_SHADER = GL_FRAGMENT_SHADER;
  26. GL::ShaderType GL::GEOMETRY_SHADER = GL_GEOMETRY_SHADER;
  27. GL::Attribute::Attribute(int count, int size, int type, bool normalized)
  28. : count(count), size(size), type(type), normalized(normalized) {
  29. }
  30. GL::Attribute GL::Attribute::newFloat(int count) {
  31. return GL::Attribute(count, sizeof(float), GL_FLOAT, false);
  32. }
  33. GL::Attribute GL::Attribute::newColor(int count) {
  34. return GL::Attribute(count, sizeof(unsigned char), GL_UNSIGNED_BYTE, true);
  35. }
  36. GL::Attribute GL::Attribute::newDummy() {
  37. return GL::Attribute(0, 0, -1, false);
  38. }
  39. bool GL::Attribute::isDummy() const {
  40. return type == -1;
  41. }
  42. int GL::Attribute::getSize() const {
  43. return count * size;
  44. }
  45. GL::TextureFormat::TextureFormat(int internalformat, int format, int type)
  46. : internalformat(internalformat), format(format), type(type) {
  47. }
  48. GL::TextureFormat GL::TextureFormat::color8(int channels) {
  49. switch(channels) {
  50. case 1: return TextureFormat(GL_RED, GL_RED, GL_UNSIGNED_BYTE);
  51. case 2: return TextureFormat(GL_RG, GL_RG, GL_UNSIGNED_BYTE);
  52. case 3: return TextureFormat(GL_RGB, GL_RGB, GL_UNSIGNED_BYTE);
  53. case 4: return TextureFormat(GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE);
  54. }
  55. LOG_ERROR(StringBuffer<50>(channels).append(
  56. " is not a valid amount of channels"));
  57. return unknown();
  58. }
  59. GL::TextureFormat GL::TextureFormat::float16(int channels) {
  60. switch(channels) {
  61. case 1: return TextureFormat(GL_R16F, GL_RED, GL_FLOAT);
  62. case 2: return TextureFormat(GL_RG16F, GL_RG, GL_FLOAT);
  63. case 3: return TextureFormat(GL_RGB16F, GL_RGB, GL_FLOAT);
  64. case 4: return TextureFormat(GL_RGBA16F, GL_RGBA, GL_FLOAT);
  65. }
  66. LOG_ERROR(StringBuffer<50>(channels).append(
  67. " is not a valid amount of channels"));
  68. return unknown();
  69. }
  70. GL::TextureFormat GL::TextureFormat::float32(int channels) {
  71. switch(channels) {
  72. case 1: return TextureFormat(GL_R32F, GL_RED, GL_FLOAT);
  73. case 2: return TextureFormat(GL_RG32F, GL_RG, GL_FLOAT);
  74. case 3: return TextureFormat(GL_RGB32F, GL_RGB, GL_FLOAT);
  75. case 4: return TextureFormat(GL_RGBA32F, GL_RGBA, GL_FLOAT);
  76. }
  77. LOG_ERROR(StringBuffer<50>(channels).append(
  78. " is not a valid amount of channels"));
  79. return unknown();
  80. }
  81. GL::TextureFormat GL::TextureFormat::depth16() {
  82. return TextureFormat(GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_FLOAT);
  83. }
  84. GL::TextureFormat GL::TextureFormat::depth32() {
  85. return TextureFormat(GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT, GL_FLOAT);
  86. }
  87. GL::TextureFormat GL::TextureFormat::unknown() {
  88. return TextureFormat(-1, -1, -1);
  89. }
  90. bool GL::printError(const char* message) {
  91. GLenum error = glGetError();
  92. if(error == GL_NO_ERROR) {
  93. LOG_ERROR(StringBuffer<100>(message).append(": ").append(
  94. gluErrorString(error)));
  95. return true;
  96. }
  97. return false;
  98. }
  99. void GL::enableDepthTesting() {
  100. glEnable(GL_DEPTH_TEST);
  101. }
  102. void GL::disableDepthTesting() {
  103. glDisable(GL_DEPTH_TEST);
  104. }
  105. void GL::bindMainFramebuffer() {
  106. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  107. }
  108. void GL::clear() {
  109. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  110. }
  111. void GL::enableBlending() {
  112. glEnable(GL_BLEND);
  113. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  114. glBlendEquation(GL_FUNC_ADD);
  115. }
  116. void GL::disableBlending() {
  117. glDisable(GL_BLEND);
  118. }
  119. void GL::setViewport(int width, int height) {
  120. glViewport(0, 0, width, height);
  121. }
  122. void GL::vertexAttribPointer(int index, const Attribute& a, int stride,
  123. int offset) {
  124. glVertexAttribPointer(index, a.count, a.type, a.normalized, stride,
  125. static_cast<char*>(0) + offset);
  126. glEnableVertexAttribArray(index);
  127. }
  128. GL::Program GL::createProgram() {
  129. return glCreateProgram();
  130. }
  131. void GL::attachShader(Program p, Shader s) {
  132. glAttachShader(p, s);
  133. }
  134. void GL::linkProgram(Program p) {
  135. glLinkProgram(p);
  136. }
  137. bool GL::logLinkerError(Program p) {
  138. GLint linked;
  139. glGetProgramiv(p, GL_LINK_STATUS, &linked);
  140. if(!linked) {
  141. Array<char, 1024> log;
  142. glGetProgramInfoLog(p, log.getLength(), nullptr, log.begin());
  143. LOG_WARNING(StringBuffer<100>("linker log: ")
  144. .append(static_cast<const char*>(log.begin())));
  145. return true;
  146. }
  147. return false;
  148. }
  149. void GL::deleteShader(Shader s) {
  150. glDeleteShader(s);
  151. }
  152. void GL::deleteProgram(Program p) {
  153. glDeleteProgram(p);
  154. }
  155. GL::Shader GL::createShader(ShaderType type) {
  156. return glCreateShader(type);
  157. }
  158. void GL::compileShader(Shader s, const char* code) {
  159. glShaderSource(s, 1, &code, nullptr);
  160. glCompileShader(s);
  161. }
  162. bool GL::logCompileError(Shader s) {
  163. GLint compiled;
  164. glGetShaderiv(s, GL_COMPILE_STATUS, &compiled);
  165. if(!compiled) {
  166. Array<char, 1024> log;
  167. glGetShaderInfoLog(s, log.getLength(), nullptr, log.begin());
  168. LOG_WARNING(StringBuffer<100>("compiler log: ")
  169. .append(static_cast<const char*>(log.begin())));
  170. return true;
  171. }
  172. return false;
  173. }
  174. void GL::useProgram(Program p) {
  175. glUseProgram(p);
  176. }
  177. void GL::setMatrix(Program p, const char* name, const float* data) {
  178. glUniformMatrix4fv(glGetUniformLocation(p, name), 1, GL_TRUE, data);
  179. }
  180. void GL::setInt(Program p, const char* name, int data) {
  181. glUniform1i(glGetUniformLocation(p, name), data);
  182. }
  183. void GL::setFloat(Program p, const char* name, float data) {
  184. glUniform1f(glGetUniformLocation(p, name), data);
  185. }
  186. void GL::set2Float(Program p, const char* name, const float* data) {
  187. glUniform2fv(glGetUniformLocation(p, name), 1, data);
  188. }
  189. void GL::set3Float(Program p, const char* name, const float* data) {
  190. glUniform3fv(glGetUniformLocation(p, name), 1, data);
  191. }
  192. void GL::set4Float(Program p, const char* name, const float* data) {
  193. glUniform4fv(glGetUniformLocation(p, name), 1, data);
  194. }
  195. void GL::texImage2D(const TextureFormat& tf, int width, int height,
  196. const void* data, int level) {
  197. glTexImage2D(GL_TEXTURE_2D, level, tf.internalformat, width, height, 0,
  198. tf.format, tf.type, data);
  199. }
  200. GL::Texture GL::genTexture() {
  201. Texture t;
  202. glGenTextures(1, &t);
  203. return t;
  204. }
  205. void GL::deleteTexture(Texture t) {
  206. glDeleteTextures(1, &t);
  207. }
  208. void GL::setNearFilter2D() {
  209. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  210. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  211. }
  212. void GL::setMipMapNearFilter2D() {
  213. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  214. GL_NEAREST_MIPMAP_LINEAR);
  215. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  216. }
  217. void GL::setLinearFilter2D() {
  218. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  219. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  220. }
  221. void GL::setMipMapLinearFilter2D() {
  222. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  223. GL_LINEAR_MIPMAP_LINEAR);
  224. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  225. }
  226. void GL::setClampWrap2D() {
  227. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  228. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  229. }
  230. void GL::setRepeatWrap2D() {
  231. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  232. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  233. }
  234. void GL::bindTexture2D(Texture t) {
  235. glBindTexture(GL_TEXTURE_2D, t);
  236. }
  237. void GL::activeTexture(int index) {
  238. glActiveTexture(GL_TEXTURE0 + index);
  239. }
  240. void GL::generateMipmap2D(int maxLevels) {
  241. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  242. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, maxLevels);
  243. glGenerateMipmap(GL_TEXTURE_2D);
  244. }
  245. void GL::deleteFramebuffers(Framebuffer fb) {
  246. glDeleteFramebuffers(1, &fb);
  247. }
  248. GL::Framebuffer GL::genFramebuffer() {
  249. Framebuffer fb;
  250. glGenFramebuffers(1, &fb);
  251. return fb;
  252. }
  253. void GL::bindFramebuffer(Framebuffer fb) {
  254. glBindFramebuffer(GL_FRAMEBUFFER, fb);
  255. }
  256. void GL::framebufferDepthTexture2D(Texture t) {
  257. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
  258. t, 0);
  259. }
  260. GL::ColorAttachment GL::framebufferColorTexture2D(Texture t, int index) {
  261. GLenum c = GL_COLOR_ATTACHMENT0 + index;
  262. glFramebufferTexture2D(GL_FRAMEBUFFER, c, GL_TEXTURE_2D, t, 0);
  263. return c;
  264. }
  265. void GL::drawBuffers(int length, ColorAttachment* c) {
  266. glDrawBuffers(length, c);
  267. }
  268. bool GL::printFramebufferError() {
  269. GLenum error = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  270. if(error != GL_FRAMEBUFFER_COMPLETE) {
  271. LOG_WARNING(StringBuffer<100>("framebuffer error: ").append(error));
  272. return true;
  273. }
  274. return false;
  275. }
  276. GL::VertexArray GL::genVertexArray() {
  277. VertexArray va;
  278. glGenVertexArrays(1, &va);
  279. return va;
  280. }
  281. GL::Buffer GL::genBuffer() {
  282. Buffer b;
  283. glGenBuffers(1, &b);
  284. return b;
  285. }
  286. void GL::deleteBuffer(Buffer b) {
  287. glDeleteBuffers(1, &b);
  288. }
  289. void GL::deleteVertexArray(VertexArray va) {
  290. glDeleteVertexArrays(1, &va);
  291. }
  292. void GL::bindVertexArray(VertexArray va) {
  293. glBindVertexArray(va);
  294. }
  295. void GL::bindBuffer(Buffer b) {
  296. glBindBuffer(GL_ARRAY_BUFFER, b);
  297. }
  298. void GL::bufferDataStatic(int size, const void* data) {
  299. glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
  300. }
  301. void GL::bufferDataStream(int size, const void* data) {
  302. glBufferData(GL_ARRAY_BUFFER, size, data, GL_STREAM_DRAW);
  303. }
  304. void GL::bufferDataDynamic(int size, const void* data) {
  305. glBufferData(GL_ARRAY_BUFFER, size, data, GL_DYNAMIC_DRAW);
  306. }
  307. void GL::bufferSubData(int offset, int size, const void* data) {
  308. glBufferSubData(GL_ARRAY_BUFFER, offset, size, data);
  309. }
  310. void GL::drawTriangles(int offset, int vertices) {
  311. glDrawArrays(GL_TRIANGLES, offset, vertices);
  312. }
  313. void GL::drawTriangleStrip(int offset, int vertices) {
  314. glDrawArrays(GL_TRIANGLE_STRIP, offset, vertices);
  315. }
  316. void GL::drawPoints(int offset, int vertices) {
  317. glDrawArrays(GL_POINTS, offset, vertices);
  318. }