GL.cpp 12 KB

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