GL.cpp 11 KB

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