GL.cpp 11 KB

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