GL.cpp 11 KB

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