rlgl.d 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  1. module rlgl;
  2. import raylib;
  3. /**********************************************************************************************
  4. *
  5. * rlgl v3.7 - raylib OpenGL abstraction layer
  6. *
  7. * rlgl is a wrapper for multiple OpenGL versions (1.1, 2.1, 3.3 Core, ES 2.0) to
  8. * pseudo-OpenGL 1.1 style functions (rlVertex, rlTranslate, rlRotate...).
  9. *
  10. * When chosing an OpenGL version greater than OpenGL 1.1, rlgl stores vertex data on internal
  11. * VBO buffers (and VAOs if available). It requires calling 3 functions:
  12. * rlglInit() - Initialize internal buffers and auxiliary resources
  13. * rlglClose() - De-initialize internal buffers data and other auxiliar resources
  14. *
  15. * CONFIGURATION:
  16. *
  17. * #define GRAPHICS_API_OPENGL_11
  18. * #define GRAPHICS_API_OPENGL_21
  19. * #define GRAPHICS_API_OPENGL_33
  20. * #define GRAPHICS_API_OPENGL_ES2
  21. * Use selected OpenGL graphics backend, should be supported by platform
  22. * Those preprocessor defines are only used on rlgl module, if OpenGL version is
  23. * required by any other module, use rlGetVersion() to check it
  24. *
  25. * #define RLGL_IMPLEMENTATION
  26. * Generates the implementation of the library into the included file.
  27. * If not defined, the library is in header only mode and can be included in other headers
  28. * or source files without problems. But only ONE file should hold the implementation.
  29. *
  30. * #define RLGL_STANDALONE
  31. * Use rlgl as standalone library (no raylib dependency)
  32. *
  33. * #define SUPPORT_GL_DETAILS_INFO
  34. * Show OpenGL extensions and capabilities detailed logs on init
  35. *
  36. * DEPENDENCIES:
  37. * raymath - 3D math functionality (Vector3, Matrix, Quaternion)
  38. * GLAD - OpenGL extensions loading (OpenGL 3.3 Core only)
  39. *
  40. *
  41. * LICENSE: zlib/libpng
  42. *
  43. * Copyright (c) 2014-2021 Ramon Santamaria (@raysan5)
  44. *
  45. * This software is provided "as-is", without any express or implied warranty. In no event
  46. * will the authors be held liable for any damages arising from the use of this software.
  47. *
  48. * Permission is granted to anyone to use this software for any purpose, including commercial
  49. * applications, and to alter it and redistribute it freely, subject to the following restrictions:
  50. *
  51. * 1. The origin of this software must not be misrepresented; you must not claim that you
  52. * wrote the original software. If you use this software in a product, an acknowledgment
  53. * in the product documentation would be appreciated but is not required.
  54. *
  55. * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
  56. * as being the original software.
  57. *
  58. * 3. This notice may not be removed or altered from any source distribution.
  59. *
  60. **********************************************************************************************/
  61. extern (C) @nogc nothrow:
  62. // We are building or using rlgl as a static library (or Linux shared library)
  63. // We are building raylib as a Win32 shared library (.dll)
  64. // We are using raylib as a Win32 shared library (.dll)
  65. // Support TRACELOG macros
  66. // Allow custom memory allocators
  67. // Required for: Shader, Texture2D
  68. // Required for: Vector3, Matrix
  69. // Security check in case no GRAPHICS_API_OPENGL_* defined
  70. // Security check in case multiple GRAPHICS_API_OPENGL_* defined
  71. // OpenGL 2.1 uses most of OpenGL 3.3 Core functionality
  72. // WARNING: Specific parts are checked with #if defines
  73. //----------------------------------------------------------------------------------
  74. // Defines and Macros
  75. //----------------------------------------------------------------------------------
  76. // Default internal render batch limits
  77. // This is the maximum amount of elements (quads) per batch
  78. // NOTE: Be careful with text, every letter maps to a quad
  79. enum DEFAULT_BATCH_BUFFER_ELEMENTS = 8192;
  80. // We reduce memory sizes for embedded systems (RPI and HTML5)
  81. // NOTE: On HTML5 (emscripten) this is allocated on heap,
  82. // by default it's only 16MB!...just take care...
  83. enum DEFAULT_BATCH_BUFFERS = 1; // Default number of batch buffers (multi-buffering)
  84. enum DEFAULT_BATCH_DRAWCALLS = 256; // Default number of batch draw calls (by state changes: mode, texture)
  85. enum MAX_BATCH_ACTIVE_TEXTURES = 4; // Maximum number of additional textures that can be activated on batch drawing (SetShaderValueTexture())
  86. // Internal Matrix stack
  87. enum MAX_MATRIX_STACK_SIZE = 32; // Maximum size of Matrix stack
  88. // Vertex buffers id limit
  89. enum MAX_MESH_VERTEX_BUFFERS = 7; // Maximum vertex buffers (VBO) per mesh
  90. // Shader and material limits
  91. enum MAX_SHADER_LOCATIONS = 32; // Maximum number of shader locations supported
  92. enum MAX_MATERIAL_MAPS = 12; // Maximum number of shader maps supported
  93. // Projection matrix culling
  94. enum RL_CULL_DISTANCE_NEAR = 0.01; // Default near cull distance
  95. enum RL_CULL_DISTANCE_FAR = 1000.0; // Default far cull distance
  96. // Texture parameters (equivalent to OpenGL defines)
  97. enum RL_TEXTURE_WRAP_S = 0x2802; // GL_TEXTURE_WRAP_S
  98. enum RL_TEXTURE_WRAP_T = 0x2803; // GL_TEXTURE_WRAP_T
  99. enum RL_TEXTURE_MAG_FILTER = 0x2800; // GL_TEXTURE_MAG_FILTER
  100. enum RL_TEXTURE_MIN_FILTER = 0x2801; // GL_TEXTURE_MIN_FILTER
  101. enum RL_TEXTURE_FILTER_NEAREST = 0x2600; // GL_NEAREST
  102. enum RL_TEXTURE_FILTER_LINEAR = 0x2601; // GL_LINEAR
  103. enum RL_TEXTURE_FILTER_MIP_NEAREST = 0x2700; // GL_NEAREST_MIPMAP_NEAREST
  104. enum RL_TEXTURE_FILTER_NEAREST_MIP_LINEAR = 0x2702; // GL_NEAREST_MIPMAP_LINEAR
  105. enum RL_TEXTURE_FILTER_LINEAR_MIP_NEAREST = 0x2701; // GL_LINEAR_MIPMAP_NEAREST
  106. enum RL_TEXTURE_FILTER_MIP_LINEAR = 0x2703; // GL_LINEAR_MIPMAP_LINEAR
  107. enum RL_TEXTURE_FILTER_ANISOTROPIC = 0x3000; // Anisotropic filter (custom identifier)
  108. enum RL_TEXTURE_WRAP_REPEAT = 0x2901; // GL_REPEAT
  109. enum RL_TEXTURE_WRAP_CLAMP = 0x812F; // GL_CLAMP_TO_EDGE
  110. enum RL_TEXTURE_WRAP_MIRROR_REPEAT = 0x8370; // GL_MIRRORED_REPEAT
  111. enum RL_TEXTURE_WRAP_MIRROR_CLAMP = 0x8742; // GL_MIRROR_CLAMP_EXT
  112. // Matrix modes (equivalent to OpenGL)
  113. enum RL_MODELVIEW = 0x1700; // GL_MODELVIEW
  114. enum RL_PROJECTION = 0x1701; // GL_PROJECTION
  115. enum RL_TEXTURE = 0x1702; // GL_TEXTURE
  116. // Primitive assembly draw modes
  117. enum RL_LINES = 0x0001; // GL_LINES
  118. enum RL_TRIANGLES = 0x0004; // GL_TRIANGLES
  119. enum RL_QUADS = 0x0007; // GL_QUADS
  120. // GL equivalent data types
  121. enum RL_UNSIGNED_BYTE = 0x1401; // GL_UNSIGNED_BYTE
  122. enum RL_FLOAT = 0x1406; // GL_FLOAT
  123. //----------------------------------------------------------------------------------
  124. // Types and Structures Definition
  125. //----------------------------------------------------------------------------------
  126. enum GlVersion
  127. {
  128. OPENGL_11 = 1,
  129. OPENGL_21 = 2,
  130. OPENGL_33 = 3,
  131. OPENGL_ES_20 = 4
  132. }
  133. enum FramebufferAttachType
  134. {
  135. RL_ATTACHMENT_COLOR_CHANNEL0 = 0,
  136. RL_ATTACHMENT_COLOR_CHANNEL1 = 1,
  137. RL_ATTACHMENT_COLOR_CHANNEL2 = 2,
  138. RL_ATTACHMENT_COLOR_CHANNEL3 = 3,
  139. RL_ATTACHMENT_COLOR_CHANNEL4 = 4,
  140. RL_ATTACHMENT_COLOR_CHANNEL5 = 5,
  141. RL_ATTACHMENT_COLOR_CHANNEL6 = 6,
  142. RL_ATTACHMENT_COLOR_CHANNEL7 = 7,
  143. RL_ATTACHMENT_DEPTH = 100,
  144. RL_ATTACHMENT_STENCIL = 200
  145. }
  146. enum FramebufferAttachTextureType
  147. {
  148. RL_ATTACHMENT_CUBEMAP_POSITIVE_X = 0,
  149. RL_ATTACHMENT_CUBEMAP_NEGATIVE_X = 1,
  150. RL_ATTACHMENT_CUBEMAP_POSITIVE_Y = 2,
  151. RL_ATTACHMENT_CUBEMAP_NEGATIVE_Y = 3,
  152. RL_ATTACHMENT_CUBEMAP_POSITIVE_Z = 4,
  153. RL_ATTACHMENT_CUBEMAP_NEGATIVE_Z = 5,
  154. RL_ATTACHMENT_TEXTURE2D = 100,
  155. RL_ATTACHMENT_RENDERBUFFER = 200
  156. }
  157. // Dynamic vertex buffers (position + texcoords + colors + indices arrays)
  158. struct VertexBuffer
  159. {
  160. int elementsCount; // Number of elements in the buffer (QUADS)
  161. int vCounter; // Vertex position counter to process (and draw) from full buffer
  162. int tcCounter; // Vertex texcoord counter to process (and draw) from full buffer
  163. int cCounter; // Vertex color counter to process (and draw) from full buffer
  164. float* vertices; // Vertex position (XYZ - 3 components per vertex) (shader-location = 0)
  165. float* texcoords; // Vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1)
  166. ubyte* colors; // Vertex colors (RGBA - 4 components per vertex) (shader-location = 3)
  167. uint* indices; // Vertex indices (in case vertex data comes indexed) (6 indices per quad)
  168. // Vertex indices (in case vertex data comes indexed) (6 indices per quad)
  169. uint vaoId; // OpenGL Vertex Array Object id
  170. uint[4] vboId; // OpenGL Vertex Buffer Objects id (4 types of vertex data)
  171. }
  172. // Draw call type
  173. // NOTE: Only texture changes register a new draw, other state-change-related elements are not
  174. // used at this moment (vaoId, shaderId, matrices), raylib just forces a batch draw call if any
  175. // of those state-change happens (this is done in core module)
  176. struct DrawCall
  177. {
  178. int mode; // Drawing mode: LINES, TRIANGLES, QUADS
  179. int vertexCount; // Number of vertex of the draw
  180. int vertexAlignment; // Number of vertex required for index alignment (LINES, TRIANGLES)
  181. //unsigned int vaoId; // Vertex array id to be used on the draw -> Using RLGL.currentBatch->vertexBuffer.vaoId
  182. //unsigned int shaderId; // Shader id to be used on the draw -> Using RLGL.currentShader.id
  183. uint textureId; // Texture id to be used on the draw -> Use to create new draw call if changes
  184. //Matrix projection; // Projection matrix for this draw -> Using RLGL.projection by default
  185. //Matrix modelview; // Modelview matrix for this draw -> Using RLGL.modelview by default
  186. }
  187. // RenderBatch type
  188. struct RenderBatch
  189. {
  190. int buffersCount; // Number of vertex buffers (multi-buffering support)
  191. int currentBuffer; // Current buffer tracking in case of multi-buffering
  192. VertexBuffer* vertexBuffer; // Dynamic buffer(s) for vertex data
  193. DrawCall* draws; // Draw calls array, depends on textureId
  194. int drawsCounter; // Draw calls counter
  195. float currentDepth; // Current depth value for next draw
  196. }
  197. // Shader attribute data types
  198. enum ShaderAttributeDataType
  199. {
  200. SHADER_ATTRIB_FLOAT = 0,
  201. SHADER_ATTRIB_VEC2 = 1,
  202. SHADER_ATTRIB_VEC3 = 2,
  203. SHADER_ATTRIB_VEC4 = 3
  204. }
  205. // Boolean type
  206. // Color type, RGBA (32bit)
  207. // Texture type
  208. // NOTE: Data stored in GPU memory
  209. // OpenGL texture id
  210. // Texture base width
  211. // Texture base height
  212. // Mipmap levels, 1 by default
  213. // Data format (PixelFormat)
  214. // Shader type (generic)
  215. // Shader program id
  216. // Shader locations array (MAX_SHADER_LOCATIONS)
  217. // TraceLog message types
  218. // Texture formats (support depends on OpenGL version)
  219. // 8 bit per pixel (no alpha)
  220. // 16 bpp
  221. // 24 bpp
  222. // 16 bpp (1 bit alpha)
  223. // 16 bpp (4 bit alpha)
  224. // 32 bpp
  225. // 32 bpp (1 channel - float)
  226. // 32*3 bpp (3 channels - float)
  227. // 32*4 bpp (4 channels - float)
  228. // 4 bpp (no alpha)
  229. // 4 bpp (1 bit alpha)
  230. // 8 bpp
  231. // 8 bpp
  232. // 4 bpp
  233. // 4 bpp
  234. // 8 bpp
  235. // 4 bpp
  236. // 4 bpp
  237. // 8 bpp
  238. // 2 bpp
  239. // Texture parameters: filter mode
  240. // NOTE 1: Filtering considers mipmaps if available in the texture
  241. // NOTE 2: Filter is accordingly set for minification and magnification
  242. // No filter, just pixel aproximation
  243. // Linear filtering
  244. // Trilinear filtering (linear with mipmaps)
  245. // Anisotropic filtering 4x
  246. // Anisotropic filtering 8x
  247. // Anisotropic filtering 16x
  248. // Texture parameters: wrap mode
  249. // Repeats texture in tiled mode
  250. // Clamps texture to edge pixel in tiled mode
  251. // Mirrors and repeats the texture in tiled mode
  252. // Mirrors and clamps to border the texture in tiled mode
  253. // Color blending modes (pre-defined)
  254. // Blend textures considering alpha (default)
  255. // Blend textures adding colors
  256. // Blend textures multiplying colors
  257. // Blend textures adding colors (alternative)
  258. // Blend textures subtracting colors (alternative)
  259. // Belnd textures using custom src/dst factors (use SetBlendModeCustom())
  260. // Shader location point type
  261. // SHADER_LOC_MAP_DIFFUSE
  262. // SHADER_LOC_MAP_SPECULAR
  263. // Shader uniform data types
  264. // Prevents name mangling of functions
  265. //------------------------------------------------------------------------------------
  266. // Functions Declaration - Matrix operations
  267. //------------------------------------------------------------------------------------
  268. void rlMatrixMode(int mode); // Choose the current matrix to be transformed
  269. void rlPushMatrix(); // Push the current matrix to stack
  270. void rlPopMatrix(); // Pop lattest inserted matrix from stack
  271. void rlLoadIdentity(); // Reset current matrix to identity matrix
  272. void rlTranslatef(float x, float y, float z); // Multiply the current matrix by a translation matrix
  273. void rlRotatef(float angleDeg, float x, float y, float z); // Multiply the current matrix by a rotation matrix
  274. void rlScalef(float x, float y, float z); // Multiply the current matrix by a scaling matrix
  275. void rlMultMatrixf(float* matf); // Multiply the current matrix by another matrix
  276. void rlFrustum(double left, double right, double bottom, double top, double znear, double zfar);
  277. void rlOrtho(double left, double right, double bottom, double top, double znear, double zfar);
  278. void rlViewport(int x, int y, int width, int height); // Set the viewport area
  279. //------------------------------------------------------------------------------------
  280. // Functions Declaration - Vertex level operations
  281. //------------------------------------------------------------------------------------
  282. void rlBegin(int mode); // Initialize drawing mode (how to organize vertex)
  283. void rlEnd(); // Finish vertex providing
  284. void rlVertex2i(int x, int y); // Define one vertex (position) - 2 int
  285. void rlVertex2f(float x, float y); // Define one vertex (position) - 2 float
  286. void rlVertex3f(float x, float y, float z); // Define one vertex (position) - 3 float
  287. void rlTexCoord2f(float x, float y); // Define one vertex (texture coordinate) - 2 float
  288. void rlNormal3f(float x, float y, float z); // Define one vertex (normal) - 3 float
  289. void rlColor4ub(ubyte r, ubyte g, ubyte b, ubyte a); // Define one vertex (color) - 4 byte
  290. void rlColor3f(float x, float y, float z); // Define one vertex (color) - 3 float
  291. void rlColor4f(float x, float y, float z, float w); // Define one vertex (color) - 4 float
  292. //------------------------------------------------------------------------------------
  293. // Functions Declaration - OpenGL style functions (common to 1.1, 3.3+, ES2)
  294. // NOTE: This functions are used to completely abstract raylib code from OpenGL layer,
  295. // some of them are direct wrappers over OpenGL calls, some others are custom
  296. //------------------------------------------------------------------------------------
  297. // Vertex buffers state
  298. bool rlEnableVertexArray(uint vaoId); // Enable vertex array (VAO, if supported)
  299. void rlDisableVertexArray(); // Disable vertex array (VAO, if supported)
  300. void rlEnableVertexBuffer(uint id); // Enable vertex buffer (VBO)
  301. void rlDisableVertexBuffer(); // Disable vertex buffer (VBO)
  302. void rlEnableVertexBufferElement(uint id); // Enable vertex buffer element (VBO element)
  303. void rlDisableVertexBufferElement(); // Disable vertex buffer element (VBO element)
  304. void rlEnableVertexAttribute(uint index); // Enable vertex attribute index
  305. void rlDisableVertexAttribute(uint index); // Disable vertex attribute index
  306. // Textures state
  307. void rlActiveTextureSlot(int slot); // Select and active a texture slot
  308. void rlEnableTexture(uint id); // Enable texture
  309. void rlDisableTexture(); // Disable texture
  310. void rlEnableTextureCubemap(uint id); // Enable texture cubemap
  311. void rlDisableTextureCubemap(); // Disable texture cubemap
  312. void rlTextureParameters(uint id, int param, int value); // Set texture parameters (filter, wrap)
  313. // Shader state
  314. void rlEnableShader(uint id); // Enable shader program
  315. void rlDisableShader(); // Disable shader program
  316. // Framebuffer state
  317. void rlEnableFramebuffer(uint id); // Enable render texture (fbo)
  318. void rlDisableFramebuffer(); // Disable render texture (fbo), return to default framebuffer
  319. // General render state
  320. void rlEnableDepthTest(); // Enable depth test
  321. void rlDisableDepthTest(); // Disable depth test
  322. void rlEnableDepthMask(); // Enable depth write
  323. void rlDisableDepthMask(); // Disable depth write
  324. void rlEnableBackfaceCulling(); // Enable backface culling
  325. void rlDisableBackfaceCulling(); // Disable backface culling
  326. void rlEnableScissorTest(); // Enable scissor test
  327. void rlDisableScissorTest(); // Disable scissor test
  328. void rlScissor(int x, int y, int width, int height); // Scissor test
  329. void rlEnableWireMode(); // Enable wire mode
  330. void rlDisableWireMode(); // Disable wire mode
  331. void rlSetLineWidth(float width); // Set the line drawing width
  332. float rlGetLineWidth(); // Get the line drawing width
  333. void rlEnableSmoothLines(); // Enable line aliasing
  334. void rlDisableSmoothLines(); // Disable line aliasing
  335. void rlEnableStereoRender(); // Enable stereo rendering
  336. void rlDisableStereoRender(); // Disable stereo rendering
  337. bool rlIsStereoRenderEnabled(); // Check if stereo render is enabled
  338. void rlClearColor(ubyte r, ubyte g, ubyte b, ubyte a); // Clear color buffer with color
  339. void rlClearScreenBuffers(); // Clear used screen buffers (color and depth)
  340. void rlCheckErrors(); // Check and log OpenGL error codes
  341. void rlSetBlendMode(int mode); // Set blending mode
  342. void rlSetBlendFactors(int glSrcFactor, int glDstFactor, int glEquation); // Set blending mode factor and equation (using OpenGL factors)
  343. //------------------------------------------------------------------------------------
  344. // Functions Declaration - rlgl functionality
  345. //------------------------------------------------------------------------------------
  346. // rlgl initialization functions
  347. void rlglInit(int width, int height); // Initialize rlgl (buffers, shaders, textures, states)
  348. void rlglClose(); // De-inititialize rlgl (buffers, shaders, textures)
  349. void rlLoadExtensions(void* loader); // Load OpenGL extensions (loader function pointer required)
  350. int rlGetVersion(); // Returns current OpenGL version
  351. int rlGetFramebufferWidth(); // Get default framebuffer width
  352. int rlGetFramebufferHeight(); // Get default framebuffer height
  353. Shader rlGetShaderDefault(); // Get default shader
  354. Texture2D rlGetTextureDefault(); // Get default texture
  355. // Render batch management
  356. // NOTE: rlgl provides a default render batch to behave like OpenGL 1.1 immediate mode
  357. // but this render batch API is exposed in case of custom batches are required
  358. RenderBatch rlLoadRenderBatch(int numBuffers, int bufferElements); // Load a render batch system
  359. void rlUnloadRenderBatch(RenderBatch batch); // Unload render batch system
  360. void rlDrawRenderBatch(RenderBatch* batch); // Draw render batch data (Update->Draw->Reset)
  361. void rlSetRenderBatchActive(RenderBatch* batch); // Set the active render batch for rlgl (NULL for default internal)
  362. void rlDrawRenderBatchActive(); // Update and draw internal render batch
  363. bool rlCheckRenderBatchLimit(int vCount); // Check internal buffer overflow for a given number of vertex
  364. void rlSetTexture(uint id); // Set current texture for render batch and check buffers limits
  365. //------------------------------------------------------------------------------------------------------------------------
  366. // Vertex buffers management
  367. uint rlLoadVertexArray(); // Load vertex array (vao) if supported
  368. uint rlLoadVertexBuffer(void* buffer, int size, bool dynamic); // Load a vertex buffer attribute
  369. uint rlLoadVertexBufferElement(void* buffer, int size, bool dynamic); // Load a new attributes element buffer
  370. void rlUpdateVertexBuffer(int bufferId, void* data, int dataSize, int offset); // Update GPU buffer with new data
  371. void rlUnloadVertexArray(uint vaoId);
  372. void rlUnloadVertexBuffer(uint vboId);
  373. void rlSetVertexAttribute(uint index, int compSize, int type, bool normalized, int stride, void* pointer);
  374. void rlSetVertexAttributeDivisor(uint index, int divisor);
  375. void rlSetVertexAttributeDefault(int locIndex, const(void)* value, int attribType, int count); // Set vertex attribute default value
  376. void rlDrawVertexArray(int offset, int count);
  377. void rlDrawVertexArrayElements(int offset, int count, void* buffer);
  378. void rlDrawVertexArrayInstanced(int offset, int count, int instances);
  379. void rlDrawVertexArrayElementsInstanced(int offset, int count, void* buffer, int instances);
  380. // Textures management
  381. uint rlLoadTexture(void* data, int width, int height, int format, int mipmapCount); // Load texture in GPU
  382. uint rlLoadTextureDepth(int width, int height, bool useRenderBuffer); // Load depth texture/renderbuffer (to be attached to fbo)
  383. uint rlLoadTextureCubemap(void* data, int size, int format); // Load texture cubemap
  384. void rlUpdateTexture(uint id, int offsetX, int offsetY, int width, int height, int format, const(void)* data); // Update GPU texture with new data
  385. void rlGetGlTextureFormats(int format, uint* glInternalFormat, uint* glFormat, uint* glType); // Get OpenGL internal formats
  386. void rlUnloadTexture(uint id); // Unload texture from GPU memory
  387. void rlGenerateMipmaps(Texture2D* texture); // Generate mipmap data for selected texture
  388. void* rlReadTexturePixels(Texture2D texture); // Read texture pixel data
  389. ubyte* rlReadScreenPixels(int width, int height); // Read screen pixel data (color buffer)
  390. // Framebuffer management (fbo)
  391. uint rlLoadFramebuffer(int width, int height); // Load an empty framebuffer
  392. void rlFramebufferAttach(uint fboId, uint texId, int attachType, int texType, int mipLevel); // Attach texture/renderbuffer to a framebuffer
  393. bool rlFramebufferComplete(uint id); // Verify framebuffer is complete
  394. void rlUnloadFramebuffer(uint id); // Delete framebuffer from GPU
  395. // Shaders management
  396. uint rlLoadShaderCode(const(char)* vsCode, const(char)* fsCode); // Load shader from code strings
  397. uint rlCompileShader(const(char)* shaderCode, int type); // Compile custom shader and return shader id (type: GL_VERTEX_SHADER, GL_FRAGMENT_SHADER)
  398. uint rlLoadShaderProgram(uint vShaderId, uint fShaderId); // Load custom shader program
  399. void rlUnloadShaderProgram(uint id); // Unload shader program
  400. int rlGetLocationUniform(uint shaderId, const(char)* uniformName); // Get shader location uniform
  401. int rlGetLocationAttrib(uint shaderId, const(char)* attribName); // Get shader location attribute
  402. void rlSetUniform(int locIndex, const(void)* value, int uniformType, int count); // Set shader value uniform
  403. void rlSetUniformMatrix(int locIndex, Matrix mat); // Set shader value matrix
  404. void rlSetUniformSampler(int locIndex, uint textureId); // Set shader value sampler
  405. void rlSetShader(Shader shader); // Set shader currently active
  406. // Matrix state management
  407. Matrix rlGetMatrixModelview(); // Get internal modelview matrix
  408. Matrix rlGetMatrixProjection(); // Get internal projection matrix
  409. Matrix rlGetMatrixTransform(); // Get internal accumulated transform matrix
  410. Matrix rlGetMatrixProjectionStereo(int eye); // Get internal projection matrix for stereo render (selected eye)
  411. Matrix rlGetMatrixViewOffsetStereo(int eye); // Get internal view offset matrix for stereo render (selected eye)
  412. void rlSetMatrixProjection(Matrix proj); // Set a custom projection matrix (replaces internal projection matrix)
  413. void rlSetMatrixModelview(Matrix view); // Set a custom modelview matrix (replaces internal modelview matrix)
  414. void rlSetMatrixProjectionStereo(Matrix right, Matrix left); // Set eyes projection matrices for stereo rendering
  415. void rlSetMatrixViewOffsetStereo(Matrix right, Matrix left); // Set eyes view offsets matrices for stereo rendering
  416. // Quick and dirty cube/quad buffers load->draw->unload
  417. void rlLoadDrawCube(); // Load and draw a cube
  418. void rlLoadDrawQuad(); // Load and draw a quad
  419. // RLGL_H
  420. /***********************************************************************************
  421. *
  422. * RLGL IMPLEMENTATION
  423. *
  424. ************************************************************************************/
  425. // Check if config flags have been externally provided on compilation line
  426. // Defines module configuration flags
  427. // Required for: Vector3 and Matrix functions
  428. // Required for: malloc(), free()
  429. // Required for: strcmp(), strlen() [Used in rlglInit(), on extensions loading]
  430. // OpenGL 1.1 library for OSX
  431. // APIENTRY for OpenGL function pointer declarations is required
  432. // WINGDIAPI definition. Some Windows OpenGL headers need it
  433. // OpenGL 1.1 library
  434. // OpenGL 3 library for OSX
  435. // OpenGL 3 extensions library for OSX
  436. // GLAD extensions loading library, includes OpenGL headers
  437. // GLAD extensions loading library, includes OpenGL headers
  438. // EGL library
  439. // OpenGL ES 2.0 library
  440. // OpenGL ES 2.0 extensions library
  441. // It seems OpenGL ES 2.0 instancing entry points are not defined on Raspberry Pi
  442. // provided headers (despite being defined in official Khronos GLES2 headers)
  443. //----------------------------------------------------------------------------------
  444. // Defines and Macros
  445. //----------------------------------------------------------------------------------
  446. // Default shader vertex attribute names to set location points
  447. // Binded by default to shader location: 0
  448. // Binded by default to shader location: 1
  449. // Binded by default to shader location: 2
  450. // Binded by default to shader location: 3
  451. // Binded by default to shader location: 4
  452. // Binded by default to shader location: 5
  453. //----------------------------------------------------------------------------------
  454. // Types and Structures Definition
  455. //----------------------------------------------------------------------------------
  456. // Current render batch
  457. // Default internal render batch
  458. // Current matrix mode
  459. // Current matrix pointer
  460. // Default modelview matrix
  461. // Default projection matrix
  462. // Transform matrix to be used with rlTranslate, rlRotate, rlScale
  463. // Require transform matrix application to current draw-call vertex (if required)
  464. // Matrix stack for push/pop
  465. // Matrix stack counter
  466. // Default texture used on shapes/poly drawing (required by shader)
  467. // Active texture ids to be enabled on batch drawing (0 active by default)
  468. // Default vertex shader id (used by default shader program)
  469. // Default fragment shader Id (used by default shader program)
  470. // Basic shader, support vertex color and diffuse texture
  471. // Shader to be used on rendering (by default, defaultShader)
  472. // Stereo rendering flag
  473. // VR stereo rendering eyes projection matrices
  474. // VR stereo rendering eyes view offset matrices
  475. // Blending mode active
  476. // Blending source factor
  477. // Blending destination factor
  478. // Blending equation
  479. // Default framebuffer width
  480. // Default framebuffer height
  481. // Renderer state
  482. // VAO support (OpenGL ES2 could not support VAO extension) (GL_ARB_vertex_array_object)
  483. // Instancing supported (GL_ANGLE_instanced_arrays, GL_EXT_draw_instanced + GL_EXT_instanced_arrays)
  484. // NPOT textures full support (GL_ARB_texture_non_power_of_two, GL_OES_texture_npot)
  485. // Depth textures supported (GL_ARB_depth_texture, GL_WEBGL_depth_texture, GL_OES_depth_texture)
  486. // float textures support (32 bit per channel) (GL_OES_texture_float)
  487. // DDS texture compression support (GL_EXT_texture_compression_s3tc, GL_WEBGL_compressed_texture_s3tc, GL_WEBKIT_WEBGL_compressed_texture_s3tc)
  488. // ETC1 texture compression support (GL_OES_compressed_ETC1_RGB8_texture, GL_WEBGL_compressed_texture_etc1)
  489. // ETC2/EAC texture compression support (GL_ARB_ES3_compatibility)
  490. // PVR texture compression support (GL_IMG_texture_compression_pvrtc)
  491. // ASTC texture compression support (GL_KHR_texture_compression_astc_hdr, GL_KHR_texture_compression_astc_ldr)
  492. // Clamp mirror wrap mode supported (GL_EXT_texture_mirror_clamp)
  493. // Anisotropic texture filtering support (GL_EXT_texture_filter_anisotropic)
  494. // Maximum anisotropy level supported (minimum is 2.0f)
  495. // Maximum bits for depth component
  496. // Extensions supported flags
  497. // GRAPHICS_API_OPENGL_33 || GRAPHICS_API_OPENGL_ES2
  498. //----------------------------------------------------------------------------------
  499. // Global Variables Definition
  500. //----------------------------------------------------------------------------------
  501. // GRAPHICS_API_OPENGL_33 || GRAPHICS_API_OPENGL_ES2
  502. // NOTE: VAO functionality is exposed through extensions (OES)
  503. // NOTE: Instancing functionality could also be available through extension
  504. //----------------------------------------------------------------------------------
  505. // Module specific Functions Declaration
  506. //----------------------------------------------------------------------------------
  507. // Load default shader (RLGL.State.defaultShader)
  508. // Unload default shader (RLGL.State.defaultShader)
  509. // Get compressed format official GL identifier name
  510. // SUPPORT_GL_DETAILS_INFO
  511. // GRAPHICS_API_OPENGL_33 || GRAPHICS_API_OPENGL_ES2
  512. // Generate mipmaps data on CPU side
  513. // Generate next mipmap level on CPU side
  514. // Get pixel data size in bytes (image or texture)
  515. //----------------------------------------------------------------------------------
  516. // Module Functions Definition - Matrix operations
  517. //----------------------------------------------------------------------------------
  518. // Fallback to OpenGL 1.1 function calls
  519. //---------------------------------------
  520. // Choose the current matrix to be transformed
  521. //else if (mode == RL_TEXTURE) // Not supported
  522. // Push the current matrix into RLGL.State.stack
  523. // Pop lattest inserted matrix from RLGL.State.stack
  524. // Reset current matrix to identity matrix
  525. // Multiply the current matrix by a translation matrix
  526. // NOTE: We transpose matrix with multiplication order
  527. // Multiply the current matrix by a rotation matrix
  528. // NOTE: We transpose matrix with multiplication order
  529. // Multiply the current matrix by a scaling matrix
  530. // NOTE: We transpose matrix with multiplication order
  531. // Multiply the current matrix by another matrix
  532. // Matrix creation from array
  533. // Multiply the current matrix by a perspective matrix generated by parameters
  534. // Multiply the current matrix by an orthographic matrix generated by parameters
  535. // NOTE: If left-right and top-botton values are equal it could create
  536. // a division by zero on MatrixOrtho(), response to it is platform/compiler dependant
  537. // Set the viewport area (transformation from normalized device coordinates to window coordinates)
  538. //----------------------------------------------------------------------------------
  539. // Module Functions Definition - Vertex level operations
  540. //----------------------------------------------------------------------------------
  541. // Fallback to OpenGL 1.1 function calls
  542. //---------------------------------------
  543. // Initialize drawing mode (how to organize vertex)
  544. // Draw mode can be RL_LINES, RL_TRIANGLES and RL_QUADS
  545. // NOTE: In all three cases, vertex are accumulated over default internal vertex buffer
  546. // Make sure current RLGL.currentBatch->draws[i].vertexCount is aligned a multiple of 4,
  547. // that way, following QUADS drawing will keep aligned with index processing
  548. // It implies adding some extra alignment vertex at the end of the draw,
  549. // those vertex are not processed but they are considered as an additional offset
  550. // for the next set of vertex to be drawn
  551. // Finish vertex providing
  552. // Make sure vertexCount is the same for vertices, texcoords, colors and normals
  553. // NOTE: In OpenGL 1.1, one glColor call can be made for all the subsequent glVertex calls
  554. // Make sure colors count match vertex count
  555. // Make sure texcoords count match vertex count
  556. // TODO: Make sure normals count match vertex count... if normals support is added in a future... :P
  557. // NOTE: Depth increment is dependant on rlOrtho(): z-near and z-far values,
  558. // as well as depth buffer bit-depth (16bit or 24bit or 32bit)
  559. // Correct increment formula would be: depthInc = (zfar - znear)/pow(2, bits)
  560. // Verify internal buffers limits
  561. // NOTE: This check is combined with usage of rlCheckRenderBatchLimit()
  562. // WARNING: If we are between rlPushMatrix() and rlPopMatrix() and we need to force a rlDrawRenderBatch(),
  563. // we need to call rlPopMatrix() before to recover *RLGL.State.currentMatrix (RLGL.State.modelview) for the next forced draw call!
  564. // If we have multiple matrix pushed, it will require "RLGL.State.stackCounter" pops before launching the draw
  565. // Define one vertex (position)
  566. // NOTE: Vertex position data is the basic information required for drawing
  567. // Transform provided vector if required
  568. // Verify that current vertex buffer elements limit has not been reached
  569. // Define one vertex (position)
  570. // Define one vertex (position)
  571. // Define one vertex (texture coordinate)
  572. // NOTE: Texture coordinates are limited to QUADS only
  573. // Define one vertex (normal)
  574. // NOTE: Normals limited to TRIANGLES only?
  575. // TODO: Normals usage...
  576. // Define one vertex (color)
  577. // Define one vertex (color)
  578. // Define one vertex (color)
  579. //--------------------------------------------------------------------------------------
  580. // Module Functions Definition - OpenGL style functions (common to 1.1, 3.3+, ES2)
  581. //--------------------------------------------------------------------------------------
  582. // Set current texture to use
  583. // NOTE: If quads batch limit is reached, we force a draw call and next batch starts
  584. // Make sure current RLGL.currentBatch->draws[i].vertexCount is aligned a multiple of 4,
  585. // that way, following QUADS drawing will keep aligned with index processing
  586. // It implies adding some extra alignment vertex at the end of the draw,
  587. // those vertex are not processed but they are considered as an additional offset
  588. // for the next set of vertex to be drawn
  589. // Select and active a texture slot
  590. // Enable texture
  591. // Disable texture
  592. // Enable texture cubemap
  593. // Core in OpenGL 1.4
  594. // Disable texture cubemap
  595. // Set texture parameters (wrap mode/filter mode)
  596. // Enable shader program
  597. // Disable shader program
  598. // Enable rendering to texture (fbo)
  599. // Disable rendering to texture
  600. // Enable depth test
  601. // Disable depth test
  602. // Enable depth write
  603. // Disable depth write
  604. // Enable backface culling
  605. // Disable backface culling
  606. // Enable scissor test
  607. // Disable scissor test
  608. // Scissor test
  609. // Enable wire mode
  610. // NOTE: glPolygonMode() not available on OpenGL ES
  611. // Disable wire mode
  612. // NOTE: glPolygonMode() not available on OpenGL ES
  613. // Set the line drawing width
  614. // Get the line drawing width
  615. // Enable line aliasing
  616. // Disable line aliasing
  617. // Enable stereo rendering
  618. // Disable stereo rendering
  619. // Check if stereo render is enabled
  620. // Clear color buffer with color
  621. // Color values clamp to 0.0f(0) and 1.0f(255)
  622. // Clear used screen buffers (color and depth)
  623. // Clear used buffers: Color and Depth (Depth is used for 3D)
  624. //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // Stencil buffer not used...
  625. // Check and log OpenGL error codes
  626. // Set blend mode
  627. // Set blending mode factor and equation
  628. //----------------------------------------------------------------------------------
  629. // Module Functions Definition - rlgl functionality
  630. //----------------------------------------------------------------------------------
  631. // Initialize rlgl: OpenGL extensions, default buffers/shaders/textures, OpenGL states
  632. // Init default white texture
  633. // 1 pixel RGBA (4 bytes)
  634. // Init default Shader (customized for GL 3.3 and ES2)
  635. // RLGL.State.defaultShader
  636. // Init default vertex arrays buffers
  637. // Init stack matrices (emulating OpenGL 1.1)
  638. // Init internal matrices
  639. // GRAPHICS_API_OPENGL_33 || GRAPHICS_API_OPENGL_ES2
  640. // Initialize OpenGL default states
  641. //----------------------------------------------------------
  642. // Init state: Depth test
  643. // Type of depth testing to apply
  644. // Disable depth testing for 2D (only used for 3D)
  645. // Init state: Blending mode
  646. // Color blending function (how colors are mixed)
  647. // Enable color blending (required to work with transparencies)
  648. // Init state: Culling
  649. // NOTE: All shapes/models triangles are drawn CCW
  650. // Cull the back face (default)
  651. // Front face are defined counter clockwise (default)
  652. // Enable backface culling
  653. // Init state: Cubemap seamless
  654. // Seamless cubemaps (not supported on OpenGL ES 2.0)
  655. // Init state: Color hints (deprecated in OpenGL 3.0+)
  656. // Improve quality of color and texture coordinate interpolation
  657. // Smooth shading between vertex (vertex colors interpolation)
  658. // Store screen size into global variables
  659. //----------------------------------------------------------
  660. // Init state: Color/Depth buffers clear
  661. // Set clear color (black)
  662. // Set clear depth value (default)
  663. // Clear color and depth buffers (depth buffer required for 3D)
  664. // Vertex Buffer Object deinitialization (memory free)
  665. // Unload default shader
  666. // Unload default texture
  667. // Load OpenGL extensions
  668. // NOTE: External loader function could be passed as a pointer
  669. // Also defined for GRAPHICS_API_OPENGL_21
  670. // NOTE: glad is generated and contains only required OpenGL 3.3 Core extensions (and lower versions)
  671. // Get number of supported extensions
  672. // Get supported extensions list
  673. // WARNING: glGetStringi() not available on OpenGL 2.1
  674. // Free extensions pointers
  675. // Register supported extensions flags
  676. // OpenGL 3.3 extensions supported by default (core)
  677. // NOTE: With GLAD, we can check if an extension is supported using the GLAD_GL_xxx booleans
  678. // Texture compression: DXT
  679. // Texture compression: ETC2/EAC
  680. // GRAPHICS_API_OPENGL_33
  681. // Get supported extensions list
  682. // Allocate 512 strings pointers (2 KB)
  683. // One big const string
  684. // NOTE: We have to duplicate string because glGetString() returns a const string
  685. // Check required extensions
  686. // Check VAO support
  687. // NOTE: Only check on OpenGL ES, OpenGL 3.3 has VAO support as core feature
  688. // The extension is supported by our hardware and driver, try to get related functions pointers
  689. // NOTE: emscripten does not support VAOs natively, it uses emulation and it reduces overall performance...
  690. //glIsVertexArray = (PFNGLISVERTEXARRAYOESPROC)eglGetProcAddress("glIsVertexArrayOES"); // NOTE: Fails in WebGL, omitted
  691. // Check instanced rendering support
  692. // Web ANGLE
  693. // Standard EXT
  694. // Check NPOT textures support
  695. // NOTE: Only check on OpenGL ES, OpenGL 3.3 has NPOT textures full support as core feature
  696. // Check texture float support
  697. // Check depth texture support
  698. // Check texture compression support: DXT
  699. // Check texture compression support: ETC1
  700. // Check texture compression support: ETC2/EAC
  701. // Check texture compression support: PVR
  702. // Check texture compression support: ASTC
  703. // Check anisotropic texture filter support
  704. // Check clamp mirror wrap mode support
  705. // Free extensions pointers
  706. // Duplicated string must be deallocated
  707. // GRAPHICS_API_OPENGL_ES2
  708. // Check OpenGL information and capabilities
  709. //------------------------------------------------------------------------------
  710. // Show current OpenGL and GLSL version
  711. // NOTE: Anisotropy levels capability is an extension
  712. // Show some OpenGL GPU capabilities
  713. /*
  714. // Following capabilities are only supported by OpenGL 4.3 or greater
  715. glGetIntegerv(GL_MAX_VERTEX_ATTRIB_BINDINGS, &capability);
  716. TRACELOG(LOG_INFO, " GL_MAX_VERTEX_ATTRIB_BINDINGS: %i", capability);
  717. glGetIntegerv(GL_MAX_UNIFORM_LOCATIONS, &capability);
  718. TRACELOG(LOG_INFO, " GL_MAX_UNIFORM_LOCATIONS: %i", capability);
  719. */
  720. // SUPPORT_GL_DETAILS_INFO
  721. // Show some basic info about GL supported features
  722. // SUPPORT_GL_DETAILS_INFO
  723. // GRAPHICS_API_OPENGL_33 || GRAPHICS_API_OPENGL_ES2
  724. // Returns current OpenGL version
  725. // NOTE: Force OpenGL 3.3 on OSX
  726. // Get default framebuffer width
  727. // Get default framebuffer height
  728. // Get default internal shader (simple texture + tint color)
  729. // Get default internal texture (white texture)
  730. // Render batch management
  731. //------------------------------------------------------------------------------------------------
  732. // Load render batch
  733. // Initialize CPU (RAM) vertex buffers (position, texcoord, color data and indexes)
  734. //--------------------------------------------------------------------------------------------
  735. // 3 float by vertex, 4 vertex by quad
  736. // 2 float by texcoord, 4 texcoord by quad
  737. // 4 float by color, 4 colors by quad
  738. // 6 int by quad (indices)
  739. // 6 int by quad (indices)
  740. // Indices can be initialized right now
  741. //--------------------------------------------------------------------------------------------
  742. // Upload to GPU (VRAM) vertex data and initialize VAOs/VBOs
  743. //--------------------------------------------------------------------------------------------
  744. // Initialize Quads VAO
  745. // Quads - Vertex buffers binding and attributes enable
  746. // Vertex position buffer (shader-location = 0)
  747. // Vertex texcoord buffer (shader-location = 1)
  748. // Vertex color buffer (shader-location = 3)
  749. // Fill index buffer
  750. // Unbind the current VAO
  751. //--------------------------------------------------------------------------------------------
  752. // Init draw calls tracking system
  753. //--------------------------------------------------------------------------------------------
  754. //batch.draws[i].vaoId = 0;
  755. //batch.draws[i].shaderId = 0;
  756. //batch.draws[i].RLGL.State.projection = MatrixIdentity();
  757. //batch.draws[i].RLGL.State.modelview = MatrixIdentity();
  758. // Record buffer count
  759. // Reset draws counter
  760. // Reset depth value
  761. //--------------------------------------------------------------------------------------------
  762. // Unload default internal buffers vertex data from CPU and GPU
  763. // Unbind everything
  764. // Unload all vertex buffers data
  765. // Delete VBOs from GPU (VRAM)
  766. // Delete VAOs from GPU (VRAM)
  767. // Free vertex arrays memory from CPU (RAM)
  768. // Unload arrays
  769. // Draw render batch
  770. // NOTE: We require a pointer to reset batch and increase current buffer (multi-buffer)
  771. // Update batch vertex buffers
  772. //------------------------------------------------------------------------------------------------------------
  773. // NOTE: If there is not vertex data, buffers doesn't need to be updated (vertexCount > 0)
  774. // TODO: If no data changed on the CPU arrays --> No need to re-update GPU arrays (change flag required)
  775. // Activate elements VAO
  776. // Vertex positions buffer
  777. //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*4*batch->vertexBuffer[batch->currentBuffer].elementsCount, batch->vertexBuffer[batch->currentBuffer].vertices, GL_DYNAMIC_DRAW); // Update all buffer
  778. // Texture coordinates buffer
  779. //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*4*batch->vertexBuffer[batch->currentBuffer].elementsCount, batch->vertexBuffer[batch->currentBuffer].texcoords, GL_DYNAMIC_DRAW); // Update all buffer
  780. // Colors buffer
  781. //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*4*batch->vertexBuffer[batch->currentBuffer].elementsCount, batch->vertexBuffer[batch->currentBuffer].colors, GL_DYNAMIC_DRAW); // Update all buffer
  782. // NOTE: glMapBuffer() causes sync issue.
  783. // If GPU is working with this buffer, glMapBuffer() will wait(stall) until GPU to finish its job.
  784. // To avoid waiting (idle), you can call first glBufferData() with NULL pointer before glMapBuffer().
  785. // If you do that, the previous data in PBO will be discarded and glMapBuffer() returns a new
  786. // allocated pointer immediately even if GPU is still working with the previous data.
  787. // Another option: map the buffer object into client's memory
  788. // Probably this code could be moved somewhere else...
  789. // batch->vertexBuffer[batch->currentBuffer].vertices = (float *)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
  790. // if (batch->vertexBuffer[batch->currentBuffer].vertices)
  791. // {
  792. // Update vertex data
  793. // }
  794. // glUnmapBuffer(GL_ARRAY_BUFFER);
  795. // Unbind the current VAO
  796. //------------------------------------------------------------------------------------------------------------
  797. // Draw batch vertex buffers (considering VR stereo if required)
  798. //------------------------------------------------------------------------------------------------------------
  799. // Setup current eye viewport (half screen width)
  800. // Set current eye view offset to modelview matrix
  801. // Set current eye projection matrix
  802. // Draw buffers
  803. // Set current shader and upload current MVP matrix
  804. // Create modelview-projection matrix and upload to shader
  805. // Bind vertex attrib: position (shader-location = 0)
  806. // Bind vertex attrib: texcoord (shader-location = 1)
  807. // Bind vertex attrib: color (shader-location = 3)
  808. // Setup some default shader values
  809. // Active default sampler2D: texture0
  810. // Activate additional sampler textures
  811. // Those additional textures will be common for all draw calls of the batch
  812. // Activate default sampler2D texture0 (one texture is always active for default batch shader)
  813. // NOTE: Batch system accumulates calls by texture0 changes, additional textures are enabled for all the draw calls
  814. // Bind current draw call texture, activated as GL_TEXTURE0 and binded to sampler2D texture0 by default
  815. // We need to define the number of indices to be processed: quadsCount*6
  816. // NOTE: The final parameter tells the GPU the offset in bytes from the
  817. // start of the index buffer to the location of the first index to process
  818. // Unbind textures
  819. // Unbind VAO
  820. // Unbind shader program
  821. //------------------------------------------------------------------------------------------------------------
  822. // Reset batch buffers
  823. //------------------------------------------------------------------------------------------------------------
  824. // Reset vertex counters for next frame
  825. // Reset depth for next draw
  826. // Restore projection/modelview matrices
  827. // Reset RLGL.currentBatch->draws array
  828. // Reset active texture units for next batch
  829. // Reset draws counter to one draw for the batch
  830. //------------------------------------------------------------------------------------------------------------
  831. // Change to next buffer in the list (in case of multi-buffering)
  832. // Set the active render batch for rlgl
  833. // Update and draw internal render batch
  834. // NOTE: Stereo rendering is checked inside
  835. // Check internal buffer overflow for a given number of vertex
  836. // and force a RenderBatch draw call if required
  837. // NOTE: Stereo rendering is checked inside
  838. // Textures data management
  839. //-----------------------------------------------------------------------------------------
  840. // Convert image data to OpenGL texture (returns OpenGL valid Id)
  841. // Free any old binding
  842. // Check texture format support by OpenGL 1.1 (compressed textures not supported)
  843. // GRAPHICS_API_OPENGL_11
  844. // Generate texture id
  845. // Mipmap data offset
  846. // Load the different mipmap levels
  847. // Security check for NPOT textures
  848. // Texture parameters configuration
  849. // NOTE: glTexParameteri does NOT affect texture uploading, just the way it's used
  850. // NOTE: OpenGL ES 2.0 with no GL_OES_texture_npot support (i.e. WebGL) has limited NPOT support, so CLAMP_TO_EDGE must be used
  851. // Set texture to repeat on x-axis
  852. // Set texture to repeat on y-axis
  853. // NOTE: If using negative texture coordinates (LoadOBJ()), it does not work!
  854. // Set texture to clamp on x-axis
  855. // Set texture to clamp on y-axis
  856. // Set texture to repeat on x-axis
  857. // Set texture to repeat on y-axis
  858. // Magnification and minification filters
  859. // Alternative: GL_LINEAR
  860. // Alternative: GL_LINEAR
  861. // Activate Trilinear filtering if mipmaps are available
  862. // At this point we have the texture loaded in GPU and texture parameters configured
  863. // NOTE: If mipmaps were not in data, they are not generated automatically
  864. // Unbind current texture
  865. // Load depth texture/renderbuffer (to be attached to fbo)
  866. // WARNING: OpenGL ES 2.0 requires GL_OES_depth_texture/WEBGL_depth_texture extensions
  867. // In case depth textures not supported, we force renderbuffer usage
  868. // NOTE: We let the implementation to choose the best bit-depth
  869. // Possible formats: GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT32 and GL_DEPTH_COMPONENT32F
  870. // Create the renderbuffer that will serve as the depth attachment for the framebuffer
  871. // NOTE: A renderbuffer is simpler than a texture and could offer better performance on embedded devices
  872. // Load texture cubemap
  873. // NOTE: Cubemap data is expected to be 6 images in a single data array (one after the other),
  874. // expected the following convention: +X, -X, +Y, -Y, +Z, -Z
  875. // Load cubemap faces
  876. // Instead of using a sized internal texture format (GL_RGB16F, GL_RGB32F), we let the driver to choose the better format for us (GL_RGB)
  877. // Set cubemap texture sampling parameters
  878. // Flag not supported on OpenGL ES 2.0
  879. // Update already loaded texture in GPU with new data
  880. // NOTE: We don't know safely if internal texture format is the expected one...
  881. // Get OpenGL internal formats and data type from raylib PixelFormat
  882. // NOTE: on OpenGL ES 2.0 (WebGL), internalFormat must match format and options allowed are: GL_LUMINANCE, GL_RGB, GL_RGBA
  883. // NOTE: Requires extension OES_texture_float
  884. // NOTE: Requires extension OES_texture_float
  885. // NOTE: Requires extension OES_texture_float
  886. // NOTE: Requires OpenGL ES 2.0 or OpenGL 4.3
  887. // NOTE: Requires OpenGL ES 3.0 or OpenGL 4.3
  888. // NOTE: Requires OpenGL ES 3.0 or OpenGL 4.3
  889. // NOTE: Requires PowerVR GPU
  890. // NOTE: Requires PowerVR GPU
  891. // NOTE: Requires OpenGL ES 3.1 or OpenGL 4.3
  892. // NOTE: Requires OpenGL ES 3.1 or OpenGL 4.3
  893. // Unload texture from GPU memory
  894. // Generate mipmap data for selected texture
  895. // Check if texture is power-of-two (POT)
  896. // WARNING: Manual mipmap generation only works for RGBA 32bit textures!
  897. // Retrieve texture data from VRAM
  898. // NOTE: Texture data size is reallocated to fit mipmaps data
  899. // NOTE: CPU mipmap generation only supports RGBA 32bit data
  900. // Load the mipmaps
  901. // Once mipmaps have been generated and data has been uploaded to GPU VRAM, we can discard RAM data
  902. //glHint(GL_GENERATE_MIPMAP_HINT, GL_DONT_CARE); // Hint for mipmaps generation algorythm: GL_FASTEST, GL_NICEST, GL_DONT_CARE
  903. // Generate mipmaps automatically
  904. // Activate Trilinear filtering for mipmaps
  905. // Read texture pixel data
  906. // NOTE: Using texture.id, we can retrieve some texture info (but not on OpenGL ES 2.0)
  907. // Possible texture info: GL_TEXTURE_RED_SIZE, GL_TEXTURE_GREEN_SIZE, GL_TEXTURE_BLUE_SIZE, GL_TEXTURE_ALPHA_SIZE
  908. //int width, height, format;
  909. //glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
  910. //glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
  911. //glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &format);
  912. // NOTE: Each row written to or read from by OpenGL pixel operations like glGetTexImage are aligned to a 4 byte boundary by default, which may add some padding.
  913. // Use glPixelStorei to modify padding with the GL_[UN]PACK_ALIGNMENT setting.
  914. // GL_PACK_ALIGNMENT affects operations that read from OpenGL memory (glReadPixels, glGetTexImage, etc.)
  915. // GL_UNPACK_ALIGNMENT affects operations that write to OpenGL memory (glTexImage, etc.)
  916. // glGetTexImage() is not available on OpenGL ES 2.0
  917. // Texture width and height are required on OpenGL ES 2.0. There is no way to get it from texture id.
  918. // Two possible Options:
  919. // 1 - Bind texture to color fbo attachment and glReadPixels()
  920. // 2 - Create an fbo, activate it, render quad with texture, glReadPixels()
  921. // We are using Option 1, just need to care for texture format on retrieval
  922. // NOTE: This behaviour could be conditioned by graphic driver...
  923. // TODO: Create depth texture/renderbuffer for fbo?
  924. // Attach our texture to FBO
  925. // We read data as RGBA because FBO texture is configured as RGBA, despite binding another texture format
  926. // Clean up temporal fbo
  927. // Read screen pixel data (color buffer)
  928. // NOTE 1: glReadPixels returns image flipped vertically -> (0,0) is the bottom left corner of the framebuffer
  929. // NOTE 2: We are getting alpha channel! Be careful, it can be transparent if not cleared properly!
  930. // Flip image vertically!
  931. // Flip line
  932. // Set alpha component value to 255 (no trasparent image retrieval)
  933. // NOTE: Alpha value has already been applied to RGB in framebuffer, we don't need it!
  934. // NOTE: image data should be freed
  935. // Framebuffer management (fbo)
  936. //-----------------------------------------------------------------------------------------
  937. // Load a framebuffer to be used for rendering
  938. // NOTE: No textures attached
  939. // Create the framebuffer object
  940. // Unbind any framebuffer
  941. // Attach color buffer texture to an fbo (unloads previous attachment)
  942. // NOTE: Attach type: 0-Color, 1-Depth renderbuffer, 2-Depth texture
  943. // Verify render texture is complete
  944. // Unload framebuffer from GPU memory
  945. // NOTE: All attached textures/cubemaps/renderbuffers are also deleted
  946. // Query depth attachment to automatically delete texture/renderbuffer
  947. // Bind framebuffer to query depth texture type
  948. // NOTE: If a texture object is deleted while its image is attached to the *currently bound* framebuffer,
  949. // the texture image is automatically detached from the currently bound framebuffer.
  950. // Vertex data management
  951. //-----------------------------------------------------------------------------------------
  952. // Load a new attributes buffer
  953. // Load a new attributes element buffer
  954. // Update GPU buffer with new data
  955. // NOTE: dataSize and offset must be provided in bytes
  956. //case GL_INDEX_ARRAY: if (buffer != NULL) glIndexPointer(GL_SHORT, 0, buffer); break; // Indexed colors
  957. //TRACELOG(LOG_INFO, "VBO: Unloaded vertex data from VRAM (GPU)");
  958. // Shaders management
  959. //-----------------------------------------------------------------------------------------------
  960. // Load shader from code strings
  961. // NOTE: If shader string is NULL, using default vertex/fragment shaders
  962. // Detach shader before deletion to make sure memory is freed
  963. // Detach shader before deletion to make sure memory is freed
  964. // Get available shader uniforms
  965. // NOTE: This information is useful for debug...
  966. // Assume no variable names longer than 256
  967. // Get the name of the uniforms
  968. // Compile custom shader and return shader id
  969. //case GL_GEOMETRY_SHADER:
  970. //case GL_COMPUTE_SHADER:
  971. //case GL_GEOMETRY_SHADER:
  972. //case GL_COMPUTE_SHADER:
  973. // Load custom shader strings and return program id
  974. // NOTE: Default attribute shader locations must be binded before linking
  975. // NOTE: If some attrib name is no found on the shader, it locations becomes -1
  976. // NOTE: All uniform variables are intitialised to 0 when a program links
  977. // Unload shader program
  978. // Get shader location uniform
  979. // Get shader location attribute
  980. // Set shader value uniform
  981. // Set shader value attribute
  982. // Set shader value uniform matrix
  983. // Set shader value uniform sampler
  984. // Check if texture is already active
  985. // Register a new active texture for the internal batch system
  986. // NOTE: Default texture is always activated as GL_TEXTURE0
  987. // Activate new texture unit
  988. // Save texture id for binding on drawing
  989. // Set shader currently active
  990. // Matrix state management
  991. //-----------------------------------------------------------------------------------------
  992. // Return internal modelview matrix
  993. // Return internal projection matrix
  994. // Get internal accumulated transform matrix
  995. // TODO: Consider possible transform matrices in the RLGL.State.stack
  996. // Is this the right order? or should we start with the first stored matrix instead of the last one?
  997. //Matrix matStackTransform = MatrixIdentity();
  998. //for (int i = RLGL.State.stackCounter; i > 0; i--) matStackTransform = MatrixMultiply(RLGL.State.stack[i], matStackTransform);
  999. // Get internal projection matrix for stereo render (selected eye)
  1000. // Get internal view offset matrix for stereo render (selected eye)
  1001. // Set a custom modelview matrix (replaces internal modelview matrix)
  1002. // Set a custom projection matrix (replaces internal projection matrix)
  1003. // Set eyes projection matrices for stereo rendering
  1004. // Set eyes view offsets matrices for stereo rendering
  1005. // Load and draw a 1x1 XY quad in NDC
  1006. // Positions Texcoords
  1007. // Gen VAO to contain VBO
  1008. // Gen and fill vertex buffer (VBO)
  1009. // Bind vertex attributes (position, texcoords)
  1010. // Positions
  1011. // Texcoords
  1012. // Draw quad
  1013. // Delete buffers (VBO and VAO)
  1014. // Load and draw a 1x1 3D cube in NDC
  1015. // Positions Normals Texcoords
  1016. // Gen VAO to contain VBO
  1017. // Gen and fill vertex buffer (VBO)
  1018. // Bind vertex attributes (position, normals, texcoords)
  1019. // Positions
  1020. // Normals
  1021. // Texcoords
  1022. // Draw cube
  1023. // Delete VBO and VAO
  1024. //----------------------------------------------------------------------------------
  1025. // Module specific Functions Definition
  1026. //----------------------------------------------------------------------------------
  1027. // Load default shader (just vertex positioning and texture coloring)
  1028. // NOTE: This shader program is used for internal buffers
  1029. // NOTE: It uses global variable: RLGL.State.defaultShader
  1030. // NOTE: All locations must be reseted to -1 (no location)
  1031. // Vertex shader directly defined, no external file required
  1032. // Fragment shader directly defined, no external file required
  1033. // Precision required for OpenGL ES2 (WebGL)
  1034. // NOTE: Compiled vertex/fragment shaders are kept for re-use
  1035. // Compile default vertex shader
  1036. // Compile default fragment shader
  1037. // Set default shader locations: attributes locations
  1038. // Set default shader locations: uniform locations
  1039. // Unload default shader
  1040. // NOTE: It uses global variable: RLGL.State.defaultShader
  1041. // Get compressed format official GL identifier name
  1042. // GL_EXT_texture_compression_s3tc
  1043. // GL_3DFX_texture_compression_FXT1
  1044. // GL_IMG_texture_compression_pvrtc
  1045. // GL_OES_compressed_ETC1_RGB8_texture
  1046. // GL_ARB_texture_compression_rgtc
  1047. // GL_ARB_texture_compression_bptc
  1048. // GL_ARB_ES3_compatibility
  1049. // GL_KHR_texture_compression_astc_hdr
  1050. // SUPPORT_GL_DETAILS_INFO
  1051. // GRAPHICS_API_OPENGL_33 || GRAPHICS_API_OPENGL_ES2
  1052. // Mipmaps data is generated after image data
  1053. // NOTE: Only works with RGBA (4 bytes) data!
  1054. // Required mipmap levels count (including base level)
  1055. // Size in bytes (will include mipmaps...), RGBA only
  1056. // Count mipmap levels required
  1057. // Add mipmap size (in bytes)
  1058. // Generate mipmaps
  1059. // NOTE: Every mipmap data is stored after data
  1060. // Size of last mipmap
  1061. // Mipmap size to store after offset
  1062. // Add mipmap to data
  1063. // free mipmap data
  1064. // Manual mipmap generation (basic scaling algorithm)
  1065. // Scaling algorithm works perfectly (box-filter)
  1066. // GRAPHICS_API_OPENGL_11
  1067. // Get pixel data size in bytes (image or texture)
  1068. // NOTE: Size depends on pixel format
  1069. // Size in bytes
  1070. // Bits per pixel
  1071. // Total data size in bytes
  1072. // Most compressed formats works on 4x4 blocks,
  1073. // if texture is smaller, minimum dataSize is 8 or 16
  1074. // RLGL_IMPLEMENTATION