GL.cpp 12 KB

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