Aros/Developer/OpenGL
Introduction
[edit | edit source]The Amiga's first introduction to hardware enhanced 3D was Warp3D in the 1990s. This was followed by an open source(software based) equivalent called Wazp3D A Wazp3D adaptation to AROS from Matthias Rustler appeared on December 2007.
During 2009 Krzysztof "Deadwood" Smiechowicz ported version 7.5 of MESA 3D to AROS.
MESA 3D provides a generic OpenGL implementation.
Then he added hardware 3D acceleration capabilities to AROS with his port of Gallium3D for MESA 3D.
Then on September 2011 Wazp3D was linked to Mesa 3D for hardware enhanced 3D.
in -> vertex -> fragment -> out
- Framebuffer – alpha, stencil, depth
- Vertex – transform and lighting
- Fragment – texturing, fog,
- Geometry – primitives (dots, lines, polygons triangle/quad), clipping
- Compute -
- Tessellation -
Far better using SDL's 1.2.x GL additions for up to and including OpenGL 2 or consider SDL 1.3 / 2.x for OpenGL 3 and above
- Web GL 1.0 – OpenGL ES 2.0 – OpenGL 2 but complete support in OpenGL 4.1
- Web GL 2.0 – Open GL ES 3.0 – OpenGL 4.3
There is no backwards compatibility from OpenGL 2 to OpenGL 1 as well as OpenGL ES 2 to OpenGL ES 1. But all later OpenGL and ES versions are retrospectively support.
Considered deprecated (ie not advised using or missing/removed)
GL1.1 – OpenGL ES 1 with Fixed Functions – glNormalPointer, glColorPointer and glVertexPointer were introduced. Nearly all of the old ID software are OpenGL 1.1 + extensions
GL1.3 -
GL1.4 – fragment support added
GL1.5 – VBOs added – OpenGL ES 1.1 – added features such as mandatory support for multitexture, better multitexture support (including combiners and dot product texture operations), automatic mipmap generation, vertex buffer objects, state queries, user clip planes, and greater control over point rendering
GL2.0 – OpenGL es 2 – Vertex / Fragment shaders introduced – WebGL minimum – GLSL shading language -
deprecated = glMatrixMode glMult/LoadMatrix glRotate/Translate/Scale glPush/PopMatrix glBegin/End glVertex glTexCoords glLight glMaterial glPush/PopAttrib
GL2.1 – 2006 – last appearance of some matrix functions/stacks – PBOs Pixel buffer objects -
If you want to be able to run on hardware incapable of OpenGL 3.0, then suggest that you not write applications for OpenGL 3.0. Many of the features of 3.0 that can be used on older hardware are backported via extensions. So instead of writing a 3.x application, you could be writing a 2.1 application that is able to use certain extensions. So just code to the GL 2.1 spec instead. You can always research the calls and features that have been since deprecated and just avoid using them in a pure 2.1 program
general method is to have different rendering paths for different features. You detect the version number. If it's one thing, you use one rendering path. If it's lower, you use another. Etc. You can do fine-grained checking by checking for the presence of certain extensions.
Minimum advised starting point for new apps which arrived with Mesa 12 ported to AROS which opened access to the following (hardware support permitting)
Gl3.0 – 2008 – Geometry shaders – Vertex Array Objects (VAOs) ie GL_ARB_vertex_array_object. Older versions need to do more vertex setup work when drawing an object, rather than setting them up once and just binding the VAO. Modern OpenGL enforces you to write your own matrices and pass those manually into shader programs. use programmable function pipelines
GL3.1 – eliminates most of the fixed-function rendering pipeline in favor of a programmable one
GL3.3
GL4.0 – 2010 – Tesselation shaders – Image load/store and atomic counters are probably the biggest ones (this includes shader storage buffers too), though simultaneously the least advertised. Shaders get to write/read from images and buffers directly, though there are a lot of caveats and synchronization issues to understand about doing this. introduces an additional pair of shader stages: one that decides how much to tessellate a primitive, and the other to decide how to generate the new values from the tessellated primitive.
GL4.1 – 2010 – incompatibilities between the desktop version of OpenGL and OpenGL ES 2.0 persisted until OpenGL 4.1, which added the GL_ARB_ES2_compatibility extension. Subroutines are an interesting but often overlooked feature. Basically, you can attach subroutines to shader programs like uniforms. So you could use the same basic program that will run a lighting function, but you can swap lighting functions without changing the program. The basic program would fetch textures, figure out what the diffuse/specular/etc material parameters are, determine the normal and lighting parameters, and pass that to the subroutine. allows you to dynamically piece together different fragments of shaders. You can more or less attach a function to a specific bind point in a program.
GL4.2 – 2011 -
GL4.3 – 2012 – Compute shaders – openGL ES 3 compatibility -
GL4.4
GL4.5
Compiling
[edit | edit source]WARP3D
[edit | edit source]Recompiling a program that use Warp3D.library is quite simple. You just need to link it to Warp3D
So compiling cow3d for Aros x86 was as simple as gcc -c -O3 CoW3D-3.c -o Cow3D-Aros -lWarp3D
Wazp3D-Prefs is a little tool for selecting how Wazp3D works (fast or nice).
Aros version is included with Aminet/Cow3D (see Wazp3D homepage for documentation) Option "Renderer:Soft to Image" is safer if your Aros system dont support LockBitmapTags() else "Renderer:Soft to Bitmap" If your Aros support "Native Graphics Aros" then you can try "Renderer:hard overlay" or "Renderer:hard" but the display is not perfect as Mesa dont support rendering to bitmap as Warp3D
You need to use W3D_DrawTriFan(), W3D_DrawTriSttrip() or better still, W3D_DrawArray() with their corresponding data structures. Suggest the latter since you have much more control over how the vertex data is organised.
You create a triangle strip or a triangle fan. In a strip, alternate triangles share two points along an edge. In a fan, all triangles share one common vertex and adjacent triangles also share an edge. The simplest to illustrate is the strip. A rectangle is simply a 2 triangle strip, something like this:
V[0]--V[1] | /| | / | | / | | / | | / | V[2]--V[3]
Using one of the strip drawing methods, the same texture is applied to all polygons in the strip. If you use the old single triangle routines, you have to either enable global texture environment or specify the texture in each polygon separately.
Warp3D is a rasterizer only. That means that it draws primitives in screen space. Strictly, the axes are defined as X=left to right in pixels, Y top to botton in pixels, Z is plane of the screen into the distance. The valid range for Z is 0.0 – 1.0.
You need to calculate your vertices in screen space directly or write your own transformation pipeline.
See Starship3D
MESA 3D
[edit | edit source]MESA 12
[edit | edit source]This is a work in progress
Creating a window in SDL and binding an OpenGL 3.2 context to it uses these steps
Initialize the SDL video subsystem using SDL_Init or SDL_VideoInit Set the parameters we require for opengl using SDL_GL_SetAttribute Create a window using SDL_CreateWindow Bind an OpenGL context to the window using SDL_GL_CreateContext
MESA 7
[edit | edit source]From client perspective using mesa.library is quite simple:
1) For programs that simply use OpenGL and Glut (like Aminet/starship) then just compile them
gcc starship.c -lglut -lgl – o starshiparos
2) For programs that use OpenGL and Amiga windowing system
- Create a window
- Call AROSMesaCreateContext passing it the window and some other stuff -> you get rendering context in return
- Call AROSMesaMakeCurrent passing it the context so that AROSMesa knows what to render on
- Render some stuff using glXXX functions
- Call AROSMesaSwapBuffers to have the content of render buffer painted onto your window
- Loop to glXXX functions
[...]
- Call AROSMesaDestroyContext(context);
- Call CloseWindow(window);
OpenGL API has been changed in ABI V1, just rename 'glA' to 'AROSMesa' The VBO functions are still there, but you need to access them via glaGetProcAddress.
/*
Copyright (C) 2006-2011 Mark Olsen
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <intuition/extensions.h>
#include <cybergraphx/cybergraphics.h>
#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/cybergraphics.h>
#include <GL/arosmesa.h>
#include "quakedef.h"
#include "input.h"
#include "keys.h"
#include "gl_local.h"
#include "in_morphos.h"
#include "vid_mode_morphos.h"
struct Library *MesaBase = 0;
struct display
{
void *inputdata;
unsigned int width, height;
int fullscreen;
char used_mode[256];
struct Screen *screen;
struct Window *window;
void *pointermem;
char pal[256*4];
AROSMesaContext mesacontext;
};
void Sys_Video_CvarInit(void)
{
}
void *Sys_Video_Open(const char *mode, unsigned int width, unsigned int height, int fullscreen, unsigned char *palette)
{
struct display *d;
struct modeinfo modeinfo;
char monitorname[128];
int r;
int i;
d = AllocVec(sizeof(*d), MEMF_CLEAR);
if (d)
{
MesaBase = OpenLibrary("mesa.library", 0);
if (MesaBase)
{
if (fullscreen)
{
if (*mode && modeline_to_modeinfo(mode, &modeinfo))
{
snprintf(monitorname, sizeof(monitorname), "%s.monitor", modeinfo.monitorname);
d->screen = OpenScreenTags(0,
SA_Width, modeinfo.width,
SA_Height, modeinfo.height,
SA_Depth, modeinfo.depth,
#if 0
SA_MonitorName, monitorname,
#endif
SA_Quiet, TRUE,
TAG_DONE);
}
else
{
d->screen = OpenScreenTags(0,
SA_Quiet, TRUE,
TAG_DONE);
}
if (d->screen)
{
width = d->screen->Width;
height = d->screen->Height;
snprintf(d->used_mode, sizeof(d->used_mode), "Dunno,%d,%d,42", width, height);
}
else
fullscreen = 0;
}
if (d->screen || !fullscreen)
{
d->window = OpenWindowTags(0,
d->screen?WA_Width:WA_InnerWidth, width,
d->screen?WA_Height:WA_InnerHeight, height,
WA_Title, "FodQuake",
WA_DragBar, d->screen?FALSE:TRUE,
WA_DepthGadget, d->screen?FALSE:TRUE,
WA_Borderless, d->screen?TRUE:FALSE,
WA_RMBTrap, TRUE,
d->screen?WA_CustomScreen:TAG_IGNORE, (IPTR)d->screen,
WA_Activate, TRUE,
TAG_DONE);
if (d->window)
{
d->mesacontext = AROSMesaCreateContextTags(
AMA_Window, d->window,
AMA_Left, d->screen?0:d->window->BorderLeft,
AMA_Top, d->screen?0:d->window->BorderTop,
AMA_Width, width,
AMA_Height, height,
AMA_NoStencil, TRUE,
AMA_NoAccum, TRUE,
TAG_DONE);
if (d->mesacontext)
{
AROSMesaMakeCurrent(d->mesacontext);
d->pointermem = AllocVec(256, MEMF_ANY|MEMF_CLEAR);
if (d->pointermem)
{
SetPointer(d->window, d->pointermem, 16, 16, 0, 0);
d->width = width;
d->height = height;
d->fullscreen = fullscreen;
d->inputdata = Sys_Input_Init(d->screen, d->window);
if (d->inputdata)
{
return d;
}
FreeVec(d->pointermem);
}
AROSMesaMakeCurrent(0);
AROSMesaDestroyContext(d->mesacontext);
}
CloseWindow(d->window);
}
if (d->screen)
CloseScreen(d->screen);
}
CloseLibrary(MesaBase);
}
FreeVec(d);
}
return 0;
}
void Sys_Video_Close(void *display)
{
struct display *d = display;
Sys_Input_Shutdown(d->inputdata);
AROSMesaMakeCurrent(0);
AROSMesaDestroyContext(d->mesacontext);
CloseWindow(d->window);
FreeVec(d->pointermem);
if (d->screen)
CloseScreen(d->screen);
CloseLibrary(MesaBase);
FreeVec(d);
}
unsigned int Sys_Video_GetNumBuffers(void *display)
{
struct display *d;
d = display;
return d->screen ? 3 : 1;
}
int Sys_Video_GetKeyEvent(void *display, keynum_t *keynum, qboolean *down)
{
struct display *d = display;
return Sys_Input_GetKeyEvent(d->inputdata, keynum, down);
}
void Sys_Video_GetMouseMovement(void *display, int *mousex, int *mousey)
{
struct display *d = display;
Sys_Input_GetMouseMovement(d->inputdata, mousex, mousey);
}
void Sys_Video_SetWindowTitle(void *display, const char *text)
{
struct display *d;
d = display;
SetWindowTitles(d->window, text, (void *)-1);
}
unsigned int Sys_Video_GetWidth(void *display)
{
struct display *d;
d = display;
return d->width;
}
unsigned int Sys_Video_GetHeight(void *display)
{
struct display *d;
d = display;
return d->height;
}
qboolean Sys_Video_GetFullscreen(void *display)
{
struct display *d;
d = display;
return d->fullscreen;
}
const char *Sys_Video_GetMode(void *display)
{
struct display *d;
d = display;
return d->used_mode;
}
void Sys_Video_BeginFrame(void *display, unsigned int *x, unsigned int *y, unsigned int *width, unsigned int *height)
{
struct display *d;
d = display;
*x = 0;
*y = 0;
*width = d->width;
*height = d->height;
}
void Sys_Video_Update(void *display, vrect_t *rects)
{
struct display *d = display;
AROSMesaSwapBuffers(d->mesacontext);
}
void Sys_Video_GrabMouse(void *display, int dograb)
{
struct display *d = display;
if (!d->screen)
{
Sys_Input_GrabMouse(d->inputdata, dograb);
if (dograb)
{
/* Hide pointer */
SetPointer(d->window, d->pointermem, 16, 16, 0, 0);
}
else
{
/* Show pointer */
ClearPointer(d->window);
}
}
}
void Sys_Video_SetGamma(void *display, unsigned short *ramps)
{
}
qboolean Sys_Video_HWGammaSupported(void *display)
{
return 0;
}
int Sys_Video_FocusChanged(void *display)
{
return 0;
}
3D Voxel C, 3D Voxel C++, voxel space c, Minecr*ft simple terrain, Voxel C++, [], [],
TD C++, 2D Tiles Sprite Sheet Game C++, 2D Tile Youtube, [], [],
GLSL openGL Shader Language
[edit | edit source]Shaders are written in the C-like language GLSL which has no pointers and is not object-oriented.
GLSL is tailored for use with graphics and contains useful features specifically targeted at vector and matrix manipulation.
GLSL 1.0 to 1.2 (OpenGL 2.x)
[edit | edit source]Shaders always begin with a version declaration, followed by a list of input and output variables, uniforms and its main function. Each shader's entry point is at its main function where we process any input variables and output the results in its output variables.
#version version_number uniform type uniform_name; void main() { // process input(s) and do_something ... // output did_somethings to output variable out_variable_name = something_done; }
Each input variable is also known as a vertex attribute. There is a maximum number of vertex attributes we're allowed to declare limited by the hardware. OpenGL guarantees there are always at least 16 4-component vertex attributes available, but some hardware may allow for more which you can retrieve by querying GL_MAX_VERTEX_ATTRIBS:
Types
GLSL has, like any other programming language, data types for specifying what kind of variable we want to work with. GLSL has most of the default basic types we know from languages like C: int, float, double, uint and bool. GLSL also features two container types that we'll be using a lot, namely vectors and matrices.
Vectors
A vector in GLSL is a 2,3 or 4 component container for any of the basic types just mentioned. They can take the following form (n represents the number of components):
- vecn: the default vector of n floats.
- bvecn: a vector of n booleans.
- ivecn: a vector of n integers.
- uvecn: a vector of n unsigned integers.
- dvecn: a vector of n double components.
Most of the time we will be using the basic vecn since floats are sufficient for most of our purposes.
Components of a vector can be accessed via vec.x where x is the first component of the vector. You can use .x, .y, .z and .w to access their first, second, third and fourth component respectively. GLSL also allows you to use rgba for colors or stpq for texture coordinates, accessing the same components.
The vector datatype allows for some interesting and flexible component selection called swizzling. Swizzling allows us to use syntax like this:
vec2 someVec; vec4 differentVec = someVec.xyxx; vec3 anotherVec = differentVec.zyw; vec4 otherVec = someVec.xxxx + anotherVec.yxzy;
You can use any combination of up to 4 letters to create a new vector (of the same type) as long as the original vector has those components; it is not allowed to access the .z component of a vec2 for example. We can also pass vectors as arguments to different vector constructor calls, reducing the number of arguments required:
vec2 vect = vec2(0.5, 0.7); vec4 result = vec4(vect, 0.0, 0.0); vec4 otherResult = vec4(result.xyz, 1.0);
Vectors are thus a flexible datatype that we can use for all kinds of input and output.
Uniforms are another way to pass data from our application on the CPU to the shaders on the GPU. Uniforms are global so is unique per shader program object, and can be accessed from any shader at any stage in the shader program. Uniforms also will keep their values until they're either reset or updated.
To declare a uniform in GLSL we simply add the uniform keyword to a shader with a type and a name. From that point on we can use the newly declared uniform in the shader.
#version 330 core out vec4 FragColor; uniform vec4 ourColor; // we set this variable in the OpenGL code. void main() { FragColor = ourColor; }
Vertex - attributes
Vectors - swizzling
worldspace (position) transformed matrix to screenspace (projection, modelview, position)
Fragment - vectors
R, G, B, A
sdf (signed distance function) 2d and 3d shapes in shaders
Tut, [], [],
SDL2
Game starter, Game start, [], [],
simple c++ platformer, [], [],
Simple C 3D engine, [], [],
[], [], [],
SDL1
[], [], [],
[], [], [],
#include <GL/glu.h>
#include <GL/glut.h>
#include <vector>
#include <cmath>
const int SCREEN_WIDTH = 1024;
const int SCREEN_HEIGHT = 1024;
const float camera[] = {.6,0,1};
const float light0_position[4] = {1,1,1,0};
void render_scene(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(camera[0], camera[1], camera[2], 0, 0, 0, 0, 1, 0);
glColor3f(.8, 0., 0.);
glutSolidTeapot(.7);
glutSwapBuffers();
}
void process_keys(unsigned char key, int x, int y) {
if (27==key) {
exit(0);
}
}
void change_size(int w, int h) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
glOrtho(-1,1,-1,1,-1,8);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(100,100);
glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
glutCreateWindow("GLSL tutorial");
glClearColor(0.0,0.0,1.0,1.0);
glutDisplayFunc(render_scene);
glutReshapeFunc(change_size);
glutKeyboardFunc(process_keys);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
glutMainLoop();
return 0;
}
#include <exec/types.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <intuition/screens.h>
#include <cybergraphx/cybergraphics.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/intuition.h>
#include <proto/timer.h>
#include <devices/timer.h>
#include <proto/cybergraphics.h>
#define GL_GLEXT_PROTOTYPES
#include <GL/arosmesa.h>
#include <stdio.h>
AROSMesaContext glcont=NULL;
double angle = 0.0;
double angle_inc = 0.0;
BOOL finished = FALSE;
struct Window * win = NULL;
struct Device * TimerBase = NULL;
struct timerequest timereq;
struct MsgPort timeport;
struct Library * CyberGfxBase = NULL;
BOOL fullscreen = FALSE;
GLuint fragmentShader = 0;
GLuint vertexShader = 0;
GLuint shaderProgram = 0;
GLint angleLocation = 0;
const GLchar * fragmentShaderSource =
"uniform float angle;"
"void main()"
"{"
" vec4 v = vec4(gl_Color);"
" float intensity = abs(1.0f - (mod(angle, 1440.0f) / 720.0f));"
" v.b = v.b * intensity;"
" v.g = v.g * (1.0f - intensity);"
" gl_FragColor = v;"
"}";
const GLchar * vertexShaderSource =
"void main()"
"{ "
" gl_FrontColor = gl_Color;"
" gl_Position = ftransform();"
"}";
#define RAND_COL 1.0f
#define DEGREES_PER_SECOND 180.0
#define USE_PERSPECTIVE 1
void prepare_shader_program()
{
#define BUFFER_LEN 2048
char buffer[BUFFER_LEN] = {0};
int len;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
glGetShaderInfoLog(fragmentShader, BUFFER_LEN, &len, buffer);
printf("Fragment shader compile output: %s\n", buffer);
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
glGetShaderInfoLog(vertexShader, BUFFER_LEN, &len, buffer);
printf("Vertex shader compile output: %s\n", buffer);
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glGetProgramInfoLog(shaderProgram, BUFFER_LEN, &len, buffer);
printf("Shader program compile output: %s\n", buffer);
#undef BUFFER_LEN
}
void cleanup_shader_program()
{
glUseProgram(0);
glDetachShader(shaderProgram, fragmentShader);
glDetachShader(shaderProgram, vertexShader);
glDeleteShader(fragmentShader);
glDeleteShader(vertexShader);
glDeleteProgram(shaderProgram);
}
void render_face()
{
glBegin(GL_QUADS);
glColor4f(RAND_COL , 0.0, RAND_COL, 0.3);
glVertex3f(-0.25, -0.25, 0.0);
glColor4f(0, RAND_COL, RAND_COL, 0.3);
glVertex3f(-0.25, 0.25, 0.0);
glColor4f(0 , 0, 0, 0.3);
glVertex3f(0.25, 0.25, 0.0);
glColor4f(RAND_COL , RAND_COL, 0, 0.3);
glVertex3f(0.25, -0.25, 0.0);
glEnd();
}
void render_cube()
{
glPushMatrix();
glRotatef(0.0, 0.0, 1.0, 0.0);
glTranslatef(0.0, 0.0, 0.25);
render_face();
glPopMatrix();
glPushMatrix();
glRotatef(90.0, 0.0, 1.0, 0.0);
glTranslatef(0.0, 0.0, 0.25);
render_face();
glPopMatrix();
glPushMatrix();
glRotatef(180.0, 0.0, 1.0, 0.0);
glTranslatef(0.0, 0.0, 0.25);
render_face();
glPopMatrix();
glPushMatrix();
glRotatef(270.0, 0.0, 1.0, 0.0);
glTranslatef(0.0, 0.0, 0.25);
render_face();
glPopMatrix();
glPushMatrix();
glRotatef(90.0, 1.0, 0.0, 0.0);
glTranslatef(0.0, 0.0, 0.25);
render_face();
glPopMatrix();
glPushMatrix();
glRotatef(-90.0, 1.0, 0.0, 0.0);
glTranslatef(0.0, 0.0, 0.25);
render_face();
glPopMatrix();
}
void render_triangle()
{
glBegin(GL_TRIANGLES);
glColor4f(1.0, 0.0, 0.0, 1.0);
glVertex3f(-0.25, -0.25, 0.0);
glColor4f(0.0, 1.0, 0.0, 1.0);
glVertex3f(-0.25, 0.25, 0.0);
glColor4f(0.0, 0.0, 1.0, 1.0);
glVertex3f( 0.25, 0.25, 0.0);
glEnd();
}
void render()
{
glLoadIdentity();
glClearColor(0.3, 0.3, 0.3, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glCullFace(GL_BACK);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
angle += angle_inc;
glUniform1f(angleLocation, angle);
#if USE_PERSPECTIVE == 1
glTranslatef(0.0, 0.0, -6.0);
#endif
glPushMatrix();
glRotatef(angle, 0.0, 1.0, 0.0);
glTranslatef(0.0, 0.0, 0.25);
glRotatef(angle, 1.0, 0.0, 1.0);
render_cube();
glPopMatrix();
glDisable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
AROSMesaSwapBuffers(glcont);
}
#define VISIBLE_WIDTH 300
#define VISIBLE_HEIGHT 300
void initmesa()
{
struct TagItem attributes [ 14 ]; /* 14 should be more than enough :) */
int i = 0;
GLfloat h = 0.0f;
attributes[i].ti_Tag = AMA_Window; attributes[i++].ti_Data = (IPTR)win;
attributes[i].ti_Tag = AMA_Left; attributes[i++].ti_Data = win->BorderLeft;
attributes[i].ti_Tag = AMA_Top; attributes[i++].ti_Data = win->BorderTop;
attributes[i].ti_Tag = AMA_Bottom; attributes[i++].ti_Data = win->BorderBottom;
attributes[i].ti_Tag = AMA_Right; attributes[i++].ti_Data = win->BorderRight;
// double buffer ?
attributes[i].ti_Tag = AMA_DoubleBuf; attributes[i++].ti_Data = GL_TRUE;
// RGB(A) Mode ?
attributes[i].ti_Tag = AMA_RGBMode; attributes[i++].ti_Data = GL_TRUE;
/* Stencil/Accum */
attributes[i].ti_Tag = AMA_NoStencil; attributes[i++].ti_Data = GL_TRUE;
attributes[i].ti_Tag = AMA_NoAccum; attributes[i++].ti_Data = GL_TRUE;
// done...
attributes[i].ti_Tag = TAG_DONE;
glcont = AROSMesaCreateContext(attributes);
if (glcont)
{
AROSMesaMakeCurrent(glcont);
h = (GLfloat)VISIBLE_HEIGHT / (GLfloat)VISIBLE_WIDTH ;
glViewport(0, 0, (GLint) VISIBLE_WIDTH, (GLint) VISIBLE_HEIGHT);
#if USE_PERSPECTIVE == 1
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -h, h, 5.0, 200.0);
glMatrixMode(GL_MODELVIEW);
#endif
prepare_shader_program();
glUseProgram(shaderProgram);
angleLocation = glGetUniformLocation(shaderProgram, "angle");
}
else
finished = TRUE; /* Failure. Stop */
}
void deinitmesa()
{
if (glcont)
{
cleanup_shader_program();
AROSMesaDestroyContext(glcont);
}
}
static int init_timerbase()
{
timeport.mp_Node.ln_Type = NT_MSGPORT;
timeport.mp_Node.ln_Pri = 0;
timeport.mp_Node.ln_Name = NULL;
timeport.mp_Flags = PA_IGNORE;
timeport.mp_SigTask = FindTask(NULL);
timeport.mp_SigBit = 0;
NEWLIST(&timeport.mp_MsgList);
timereq.tr_node.io_Message.mn_Node.ln_Type = NT_MESSAGE;
timereq.tr_node.io_Message.mn_Node.ln_Pri = 0;
timereq.tr_node.io_Message.mn_Node.ln_Name = NULL;
timereq.tr_node.io_Message.mn_ReplyPort = &timeport;
timereq.tr_node.io_Message.mn_Length = sizeof (timereq);
if(OpenDevice("timer.device",UNIT_VBLANK,(struct IORequest *)&timereq,0) == 0)
{
TimerBase = (struct Device *)timereq.tr_node.io_Device;
return 1;
}
else
{
return 0;
}
}
static void deinit_timerbase()
{
if (TimerBase != NULL)
CloseDevice((struct IORequest *)&timereq);
}
void HandleIntuiMessages(void)
{
struct IntuiMessage *msg;
while((msg = (struct IntuiMessage *)GetMsg(win->UserPort)))
{
switch(msg->Class)
{
case IDCMP_CLOSEWINDOW:
finished = TRUE;
break;
case IDCMP_VANILLAKEY:
if (msg->Code == 27 /* ESC */) finished = TRUE;
break;
}
ReplyMsg((struct Message *)msg);
}
}
#define ARG_FULLSCREEN 0
#define NUM_ARGS 1
STATIC CONST_STRPTR TEMPLATE=(CONST_STRPTR) "FULLSCREEN/S";
static struct RDArgs *myargs;
static IPTR args[NUM_ARGS];
void get_arguments(void)
{
if((myargs = ReadArgs(TEMPLATE, args, NULL)))
{
fullscreen = (BOOL)args[ARG_FULLSCREEN];
FreeArgs(myargs);
}
}
/*
** Open a simple window using OpenWindowTagList()
*/
int main(void)
{
ULONG fps = 0;
// ULONG exitcounter = 0;
TEXT title[100];
struct Screen * pubscreen = NULL;
struct Screen * customscreen = NULL;
LONG modeid;
struct timeval tv;
UQUAD lastmicrosecs = 0L;
UQUAD currmicrosecs = 0L;
UQUAD fpsmicrosecs = 0L;
get_arguments();
init_timerbase();
GetSysTime(&tv);
lastmicrosecs = tv.tv_secs * 1000000 + tv.tv_micro;
fpsmicrosecs = lastmicrosecs;
if (fullscreen)
{
CyberGfxBase = OpenLibrary("cybergraphics.library", 0L);
modeid = BestCModeIDTags(CYBRBIDTG_NominalWidth, VISIBLE_WIDTH,
CYBRBIDTG_NominalHeight, VISIBLE_HEIGHT,
TAG_DONE);
customscreen = OpenScreenTags(NULL,
SA_Type, CUSTOMSCREEN,
SA_DisplayID, modeid,
SA_Width, VISIBLE_WIDTH,
SA_Height, VISIBLE_HEIGHT,
SA_ShowTitle, FALSE,
SA_Quiet, TRUE,
TAG_DONE);
win = OpenWindowTags(NULL,
WA_Left, 0,
WA_Top, 200,
WA_InnerWidth, VISIBLE_WIDTH,
WA_InnerHeight, VISIBLE_HEIGHT,
WA_CustomScreen, (IPTR)customscreen,
WA_Flags, WFLG_ACTIVATE | WFLG_BACKDROP | WFLG_BORDERLESS | WFLG_RMBTRAP,
WA_IDCMP, IDCMP_VANILLAKEY,
TAG_DONE);
}
else
{
if ((pubscreen = LockPubScreen(NULL)) == NULL) return 1;
win = OpenWindowTags(0,
WA_Title, (IPTR)"MesaSimpleRendering",
WA_PubScreen, pubscreen,
WA_CloseGadget, TRUE,
WA_DragBar, TRUE,
WA_DepthGadget, TRUE,
WA_Left, 50,
WA_Top, 200,
WA_InnerWidth, VISIBLE_WIDTH,
WA_InnerHeight, VISIBLE_HEIGHT,
WA_Activate, TRUE,
WA_RMBTrap, TRUE,
WA_SimpleRefresh, TRUE,
WA_NoCareRefresh, TRUE,
WA_IDCMP, IDCMP_VANILLAKEY | IDCMP_CLOSEWINDOW,
TAG_DONE);
UnlockPubScreen(NULL, pubscreen);
}
initmesa();
// finished = TRUE;
while(!finished)
{
GetSysTime(&tv);
currmicrosecs = tv.tv_secs * 1000000 + tv.tv_micro;
if (currmicrosecs - fpsmicrosecs > 1000000)
{
/* FPS counting is naive! */
fpsmicrosecs += 1000000;
sprintf(title, "MesaSimpleRendering, FPS: %d", fps);
SetWindowTitles(win, title, (UBYTE *)~0L);
fps = 0;
}
angle_inc = ((double)(currmicrosecs - lastmicrosecs) / 1000000.0) * DEGREES_PER_SECOND;
lastmicrosecs = currmicrosecs;
fps++;
render();
HandleIntuiMessages();
// exitcounter++;
// Delay(10);
// if (exitcounter > 0) finished = TRUE;
}
deinitmesa();
deinit_timerbase();
CloseWindow(win);
if (customscreen) CloseScreen(customscreen);
if (CyberGfxBase) CloseLibrary(CyberGfxBase);
return 0;
}
GLSL tut, [], [], []
load a vertex shader file and fragment shader file and store each in a separate C string call glCreateShader twice; for 1 vertex and 1 fragment shader index call glShaderSource to copy code from a string for each of the above call glCompileShader for both shader indices call glCreateProgram to create an index to a new program call glAttachShader twice, to attach both shader indices to the program call glLinkProgram call glGetUniformLocation to get the unique location of the variable called "inputColour" call glUseProgram to switch to your shader before calling... glUniform4f(location, r,g,b,a) to assign an initial colour to your fragment shader (e.g. glUniform4f(colour_loc, 1.0f, 0.0f, 0.0f, 1.0f) for red)
For a complete list of OpenGL shader functions see the Quick Reference Card. The most useful functions are below
OpenGL "Shader" (Separate Shader Code) Functions Function Name Description glCreateShader() create a variable for storing a shader's code in OpenGL. returns unsigned int index to it. glShaderSource() copy shader code from C string into an OpenGL shader variable glCompileShader() compile an OpenGL shader variable that has code in it glGetShaderiv() can be used to check if compile found errors glGetShaderInfoLog() creates a string with any error information glDeleteShader() free memory used by an OpenGL shader variable OpenGL "Program" (Combined Shader Programme) Functions Function Name Description glCreateProgram() create a variable for storing a combined shader programme in OpenGL. returns unsigned int index to it. glAttachShader() attach a compiled OpenGL shader variable to a shader programme variable glLinkProgram() after all shaders are attached, link the parts into a complete shader programme glValidateProgram() check if a program is ready to execute. information stored in a log glGetProgramiv() can be used to check for link and validate errors glGetProgramInfoLog() writes any information from link and validate to a C string glUseProgram() switch to drawing with a specified shader programme glGetActiveAttrib() get details of a numbered per-vertex attribute used in the shader glGetAttribLocation() get the unique "location" identifier of a named per-vertex attribute glGetUniformLocation() get the unique "location" identifier of a named uniform variable glGetActiveUniform() get details of a named uniform variable used in the shader glUniform{1234}{ifd}() set the value of a uniform variable of a given shader (function name varies by dimensionality and data type) glUniform{1234}{ifd}v() same as above, but with a whole array of values glUniformMatrix{234}{fd}v() same as above, but for matrices of dimensions 2x2,3x3, or 4x4
GLSL 1.3 to 1.5 (OpenGL 3.x) GLSL 4.x (OpenGL 4.x)
[edit | edit source]Some changes occurred with later OpenGL specs compared to OpenGL 2 and so
#version version_number in type in_variable_name; in type in_variable_name; out type out_variable_name; uniform type uniform_name; void main() { // process input(s) and do_something ... // output did_somethings to output variable out_variable_name = something_done; }
Want to have inputs and outputs on the individual shaders so that we can move stuff around. GLSL defined the in and out keywords specifically for that purpose. Each shader can specify inputs and outputs using those keywords and wherever an output variable matches with an input variable of the next shader stage they're passed along. The vertex and fragment shader differ
Where Raylib lies regarding other systems
- 1st - C, C++ base level
- 2nd - SDL with OpenGL framework, Raylib with OpenGL
- 3rd - Godot engines, Ren'Py, GameMaker, Scratch, Unity, Unreal ect (AROS does not have)
Raylib's history
- 2013 1.0
- 2016 2.0
- 2019 3.0
- 2021 3.7
- 2023 5.0
It is highly inspired by XNA framework and by Borland BGI graphics lib
- Made for games and animations
- Input polled
- every frame starts from fresh empty screen, you are tracking any state entity data per refresh
- nothing happens until EndDrawing is called
Game loop - Quit -> Handle Input -> Update Game -> begin Drawing -> Draw Game -> End Drawing -> to Quit
DrawFPS, SetTargetFPS,
DrawTexturePro (tex2d, src, dest, origin, rot, color)
Flexible Materials system, supporting classic maps and PBR maps
Camera2d (), beginMode2D , EndMode2D, GetworldToScreen2D (pos, camera) for HUD, health etc, GetScreenToWorld2D (pos, camera) what World items clicked,
Animations, your code covers all motions, use time functions GetFrameTime()
iqm model and animation support -> binary file -> resource package
Animated 3D models supported (skeletal bones animation) (raylib supports several 3d model formats (obj, gltf/glb, iqm, m3d, vox), some formats support must be enabled in raylib/src/config.h file!)
A* (A star) pathfinding, [], [], Dijkstra, BreadthFirstS and DepthFirstS
2D Physics engines like Box2D, Velocity Raptor, Torque2D, Chipmunk2D, [], [], Fluids,
3D Physics like Torque6, [1], ODE, Jolt, [2], Cu, Vehicle, Physics3D, qu3e, Newton,
gcc -o game main.c -lraylib
#include "raylib.h"
int main(void)
{
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib ");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
while (!WindowShouldClose()) // Detect window close button or ESC key
{
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode2D(camera);
EndMode2D();
EndDrawing();
}
CloseWindow(); // Close window and OpenGL context
return 0;
}
#include <stdio.h>
#include "raylib.h"
int main()
{
Texture2D sprite;
Sound sound;
Music music;
InitWindow(800, 450, "basic window");
InitAudioDevice();
sprite = LoadTexture("sprite.png");
sound = LoadSound("sound.ogg");
music = LoadMusicStream("music.mp3");
float posX = -sprite.width;
while(!WindowShouldClose())
{
UpdateMusicStream(music);
posX += GetFrameTime() * 300;
if (posX > 800)
{
posX = -sprite.width;
}
if (IsKeyPressed(KEY_SPACE))
{
StopMusicStream(music);
PlayMusicStream(music);
}
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
{
Rectangle spriteRect = {
posX,
10,
sprite.width,
sprite.height
};
if (CheckCollisionPointRec(GetMousePosition(), spriteRect))
{
PlaySound(sound);
}
}
BeginDrawing();
ClearBackground(RAYWHITE);
DrawTexture(sprite, posX, 10, WHITE);
// DrawCircle(posX, 10, 5, GREEN);
EndDrawing();
}
StopMusicStream(music);
CloseAudioDevice();
CloseWindow();
return 0;
}
/*******************************************************************************************
*
* raylib [core] example - 2D Camera system
*
* Example originally created with raylib 1.5, last time updated with raylib 3.0
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2016-2024 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#define MAX_BUILDINGS 100
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera");
Rectangle player = { 400, 280, 40, 40 };
Rectangle buildings[MAX_BUILDINGS] = { 0 };
Color buildColors[MAX_BUILDINGS] = { 0 };
int spacing = 0;
for (int i = 0; i < MAX_BUILDINGS; i++)
{
buildings[i].width = (float)GetRandomValue(50, 200);
buildings[i].height = (float)GetRandomValue(100, 800);
buildings[i].y = screenHeight - 130.0f - buildings[i].height;
buildings[i].x = -6000.0f + spacing;
spacing += (int)buildings[i].width;
buildColors[i] = (Color){ GetRandomValue(200, 240), GetRandomValue(200, 240), GetRandomValue(200, 250), 255 };
}
Camera2D camera = { 0 };
camera.target = (Vector2){ player.x + 20.0f, player.y + 20.0f };
camera.offset = (Vector2){ screenWidth/2.0f, screenHeight/2.0f };
camera.rotation = 0.0f;
camera.zoom = 1.0f;
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// Player movement
if (IsKeyDown(KEY_RIGHT)) player.x += 2;
else if (IsKeyDown(KEY_LEFT)) player.x -= 2;
// Camera target follows player
camera.target = (Vector2){ player.x + 20, player.y + 20 };
// Camera rotation controls
if (IsKeyDown(KEY_A)) camera.rotation--;
else if (IsKeyDown(KEY_S)) camera.rotation++;
// Limit camera rotation to 80 degrees (-40 to 40)
if (camera.rotation > 40) camera.rotation = 40;
else if (camera.rotation < -40) camera.rotation = -40;
// Camera zoom controls
camera.zoom += ((float)GetMouseWheelMove()*0.05f);
if (camera.zoom > 3.0f) camera.zoom = 3.0f;
else if (camera.zoom < 0.1f) camera.zoom = 0.1f;
// Camera reset (zoom and rotation)
if (IsKeyPressed(KEY_R))
{
camera.zoom = 1.0f;
camera.rotation = 0.0f;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode2D(camera);
DrawRectangle(-6000, 320, 13000, 8000, DARKGRAY);
for (int i = 0; i < MAX_BUILDINGS; i++) DrawRectangleRec(buildings[i], buildColors[i]);
DrawRectangleRec(player, RED);
DrawLine((int)camera.target.x, -screenHeight*10, (int)camera.target.x, screenHeight*10, GREEN);
DrawLine(-screenWidth*10, (int)camera.target.y, screenWidth*10, (int)camera.target.y, GREEN);
EndMode2D();
DrawText("SCREEN AREA", 640, 10, 20, RED);
DrawRectangle(0, 0, screenWidth, 5, RED);
DrawRectangle(0, 5, 5, screenHeight - 10, RED);
DrawRectangle(screenWidth - 5, 5, 5, screenHeight - 10, RED);
DrawRectangle(0, screenHeight - 5, screenWidth, 5, RED);
DrawRectangle( 10, 10, 250, 113, Fade(SKYBLUE, 0.5f));
DrawRectangleLines( 10, 10, 250, 113, BLUE);
DrawText("Free 2d camera controls:", 20, 20, 10, BLACK);
DrawText("- Right/Left to move Offset", 40, 40, 10, DARKGRAY);
DrawText("- Mouse Wheel to Zoom in-out", 40, 60, 10, DARKGRAY);
DrawText("- A / S to Rotate", 40, 80, 10, DARKGRAY);
DrawText("- R to reset Zoom and Rotation", 40, 100, 10, DARKGRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
/*******************************************************************************************
*
* raylib [core] example - 2D Camera platformer
*
* Example originally created with raylib 2.5, last time updated with raylib 3.0
*
* Example contributed by arvyy (@arvyy) and reviewed by Ramon Santamaria (@raysan5)
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2019-2024 arvyy (@arvyy)
*
********************************************************************************************/
#include "raylib.h"
#include "raymath.h"
#define G 400
#define PLAYER_JUMP_SPD 350.0f
#define PLAYER_HOR_SPD 200.0f
typedef struct Player {
Vector2 position;
float speed;
bool canJump;
} Player;
typedef struct EnvItem {
Rectangle rect;
int blocking;
Color color;
} EnvItem;
//----------------------------------------------------------------------------------
// Module functions declaration
//----------------------------------------------------------------------------------
void UpdatePlayer(Player *player, EnvItem *envItems, int envItemsLength, float delta);
void UpdateCameraCenter(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height);
void UpdateCameraCenterInsideMap(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height);
void UpdateCameraCenterSmoothFollow(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height);
void UpdateCameraEvenOutOnLanding(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height);
void UpdateCameraPlayerBoundsPush(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height);
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera");
Player player = { 0 };
player.position = (Vector2){ 400, 280 };
player.speed = 0;
player.canJump = false;
EnvItem envItems[] = {
{{ 0, 0, 1000, 400 }, 0, LIGHTGRAY },
{{ 0, 400, 1000, 200 }, 1, GRAY },
{{ 300, 200, 400, 10 }, 1, GRAY },
{{ 250, 300, 100, 10 }, 1, GRAY },
{{ 650, 300, 100, 10 }, 1, GRAY }
};
int envItemsLength = sizeof(envItems)/sizeof(envItems[0]);
Camera2D camera = { 0 };
camera.target = player.position;
camera.offset = (Vector2){ screenWidth/2.0f, screenHeight/2.0f };
camera.rotation = 0.0f;
camera.zoom = 1.0f;
// Store pointers to the multiple update camera functions
void (*cameraUpdaters[])(Camera2D*, Player*, EnvItem*, int, float, int, int) = {
UpdateCameraCenter,
UpdateCameraCenterInsideMap,
UpdateCameraCenterSmoothFollow,
UpdateCameraEvenOutOnLanding,
UpdateCameraPlayerBoundsPush
};
int cameraOption = 0;
int cameraUpdatersLength = sizeof(cameraUpdaters)/sizeof(cameraUpdaters[0]);
char *cameraDescriptions[] = {
"Follow player center",
"Follow player center, but clamp to map edges",
"Follow player center; smoothed",
"Follow player center horizontally; update player center vertically after landing",
"Player push camera on getting too close to screen edge"
};
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
float deltaTime = GetFrameTime();
UpdatePlayer(&player, envItems, envItemsLength, deltaTime);
camera.zoom += ((float)GetMouseWheelMove()*0.05f);
if (camera.zoom > 3.0f) camera.zoom = 3.0f;
else if (camera.zoom < 0.25f) camera.zoom = 0.25f;
if (IsKeyPressed(KEY_R))
{
camera.zoom = 1.0f;
player.position = (Vector2){ 400, 280 };
}
if (IsKeyPressed(KEY_C)) cameraOption = (cameraOption + 1)%cameraUpdatersLength;
// Call update camera function by its pointer
cameraUpdaters[cameraOption](&camera, &player, envItems, envItemsLength, deltaTime, screenWidth, screenHeight);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(LIGHTGRAY);
BeginMode2D(camera);
for (int i = 0; i < envItemsLength; i++) DrawRectangleRec(envItems[i].rect, envItems[i].color);
Rectangle playerRect = { player.position.x - 20, player.position.y - 40, 40, 40 };
DrawRectangleRec(playerRect, RED);
DrawCircle(player.position.x, player.position.y, 5, GOLD);
EndMode2D();
DrawText("Controls:", 20, 20, 10, BLACK);
DrawText("- Right/Left to move", 40, 40, 10, DARKGRAY);
DrawText("- Space to jump", 40, 60, 10, DARKGRAY);
DrawText("- Mouse Wheel to Zoom in-out, R to reset zoom", 40, 80, 10, DARKGRAY);
DrawText("- C to change camera mode", 40, 100, 10, DARKGRAY);
DrawText("Current camera mode:", 20, 120, 10, BLACK);
DrawText(cameraDescriptions[cameraOption], 40, 140, 10, DARKGRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
void UpdatePlayer(Player *player, EnvItem *envItems, int envItemsLength, float delta)
{
if (IsKeyDown(KEY_LEFT)) player->position.x -= PLAYER_HOR_SPD*delta;
if (IsKeyDown(KEY_RIGHT)) player->position.x += PLAYER_HOR_SPD*delta;
if (IsKeyDown(KEY_SPACE) && player->canJump)
{
player->speed = -PLAYER_JUMP_SPD;
player->canJump = false;
}
bool hitObstacle = false;
for (int i = 0; i < envItemsLength; i++)
{
EnvItem *ei = envItems + i;
Vector2 *p = &(player->position);
if (ei->blocking &&
ei->rect.x <= p->x &&
ei->rect.x + ei->rect.width >= p->x &&
ei->rect.y >= p->y &&
ei->rect.y <= p->y + player->speed*delta)
{
hitObstacle = true;
player->speed = 0.0f;
p->y = ei->rect.y;
break;
}
}
if (!hitObstacle)
{
player->position.y += player->speed*delta;
player->speed += G*delta;
player->canJump = false;
}
else player->canJump = true;
}
void UpdateCameraCenter(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height)
{
camera->offset = (Vector2){ width/2.0f, height/2.0f };
camera->target = player->position;
}
void UpdateCameraCenterInsideMap(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height)
{
camera->target = player->position;
camera->offset = (Vector2){ width/2.0f, height/2.0f };
float minX = 1000, minY = 1000, maxX = -1000, maxY = -1000;
for (int i = 0; i < envItemsLength; i++)
{
EnvItem *ei = envItems + i;
minX = fminf(ei->rect.x, minX);
maxX = fmaxf(ei->rect.x + ei->rect.width, maxX);
minY = fminf(ei->rect.y, minY);
maxY = fmaxf(ei->rect.y + ei->rect.height, maxY);
}
Vector2 max = GetWorldToScreen2D((Vector2){ maxX, maxY }, *camera);
Vector2 min = GetWorldToScreen2D((Vector2){ minX, minY }, *camera);
if (max.x < width) camera->offset.x = width - (max.x - width/2);
if (max.y < height) camera->offset.y = height - (max.y - height/2);
if (min.x > 0) camera->offset.x = width/2 - min.x;
if (min.y > 0) camera->offset.y = height/2 - min.y;
}
void UpdateCameraCenterSmoothFollow(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height)
{
static float minSpeed = 30;
static float minEffectLength = 10;
static float fractionSpeed = 0.8f;
camera->offset = (Vector2){ width/2.0f, height/2.0f };
Vector2 diff = Vector2Subtract(player->position, camera->target);
float length = Vector2Length(diff);
if (length > minEffectLength)
{
float speed = fmaxf(fractionSpeed*length, minSpeed);
camera->target = Vector2Add(camera->target, Vector2Scale(diff, speed*delta/length));
}
}
void UpdateCameraEvenOutOnLanding(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height)
{
static float evenOutSpeed = 700;
static int eveningOut = false;
static float evenOutTarget;
camera->offset = (Vector2){ width/2.0f, height/2.0f };
camera->target.x = player->position.x;
if (eveningOut)
{
if (evenOutTarget > camera->target.y)
{
camera->target.y += evenOutSpeed*delta;
if (camera->target.y > evenOutTarget)
{
camera->target.y = evenOutTarget;
eveningOut = 0;
}
}
else
{
camera->target.y -= evenOutSpeed*delta;
if (camera->target.y < evenOutTarget)
{
camera->target.y = evenOutTarget;
eveningOut = 0;
}
}
}
else
{
if (player->canJump && (player->speed == 0) && (player->position.y != camera->target.y))
{
eveningOut = 1;
evenOutTarget = player->position.y;
}
}
}
void UpdateCameraPlayerBoundsPush(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height)
{
static Vector2 bbox = { 0.2f, 0.2f };
Vector2 bboxWorldMin = GetScreenToWorld2D((Vector2){ (1 - bbox.x)*0.5f*width, (1 - bbox.y)*0.5f*height }, *camera);
Vector2 bboxWorldMax = GetScreenToWorld2D((Vector2){ (1 + bbox.x)*0.5f*width, (1 + bbox.y)*0.5f*height }, *camera);
camera->offset = (Vector2){ (1 - bbox.x)*0.5f * width, (1 - bbox.y)*0.5f*height };
if (player->position.x < bboxWorldMin.x) camera->target.x = player->position.x;
if (player->position.y < bboxWorldMin.y) camera->target.y = player->position.y;
if (player->position.x > bboxWorldMax.x) camera->target.x = bboxWorldMin.x + (player->position.x - bboxWorldMax.x);
if (player->position.y > bboxWorldMax.y) camera->target.y = bboxWorldMin.y + (player->position.y - bboxWorldMax.y);
}
/*
Raylib example file.
This is an example main file for a simple C/C++raylib project.
*/
#include "raylib.h"
#include "raymath.h"
// define a timer
typedef struct
{
float Lifetime;
}Timer;
// start or restart a timer with a specific lifetime
void StartTimer(Timer* timer, float lifetime)
{
if (timer != NULL)
timer->Lifetime = lifetime;
}
// update a timer with the current frame time
void UpdateTimer(Timer* timer)
{
// subtract this frame from the timer if it's not allready expired
if (timer != NULL && timer->Lifetime > 0)
timer->Lifetime -= GetFrameTime();
}
// check if a timer is done.
bool TimerDone(Timer* timer)
{
if (timer != NULL)
return timer->Lifetime <= 0;
return false;
}
int main ()
{
// set up the window
InitWindow(1280, 800, "Raylib Timer Example");
SetTargetFPS(144);
// setup ball info
float radius = 20;
float speed = 400;
Vector2 pos = { radius, 400 };
Vector2 dir = { 1,0 };
float ballLife = 2.0f;
// a timer for the ball
Timer ballTimer = { 0 };
// game loop
while (!WindowShouldClose())
{
// check to see if the user clicked
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
{
// if they did, move the ball to the current position and restart the timer
pos = GetMousePosition();
StartTimer(&ballTimer, ballLife);
}
// tick our timer
UpdateTimer(&ballTimer);
// if the timer hasn't expired, move the ball
if (!TimerDone(&ballTimer))
{
// move the ball based on the speed and the frame time
pos = Vector2Add(pos, Vector2Scale(dir, GetFrameTime() * speed));
if (pos.x > GetScreenWidth() - radius) // check if we have gone over the right edge, and if so, bounce us
{
pos.x = GetScreenWidth() - radius;
dir.x = -1;
}
else if (pos.x < radius) // check if we have gone over the left edge
{
pos.x = radius;
dir.x = 1;
}
}
// drawing
BeginDrawing();
ClearBackground(BLACK);
// draw the ball where it is if the timer is alive
if (!TimerDone(&ballTimer))
DrawCircleV(pos, radius, RED);
EndDrawing();
}
// cleanup
CloseWindow();
return 0;
}
#include "raylib.h"
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <math.h>
#define SCALE_FACTOR 1.6
typedef struct {
int x;
int y;
int width;
int height;
int velocity;
int direction;
int speed;
bool jumping;
bool walking;
} Character;
typedef enum {
PLATFORM,
FLOOR,
} PlatformType;
typedef struct {
int x;
int y;
int width;
int height;
PlatformType type;
} Platform;
float rand_float() {
return (float) ( (float) rand() / (float) RAND_MAX );
}
int character_on_platform( Character character, Platform platforms[], int count ) {
for( int i = 0; i < count; i++ ) {
Rectangle platform_rec = {
.x = platforms[i].x,
.y = platforms[i].y,
.width = platforms[i].width,
.height = platforms[i].height,
};
Rectangle character_rec = {
.x = character.x + 10,
.y = character.y + character.height - character.height * 0.2,
.width = character.width - 15,
.height = character.height * 0.2 + 1,
};
if( CheckCollisionRecs( character_rec, platform_rec ) ) {
return i;
}
}
return -1;
}
int main() {
srand(time(NULL));
int window_width = 800 * SCALE_FACTOR;
int window_height = 600 * SCALE_FACTOR;
InitWindow( window_width, window_height, "My Game" );
SetTargetFPS( 60 );
Character character = {
.x = window_width/2,
.y = window_height/2,
.width = 101,
.height = 260,
.velocity = 4 * SCALE_FACTOR,
.speed = 6,
.walking = false,
.jumping = false,
};
int gravity = 2 * SCALE_FACTOR;
Vector2 camera_offset = {
.x = 0,
.y = 0,
};
Vector2 camera_target = {
.x = 0,
.y = 0,
};
Camera2D camera = {
.offset = camera_offset,
.target = camera_target,
.rotation = 0,
.zoom = 1,
};
float platform_spacing = 0.01;
int world_width = window_width * 10;
int platform_width = 180;
int platform_count = world_width / ( platform_width + platform_spacing * window_width );
int floor_piece_width = 490;
int floor_piece_height = 190;
int floor_piece_count = ceil( (float) world_width / (float) floor_piece_width );
int floor_whitespace = 33;
int platform_height = 50;
int platform_min_y = window_height * 0.2;
int platform_max_y = window_height - floor_piece_height - platform_height - platform_min_y;
int platform1_whitespace = 45;
int platform2_whitespace = 20;
platform_count += floor_piece_count;
Platform platforms[platform_count+1];
Texture2D background_texture = LoadTexture( "img/background.png" );
int background_width = 1792;
int background_overflow = background_width - window_width;
float background_ratio = 1 / ((float)(world_width - window_width) / (float)background_overflow);
int background_x = 0;
Image platform1_image = LoadImage( "img/platform1.png" );
Texture2D platform1_texture = LoadTextureFromImage( platform1_image );
UnloadImage( platform1_image );
Image platform2_image = LoadImage( "img/platform2.png" );
Texture2D platform2_texture = LoadTextureFromImage( platform2_image );
UnloadImage( platform2_image );
// Create floor
Image floor_image = LoadImage( "img/floor.png" );
Texture2D floor_piece_texture = LoadTextureFromImage( floor_image );
UnloadImage( floor_image );
int i = 0;
int floor_x = 0;
for( ; i < floor_piece_count; i++ ) {
platforms[i].x = floor_x;
platforms[i].y = window_height - floor_piece_height + floor_whitespace;
platforms[i].width = floor_piece_width;
platforms[i].height = floor_piece_height;
platforms[i].type = FLOOR;
floor_x += platforms[i].width;
}
int platform_x = window_width * 0.1;
for( ; i <= platform_count; i++ ) {
platforms[i].x = platform_x;
platforms[i].y = rand_float() * platform_max_y + platform_min_y;
platforms[i].width = platform_width;
platforms[i].height = platform_height;
platforms[i].type = PLATFORM;
platform_x += platforms[i].width + window_width * platform_spacing;
}
Image char_stand_img = LoadImage( "img/standing.png" );
Texture2D char_stand_right = LoadTextureFromImage( char_stand_img );
ImageFlipHorizontal( &char_stand_img );
Texture2D char_stand_left = LoadTextureFromImage( char_stand_img );
UnloadImage( char_stand_img );
Image char_jump_img = LoadImage( "img/jumping.png" );
Texture2D char_jump_right = LoadTextureFromImage( char_jump_img );
ImageFlipHorizontal( &char_jump_img );
Texture2D char_jump_left = LoadTextureFromImage( char_jump_img );
UnloadImage( char_jump_img );
Image char_walk1_img = LoadImage( "img/walk1.png" );
Texture2D char_walk1_right = LoadTextureFromImage( char_walk1_img );
ImageFlipHorizontal( &char_walk1_img );
Texture2D char_walk1_left = LoadTextureFromImage( char_walk1_img );
UnloadImage( char_walk1_img );
Image char_walk2_img = LoadImage( "img/walk2.png" );
Texture2D char_walk2_right = LoadTextureFromImage( char_walk2_img );
ImageFlipHorizontal( &char_walk2_img );
Texture2D char_walk2_left = LoadTextureFromImage( char_walk2_img );
UnloadImage( char_walk2_img );
while( ! WindowShouldClose() ) {
BeginDrawing();
character.walking = false;
if( character.x > window_width * 0.6 ) {
camera.offset.x = -(character.x - window_width * 0.6);
} else if( character.x < window_width * 0.4 ) {
camera.offset.x = -(character.x - window_width * 0.4);
}
if( camera.offset.x > 0 ) {
camera.offset.x = 0;
}
if( camera.offset.x < -(world_width - window_width) ) {
camera.offset.x = -(world_width - window_width);
}
BeginMode2D( camera );
background_x = -camera.offset.x;
background_x -= background_x * background_ratio;
character.y += character.velocity;
character.velocity += gravity;
int current_platform = character_on_platform(
character, platforms, platform_count
);
if( current_platform != -1 ) {
if( character.velocity > 0 ) {
character.y = platforms[current_platform].y - character.height;
character.velocity = 0;
character.jumping = false;
}
if( IsKeyPressed( KEY_SPACE ) ) {
character.velocity = -30 * SCALE_FACTOR;
character.jumping = true;
}
}
if( IsKeyDown( KEY_LEFT ) ) {
character.walking = true;
character.x -= character.speed * SCALE_FACTOR;
character.direction = -1;
}
if( IsKeyDown( KEY_RIGHT ) ) {
character.walking = true;
character.x += character.speed * SCALE_FACTOR;
character.direction = 1;
}
ClearBackground( WHITE );
DrawTexture( background_texture, background_x, 0, WHITE );
for( int i = 0; i < platform_count; i++ ) {
if( platforms[i].type == FLOOR ) {
DrawTexture( floor_piece_texture, platforms[i].x, platforms[i].y - floor_whitespace, WHITE );
} else {
Texture2D platform_texture = platform1_texture;
int whitespace = platform1_whitespace;
if( i % 2 == 0 ) {
platform_texture = platform2_texture;
whitespace = platform2_whitespace;
}
DrawTexture( platform_texture, platforms[i].x - 10, platforms[i].y - whitespace, WHITE );
}
}
Texture2D char_texture;
if( character.jumping ) {
if( character.direction == -1 ) {
char_texture = char_jump_left;
} else {
char_texture = char_jump_right;
}
} else if( character.walking ) {
double time = GetTime() * 10;
if( character.direction == -1 ) {
if( ((int) time) % 2 == 0 ) {
char_texture = char_walk1_left;
} else {
char_texture = char_walk2_left;
}
} else {
if( ((int) time) % 2 == 0 ) {
char_texture = char_walk1_right;
} else {
char_texture = char_walk2_right;
}
}
} else {
if( character.direction == -1 ) {
char_texture = char_stand_left;
} else {
char_texture = char_stand_right;
}
}
if( character.x < 0 ) {
character.x = 0;
}
if( character.x > world_width - character.width ) {
character.x = world_width - character.width;
}
DrawTexture( char_texture, character.x, character.y, WHITE );
EndMode2D();
EndDrawing();
}
UnloadTexture( char_stand_right );
UnloadTexture( char_stand_left );
UnloadTexture( char_jump_right );
UnloadTexture( char_jump_left );
UnloadTexture( char_walk1_right );
UnloadTexture( char_walk1_left );
UnloadTexture( char_walk2_right );
UnloadTexture( char_walk2_left );
UnloadTexture( floor_piece_texture );
UnloadTexture( platform1_texture );
UnloadTexture( platform2_texture );
UnloadTexture( background_texture );
return 0;
}
// Raylib Minesweeper
// Andrew Hamel Codes
// https://github.com/AndrewHamel111/raylib-minesweeper
// https://www.youtube.com/watch?v=3-EYrPRdjp4
#include <stdlib.h>
#include <time.h>
#include "raylib.h"
#include "raymath.h"
#define COLS 15
#define ROWS 15
const int screenWidth = 600;
const int screenHeight = 600;
const int cellWidth = screenWidth / COLS;
const int cellHeight = screenHeight / ROWS;
const char* youLose = "YOU LOSE!";
const char* youWin = "YOU WIN!";
const char* pressRToRestart = "Press 'r' to play again!";
typedef struct Cell
{
int i;
int j;
bool containsMine;
bool revealed;
bool flagged;
int nearbyMines;
} Cell;
Cell grid[COLS][ROWS];
Texture2D flagSprite;
int tilesRevealed;
int minesPresent;
typedef enum GameState
{
PLAYING,
LOSE,
WIN
} GameState;
GameState state;
float timeGameStarted;
float timeGameEnded;
void CellDraw(Cell);
bool IndexIsValid(int, int);
void CellReveal(int, int);
void CellFlag(int, int);
int CellCountMines(int, int);
void GridInit(void);
void GridFloodClearFrom(int, int);
void GameInit(void);
int main()
{
srand(time(0));
InitWindow(screenWidth, screenHeight, "Raylib Minesweeper by Andrew Hamel");
flagSprite = LoadTexture("resources/flag.png");
GameInit();
while(!WindowShouldClose())
{
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
{
Vector2 mPos = GetMousePosition();
int indexI = mPos.x / cellWidth;
int indexJ = mPos.y / cellHeight;
if (state == PLAYING && IndexIsValid(indexI, indexJ))
{
CellReveal(indexI, indexJ);
}
}
else if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT))
{
Vector2 mPos = GetMousePosition();
int indexI = mPos.x / cellWidth;
int indexJ = mPos.y / cellHeight;
if (state == PLAYING && IndexIsValid(indexI, indexJ))
{
CellFlag(indexI, indexJ);
}
}
if (IsKeyPressed(KEY_R))
{
GameInit();
}
BeginDrawing();
ClearBackground(RAYWHITE);
for (int i = 0; i < COLS; i++)
{
for (int j = 0; j < ROWS; j++)
{
CellDraw(grid[i][j]);
}
}
if (state == LOSE)
{
DrawRectangle(0, 0, screenWidth,screenHeight, Fade(WHITE, 0.8f));
DrawText(youLose, screenWidth / 2 - MeasureText(youLose, 40) / 2, screenHeight / 2 - 10, 40, DARKGRAY);
DrawText(pressRToRestart, screenWidth / 2 - MeasureText(pressRToRestart, 20) / 2, screenHeight * 0.75f - 10, 20, DARKGRAY);
int minutes = (int)(timeGameEnded - timeGameStarted) / 60;
int seconds = (int)(timeGameEnded - timeGameStarted) % 60;
DrawText(TextFormat("Time played: %d minutes, %d seconds.", minutes, seconds), 20, screenHeight - 40, 20, DARKGRAY);
}
if (state == WIN)
{
DrawRectangle(0, 0, screenWidth,screenHeight, Fade(WHITE, 0.8f));
DrawText(youWin, screenWidth / 2 - MeasureText(youWin, 40) / 2, screenHeight / 2 - 10, 40, DARKGRAY);
DrawText(pressRToRestart, screenWidth / 2 - MeasureText(pressRToRestart, 20) / 2, screenHeight * 0.75f - 10, 20, DARKGRAY);
int minutes = (int)(timeGameEnded - timeGameStarted) / 60;
int seconds = (int)(timeGameEnded - timeGameStarted) % 60;
DrawText(TextFormat("Time played: %d minutes, %d seconds.", minutes, seconds), 20, screenHeight - 40, 20, DARKGRAY);
}
EndDrawing();
}
CloseWindow();
return 0;
}
void CellDraw(Cell cell)
{
if (cell.revealed)
{
if (cell.containsMine)
{
DrawRectangle(cell.i * cellWidth, cell.j * cellHeight, cellWidth, cellHeight, RED);
}
else
{
DrawRectangle(cell.i * cellWidth, cell.j * cellHeight, cellWidth, cellHeight, LIGHTGRAY);
if (cell.nearbyMines > 0)
{
DrawText(TextFormat("%d", cell.nearbyMines), cell.i * cellWidth + 12, cell.j * cellHeight + 4, cellHeight - 8, DARKGRAY);
}
}
}
else if (cell.flagged)
{
// draw flag
Rectangle source = {0, 0, flagSprite.width, flagSprite.height};
Rectangle dest = {cell.i * cellWidth, cell.j * cellHeight, cellWidth, cellHeight};
Vector2 origin = {0, 0};
DrawTexturePro(flagSprite, source, dest, origin, 0.0f, Fade(WHITE, 0.5f));
}
DrawRectangleLines(cell.i * cellWidth, cell.j * cellHeight, cellWidth, cellHeight, BLACK);
}
bool IndexIsValid(int i, int j)
{
return i >= 0 && i < COLS && j >= 0 && j < ROWS;
}
void CellReveal(int i, int j)
{
if (grid[i][j].flagged || grid[i][j].revealed)
{
return;
}
grid[i][j].revealed = true;
if (grid[i][j].containsMine)
{
state = LOSE;
timeGameEnded = GetTime();
}
else
{
if (grid[i][j].nearbyMines == 0)
{
GridFloodClearFrom(i, j);
}
tilesRevealed++;
if (tilesRevealed >= ROWS * COLS - minesPresent)
{
state = WIN;
timeGameEnded = GetTime();
}
}
}
void CellFlag(int i, int j)
{
if (grid[i][j].revealed)
{
return;
}
grid[i][j].flagged = !grid[i][j].flagged;
}
int CellCountMines(int i, int j)
{
int count = 0;
for (int iOff = -1; iOff <= 1; iOff++)
{
for (int jOff = -1; jOff <= 1; jOff++)
{
if (iOff == 0 && jOff == 0)
{
continue;
}
if (!IndexIsValid(i + iOff, j + jOff))
{
continue;
}
if (grid[i + iOff][j + jOff].containsMine)
{
count++;
}
}
}
return count;
}
void GridInit(void)
{
for (int i = 0; i < COLS; i++)
{
for (int j = 0; j < ROWS; j++)
{
grid[i][j] = (Cell)
{
.i = i,
.j = j,
.containsMine = false,
.revealed = false,
.flagged = false,
.nearbyMines = -1
};
}
}
minesPresent = (int)(ROWS * COLS * 0.1f);
int minesToPlace = minesPresent;
while (minesToPlace > 0)
{
int i = rand() % COLS;
int j = rand() % ROWS;
if (!grid[i][j].containsMine)
{
grid[i][j].containsMine = true;
minesToPlace--;
}
}
for (int i = 0; i < COLS; i++)
{
for (int j = 0; j < ROWS; j++)
{
if (!grid[i][j].containsMine)
{
grid[i][j].nearbyMines = CellCountMines(i, j);
}
}
}
}
void GridFloodClearFrom(int i, int j)
{
for (int iOff = -1; iOff <= 1; iOff++)
{
for (int jOff = -1; jOff <= 1; jOff++)
{
if (iOff == 0 && jOff == 0)
{
continue;
}
if (!IndexIsValid(i + iOff, j + jOff))
{
continue;
}
CellReveal(i + iOff, j + jOff);
}
}
}
void GameInit(void)
{
GridInit();
state = PLAYING;
tilesRevealed = 0;
timeGameStarted = GetTime();
}
#************************************************************************************************** # # raylib makefile for Desktop platforms, Raspberry Pi, Android and HTML5 # # Copyright (c) 2013-2024 Ramon Santamaria (@raysan5) # # This software is provided "as-is", without any express or implied warranty. In no event # will the authors be held liable for any damages arising from the use of this software. # # Permission is granted to anyone to use this software for any purpose, including commercial # applications, and to alter it and redistribute it freely, subject to the following restrictions: # # 1. The origin of this software must not be misrepresented; you must not claim that you # wrote the original software. If you use this software in a product, an acknowledgment # in the product documentation would be appreciated but is not required. # # 2. Altered source versions must be plainly marked as such, and must not be misrepresented # as being the original software. # # 3. This notice may not be removed or altered from any source distribution. # #************************************************************************************************** .PHONY: all clean run # Define required environment variables #------------------------------------------------------------------------------------------------ # Define target platform: PLATFORM_DESKTOP, PLATFORM_RPI, PLATFORM_DRM, PLATFORM_ANDROID, PLATFORM_WEB PLATFORM ?= PLATFORM_DESKTOP # Define project variables PROJECT_NAME ?= raylib_game PROJECT_VERSION ?= 1.0 PROJECT_BUILD_PATH ?= . RAYLIB_PATH ?= ../../raylib # Locations of raylib.h and libraylib.a/libraylib.so # NOTE: Those variables are only used for PLATFORM_OS: LINUX, BSD RAYLIB_INCLUDE_PATH ?= /usr/local/include RAYLIB_LIB_PATH ?= /usr/local/lib # Library type compilation: STATIC (.a) or SHARED (.so/.dll) RAYLIB_LIBTYPE ?= STATIC # Build mode for project: DEBUG or RELEASE BUILD_MODE ?= RELEASE # Use Wayland display server protocol on Linux desktop (by default it uses X11 windowing system) # NOTE: This variable is only used for PLATFORM_OS: LINUX USE_WAYLAND_DISPLAY ?= FALSE # PLATFORM_WEB: Default properties BUILD_WEB_ASYNCIFY ?= FALSE BUILD_WEB_SHELL ?= minshell.html BUILD_WEB_HEAP_SIZE ?= 134217728 BUILD_WEB_RESOURCES ?= TRUE BUILD_WEB_RESOURCES_PATH ?= resources # Use cross-compiler for PLATFORM_RPI ifeq ($(PLATFORM),PLATFORM_RPI) USE_RPI_CROSS_COMPILER ?= FALSE ifeq ($(USE_RPI_CROSS_COMPILER),TRUE) RPI_TOOLCHAIN ?= C:/SysGCC/Raspberry RPI_TOOLCHAIN_SYSROOT ?= $(RPI_TOOLCHAIN)/arm-linux-gnueabihf/sysroot endif endif # Determine PLATFORM_OS in case PLATFORM_DESKTOP selected ifeq ($(PLATFORM),PLATFORM_DESKTOP) # No uname.exe on MinGW!, but OS=Windows_NT on Windows! # ifeq ($(UNAME),Msys) -> Windows ifeq ($(OS),Windows_NT) PLATFORM_OS = WINDOWS ifndef PLATFORM_SHELL PLATFORM_SHELL = cmd endif else UNAMEOS = $(shell uname) ifeq ($(UNAMEOS),Linux) PLATFORM_OS = LINUX endif ifeq ($(UNAMEOS),FreeBSD) PLATFORM_OS = BSD endif ifeq ($(UNAMEOS),OpenBSD) PLATFORM_OS = BSD endif ifeq ($(UNAMEOS),NetBSD) PLATFORM_OS = BSD endif ifeq ($(UNAMEOS),DragonFly) PLATFORM_OS = BSD endif ifeq ($(UNAMEOS),Darwin) PLATFORM_OS = OSX endif ifndef PLATFORM_SHELL PLATFORM_SHELL = sh endif endif endif ifeq ($(PLATFORM),PLATFORM_RPI) UNAMEOS = $(shell uname) ifeq ($(UNAMEOS),Linux) PLATFORM_OS = LINUX endif ifndef PLATFORM_SHELL PLATFORM_SHELL = sh endif endif ifeq ($(PLATFORM),PLATFORM_DRM) UNAMEOS = $(shell uname) ifeq ($(UNAMEOS),Linux) PLATFORM_OS = LINUX endif ifndef PLATFORM_SHELL PLATFORM_SHELL = sh endif endif ifeq ($(PLATFORM),PLATFORM_WEB) ifeq ($(OS),Windows_NT) PLATFORM_OS = WINDOWS ifndef PLATFORM_SHELL PLATFORM_SHELL = cmd endif else UNAMEOS = $(shell uname) ifeq ($(UNAMEOS),Linux) PLATFORM_OS = LINUX endif ifeq ($(UNAMEOS),Darwin) PLATFORM_OS = OSX endif ifndef PLATFORM_SHELL PLATFORM_SHELL = sh endif endif endif # Default path for raylib on Raspberry Pi ifeq ($(PLATFORM),PLATFORM_RPI) RAYLIB_PATH ?= /home/pi/raylib endif ifeq ($(PLATFORM),PLATFORM_DRM) RAYLIB_PATH ?= /home/pi/raylib endif # Define raylib release directory for compiled library RAYLIB_RELEASE_PATH ?= $(RAYLIB_PATH)/src ifeq ($(OS),Windows_NT) ifeq ($(PLATFORM),PLATFORM_WEB) # Emscripten required variables EMSDK_PATH ?= C:/emsdk EMSCRIPTEN_PATH ?= $(EMSDK_PATH)/upstream/emscripten CLANG_PATH = $(EMSDK_PATH)/upstream/bin PYTHON_PATH = $(EMSDK_PATH)/python/3.9.2-nuget_64bit NODE_PATH = $(EMSDK_PATH)/node/14.15.5_64bit/bin export PATH = $(EMSDK_PATH);$(EMSCRIPTEN_PATH);$(CLANG_PATH);$(NODE_PATH);$(PYTHON_PATH):$$(PATH) endif endif # Define default C compiler: CC #------------------------------------------------------------------------------------------------ CC = gcc ifeq ($(PLATFORM),PLATFORM_DESKTOP) ifeq ($(PLATFORM_OS),OSX) # OSX default compiler CC = clang endif ifeq ($(PLATFORM_OS),BSD) # FreeBSD, OpenBSD, NetBSD, DragonFly default compiler CC = clang endif endif ifeq ($(PLATFORM),PLATFORM_RPI) ifeq ($(USE_RPI_CROSS_COMPILER),TRUE) # Define RPI cross-compiler #CC = armv6j-hardfloat-linux-gnueabi-gcc CC = $(RPI_TOOLCHAIN)/bin/arm-linux-gnueabihf-gcc endif endif ifeq ($(PLATFORM),PLATFORM_WEB) # HTML5 emscripten compiler # WARNING: To compile to HTML5, code must be redesigned # to use emscripten.h and emscripten_set_main_loop() CC = emcc endif # Define default make program: MAKE #------------------------------------------------------------------------------------------------ MAKE ?= make ifeq ($(PLATFORM),PLATFORM_DESKTOP) ifeq ($(PLATFORM_OS),WINDOWS) MAKE = mingw32-make endif endif ifeq ($(PLATFORM),PLATFORM_ANDROID) MAKE = mingw32-make endif # Define compiler flags: CFLAGS #------------------------------------------------------------------------------------------------ # -O1 defines optimization level # -g include debug information on compilation # -s strip unnecessary data from build # -Wall turns on most, but not all, compiler warnings # -std=c99 defines C language mode (standard C from 1999 revision) # -std=gnu99 defines C language mode (GNU C from 1999 revision) # -Wno-missing-braces ignore invalid warning (GCC bug 53119) # -Wno-unused-value ignore unused return values of some functions (i.e. fread()) # -D_DEFAULT_SOURCE use with -std=c99 on Linux and PLATFORM_WEB, required for timespec CFLAGS = -std=c99 -Wall -Wno-missing-braces -Wunused-result -D_DEFAULT_SOURCE ifeq ($(BUILD_MODE),DEBUG) CFLAGS += -g -D_DEBUG else ifeq ($(PLATFORM),PLATFORM_WEB) ifeq ($(BUILD_WEB_ASYNCIFY),TRUE) CFLAGS += -O3 else CFLAGS += -Os endif else CFLAGS += -s -O2 endif endif # Additional flags for compiler (if desired) #CFLAGS += -Wextra -Wmissing-prototypes -Wstrict-prototypes ifeq ($(PLATFORM),PLATFORM_DESKTOP) ifeq ($(PLATFORM_OS),LINUX) ifeq ($(RAYLIB_LIBTYPE),STATIC) CFLAGS += -D_DEFAULT_SOURCE endif ifeq ($(RAYLIB_LIBTYPE),SHARED) # Explicitly enable runtime link to libraylib.so CFLAGS += -Wl,-rpath,$(RAYLIB_RELEASE_PATH) endif endif endif ifeq ($(PLATFORM),PLATFORM_RPI) CFLAGS += -std=gnu99 endif ifeq ($(PLATFORM),PLATFORM_DRM) CFLAGS += -std=gnu99 -DEGL_NO_X11 endif # Define include paths for required headers: INCLUDE_PATHS # NOTE: Some external/extras libraries could be required (stb, physac, easings...) #------------------------------------------------------------------------------------------------ INCLUDE_PATHS = -I. -I$(RAYLIB_PATH)/src -I$(RAYLIB_PATH)/src/external -I$(RAYLIB_PATH)/src/extras # Define additional directories containing required header files ifeq ($(PLATFORM),PLATFORM_DESKTOP) ifeq ($(PLATFORM_OS),BSD) INCLUDE_PATHS += -I$(RAYLIB_INCLUDE_PATH) endif ifeq ($(PLATFORM_OS),LINUX) INCLUDE_PATHS += -I$(RAYLIB_INCLUDE_PATH) endif endif ifeq ($(PLATFORM),PLATFORM_RPI) INCLUDE_PATHS += -I$(RPI_TOOLCHAIN_SYSROOT)/opt/vc/include INCLUDE_PATHS += -I$(RPI_TOOLCHAIN_SYSROOT)/opt/vc/include/interface/vmcs_host/linux INCLUDE_PATHS += -I$(RPI_TOOLCHAIN_SYSROOT)/opt/vc/include/interface/vcos/pthreads endif ifeq ($(PLATFORM),PLATFORM_DRM) INCLUDE_PATHS += -I/usr/include/libdrm endif ifeq ($(PLATFORM),PLATFORM_WEB) INCLUDE_PATHS += -I$(EMSCRIPTEN_PATH)/cache/sysroot/include endif # Define library paths containing required libs: LDFLAGS #------------------------------------------------------------------------------------------------ LDFLAGS = -L. -L$(RAYLIB_RELEASE_PATH) -L$(RAYLIB_PATH)/src ifeq ($(PLATFORM),PLATFORM_DESKTOP) ifeq ($(PLATFORM_OS),WINDOWS) # NOTE: The resource .rc file contains windows executable icon and properties LDFLAGS += $(RAYLIB_PATH)/src/raylib.rc.data # -Wl,--subsystem,windows hides the console window ifeq ($(BUILD_MODE), RELEASE) LDFLAGS += -Wl,--subsystem,windows endif endif ifeq ($(PLATFORM_OS),LINUX) LDFLAGS += -L$(RAYLIB_LIB_PATH) endif ifeq ($(PLATFORM_OS),BSD) LDFLAGS += -Lsrc -L$(RAYLIB_LIB_PATH) endif endif ifeq ($(PLATFORM),PLATFORM_WEB) # -Os # size optimization # -O2 # optimization level 2, if used, also set --memory-init-file 0 # -s USE_GLFW=3 # Use glfw3 library (context/input management) # -s ALLOW_MEMORY_GROWTH=1 # to allow memory resizing -> WARNING: Audio buffers could FAIL! # -s TOTAL_MEMORY=16777216 # to specify heap memory size (default = 16MB) (67108864 = 64MB) # -s USE_PTHREADS=1 # multithreading support # -s WASM=0 # disable Web Assembly, emitted by default # -s ASYNCIFY # lets synchronous C/C++ code interact with asynchronous JS # -s FORCE_FILESYSTEM=1 # force filesystem to load/save files data # -s ASSERTIONS=1 # enable runtime checks for common memory allocation errors (-O1 and above turn it off) # --profiling # include information for code profiling # --memory-init-file 0 # to avoid an external memory initialization code file (.mem) # --preload-file resources # specify a resources folder for data compilation # --source-map-base # allow debugging in browser with source map LDFLAGS += -s USE_GLFW=3 -s TOTAL_MEMORY=$(BUILD_WEB_HEAP_SIZE) -s FORCE_FILESYSTEM=1 # Build using asyncify ifeq ($(BUILD_WEB_ASYNCIFY),TRUE) LDFLAGS += -s ASYNCIFY endif # Add resources building if required ifeq ($(BUILD_WEB_RESOURCES),TRUE) LDFLAGS += --preload-file $(BUILD_WEB_RESOURCES_PATH) endif # Add debug mode flags if required ifeq ($(BUILD_MODE),DEBUG) LDFLAGS += -s ASSERTIONS=1 --profiling endif # Define a custom shell .html and output extension LDFLAGS += --shell-file $(BUILD_WEB_SHELL) EXT = .html endif ifeq ($(PLATFORM),PLATFORM_RPI) LDFLAGS += -L$(RPI_TOOLCHAIN_SYSROOT)/opt/vc/lib endif # Define libraries required on linking: LDLIBS # NOTE: To link libraries (lib<name>.so or lib<name>.a), use -l<name> #------------------------------------------------------------------------------------------------ ifeq ($(PLATFORM),PLATFORM_DESKTOP) ifeq ($(PLATFORM_OS),WINDOWS) # Libraries for Windows desktop compilation # NOTE: WinMM library required to set high-res timer resolution LDLIBS = -lraylib -lopengl32 -lgdi32 -lwinmm # Required for physac examples LDLIBS += -static -lpthread endif ifeq ($(PLATFORM_OS),LINUX) # Libraries for Debian GNU/Linux desktop compiling # NOTE: Required packages: libegl1-mesa-dev LDLIBS = -lraylib -lGL -lm -lpthread -ldl -lrt # On X11 requires also below libraries LDLIBS += -lX11 # NOTE: It seems additional libraries are not required any more, latest GLFW just dlopen them #LDLIBS += -lXrandr -lXinerama -lXi -lXxf86vm -lXcursor # On Wayland windowing system, additional libraries requires ifeq ($(USE_WAYLAND_DISPLAY),TRUE) LDLIBS += -lwayland-client -lwayland-cursor -lwayland-egl -lxkbcommon endif # Explicit link to libc ifeq ($(RAYLIB_LIBTYPE),SHARED) LDLIBS += -lc endif endif ifeq ($(PLATFORM_OS),OSX) # Libraries for OSX 10.9 desktop compiling # NOTE: Required packages: libopenal-dev libegl1-mesa-dev LDLIBS = -lraylib -framework OpenGL -framework Cocoa -framework IOKit -framework CoreAudio -framework CoreVideo endif ifeq ($(PLATFORM_OS),BSD) # Libraries for FreeBSD, OpenBSD, NetBSD, DragonFly desktop compiling # NOTE: Required packages: mesa-libs LDLIBS = -lraylib -lGL -lpthread -lm # On XWindow requires also below libraries LDLIBS += -lX11 -lXrandr -lXinerama -lXi -lXxf86vm -lXcursor endif endif ifeq ($(PLATFORM),PLATFORM_RPI) # Libraries for Raspberry Pi compiling # NOTE: Required packages: libasound2-dev (ALSA) LDLIBS = -lraylib -lbrcmGLESv2 -lbrcmEGL -lpthread -lrt -lm -lbcm_host -ldl ifeq ($(USE_RPI_CROSS_COMPILER),TRUE) LDLIBS += -lvchiq_arm -lvcos endif endif ifeq ($(PLATFORM),PLATFORM_DRM) # Libraries for DRM compiling # NOTE: Required packages: libasound2-dev (ALSA) LDLIBS = -lraylib -lGLESv2 -lEGL -lpthread -lrt -lm -lgbm -ldrm -ldl endif ifeq ($(PLATFORM),PLATFORM_WEB) # Libraries for web (HTML5) compiling LDLIBS = $(RAYLIB_RELEASE_PATH)/libraylib.a endif # Define source code object files required #------------------------------------------------------------------------------------------------ PROJECT_SOURCE_FILES ?= \ raylib_game.c \ screen_logo.c \ screen_title.c \ screen_options.c \ screen_gameplay.c \ screen_ending.c # Define all object files from source files OBJS = $(patsubst %.c, %.o, $(PROJECT_SOURCE_FILES)) # Define processes to execute #------------------------------------------------------------------------------------------------ # For Android platform we call a custom Makefile.Android ifeq ($(PLATFORM),PLATFORM_ANDROID) MAKEFILE_PARAMS = -f Makefile.Android export PROJECT_NAME export PROJECT_SOURCE_FILES else MAKEFILE_PARAMS = $(PROJECT_NAME) endif # Default target entry # NOTE: We call this Makefile target or Makefile.Android target all: $(MAKE) $(MAKEFILE_PARAMS) # Project target defined by PROJECT_NAME $(PROJECT_NAME): $(OBJS) $(CC) -o $(PROJECT_NAME)$(EXT) $(OBJS) $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM) # Compile source files # NOTE: This pattern will compile every module defined on $(OBJS) %.o: %.c $(CC) -c $< -o $@ $(CFLAGS) $(INCLUDE_PATHS) -D$(PLATFORM) run: $(MAKE) $(MAKEFILE_PARAMS) ifeq ($(PLATFORM),PLATFORM_DESKTOP) ifeq ($(PLATFORM_OS),WINDOWS) $(PROJECT_NAME) endif ifeq ($(PLATFORM_OS),LINUX) ./$(PROJECT_NAME) endif ifeq ($(PLATFORM_OS),OSX) ./$(PROJECT_NAME) endif endif .PHONY: clean_shell_cmd clean_shell_sh # Clean everything clean: clean_shell_$(PLATFORM_SHELL) @echo Cleaning done clean_shell_sh: ifeq ($(PLATFORM),PLATFORM_DESKTOP) ifeq ($(PLATFORM_OS),LINUX) find . -type f -executable -delete rm -fv *.o endif ifeq ($(PLATFORM_OS),OSX) find . -type f -perm +ugo+x -delete rm -f *.o endif endif ifeq ($(PLATFORM),PLATFORM_RPI) find . -type f -executable -delete rm -fv *.o endif ifeq ($(PLATFORM),PLATFORM_DRM) find . -type f -executable -delete rm -fv *.o endif ifeq ($(PLATFORM),PLATFORM_WEB) ifeq ($(PLATFORM_OS),LINUX) rm -fv *.o $(PROJECT_NAME).data $(PROJECT_NAME).html $(PROJECT_NAME).js $(PROJECT_NAME).wasm endif ifeq ($(PLATFORM_OS),OSX) rm -f *.o $(PROJECT_NAME).data $(PROJECT_NAME).html $(PROJECT_NAME).js $(PROJECT_NAME).wasm endif endif # Set specific target variable clean_shell_cmd: SHELL=cmd clean_shell_cmd: del *.o *.exe $(PROJECT_NAME).data $(PROJECT_NAME).html $(PROJECT_NAME).js $(PROJECT_NAME).wasm /s
module: rcore
// Window-related functions
void InitWindow(int width, int height, const char *title); // Initialize window and OpenGL context
void CloseWindow(void); // Close window and unload OpenGL context
bool WindowShouldClose(void); // Check if application should close (KEY_ESCAPE pressed or windows close icon clicked)
bool IsWindowReady(void); // Check if window has been initialized successfully
bool IsWindowFullscreen(void); // Check if window is currently fullscreen
bool IsWindowHidden(void); // Check if window is currently hidden (only PLATFORM_DESKTOP)
bool IsWindowMinimized(void); // Check if window is currently minimized (only PLATFORM_DESKTOP)
bool IsWindowMaximized(void); // Check if window is currently maximized (only PLATFORM_DESKTOP)
bool IsWindowFocused(void); // Check if window is currently focused (only PLATFORM_DESKTOP)
bool IsWindowResized(void); // Check if window has been resized last frame
bool IsWindowState(unsigned int flag); // Check if one specific window flag is enabled
void SetWindowState(unsigned int flags); // Set window configuration state using flags (only PLATFORM_DESKTOP)
void ClearWindowState(unsigned int flags); // Clear window configuration state flags
void ToggleFullscreen(void); // Toggle window state: fullscreen/windowed (only PLATFORM_DESKTOP)
void ToggleBorderlessWindowed(void); // Toggle window state: borderless windowed (only PLATFORM_DESKTOP)
void MaximizeWindow(void); // Set window state: maximized, if resizable (only PLATFORM_DESKTOP)
void MinimizeWindow(void); // Set window state: minimized, if resizable (only PLATFORM_DESKTOP)
void RestoreWindow(void); // Set window state: not minimized/maximized (only PLATFORM_DESKTOP)
void SetWindowIcon(Image image); // Set icon for window (single image, RGBA 32bit, only PLATFORM_DESKTOP)
void SetWindowIcons(Image *images, int count); // Set icon for window (multiple images, RGBA 32bit, only PLATFORM_DESKTOP)
void SetWindowTitle(const char *title); // Set title for window (only PLATFORM_DESKTOP and PLATFORM_WEB)
void SetWindowPosition(int x, int y); // Set window position on screen (only PLATFORM_DESKTOP)
void SetWindowMonitor(int monitor); // Set monitor for the current window
void SetWindowMinSize(int width, int height); // Set window minimum dimensions (for FLAG_WINDOW_RESIZABLE)
void SetWindowMaxSize(int width, int height); // Set window maximum dimensions (for FLAG_WINDOW_RESIZABLE)
void SetWindowSize(int width, int height); // Set window dimensions
void SetWindowOpacity(float opacity); // Set window opacity [0.0f..1.0f] (only PLATFORM_DESKTOP)
void SetWindowFocused(void); // Set window focused (only PLATFORM_DESKTOP)
void *GetWindowHandle(void); // Get native window handle
int GetScreenWidth(void); // Get current screen width
int GetScreenHeight(void); // Get current screen height
int GetRenderWidth(void); // Get current render width (it considers HiDPI)
int GetRenderHeight(void); // Get current render height (it considers HiDPI)
int GetMonitorCount(void); // Get number of connected monitors
int GetCurrentMonitor(void); // Get current connected monitor
Vector2 GetMonitorPosition(int monitor); // Get specified monitor position
int GetMonitorWidth(int monitor); // Get specified monitor width (current video mode used by monitor)
int GetMonitorHeight(int monitor); // Get specified monitor height (current video mode used by monitor)
int GetMonitorPhysicalWidth(int monitor); // Get specified monitor physical width in millimetres
int GetMonitorPhysicalHeight(int monitor); // Get specified monitor physical height in millimetres
int GetMonitorRefreshRate(int monitor); // Get specified monitor refresh rate
Vector2 GetWindowPosition(void); // Get window position XY on monitor
Vector2 GetWindowScaleDPI(void); // Get window scale DPI factor
const char *GetMonitorName(int monitor); // Get the human-readable, UTF-8 encoded name of the specified monitor
void SetClipboardText(const char *text); // Set clipboard text content
const char *GetClipboardText(void); // Get clipboard text content
void EnableEventWaiting(void); // Enable waiting for events on EndDrawing(), no automatic event polling
void DisableEventWaiting(void); // Disable waiting for events on EndDrawing(), automatic events polling
// Cursor-related functions
void ShowCursor(void); // Shows cursor
void HideCursor(void); // Hides cursor
bool IsCursorHidden(void); // Check if cursor is not visible
void EnableCursor(void); // Enables cursor (unlock cursor)
void DisableCursor(void); // Disables cursor (lock cursor)
bool IsCursorOnScreen(void); // Check if cursor is on the screen
// Drawing-related functions
void ClearBackground(Color color); // Set background color (framebuffer clear color)
void BeginDrawing(void); // Setup canvas (framebuffer) to start drawing
void EndDrawing(void); // End canvas drawing and swap buffers (double buffering)
void BeginMode2D(Camera2D camera); // Begin 2D mode with custom camera (2D)
void EndMode2D(void); // Ends 2D mode with custom camera
void BeginMode3D(Camera3D camera); // Begin 3D mode with custom camera (3D)
void EndMode3D(void); // Ends 3D mode and returns to default 2D orthographic mode
void BeginTextureMode(RenderTexture2D target); // Begin drawing to render texture
void EndTextureMode(void); // Ends drawing to render texture
void BeginShaderMode(Shader shader); // Begin custom shader drawing
void EndShaderMode(void); // End custom shader drawing (use default shader)
void BeginBlendMode(int mode); // Begin blending mode (alpha, additive, multiplied, subtract, custom)
void EndBlendMode(void); // End blending mode (reset to default: alpha blending)
void BeginScissorMode(int x, int y, int width, int height); // Begin scissor mode (define screen area for following drawing)
void EndScissorMode(void); // End scissor mode
void BeginVrStereoMode(VrStereoConfig config); // Begin stereo rendering (requires VR simulator)
void EndVrStereoMode(void); // End stereo rendering (requires VR simulator)
// VR stereo config functions for VR simulator
VrStereoConfig LoadVrStereoConfig(VrDeviceInfo device); // Load VR stereo config for VR simulator device parameters
void UnloadVrStereoConfig(VrStereoConfig config); // Unload VR stereo config
// Shader management functions
// NOTE: Shader functionality is not available on OpenGL 1.1
Shader LoadShader(const char *vsFileName, const char *fsFileName); // Load shader from files and bind default locations
Shader LoadShaderFromMemory(const char *vsCode, const char *fsCode); // Load shader from code strings and bind default locations
bool IsShaderReady(Shader shader); // Check if a shader is ready
int GetShaderLocation(Shader shader, const char *uniformName); // Get shader uniform location
int GetShaderLocationAttrib(Shader shader, const char *attribName); // Get shader attribute location
void SetShaderValue(Shader shader, int locIndex, const void *value, int uniformType); // Set shader uniform value
void SetShaderValueV(Shader shader, int locIndex, const void *value, int uniformType, int count); // Set shader uniform value vector
void SetShaderValueMatrix(Shader shader, int locIndex, Matrix mat); // Set shader uniform value (matrix 4x4)
void SetShaderValueTexture(Shader shader, int locIndex, Texture2D texture); // Set shader uniform value for texture (sampler2d)
void UnloadShader(Shader shader); // Unload shader from GPU memory (VRAM)
// Screen-space-related functions
Ray GetMouseRay(Vector2 mousePosition, Camera camera); // Get a ray trace from mouse position
Matrix GetCameraMatrix(Camera camera); // Get camera transform matrix (view matrix)
Matrix GetCameraMatrix2D(Camera2D camera); // Get camera 2d transform matrix
Vector2 GetWorldToScreen(Vector3 position, Camera camera); // Get the screen space position for a 3d world space position
Vector2 GetScreenToWorld2D(Vector2 position, Camera2D camera); // Get the world space position for a 2d camera screen space position
Vector2 GetWorldToScreenEx(Vector3 position, Camera camera, int width, int height); // Get size position for a 3d world space position
Vector2 GetWorldToScreen2D(Vector2 position, Camera2D camera); // Get the screen space position for a 2d camera world space position
// Timing-related functions
void SetTargetFPS(int fps); // Set target FPS (maximum)
float GetFrameTime(void); // Get time in seconds for last frame drawn (delta time)
double GetTime(void); // Get elapsed time in seconds since InitWindow()
int GetFPS(void); // Get current FPS
// Custom frame control functions
// NOTE: Those functions are intended for advance users that want full control over the frame processing
// By default EndDrawing() does this job: draws everything + SwapScreenBuffer() + manage frame timing + PollInputEvents()
// To avoid that behaviour and control frame processes manually, enable in config.h: SUPPORT_CUSTOM_FRAME_CONTROL
void SwapScreenBuffer(void); // Swap back buffer with front buffer (screen drawing)
void PollInputEvents(void); // Register all input events
void WaitTime(double seconds); // Wait for some time (halt program execution)
// Random values generation functions
void SetRandomSeed(unsigned int seed); // Set the seed for the random number generator
int GetRandomValue(int min, int max); // Get a random value between min and max (both included)
int *LoadRandomSequence(unsigned int count, int min, int max); // Load random values sequence, no values repeated
void UnloadRandomSequence(int *sequence); // Unload random values sequence
// Misc. functions
void TakeScreenshot(const char *fileName); // Takes a screenshot of current screen (filename extension defines format)
void SetConfigFlags(unsigned int flags); // Setup init configuration flags (view FLAGS)
void OpenURL(const char *url); // Open URL with default system browser (if available)
// NOTE: Following functions implemented in module [utils]
//------------------------------------------------------------------
void TraceLog(int logLevel, const char *text, ...); // Show trace log messages (LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERROR...)
void SetTraceLogLevel(int logLevel); // Set the current threshold (minimum) log level
void *MemAlloc(unsigned int size); // Internal memory allocator
void *MemRealloc(void *ptr, unsigned int size); // Internal memory reallocator
void MemFree(void *ptr); // Internal memory free
// Set custom callbacks
// WARNING: Callbacks setup is intended for advance users
void SetTraceLogCallback(TraceLogCallback callback); // Set custom trace log
void SetLoadFileDataCallback(LoadFileDataCallback callback); // Set custom file binary data loader
void SetSaveFileDataCallback(SaveFileDataCallback callback); // Set custom file binary data saver
void SetLoadFileTextCallback(LoadFileTextCallback callback); // Set custom file text data loader
void SetSaveFileTextCallback(SaveFileTextCallback callback); // Set custom file text data saver
// Files management functions
unsigned char *LoadFileData(const char *fileName, int *dataSize); // Load file data as byte array (read)
void UnloadFileData(unsigned char *data); // Unload file data allocated by LoadFileData()
bool SaveFileData(const char *fileName, void *data, int dataSize); // Save data to file from byte array (write), returns true on success
bool ExportDataAsCode(const unsigned char *data, int dataSize, const char *fileName); // Export data to code (.h), returns true on success
char *LoadFileText(const char *fileName); // Load text data from file (read), returns a '\0' terminated string
void UnloadFileText(char *text); // Unload file text data allocated by LoadFileText()
bool SaveFileText(const char *fileName, char *text); // Save text data to file (write), string must be '\0' terminated, returns true on success
//------------------------------------------------------------------
// File system functions
bool FileExists(const char *fileName); // Check if file exists
bool DirectoryExists(const char *dirPath); // Check if a directory path exists
bool IsFileExtension(const char *fileName, const char *ext); // Check file extension (including point: .png, .wav)
int GetFileLength(const char *fileName); // Get file length in bytes (NOTE: GetFileSize() conflicts with windows.h)
const char *GetFileExtension(const char *fileName); // Get pointer to extension for a filename string (includes dot: '.png')
const char *GetFileName(const char *filePath); // Get pointer to filename for a path string
const char *GetFileNameWithoutExt(const char *filePath); // Get filename string without extension (uses static string)
const char *GetDirectoryPath(const char *filePath); // Get full path for a given fileName with path (uses static string)
const char *GetPrevDirectoryPath(const char *dirPath); // Get previous directory path for a given path (uses static string)
const char *GetWorkingDirectory(void); // Get current working directory (uses static string)
const char *GetApplicationDirectory(void); // Get the directory of the running application (uses static string)
bool ChangeDirectory(const char *dir); // Change working directory, return true on success
bool IsPathFile(const char *path); // Check if a given path is a file or a directory
FilePathList LoadDirectoryFiles(const char *dirPath); // Load directory filepaths
FilePathList LoadDirectoryFilesEx(const char *basePath, const char *filter, bool scanSubdirs); // Load directory filepaths with extension filtering and recursive directory scan
void UnloadDirectoryFiles(FilePathList files); // Unload filepaths
bool IsFileDropped(void); // Check if a file has been dropped into window
FilePathList LoadDroppedFiles(void); // Load dropped filepaths
void UnloadDroppedFiles(FilePathList files); // Unload dropped filepaths
long GetFileModTime(const char *fileName); // Get file modification time (last write time)
// Compression/Encoding functionality
unsigned char *CompressData(const unsigned char *data, int dataSize, int *compDataSize); // Compress data (DEFLATE algorithm), memory must be MemFree()
unsigned char *DecompressData(const unsigned char *compData, int compDataSize, int *dataSize); // Decompress data (DEFLATE algorithm), memory must be MemFree()
char *EncodeDataBase64(const unsigned char *data, int dataSize, int *outputSize); // Encode data to Base64 string, memory must be MemFree()
unsigned char *DecodeDataBase64(const unsigned char *data, int *outputSize); // Decode Base64 string data, memory must be MemFree()
// Automation events functionality
AutomationEventList LoadAutomationEventList(const char *fileName); // Load automation events list from file, NULL for empty list, capacity = MAX_AUTOMATION_EVENTS
void UnloadAutomationEventList(AutomationEventList *list); // Unload automation events list from file
bool ExportAutomationEventList(AutomationEventList list, const char *fileName); // Export automation events list as text file
void SetAutomationEventList(AutomationEventList *list); // Set automation event list to record to
void SetAutomationEventBaseFrame(int frame); // Set automation event internal base frame to start recording
void StartAutomationEventRecording(void); // Start recording automation events (AutomationEventList must be set)
void StopAutomationEventRecording(void); // Stop recording automation events
void PlayAutomationEvent(AutomationEvent event); // Play a recorded automation event
//------------------------------------------------------------------------------------
// Input Handling Functions (Module: core)
//------------------------------------------------------------------------------------
// Input-related functions: keyboard
bool IsKeyPressed(int key); // Check if a key has been pressed once
bool IsKeyPressedRepeat(int key); // Check if a key has been pressed again (Only PLATFORM_DESKTOP)
bool IsKeyDown(int key); // Check if a key is being pressed
bool IsKeyReleased(int key); // Check if a key has been released once
bool IsKeyUp(int key); // Check if a key is NOT being pressed
int GetKeyPressed(void); // Get key pressed (keycode), call it multiple times for keys queued, returns 0 when the queue is empty
int GetCharPressed(void); // Get char pressed (unicode), call it multiple times for chars queued, returns 0 when the queue is empty
void SetExitKey(int key); // Set a custom key to exit program (default is ESC)
// Input-related functions: gamepads
bool IsGamepadAvailable(int gamepad); // Check if a gamepad is available
const char *GetGamepadName(int gamepad); // Get gamepad internal name id
bool IsGamepadButtonPressed(int gamepad, int button); // Check if a gamepad button has been pressed once
bool IsGamepadButtonDown(int gamepad, int button); // Check if a gamepad button is being pressed
bool IsGamepadButtonReleased(int gamepad, int button); // Check if a gamepad button has been released once
bool IsGamepadButtonUp(int gamepad, int button); // Check if a gamepad button is NOT being pressed
int GetGamepadButtonPressed(void); // Get the last gamepad button pressed
int GetGamepadAxisCount(int gamepad); // Get gamepad axis count for a gamepad
float GetGamepadAxisMovement(int gamepad, int axis); // Get axis movement value for a gamepad axis
int SetGamepadMappings(const char *mappings); // Set internal gamepad mappings (SDL_GameControllerDB)
// Input-related functions: mouse
bool IsMouseButtonPressed(int button); // Check if a mouse button has been pressed once
bool IsMouseButtonDown(int button); // Check if a mouse button is being pressed
bool IsMouseButtonReleased(int button); // Check if a mouse button has been released once
bool IsMouseButtonUp(int button); // Check if a mouse button is NOT being pressed
int GetMouseX(void); // Get mouse position X
int GetMouseY(void); // Get mouse position Y
Vector2 GetMousePosition(void); // Get mouse position XY
Vector2 GetMouseDelta(void); // Get mouse delta between frames
void SetMousePosition(int x, int y); // Set mouse position XY
void SetMouseOffset(int offsetX, int offsetY); // Set mouse offset
void SetMouseScale(float scaleX, float scaleY); // Set mouse scaling
float GetMouseWheelMove(void); // Get mouse wheel movement for X or Y, whichever is larger
Vector2 GetMouseWheelMoveV(void); // Get mouse wheel movement for both X and Y
void SetMouseCursor(int cursor); // Set mouse cursor
// Input-related functions: touch
int GetTouchX(void); // Get touch position X for touch point 0 (relative to screen size)
int GetTouchY(void); // Get touch position Y for touch point 0 (relative to screen size)
Vector2 GetTouchPosition(int index); // Get touch position XY for a touch point index (relative to screen size)
int GetTouchPointId(int index); // Get touch point identifier for given index
int GetTouchPointCount(void); // Get number of touch points
//------------------------------------------------------------------------------------
// Gestures and Touch Handling Functions (Module: rgestures)
//------------------------------------------------------------------------------------
void SetGesturesEnabled(unsigned int flags); // Enable a set of gestures using flags
bool IsGestureDetected(unsigned int gesture); // Check if a gesture have been detected
int GetGestureDetected(void); // Get latest detected gesture
float GetGestureHoldDuration(void); // Get gesture hold time in milliseconds
Vector2 GetGestureDragVector(void); // Get gesture drag vector
float GetGestureDragAngle(void); // Get gesture drag angle
Vector2 GetGesturePinchVector(void); // Get gesture pinch delta
float GetGesturePinchAngle(void); // Get gesture pinch angle
//------------------------------------------------------------------------------------
// Camera System Functions (Module: rcamera)
//------------------------------------------------------------------------------------
void UpdateCamera(Camera *camera, int mode); // Update camera position for selected mode
void UpdateCameraPro(Camera *camera, Vector3 movement, Vector3 rotation, float zoom); // Update camera movement/rotation
module: rshapes
// NOTE: It can be useful when using basic shapes and one single font,
// defining a font char white rectangle would allow drawing everything in a single draw call
void SetShapesTexture(Texture2D texture, Rectangle source); // Set texture and rectangle to be used on shapes drawing
// Basic shapes drawing functions
void DrawPixel(int posX, int posY, Color color); // Draw a pixel
void DrawPixelV(Vector2 position, Color color); // Draw a pixel (Vector version)
void DrawLine(int startPosX, int startPosY, int endPosX, int endPosY, Color color); // Draw a line
void DrawLineV(Vector2 startPos, Vector2 endPos, Color color); // Draw a line (using gl lines)
void DrawLineEx(Vector2 startPos, Vector2 endPos, float thick, Color color); // Draw a line (using triangles/quads)
void DrawLineStrip(Vector2 *points, int pointCount, Color color); // Draw lines sequence (using gl lines)
void DrawLineBezier(Vector2 startPos, Vector2 endPos, float thick, Color color); // Draw line segment cubic-bezier in-out interpolation
void DrawCircle(int centerX, int centerY, float radius, Color color); // Draw a color-filled circle
void DrawCircleSector(Vector2 center, float radius, float startAngle, float endAngle, int segments, Color color); // Draw a piece of a circle
void DrawCircleSectorLines(Vector2 center, float radius, float startAngle, float endAngle, int segments, Color color); // Draw circle sector outline
void DrawCircleGradient(int centerX, int centerY, float radius, Color color1, Color color2); // Draw a gradient-filled circle
void DrawCircleV(Vector2 center, float radius, Color color); // Draw a color-filled circle (Vector version)
void DrawCircleLines(int centerX, int centerY, float radius, Color color); // Draw circle outline
void DrawCircleLinesV(Vector2 center, float radius, Color color); // Draw circle outline (Vector version)
void DrawEllipse(int centerX, int centerY, float radiusH, float radiusV, Color color); // Draw ellipse
void DrawEllipseLines(int centerX, int centerY, float radiusH, float radiusV, Color color); // Draw ellipse outline
void DrawRing(Vector2 center, float innerRadius, float outerRadius, float startAngle, float endAngle, int segments, Color color); // Draw ring
void DrawRingLines(Vector2 center, float innerRadius, float outerRadius, float startAngle, float endAngle, int segments, Color color); // Draw ring outline
void DrawRectangle(int posX, int posY, int width, int height, Color color); // Draw a color-filled rectangle
void DrawRectangleV(Vector2 position, Vector2 size, Color color); // Draw a color-filled rectangle (Vector version)
void DrawRectangleRec(Rectangle rec, Color color); // Draw a color-filled rectangle
void DrawRectanglePro(Rectangle rec, Vector2 origin, float rotation, Color color); // Draw a color-filled rectangle with pro parameters
void DrawRectangleGradientV(int posX, int posY, int width, int height, Color color1, Color color2);// Draw a vertical-gradient-filled rectangle
void DrawRectangleGradientH(int posX, int posY, int width, int height, Color color1, Color color2);// Draw a horizontal-gradient-filled rectangle
void DrawRectangleGradientEx(Rectangle rec, Color col1, Color col2, Color col3, Color col4); // Draw a gradient-filled rectangle with custom vertex colors
void DrawRectangleLines(int posX, int posY, int width, int height, Color color); // Draw rectangle outline
void DrawRectangleLinesEx(Rectangle rec, float lineThick, Color color); // Draw rectangle outline with extended parameters
void DrawRectangleRounded(Rectangle rec, float roundness, int segments, Color color); // Draw rectangle with rounded edges
void DrawRectangleRoundedLines(Rectangle rec, float roundness, int segments, float lineThick, Color color); // Draw rectangle with rounded edges outline
void DrawTriangle(Vector2 v1, Vector2 v2, Vector2 v3, Color color); // Draw a color-filled triangle (vertex in counter-clockwise order!)
void DrawTriangleLines(Vector2 v1, Vector2 v2, Vector2 v3, Color color); // Draw triangle outline (vertex in counter-clockwise order!)
void DrawTriangleFan(Vector2 *points, int pointCount, Color color); // Draw a triangle fan defined by points (first vertex is the center)
void DrawTriangleStrip(Vector2 *points, int pointCount, Color color); // Draw a triangle strip defined by points
void DrawPoly(Vector2 center, int sides, float radius, float rotation, Color color); // Draw a regular polygon (Vector version)
void DrawPolyLines(Vector2 center, int sides, float radius, float rotation, Color color); // Draw a polygon outline of n sides
void DrawPolyLinesEx(Vector2 center, int sides, float radius, float rotation, float lineThick, Color color); // Draw a polygon outline of n sides with extended parameters
// Splines drawing functions
void DrawSplineLinear(Vector2 *points, int pointCount, float thick, Color color); // Draw spline: Linear, minimum 2 points
void DrawSplineBasis(Vector2 *points, int pointCount, float thick, Color color); // Draw spline: B-Spline, minimum 4 points
void DrawSplineCatmullRom(Vector2 *points, int pointCount, float thick, Color color); // Draw spline: Catmull-Rom, minimum 4 points
void DrawSplineBezierQuadratic(Vector2 *points, int pointCount, float thick, Color color); // Draw spline: Quadratic Bezier, minimum 3 points (1 control point): [p1, c2, p3, c4...]
void DrawSplineBezierCubic(Vector2 *points, int pointCount, float thick, Color color); // Draw spline: Cubic Bezier, minimum 4 points (2 control points): [p1, c2, c3, p4, c5, c6...]
void DrawSplineSegmentLinear(Vector2 p1, Vector2 p2, float thick, Color color); // Draw spline segment: Linear, 2 points
void DrawSplineSegmentBasis(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, float thick, Color color); // Draw spline segment: B-Spline, 4 points
void DrawSplineSegmentCatmullRom(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, float thick, Color color); // Draw spline segment: Catmull-Rom, 4 points
void DrawSplineSegmentBezierQuadratic(Vector2 p1, Vector2 c2, Vector2 p3, float thick, Color color); // Draw spline segment: Quadratic Bezier, 2 points, 1 control point
void DrawSplineSegmentBezierCubic(Vector2 p1, Vector2 c2, Vector2 c3, Vector2 p4, float thick, Color color); // Draw spline segment: Cubic Bezier, 2 points, 2 control points
// Spline segment point evaluation functions, for a given t [0.0f .. 1.0f]
Vector2 GetSplinePointLinear(Vector2 startPos, Vector2 endPos, float t); // Get (evaluate) spline point: Linear
Vector2 GetSplinePointBasis(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, float t); // Get (evaluate) spline point: B-Spline
Vector2 GetSplinePointCatmullRom(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, float t); // Get (evaluate) spline point: Catmull-Rom
Vector2 GetSplinePointBezierQuad(Vector2 p1, Vector2 c2, Vector2 p3, float t); // Get (evaluate) spline point: Quadratic Bezier
Vector2 GetSplinePointBezierCubic(Vector2 p1, Vector2 c2, Vector2 c3, Vector2 p4, float t); // Get (evaluate) spline point: Cubic Bezier
// Basic shapes collision detection functions
bool CheckCollisionRecs(Rectangle rec1, Rectangle rec2); // Check collision between two rectangles
bool CheckCollisionCircles(Vector2 center1, float radius1, Vector2 center2, float radius2); // Check collision between two circles
bool CheckCollisionCircleRec(Vector2 center, float radius, Rectangle rec); // Check collision between circle and rectangle
bool CheckCollisionPointRec(Vector2 point, Rectangle rec); // Check if point is inside rectangle
bool CheckCollisionPointCircle(Vector2 point, Vector2 center, float radius); // Check if point is inside circle
bool CheckCollisionPointTriangle(Vector2 point, Vector2 p1, Vector2 p2, Vector2 p3); // Check if point is inside a triangle
bool CheckCollisionPointPoly(Vector2 point, Vector2 *points, int pointCount); // Check if point is within a polygon described by array of vertices
bool CheckCollisionLines(Vector2 startPos1, Vector2 endPos1, Vector2 startPos2, Vector2 endPos2, Vector2 *collisionPoint); // Check the collision between two lines defined by two points each, returns collision point by reference
bool CheckCollisionPointLine(Vector2 point, Vector2 p1, Vector2 p2, int threshold); // Check if point belongs to line created between two points [p1] and [p2] with defined margin in pixels [threshold]
Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2); // Get collision rectangle for two rectangles collision
module: rtextures
// Image loading functions
// NOTE: These functions do not require GPU access
Image LoadImage(const char *fileName); // Load image from file into CPU memory (RAM)
Image LoadImageRaw(const char *fileName, int width, int height, int format, int headerSize); // Load image from RAW file data
Image LoadImageSvg(const char *fileNameOrString, int width, int height); // Load image from SVG file data or string with specified size
Image LoadImageAnim(const char *fileName, int *frames); // Load image sequence from file (frames appended to image.data)
Image LoadImageFromMemory(const char *fileType, const unsigned char *fileData, int dataSize); // Load image from memory buffer, fileType refers to extension: i.e. '.png'
Image LoadImageFromTexture(Texture2D texture); // Load image from GPU texture data
Image LoadImageFromScreen(void); // Load image from screen buffer and (screenshot)
bool IsImageReady(Image image); // Check if an image is ready
void UnloadImage(Image image); // Unload image from CPU memory (RAM)
bool ExportImage(Image image, const char *fileName); // Export image data to file, returns true on success
unsigned char *ExportImageToMemory(Image image, const char *fileType, int *fileSize); // Export image to memory buffer
bool ExportImageAsCode(Image image, const char *fileName); // Export image as code file defining an array of bytes, returns true on success
// Image generation functions
Image GenImageColor(int width, int height, Color color); // Generate image: plain color
Image GenImageGradientLinear(int width, int height, int direction, Color start, Color end); // Generate image: linear gradient, direction in degrees [0..360], 0=Vertical gradient
Image GenImageGradientRadial(int width, int height, float density, Color inner, Color outer); // Generate image: radial gradient
Image GenImageGradientSquare(int width, int height, float density, Color inner, Color outer); // Generate image: square gradient
Image GenImageChecked(int width, int height, int checksX, int checksY, Color col1, Color col2); // Generate image: checked
Image GenImageWhiteNoise(int width, int height, float factor); // Generate image: white noise
Image GenImagePerlinNoise(int width, int height, int offsetX, int offsetY, float scale); // Generate image: perlin noise
Image GenImageCellular(int width, int height, int tileSize); // Generate image: cellular algorithm, bigger tileSize means bigger cells
Image GenImageText(int width, int height, const char *text); // Generate image: grayscale image from text data
// Image manipulation functions
Image ImageCopy(Image image); // Create an image duplicate (useful for transformations)
Image ImageFromImage(Image image, Rectangle rec); // Create an image from another image piece
Image ImageText(const char *text, int fontSize, Color color); // Create an image from text (default font)
Image ImageTextEx(Font font, const char *text, float fontSize, float spacing, Color tint); // Create an image from text (custom sprite font)
void ImageFormat(Image *image, int newFormat); // Convert image data to desired format
void ImageToPOT(Image *image, Color fill); // Convert image to POT (power-of-two)
void ImageCrop(Image *image, Rectangle crop); // Crop an image to a defined rectangle
void ImageAlphaCrop(Image *image, float threshold); // Crop image depending on alpha value
void ImageAlphaClear(Image *image, Color color, float threshold); // Clear alpha channel to desired color
void ImageAlphaMask(Image *image, Image alphaMask); // Apply alpha mask to image
void ImageAlphaPremultiply(Image *image); // Premultiply alpha channel
void ImageBlurGaussian(Image *image, int blurSize); // Apply Gaussian blur using a box blur approximation
void ImageResize(Image *image, int newWidth, int newHeight); // Resize image (Bicubic scaling algorithm)
void ImageResizeNN(Image *image, int newWidth,int newHeight); // Resize image (Nearest-Neighbor scaling algorithm)
void ImageResizeCanvas(Image *image, int newWidth, int newHeight, int offsetX, int offsetY, Color fill); // Resize canvas and fill with color
void ImageMipmaps(Image *image); // Compute all mipmap levels for a provided image
void ImageDither(Image *image, int rBpp, int gBpp, int bBpp, int aBpp); // Dither image data to 16bpp or lower (Floyd-Steinberg dithering)
void ImageFlipVertical(Image *image); // Flip image vertically
void ImageFlipHorizontal(Image *image); // Flip image horizontally
void ImageRotate(Image *image, int degrees); // Rotate image by input angle in degrees (-359 to 359)
void ImageRotateCW(Image *image); // Rotate image clockwise 90deg
void ImageRotateCCW(Image *image); // Rotate image counter-clockwise 90deg
void ImageColorTint(Image *image, Color color); // Modify image color: tint
void ImageColorInvert(Image *image); // Modify image color: invert
void ImageColorGrayscale(Image *image); // Modify image color: grayscale
void ImageColorContrast(Image *image, float contrast); // Modify image color: contrast (-100 to 100)
void ImageColorBrightness(Image *image, int brightness); // Modify image color: brightness (-255 to 255)
void ImageColorReplace(Image *image, Color color, Color replace); // Modify image color: replace color
Color *LoadImageColors(Image image); // Load color data from image as a Color array (RGBA - 32bit)
Color *LoadImagePalette(Image image, int maxPaletteSize, int *colorCount); // Load colors palette from image as a Color array (RGBA - 32bit)
void UnloadImageColors(Color *colors); // Unload color data loaded with LoadImageColors()
void UnloadImagePalette(Color *colors); // Unload colors palette loaded with LoadImagePalette()
Rectangle GetImageAlphaBorder(Image image, float threshold); // Get image alpha border rectangle
Color GetImageColor(Image image, int x, int y); // Get image pixel color at (x, y) position
// Image drawing functions
// NOTE: Image software-rendering functions (CPU)
void ImageClearBackground(Image *dst, Color color); // Clear image background with given color
void ImageDrawPixel(Image *dst, int posX, int posY, Color color); // Draw pixel within an image
void ImageDrawPixelV(Image *dst, Vector2 position, Color color); // Draw pixel within an image (Vector version)
void ImageDrawLine(Image *dst, int startPosX, int startPosY, int endPosX, int endPosY, Color color); // Draw line within an image
void ImageDrawLineV(Image *dst, Vector2 start, Vector2 end, Color color); // Draw line within an image (Vector version)
void ImageDrawCircle(Image *dst, int centerX, int centerY, int radius, Color color); // Draw a filled circle within an image
void ImageDrawCircleV(Image *dst, Vector2 center, int radius, Color color); // Draw a filled circle within an image (Vector version)
void ImageDrawCircleLines(Image *dst, int centerX, int centerY, int radius, Color color); // Draw circle outline within an image
void ImageDrawCircleLinesV(Image *dst, Vector2 center, int radius, Color color); // Draw circle outline within an image (Vector version)
void ImageDrawRectangle(Image *dst, int posX, int posY, int width, int height, Color color); // Draw rectangle within an image
void ImageDrawRectangleV(Image *dst, Vector2 position, Vector2 size, Color color); // Draw rectangle within an image (Vector version)
void ImageDrawRectangleRec(Image *dst, Rectangle rec, Color color); // Draw rectangle within an image
void ImageDrawRectangleLines(Image *dst, Rectangle rec, int thick, Color color); // Draw rectangle lines within an image
void ImageDraw(Image *dst, Image src, Rectangle srcRec, Rectangle dstRec, Color tint); // Draw a source image within a destination image (tint applied to source)
void ImageDrawText(Image *dst, const char *text, int posX, int posY, int fontSize, Color color); // Draw text (using default font) within an image (destination)
void ImageDrawTextEx(Image *dst, Font font, const char *text, Vector2 position, float fontSize, float spacing, Color tint); // Draw text (custom sprite font) within an image (destination)
// Texture loading functions
// NOTE: These functions require GPU access
Texture2D LoadTexture(const char *fileName); // Load texture from file into GPU memory (VRAM)
Texture2D LoadTextureFromImage(Image image); // Load texture from image data
TextureCubemap LoadTextureCubemap(Image image, int layout); // Load cubemap from image, multiple image cubemap layouts supported
RenderTexture2D LoadRenderTexture(int width, int height); // Load texture for rendering (framebuffer)
bool IsTextureReady(Texture2D texture); // Check if a texture is ready
void UnloadTexture(Texture2D texture); // Unload texture from GPU memory (VRAM)
bool IsRenderTextureReady(RenderTexture2D target); // Check if a render texture is ready
void UnloadRenderTexture(RenderTexture2D target); // Unload render texture from GPU memory (VRAM)
void UpdateTexture(Texture2D texture, const void *pixels); // Update GPU texture with new data
void UpdateTextureRec(Texture2D texture, Rectangle rec, const void *pixels); // Update GPU texture rectangle with new data
// Texture configuration functions
void GenTextureMipmaps(Texture2D *texture); // Generate GPU mipmaps for a texture
void SetTextureFilter(Texture2D texture, int filter); // Set texture scaling filter mode
void SetTextureWrap(Texture2D texture, int wrap); // Set texture wrapping mode
// Texture drawing functions
void DrawTexture(Texture2D texture, int posX, int posY, Color tint); // Draw a Texture2D
void DrawTextureV(Texture2D texture, Vector2 position, Color tint); // Draw a Texture2D with position defined as Vector2
void DrawTextureEx(Texture2D texture, Vector2 position, float rotation, float scale, Color tint); // Draw a Texture2D with extended parameters
void DrawTextureRec(Texture2D texture, Rectangle source, Vector2 position, Color tint); // Draw a part of a texture defined by a rectangle
void DrawTexturePro(Texture2D texture, Rectangle source, Rectangle dest, Vector2 origin, float rotation, Color tint); // Draw a part of a texture defined by a rectangle with 'pro' parameters
void DrawTextureNPatch(Texture2D texture, NPatchInfo nPatchInfo, Rectangle dest, Vector2 origin, float rotation, Color tint); // Draws a texture (or part of it) that stretches or shrinks nicely
// Color/pixel related functions
Color Fade(Color color, float alpha); // Get color with alpha applied, alpha goes from 0.0f to 1.0f
int ColorToInt(Color color); // Get hexadecimal value for a Color
Vector4 ColorNormalize(Color color); // Get Color normalized as float [0..1]
Color ColorFromNormalized(Vector4 normalized); // Get Color from normalized values [0..1]
Vector3 ColorToHSV(Color color); // Get HSV values for a Color, hue [0..360], saturation/value [0..1]
Color ColorFromHSV(float hue, float saturation, float value); // Get a Color from HSV values, hue [0..360], saturation/value [0..1]
Color ColorTint(Color color, Color tint); // Get color multiplied with another color
Color ColorBrightness(Color color, float factor); // Get color with brightness correction, brightness factor goes from -1.0f to 1.0f
Color ColorContrast(Color color, float contrast); // Get color with contrast correction, contrast values between -1.0f and 1.0f
Color ColorAlpha(Color color, float alpha); // Get color with alpha applied, alpha goes from 0.0f to 1.0f
Color ColorAlphaBlend(Color dst, Color src, Color tint); // Get src alpha-blended into dst color with tint
Color GetColor(unsigned int hexValue); // Get Color structure from hexadecimal value
Color GetPixelColor(void *srcPtr, int format); // Get Color from a source pixel pointer of certain format
void SetPixelColor(void *dstPtr, Color color, int format); // Set color formatted into destination pixel pointer
int GetPixelDataSize(int width, int height, int format); // Get pixel data size in bytes for certain format
module: rtext
// Font loading/unloading functions
Font GetFontDefault(void); // Get the default Font
Font LoadFont(const char *fileName); // Load font from file into GPU memory (VRAM)
Font LoadFontEx(const char *fileName, int fontSize, int *codepoints, int codepointCount); // Load font from file with extended parameters, use NULL for codepoints and 0 for codepointCount to load the default character set
Font LoadFontFromImage(Image image, Color key, int firstChar); // Load font from Image (XNA style)
Font LoadFontFromMemory(const char *fileType, const unsigned char *fileData, int dataSize, int fontSize, int *codepoints, int codepointCount); // Load font from memory buffer, fileType refers to extension: i.e. '.ttf'
bool IsFontReady(Font font); // Check if a font is ready
GlyphInfo *LoadFontData(const unsigned char *fileData, int dataSize, int fontSize, int *codepoints, int codepointCount, int type); // Load font data for further use
Image GenImageFontAtlas(const GlyphInfo *glyphs, Rectangle **glyphRecs, int glyphCount, int fontSize, int padding, int packMethod); // Generate image font atlas using chars info
void UnloadFontData(GlyphInfo *glyphs, int glyphCount); // Unload font chars info data (RAM)
void UnloadFont(Font font); // Unload font from GPU memory (VRAM)
bool ExportFontAsCode(Font font, const char *fileName); // Export font as code file, returns true on success
// Text drawing functions
void DrawFPS(int posX, int posY); // Draw current FPS
void DrawText(const char *text, int posX, int posY, int fontSize, Color color); // Draw text (using default font)
void DrawTextEx(Font font, const char *text, Vector2 position, float fontSize, float spacing, Color tint); // Draw text using font and additional parameters
void DrawTextPro(Font font, const char *text, Vector2 position, Vector2 origin, float rotation, float fontSize, float spacing, Color tint); // Draw text using Font and pro parameters (rotation)
void DrawTextCodepoint(Font font, int codepoint, Vector2 position, float fontSize, Color tint); // Draw one character (codepoint)
void DrawTextCodepoints(Font font, const int *codepoints, int codepointCount, Vector2 position, float fontSize, float spacing, Color tint); // Draw multiple character (codepoint)
// Text font info functions
void SetTextLineSpacing(int spacing); // Set vertical line spacing when drawing with line-breaks
int MeasureText(const char *text, int fontSize); // Measure string width for default font
Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing); // Measure string size for Font
int GetGlyphIndex(Font font, int codepoint); // Get glyph index position in font for a codepoint (unicode character), fallback to '?' if not found
GlyphInfo GetGlyphInfo(Font font, int codepoint); // Get glyph font info data for a codepoint (unicode character), fallback to '?' if not found
Rectangle GetGlyphAtlasRec(Font font, int codepoint); // Get glyph rectangle in font atlas for a codepoint (unicode character), fallback to '?' if not found
// Text codepoints management functions (unicode characters)
char *LoadUTF8(const int *codepoints, int length); // Load UTF-8 text encoded from codepoints array
void UnloadUTF8(char *text); // Unload UTF-8 text encoded from codepoints array
int *LoadCodepoints(const char *text, int *count); // Load all codepoints from a UTF-8 text string, codepoints count returned by parameter
void UnloadCodepoints(int *codepoints); // Unload codepoints data from memory
int GetCodepointCount(const char *text); // Get total number of codepoints in a UTF-8 encoded string
int GetCodepoint(const char *text, int *codepointSize); // Get next codepoint in a UTF-8 encoded string, 0x3f('?') is returned on failure
int GetCodepointNext(const char *text, int *codepointSize); // Get next codepoint in a UTF-8 encoded string, 0x3f('?') is returned on failure
int GetCodepointPrevious(const char *text, int *codepointSize); // Get previous codepoint in a UTF-8 encoded string, 0x3f('?') is returned on failure
const char *CodepointToUTF8(int codepoint, int *utf8Size); // Encode one codepoint into UTF-8 byte array (array length returned as parameter)
// Text strings management functions (no UTF-8 strings, only byte chars)
// NOTE: Some strings allocate memory internally for returned strings, just be careful!
int TextCopy(char *dst, const char *src); // Copy one string to another, returns bytes copied
bool TextIsEqual(const char *text1, const char *text2); // Check if two text string are equal
unsigned int TextLength(const char *text); // Get text length, checks for '\0' ending
const char *TextFormat(const char *text, ...); // Text formatting with variables (sprintf() style)
const char *TextSubtext(const char *text, int position, int length); // Get a piece of a text string
char *TextReplace(char *text, const char *replace, const char *by); // Replace text string (WARNING: memory must be freed!)
char *TextInsert(const char *text, const char *insert, int position); // Insert text in a position (WARNING: memory must be freed!)
const char *TextJoin(const char **textList, int count, const char *delimiter); // Join text strings with delimiter
const char **TextSplit(const char *text, char delimiter, int *count); // Split text into multiple strings
void TextAppend(char *text, const char *append, int *position); // Append text at specific position and move cursor!
int TextFindIndex(const char *text, const char *find); // Find first text occurrence within a string
const char *TextToUpper(const char *text); // Get upper case version of provided string
const char *TextToLower(const char *text); // Get lower case version of provided string
const char *TextToPascal(const char *text); // Get Pascal case notation version of provided string
int TextToInteger(const char *text); // Get integer value from text (negative values not supported)
module: rmodels
// Basic geometric 3D shapes drawing functions
void DrawLine3D(Vector3 startPos, Vector3 endPos, Color color); // Draw a line in 3D world space
void DrawPoint3D(Vector3 position, Color color); // Draw a point in 3D space, actually a small line
void DrawCircle3D(Vector3 center, float radius, Vector3 rotationAxis, float rotationAngle, Color color); // Draw a circle in 3D world space
void DrawTriangle3D(Vector3 v1, Vector3 v2, Vector3 v3, Color color); // Draw a color-filled triangle (vertex in counter-clockwise order!)
void DrawTriangleStrip3D(Vector3 *points, int pointCount, Color color); // Draw a triangle strip defined by points
void DrawCube(Vector3 position, float width, float height, float length, Color color); // Draw cube
void DrawCubeV(Vector3 position, Vector3 size, Color color); // Draw cube (Vector version)
void DrawCubeWires(Vector3 position, float width, float height, float length, Color color); // Draw cube wires
void DrawCubeWiresV(Vector3 position, Vector3 size, Color color); // Draw cube wires (Vector version)
void DrawSphere(Vector3 centerPos, float radius, Color color); // Draw sphere
void DrawSphereEx(Vector3 centerPos, float radius, int rings, int slices, Color color); // Draw sphere with extended parameters
void DrawSphereWires(Vector3 centerPos, float radius, int rings, int slices, Color color); // Draw sphere wires
void DrawCylinder(Vector3 position, float radiusTop, float radiusBottom, float height, int slices, Color color); // Draw a cylinder/cone
void DrawCylinderEx(Vector3 startPos, Vector3 endPos, float startRadius, float endRadius, int sides, Color color); // Draw a cylinder with base at startPos and top at endPos
void DrawCylinderWires(Vector3 position, float radiusTop, float radiusBottom, float height, int slices, Color color); // Draw a cylinder/cone wires
void DrawCylinderWiresEx(Vector3 startPos, Vector3 endPos, float startRadius, float endRadius, int sides, Color color); // Draw a cylinder wires with base at startPos and top at endPos
void DrawCapsule(Vector3 startPos, Vector3 endPos, float radius, int slices, int rings, Color color); // Draw a capsule with the center of its sphere caps at startPos and endPos
void DrawCapsuleWires(Vector3 startPos, Vector3 endPos, float radius, int slices, int rings, Color color); // Draw capsule wireframe with the center of its sphere caps at startPos and endPos
void DrawPlane(Vector3 centerPos, Vector2 size, Color color); // Draw a plane XZ
void DrawRay(Ray ray, Color color); // Draw a ray line
void DrawGrid(int slices, float spacing); // Draw a grid (centered at (0, 0, 0))
//------------------------------------------------------------------------------------
// Model 3d Loading and Drawing Functions (Module: models)
//------------------------------------------------------------------------------------
// Model management functions
Model LoadModel(const char *fileName); // Load model from files (meshes and materials)
Model LoadModelFromMesh(Mesh mesh); // Load model from generated mesh (default material)
bool IsModelReady(Model model); // Check if a model is ready
void UnloadModel(Model model); // Unload model (including meshes) from memory (RAM and/or VRAM)
BoundingBox GetModelBoundingBox(Model model); // Compute model bounding box limits (considers all meshes)
// Model drawing functions
void DrawModel(Model model, Vector3 position, float scale, Color tint); // Draw a model (with texture if set)
void DrawModelEx(Model model, Vector3 position, Vector3 rotationAxis, float rotationAngle, Vector3 scale, Color tint); // Draw a model with extended parameters
void DrawModelWires(Model model, Vector3 position, float scale, Color tint); // Draw a model wires (with texture if set)
void DrawModelWiresEx(Model model, Vector3 position, Vector3 rotationAxis, float rotationAngle, Vector3 scale, Color tint); // Draw a model wires (with texture if set) with extended parameters
void DrawBoundingBox(BoundingBox box, Color color); // Draw bounding box (wires)
void DrawBillboard(Camera camera, Texture2D texture, Vector3 position, float size, Color tint); // Draw a billboard texture
void DrawBillboardRec(Camera camera, Texture2D texture, Rectangle source, Vector3 position, Vector2 size, Color tint); // Draw a billboard texture defined by source
void DrawBillboardPro(Camera camera, Texture2D texture, Rectangle source, Vector3 position, Vector3 up, Vector2 size, Vector2 origin, float rotation, Color tint); // Draw a billboard texture defined by source and rotation
// Mesh management functions
void UploadMesh(Mesh *mesh, bool dynamic); // Upload mesh vertex data in GPU and provide VAO/VBO ids
void UpdateMeshBuffer(Mesh mesh, int index, const void *data, int dataSize, int offset); // Update mesh vertex data in GPU for a specific buffer index
void UnloadMesh(Mesh mesh); // Unload mesh data from CPU and GPU
void DrawMesh(Mesh mesh, Material material, Matrix transform); // Draw a 3d mesh with material and transform
void DrawMeshInstanced(Mesh mesh, Material material, const Matrix *transforms, int instances); // Draw multiple mesh instances with material and different transforms
bool ExportMesh(Mesh mesh, const char *fileName); // Export mesh data to file, returns true on success
BoundingBox GetMeshBoundingBox(Mesh mesh); // Compute mesh bounding box limits
void GenMeshTangents(Mesh *mesh); // Compute mesh tangents
// Mesh generation functions
Mesh GenMeshPoly(int sides, float radius); // Generate polygonal mesh
Mesh GenMeshPlane(float width, float length, int resX, int resZ); // Generate plane mesh (with subdivisions)
Mesh GenMeshCube(float width, float height, float length); // Generate cuboid mesh
Mesh GenMeshSphere(float radius, int rings, int slices); // Generate sphere mesh (standard sphere)
Mesh GenMeshHemiSphere(float radius, int rings, int slices); // Generate half-sphere mesh (no bottom cap)
Mesh GenMeshCylinder(float radius, float height, int slices); // Generate cylinder mesh
Mesh GenMeshCone(float radius, float height, int slices); // Generate cone/pyramid mesh
Mesh GenMeshTorus(float radius, float size, int radSeg, int sides); // Generate torus mesh
Mesh GenMeshKnot(float radius, float size, int radSeg, int sides); // Generate trefoil knot mesh
Mesh GenMeshHeightmap(Image heightmap, Vector3 size); // Generate heightmap mesh from image data
Mesh GenMeshCubicmap(Image cubicmap, Vector3 cubeSize); // Generate cubes-based map mesh from image data
// Material loading/unloading functions
Material *LoadMaterials(const char *fileName, int *materialCount); // Load materials from model file
Material LoadMaterialDefault(void); // Load default material (Supports: DIFFUSE, SPECULAR, NORMAL maps)
bool IsMaterialReady(Material material); // Check if a material is ready
void UnloadMaterial(Material material); // Unload material from GPU memory (VRAM)
void SetMaterialTexture(Material *material, int mapType, Texture2D texture); // Set texture for a material map type (MATERIAL_MAP_DIFFUSE, MATERIAL_MAP_SPECULAR...)
void SetModelMeshMaterial(Model *model, int meshId, int materialId); // Set material for a mesh
// Model animations loading/unloading functions
ModelAnimation *LoadModelAnimations(const char *fileName, int *animCount); // Load model animations from file
void UpdateModelAnimation(Model model, ModelAnimation anim, int frame); // Update model animation pose
void UnloadModelAnimation(ModelAnimation anim); // Unload animation data
void UnloadModelAnimations(ModelAnimation *animations, int animCount); // Unload animation array data
bool IsModelAnimationValid(Model model, ModelAnimation anim); // Check model animation skeleton match
// Collision detection functions
bool CheckCollisionSpheres(Vector3 center1, float radius1, Vector3 center2, float radius2); // Check collision between two spheres
bool CheckCollisionBoxes(BoundingBox box1, BoundingBox box2); // Check collision between two bounding boxes
bool CheckCollisionBoxSphere(BoundingBox box, Vector3 center, float radius); // Check collision between box and sphere
RayCollision GetRayCollisionSphere(Ray ray, Vector3 center, float radius); // Get collision info between ray and sphere
RayCollision GetRayCollisionBox(Ray ray, BoundingBox box); // Get collision info between ray and box
RayCollision GetRayCollisionMesh(Ray ray, Mesh mesh, Matrix transform); // Get collision info between ray and mesh
RayCollision GetRayCollisionTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3); // Get collision info between ray and triangle
RayCollision GetRayCollisionQuad(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4); // Get collision info between ray and quad
module: raudio
// Audio device management functions
void InitAudioDevice(void); // Initialize audio device and context
void CloseAudioDevice(void); // Close the audio device and context
bool IsAudioDeviceReady(void); // Check if audio device has been initialized successfully
void SetMasterVolume(float volume); // Set master volume (listener)
float GetMasterVolume(void); // Get master volume (listener)
// Wave/Sound loading/unloading functions
Wave LoadWave(const char *fileName); // Load wave data from file
Wave LoadWaveFromMemory(const char *fileType, const unsigned char *fileData, int dataSize); // Load wave from memory buffer, fileType refers to extension: i.e. '.wav'
bool IsWaveReady(Wave wave); // Checks if wave data is ready
Sound LoadSound(const char *fileName); // Load sound from file
Sound LoadSoundFromWave(Wave wave); // Load sound from wave data
Sound LoadSoundAlias(Sound source); // Create a new sound that shares the same sample data as the source sound, does not own the sound data
bool IsSoundReady(Sound sound); // Checks if a sound is ready
void UpdateSound(Sound sound, const void *data, int sampleCount); // Update sound buffer with new data
void UnloadWave(Wave wave); // Unload wave data
void UnloadSound(Sound sound); // Unload sound
void UnloadSoundAlias(Sound alias); // Unload a sound alias (does not deallocate sample data)
bool ExportWave(Wave wave, const char *fileName); // Export wave data to file, returns true on success
bool ExportWaveAsCode(Wave wave, const char *fileName); // Export wave sample data to code (.h), returns true on success
// Wave/Sound management functions
void PlaySound(Sound sound); // Play a sound
void StopSound(Sound sound); // Stop playing a sound
void PauseSound(Sound sound); // Pause a sound
void ResumeSound(Sound sound); // Resume a paused sound
bool IsSoundPlaying(Sound sound); // Check if a sound is currently playing
void SetSoundVolume(Sound sound, float volume); // Set volume for a sound (1.0 is max level)
void SetSoundPitch(Sound sound, float pitch); // Set pitch for a sound (1.0 is base level)
void SetSoundPan(Sound sound, float pan); // Set pan for a sound (0.5 is center)
Wave WaveCopy(Wave wave); // Copy a wave to a new wave
void WaveCrop(Wave *wave, int initSample, int finalSample); // Crop a wave to defined samples range
void WaveFormat(Wave *wave, int sampleRate, int sampleSize, int channels); // Convert wave data to desired format
float *LoadWaveSamples(Wave wave); // Load samples data from wave as a 32bit float data array
void UnloadWaveSamples(float *samples); // Unload samples data loaded with LoadWaveSamples()
// Music management functions
Music LoadMusicStream(const char *fileName); // Load music stream from file
Music LoadMusicStreamFromMemory(const char *fileType, const unsigned char *data, int dataSize); // Load music stream from data
bool IsMusicReady(Music music); // Checks if a music stream is ready
void UnloadMusicStream(Music music); // Unload music stream
void PlayMusicStream(Music music); // Start music playing
bool IsMusicStreamPlaying(Music music); // Check if music is playing
void UpdateMusicStream(Music music); // Updates buffers for music streaming
void StopMusicStream(Music music); // Stop music playing
void PauseMusicStream(Music music); // Pause music playing
void ResumeMusicStream(Music music); // Resume playing paused music
void SeekMusicStream(Music music, float position); // Seek music to a position (in seconds)
void SetMusicVolume(Music music, float volume); // Set volume for music (1.0 is max level)
void SetMusicPitch(Music music, float pitch); // Set pitch for a music (1.0 is base level)
void SetMusicPan(Music music, float pan); // Set pan for a music (0.5 is center)
float GetMusicTimeLength(Music music); // Get music time length (in seconds)
float GetMusicTimePlayed(Music music); // Get current music time played (in seconds)
// AudioStream management functions
AudioStream LoadAudioStream(unsigned int sampleRate, unsigned int sampleSize, unsigned int channels); // Load audio stream (to stream raw audio pcm data)
bool IsAudioStreamReady(AudioStream stream); // Checks if an audio stream is ready
void UnloadAudioStream(AudioStream stream); // Unload audio stream and free memory
void UpdateAudioStream(AudioStream stream, const void *data, int frameCount); // Update audio stream buffers with data
bool IsAudioStreamProcessed(AudioStream stream); // Check if any audio stream buffers requires refill
void PlayAudioStream(AudioStream stream); // Play audio stream
void PauseAudioStream(AudioStream stream); // Pause audio stream
void ResumeAudioStream(AudioStream stream); // Resume audio stream
bool IsAudioStreamPlaying(AudioStream stream); // Check if audio stream is playing
void StopAudioStream(AudioStream stream); // Stop audio stream
void SetAudioStreamVolume(AudioStream stream, float volume); // Set volume for audio stream (1.0 is max level)
void SetAudioStreamPitch(AudioStream stream, float pitch); // Set pitch for audio stream (1.0 is base level)
void SetAudioStreamPan(AudioStream stream, float pan); // Set pan for audio stream (0.5 is centered)
void SetAudioStreamBufferSizeDefault(int size); // Default size for new audio streams
void SetAudioStreamCallback(AudioStream stream, AudioCallback callback); // Audio thread callback to request new data
void AttachAudioStreamProcessor(AudioStream stream, AudioCallback processor); // Attach audio stream processor to stream, receives the samples as <float>s
void DetachAudioStreamProcessor(AudioStream stream, AudioCallback processor); // Detach audio stream processor from stream
void AttachAudioMixedProcessor(AudioCallback processor); // Attach audio stream processor to the entire audio pipeline, receives the samples as <float>s
void DetachAudioMixedProcessor(AudioCallback processor); // Detach audio stream processor from the entire audio pipeline
structs
struct Vector2; // Vector2, 2 components
struct Vector3; // Vector3, 3 components
struct Vector4; // Vector4, 4 components
struct Matrix; // Matrix, 4x4 components, column major, OpenGL style, right handed
struct Color; // Color, 4 components, R8G8B8A8 (32bit)
struct Rectangle; // Rectangle, 4 components
struct Image; // Image, pixel data stored in CPU memory (RAM)
struct Texture; // Texture, tex data stored in GPU memory (VRAM)
struct RenderTexture; // RenderTexture, fbo for texture rendering
struct NPatchInfo; // NPatchInfo, n-patch layout info
struct GlyphInfo; // GlyphInfo, font characters glyphs info
struct Font; // Font, font texture and GlyphInfo array data
struct Camera3D; // Camera, defines position/orientation in 3d space
struct Camera2D; // Camera2D, defines position/orientation in 2d space
struct Mesh; // Mesh, vertex data and vao/vbo
struct Shader; // Shader
struct MaterialMap; // MaterialMap
struct Material; // Material, includes shader and maps
struct Transform; // Transform, vectex transformation data
struct BoneInfo; // Bone, skeletal animation bone
struct Model; // Model, meshes, materials and animation data
struct ModelAnimation; // ModelAnimation
struct Ray; // Ray, ray for raycasting
struct RayCollision; // RayCollision, ray hit information
struct BoundingBox; // BoundingBox
struct Wave; // Wave, audio wave data
struct AudioStream; // AudioStream, custom audio stream
struct Sound; // Sound
struct Music; // Music, audio stream, anything longer than ~10 seconds should be streamed
struct VrDeviceInfo; // VrDeviceInfo, Head-Mounted-Display device parameters
struct VrStereoConfig; // VrStereoConfig, VR stereo rendering configuration for simulator
struct FilePathList; // File path list
struct AutomationEvent; // Automation event
struct AutomationEventList; // Automation event list
colors
// Custom raylib color palette for amazing visuals on WHITE background
#define LIGHTGRAY (Color){ 200, 200, 200, 255 } // Light Gray
#define GRAY (Color){ 130, 130, 130, 255 } // Gray
#define DARKGRAY (Color){ 80, 80, 80, 255 } // Dark Gray
#define YELLOW (Color){ 253, 249, 0, 255 } // Yellow
#define GOLD (Color){ 255, 203, 0, 255 } // Gold
#define ORANGE (Color){ 255, 161, 0, 255 } // Orange
#define PINK (Color){ 255, 109, 194, 255 } // Pink
#define RED (Color){ 230, 41, 55, 255 } // Red
#define MAROON (Color){ 190, 33, 55, 255 } // Maroon
#define GREEN (Color){ 0, 228, 48, 255 } // Green
#define LIME (Color){ 0, 158, 47, 255 } // Lime
#define DARKGREEN (Color){ 0, 117, 44, 255 } // Dark Green
#define SKYBLUE (Color){ 102, 191, 255, 255 } // Sky Blue
#define BLUE (Color){ 0, 121, 241, 255 } // Blue
#define DARKBLUE (Color){ 0, 82, 172, 255 } // Dark Blue
#define PURPLE (Color){ 200, 122, 255, 255 } // Purple
#define VIOLET (Color){ 135, 60, 190, 255 } // Violet
#define DARKPURPLE (Color){ 112, 31, 126, 255 } // Dark Purple
#define BEIGE (Color){ 211, 176, 131, 255 } // Beige
#define BROWN (Color){ 127, 106, 79, 255 } // Brown
#define DARKBROWN (Color){ 76, 63, 47, 255 } // Dark Brown
#define WHITE (Color){ 255, 255, 255, 255 } // White
#define BLACK (Color){ 0, 0, 0, 255 } // Black
#define BLANK (Color){ 0, 0, 0, 0 } // Blank (Transparent)
#define MAGENTA (Color){ 255, 0, 255, 255 } // Magenta
#define RAYWHITE (Color){ 245, 245, 245, 255 } // My own White (raylib logo)
module: raymath
// Utils math
float Clamp(float value, float min, float max); // Function specifiers definition Defines and Macros Get float vector for Matrix Get float vector for Vector3 Types and Structures Definition Vector2 type Vector3 type Vector4 type Quaternion type Matrix type (OpenGL style 4x4 - right handed, column major) NOTE: Helper types to be used instead of array return types for *ToFloat functions Clamp float value
float Lerp(float start, float end, float amount); // Calculate linear interpolation between two floats
float Normalize(float value, float start, float end); // Normalize input value within input range
float Remap(float value, float inputStart, float inputEnd, float outputStart, float outputEnd); // Remap input value within input range to output range
float Wrap(float value, float min, float max); // Wrap input value from min to max
int FloatEquals(float x, float y); // Check whether two given floats are almost equal
// Vector2 math
Vector2 Vector2Zero(void); // Vector with components value 0.0f
Vector2 Vector2One(void); // Vector with components value 1.0f
Vector2 Vector2Add(Vector2 v1, Vector2 v2); // Add two vectors (v1 + v2)
Vector2 Vector2AddValue(Vector2 v, float add); // Add vector and float value
Vector2 Vector2Subtract(Vector2 v1, Vector2 v2); // Subtract two vectors (v1 - v2)
Vector2 Vector2SubtractValue(Vector2 v, float sub); // Subtract vector by float value
float Vector2Length(Vector2 v); // Calculate vector length
float Vector2LengthSqr(Vector2 v); // Calculate vector square length
float Vector2DotProduct(Vector2 v1, Vector2 v2); // Calculate two vectors dot product
float Vector2Distance(Vector2 v1, Vector2 v2); // Calculate distance between two vectors
float Vector2DistanceSqr(Vector2 v1, Vector2 v2); // Calculate square distance between two vectors
float Vector2Angle(Vector2 v1, Vector2 v2); // Calculate angle from two vectors
Vector2 Vector2Scale(Vector2 v, float scale); // Scale vector (multiply by value)
Vector2 Vector2Multiply(Vector2 v1, Vector2 v2); // Multiply vector by vector
Vector2 Vector2Negate(Vector2 v); // Negate vector
Vector2 Vector2Divide(Vector2 v1, Vector2 v2); // Divide vector by vector
Vector2 Vector2Normalize(Vector2 v); // Normalize provided vector
Vector2 Vector2Transform(Vector2 v, Matrix mat); // Transforms a Vector2 by a given Matrix
Vector2 Vector2Lerp(Vector2 v1, Vector2 v2, float amount); // Calculate linear interpolation between two vectors
Vector2 Vector2Reflect(Vector2 v, Vector2 normal); // Calculate reflected vector to normal
Vector2 Vector2Rotate(Vector2 v, float angle); // Rotate vector by angle
Vector2 Vector2MoveTowards(Vector2 v, Vector2 target, float maxDistance); // Move Vector towards target
Vector2 Vector2Invert(Vector2 v); // Invert the given vector
Vector2 Vector2Clamp(Vector2 v, Vector2 min, Vector2 max); // Clamp the components of the vector between min and max values specified by the given vectors
Vector2 Vector2ClampValue(Vector2 v, float min, float max); // Clamp the magnitude of the vector between two min and max values
int Vector2Equals(Vector2 p, Vector2 q); // Check whether two given vectors are almost equal
// Vector3 math
Vector3 Vector3Zero(void); // Vector with components value 0.0f
Vector3 Vector3One(void); // Vector with components value 1.0f
Vector3 Vector3Add(Vector3 v1, Vector3 v2); // Add two vectors
Vector3 Vector3AddValue(Vector3 v, float add); // Add vector and float value
Vector3 Vector3Subtract(Vector3 v1, Vector3 v2); // Subtract two vectors
Vector3 Vector3SubtractValue(Vector3 v, float sub); // Subtract vector by float value
Vector3 Vector3Scale(Vector3 v, float scalar); // Multiply vector by scalar
Vector3 Vector3Multiply(Vector3 v1, Vector3 v2); // Multiply vector by vector
Vector3 Vector3CrossProduct(Vector3 v1, Vector3 v2); // Calculate two vectors cross product
Vector3 Vector3Perpendicular(Vector3 v); // Calculate one vector perpendicular vector
float Vector3Length(const Vector3 v); // Calculate vector length
float Vector3LengthSqr(const Vector3 v); // Calculate vector square length
float Vector3DotProduct(Vector3 v1, Vector3 v2); // Calculate two vectors dot product
float Vector3Distance(Vector3 v1, Vector3 v2); // Calculate distance between two vectors
float Vector3DistanceSqr(Vector3 v1, Vector3 v2); // Calculate square distance between two vectors
float Vector3Angle(Vector3 v1, Vector3 v2); // Calculate angle between two vectors
Vector3 Vector3Negate(Vector3 v); // Negate provided vector (invert direction)
Vector3 Vector3Divide(Vector3 v1, Vector3 v2); // Divide vector by vector
Vector3 Vector3Normalize(Vector3 v); // Normalize provided vector
void Vector3OrthoNormalize(Vector3 *v1, Vector3 *v2); // Orthonormalize provided vectors Makes vectors normalized and orthogonal to each other Gram-Schmidt function implementation
Vector3 Vector3Transform(Vector3 v, Matrix mat); // Transforms a Vector3 by a given Matrix
Vector3 Vector3RotateByQuaternion(Vector3 v, Quaternion q); // Transform a vector by quaternion rotation
Vector3 Vector3RotateByAxisAngle(Vector3 v, Vector3 axis, float angle); // Rotates a vector around an axis
Vector3 Vector3Lerp(Vector3 v1, Vector3 v2, float amount); // Calculate linear interpolation between two vectors
Vector3 Vector3Reflect(Vector3 v, Vector3 normal); // Calculate reflected vector to normal
Vector3 Vector3Min(Vector3 v1, Vector3 v2); // Get min value for each pair of components
Vector3 Vector3Max(Vector3 v1, Vector3 v2); // Get max value for each pair of components
Vector3 Vector3Barycenter(Vector3 p, Vector3 a, Vector3 b, Vector3 c); // Compute barycenter coordinates (u, v, w) for point p with respect to triangle (a, b, c) NOTE: Assumes P is on the plane of the triangle
Vector3 Vector3Unproject(Vector3 source, Matrix projection, Matrix view); // Projects a Vector3 from screen space into object space NOTE: We are avoiding calling other raymath functions despite available
float3 Vector3ToFloatV(Vector3 v); // Get Vector3 as float array
Vector3 Vector3Invert(Vector3 v); // Invert the given vector
Vector3 Vector3Clamp(Vector3 v, Vector3 min, Vector3 max); // Clamp the components of the vector between min and max values specified by the given vectors
Vector3 Vector3ClampValue(Vector3 v, float min, float max); // Clamp the magnitude of the vector between two values
int Vector3Equals(Vector3 p, Vector3 q); // Check whether two given vectors are almost equal
Vector3 Vector3Refract(Vector3 v, Vector3 n, float r); // Compute the direction of a refracted ray where v specifies the normalized direction of the incoming ray, n specifies the normalized normal vector of the interface of two optical media, and r specifies the ratio of the refractive index of the medium from where the ray comes to the refractive index of the medium on the other side of the surface
// Matrix math
float MatrixDeterminant(Matrix mat); // Compute matrix determinant
float MatrixTrace(Matrix mat); // Get the trace of the matrix (sum of the values along the diagonal)
Matrix MatrixTranspose(Matrix mat); // Transposes provided matrix
Matrix MatrixInvert(Matrix mat); // Invert provided matrix
Matrix MatrixIdentity(void); // Get identity matrix
Matrix MatrixAdd(Matrix left, Matrix right); // Add two matrices
Matrix MatrixSubtract(Matrix left, Matrix right); // Subtract two matrices (left - right)
Matrix MatrixMultiply(Matrix left, Matrix right); // Get two matrix multiplication NOTE: When multiplying matrices... the order matters!
Matrix MatrixTranslate(float x, float y, float z); // Get translation matrix
Matrix MatrixRotate(Vector3 axis, float angle); // Create rotation matrix from axis and angle NOTE: Angle should be provided in radians
Matrix MatrixRotateX(float angle); // Get x-rotation matrix NOTE: Angle must be provided in radians
Matrix MatrixRotateY(float angle); // Get y-rotation matrix NOTE: Angle must be provided in radians
Matrix MatrixRotateZ(float angle); // Get z-rotation matrix NOTE: Angle must be provided in radians
Matrix MatrixRotateXYZ(Vector3 angle); // Get xyz-rotation matrix NOTE: Angle must be provided in radians
Matrix MatrixRotateZYX(Vector3 angle); // Get zyx-rotation matrix NOTE: Angle must be provided in radians
Matrix MatrixScale(float x, float y, float z); // Get scaling matrix
Matrix MatrixFrustum(double left, double right, double bottom, double top, double near, double far); // Get perspective projection matrix
Matrix MatrixPerspective(double fovy, double aspect, double near, double far); // Get perspective projection matrix NOTE: Fovy angle must be provided in radians
Matrix MatrixOrtho(double left, double right, double bottom, double top, double near, double far); // Get orthographic projection matrix
Matrix MatrixLookAt(Vector3 eye, Vector3 target, Vector3 up); // Get camera look-at matrix (view matrix)
float16 MatrixToFloatV(Matrix mat); // Get float array of matrix data
// Quaternion math
Quaternion QuaternionAdd(Quaternion q1, Quaternion q2); // Add two quaternions
Quaternion QuaternionAddValue(Quaternion q, float add); // Add quaternion and float value
Quaternion QuaternionSubtract(Quaternion q1, Quaternion q2); // Subtract two quaternions
Quaternion QuaternionSubtractValue(Quaternion q, float sub); // Subtract quaternion and float value
Quaternion QuaternionIdentity(void); // Get identity quaternion
float QuaternionLength(Quaternion q); // Computes the length of a quaternion
Quaternion QuaternionNormalize(Quaternion q); // Normalize provided quaternion
Quaternion QuaternionInvert(Quaternion q); // Invert provided quaternion
Quaternion QuaternionMultiply(Quaternion q1, Quaternion q2); // Calculate two quaternion multiplication
Quaternion QuaternionScale(Quaternion q, float mul); // Scale quaternion by float value
Quaternion QuaternionDivide(Quaternion q1, Quaternion q2); // Divide two quaternions
Quaternion QuaternionLerp(Quaternion q1, Quaternion q2, float amount); // Calculate linear interpolation between two quaternions
Quaternion QuaternionNlerp(Quaternion q1, Quaternion q2, float amount); // Calculate slerp-optimized interpolation between two quaternions
Quaternion QuaternionSlerp(Quaternion q1, Quaternion q2, float amount); // Calculates spherical linear interpolation between two quaternions
Quaternion QuaternionFromVector3ToVector3(Vector3 from, Vector3 to); // Calculate quaternion based on the rotation from one vector to another
Quaternion QuaternionFromMatrix(Matrix mat); // Get a quaternion for a given rotation matrix
Matrix QuaternionToMatrix(Quaternion q); // Get a matrix for a given quaternion
Quaternion QuaternionFromAxisAngle(Vector3 axis, float angle); // Get rotation quaternion for an angle and axis NOTE: Angle must be provided in radians
void QuaternionToAxisAngle(Quaternion q, Vector3 *outAxis, float *outAngle); // Get the rotation angle and axis for a given quaternion
Quaternion QuaternionFromEuler(float pitch, float yaw, float roll); // Get the quaternion equivalent to Euler angles NOTE: Rotation order is ZYX
Vector3 QuaternionToEuler(Quaternion q); // Get the Euler angles equivalent to quaternion (roll, pitch, yaw) NOTE: Angles are returned in a Vector3 struct in radians
Quaternion QuaternionTransform(Quaternion q, Matrix mat); // Transform a quaternion given a transformation matrix
int QuaternionEquals(Quaternion p, Quaternion q); // Check whether two given quaternions are almost equal
RLGL
[edit | edit source]#include <raylib.h>
int main() {
InitWindow(1280, 720, "Model Loading");
Model model = LoadModel("Downloads/LOD0.obj");
Texture2D tex = LoadTexture("Downloads/Thing.png");
model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = tex;
Camera cam = {0};
cam.position = (Vector3){50.0f,50.0f,50.0f};
cam.target = (Vector3){0.0f,0.0f,0.0f};
cam.up = (Vector3){0.0f,1.0f,0.0f};
cam.fovy = 90.f;
cam.projection = CAMERA_PERSPECTIVE;
Vector3 pos = {0.0f,0.0f,0.0f};
Vector3 pos2 = {200.0f,1.0f,0.0f};
BoundingBox bounds = GetMeshBoundingBox(model.meshes[0]);
SetTargetFPS(60);
SetCameraMode(cam, CAMERA_THIRD_PERSON);
while(!WindowShouldClose()) {
UpdateCamera(&cam);
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode3D(cam);
DrawModel(model, pos, 1.0f, WHITE);
DrawModel(model, pos2, 1.0f, WHITE);
DrawGrid(20, 10.0f);
DrawBoundingBox(bounds, GREEN);
EndMode3D();
DrawText("Loading obj file", 10, GetScreenHeight()-25, 25, DARKGRAY);
DrawFPS(10,10);
EndDrawing();
}
UnloadTexture(tex);
UnloadModel(model);
CloseWindow();
return 0;
}
/*******************************************************************************************
*
* raylib [models] example - rlgl module usage with push/pop matrix transformations
*
* NOTE: This example uses [rlgl] module functionality (pseudo-OpenGL 1.1 style coding)
*
* Example originally created with raylib 2.5, last time updated with raylib 4.0
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2018-2024 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#include "rlgl.h"
#include <math.h> // Required for: cosf(), sinf()
//------------------------------------------------------------------------------------
// Module Functions Declaration
//------------------------------------------------------------------------------------
void DrawSphereBasic(Color color); // Draw sphere without any matrix transformation
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
const float sunRadius = 4.0f;
const float earthRadius = 0.6f;
const float earthOrbitRadius = 8.0f;
const float moonRadius = 0.16f;
const float moonOrbitRadius = 1.5f;
InitWindow(screenWidth, screenHeight, "raylib [models] example - rlgl module usage with push/pop matrix transformations");
// Define the camera to look into our 3d world
Camera camera = { 0 };
camera.position = (Vector3){ 16.0f, 16.0f, 16.0f }; // Camera position
camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
camera.fovy = 45.0f; // Camera field-of-view Y
camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
float rotationSpeed = 0.2f; // General system rotation speed
float earthRotation = 0.0f; // Rotation of earth around itself (days) in degrees
float earthOrbitRotation = 0.0f; // Rotation of earth around the Sun (years) in degrees
float moonRotation = 0.0f; // Rotation of moon around itself
float moonOrbitRotation = 0.0f; // Rotation of moon around earth in degrees
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(&camera, CAMERA_ORBITAL);
earthRotation += (5.0f*rotationSpeed);
earthOrbitRotation += (365/360.0f*(5.0f*rotationSpeed)*rotationSpeed);
moonRotation += (2.0f*rotationSpeed);
moonOrbitRotation += (8.0f*rotationSpeed);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode3D(camera);
rlPushMatrix();
rlScalef(sunRadius, sunRadius, sunRadius); // Scale Sun
DrawSphereBasic(GOLD); // Draw the Sun
rlPopMatrix();
rlPushMatrix();
rlRotatef(earthOrbitRotation, 0.0f, 1.0f, 0.0f); // Rotation for Earth orbit around Sun
rlTranslatef(earthOrbitRadius, 0.0f, 0.0f); // Translation for Earth orbit
rlPushMatrix();
rlRotatef(earthRotation, 0.25, 1.0, 0.0); // Rotation for Earth itself
rlScalef(earthRadius, earthRadius, earthRadius);// Scale Earth
DrawSphereBasic(BLUE); // Draw the Earth
rlPopMatrix();
rlRotatef(moonOrbitRotation, 0.0f, 1.0f, 0.0f); // Rotation for Moon orbit around Earth
rlTranslatef(moonOrbitRadius, 0.0f, 0.0f); // Translation for Moon orbit
rlRotatef(moonRotation, 0.0f, 1.0f, 0.0f); // Rotation for Moon itself
rlScalef(moonRadius, moonRadius, moonRadius); // Scale Moon
DrawSphereBasic(LIGHTGRAY); // Draw the Moon
rlPopMatrix();
// Some reference elements (not affected by previous matrix transformations)
DrawCircle3D((Vector3){ 0.0f, 0.0f, 0.0f }, earthOrbitRadius, (Vector3){ 1, 0, 0 }, 90.0f, Fade(RED, 0.5f));
DrawGrid(20, 1.0f);
EndMode3D();
DrawText("EARTH ORBITING AROUND THE SUN!", 400, 10, 20, MAROON);
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
//--------------------------------------------------------------------------------------------
// Module Functions Definitions (local)
//--------------------------------------------------------------------------------------------
// Draw sphere without any matrix transformation
// NOTE: Sphere is drawn in world position ( 0, 0, 0 ) with radius 1.0f
void DrawSphereBasic(Color color)
{
int rings = 16;
int slices = 16;
// Make sure there is enough space in the internal render batch
// buffer to store all required vertex, batch is reseted if required
rlCheckRenderBatchLimit((rings + 2)*slices*6);
rlBegin(RL_TRIANGLES);
rlColor4ub(color.r, color.g, color.b, color.a);
for (int i = 0; i < (rings + 2); i++)
{
for (int j = 0; j < slices; j++)
{
rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*i))*sinf(DEG2RAD*(j*360/slices)),
sinf(DEG2RAD*(270+(180/(rings + 1))*i)),
cosf(DEG2RAD*(270+(180/(rings + 1))*i))*cosf(DEG2RAD*(j*360/slices)));
rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*sinf(DEG2RAD*((j+1)*360/slices)),
sinf(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*cosf(DEG2RAD*((j+1)*360/slices)));
rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*sinf(DEG2RAD*(j*360/slices)),
sinf(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*cosf(DEG2RAD*(j*360/slices)));
rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*i))*sinf(DEG2RAD*(j*360/slices)),
sinf(DEG2RAD*(270+(180/(rings + 1))*i)),
cosf(DEG2RAD*(270+(180/(rings + 1))*i))*cosf(DEG2RAD*(j*360/slices)));
rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i)))*sinf(DEG2RAD*((j+1)*360/slices)),
sinf(DEG2RAD*(270+(180/(rings + 1))*(i))),
cosf(DEG2RAD*(270+(180/(rings + 1))*(i)))*cosf(DEG2RAD*((j+1)*360/slices)));
rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*sinf(DEG2RAD*((j+1)*360/slices)),
sinf(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*cosf(DEG2RAD*((j+1)*360/slices)));
}
}
rlEnd();
}
/*******************************************************************************************
*
* raylib [rlgl] example - compute shader - Conway's Game of Life
*
* NOTE: This example requires raylib OpenGL 4.3 versions for compute shaders support,
* shaders used in this example are #version 430 (OpenGL 4.3)
*
* Example originally created with raylib 4.0, last time updated with raylib 2.5
*
* Example contributed by Teddy Astie (@tsnake41) and reviewed by Ramon Santamaria (@raysan5)
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2021-2024 Teddy Astie (@tsnake41)
*
********************************************************************************************/
#include "raylib.h"
#include "rlgl.h"
#include <stdlib.h>
// IMPORTANT: This must match gol*.glsl GOL_WIDTH constant.
// This must be a multiple of 16 (check golLogic compute dispatch).
#define GOL_WIDTH 768
// Maximum amount of queued draw commands (squares draw from mouse down events).
#define MAX_BUFFERED_TRANSFERTS 48
// Game Of Life Update Command
typedef struct GolUpdateCmd {
unsigned int x; // x coordinate of the gol command
unsigned int y; // y coordinate of the gol command
unsigned int w; // width of the filled zone
unsigned int enabled; // whether to enable or disable zone
} GolUpdateCmd;
// Game Of Life Update Commands SSBO
typedef struct GolUpdateSSBO {
unsigned int count;
GolUpdateCmd commands[MAX_BUFFERED_TRANSFERTS];
} GolUpdateSSBO;
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(GOL_WIDTH, GOL_WIDTH, "raylib [rlgl] example - compute shader - game of life");
const Vector2 resolution = { GOL_WIDTH, GOL_WIDTH };
unsigned int brushSize = 8;
// Game of Life logic compute shader
char *golLogicCode = LoadFileText("resources/shaders/glsl430/gol.glsl");
unsigned int golLogicShader = rlCompileShader(golLogicCode, RL_COMPUTE_SHADER);
unsigned int golLogicProgram = rlLoadComputeShaderProgram(golLogicShader);
UnloadFileText(golLogicCode);
// Game of Life logic render shader
Shader golRenderShader = LoadShader(NULL, "resources/shaders/glsl430/gol_render.glsl");
int resUniformLoc = GetShaderLocation(golRenderShader, "resolution");
// Game of Life transfert shader (CPU<->GPU download and upload)
char *golTransfertCode = LoadFileText("resources/shaders/glsl430/gol_transfert.glsl");
unsigned int golTransfertShader = rlCompileShader(golTransfertCode, RL_COMPUTE_SHADER);
unsigned int golTransfertProgram = rlLoadComputeShaderProgram(golTransfertShader);
UnloadFileText(golTransfertCode);
// Load shader storage buffer object (SSBO), id returned
unsigned int ssboA = rlLoadShaderBuffer(GOL_WIDTH*GOL_WIDTH*sizeof(unsigned int), NULL, RL_DYNAMIC_COPY);
unsigned int ssboB = rlLoadShaderBuffer(GOL_WIDTH*GOL_WIDTH*sizeof(unsigned int), NULL, RL_DYNAMIC_COPY);
unsigned int ssboTransfert = rlLoadShaderBuffer(sizeof(GolUpdateSSBO), NULL, RL_DYNAMIC_COPY);
GolUpdateSSBO transfertBuffer = { 0 };
// Create a white texture of the size of the window to update
// each pixel of the window using the fragment shader: golRenderShader
Image whiteImage = GenImageColor(GOL_WIDTH, GOL_WIDTH, WHITE);
Texture whiteTex = LoadTextureFromImage(whiteImage);
UnloadImage(whiteImage);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
brushSize += (int)GetMouseWheelMove();
if ((IsMouseButtonDown(MOUSE_BUTTON_LEFT) || IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
&& (transfertBuffer.count < MAX_BUFFERED_TRANSFERTS))
{
// Buffer a new command
transfertBuffer.commands[transfertBuffer.count].x = GetMouseX() - brushSize/2;
transfertBuffer.commands[transfertBuffer.count].y = GetMouseY() - brushSize/2;
transfertBuffer.commands[transfertBuffer.count].w = brushSize;
transfertBuffer.commands[transfertBuffer.count].enabled = IsMouseButtonDown(MOUSE_BUTTON_LEFT);
transfertBuffer.count++;
}
else if (transfertBuffer.count > 0) // Process transfert buffer
{
// Send SSBO buffer to GPU
rlUpdateShaderBuffer(ssboTransfert, &transfertBuffer, sizeof(GolUpdateSSBO), 0);
// Process SSBO commands on GPU
rlEnableShader(golTransfertProgram);
rlBindShaderBuffer(ssboA, 1);
rlBindShaderBuffer(ssboTransfert, 3);
rlComputeShaderDispatch(transfertBuffer.count, 1, 1); // Each GPU unit will process a command!
rlDisableShader();
transfertBuffer.count = 0;
}
else
{
// Process game of life logic
rlEnableShader(golLogicProgram);
rlBindShaderBuffer(ssboA, 1);
rlBindShaderBuffer(ssboB, 2);
rlComputeShaderDispatch(GOL_WIDTH/16, GOL_WIDTH/16, 1);
rlDisableShader();
// ssboA <-> ssboB
int temp = ssboA;
ssboA = ssboB;
ssboB = temp;
}
rlBindShaderBuffer(ssboA, 1);
SetShaderValue(golRenderShader, resUniformLoc, &resolution, SHADER_UNIFORM_VEC2);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(BLANK);
BeginShaderMode(golRenderShader);
DrawTexture(whiteTex, 0, 0, WHITE);
EndShaderMode();
DrawRectangleLines(GetMouseX() - brushSize/2, GetMouseY() - brushSize/2, brushSize, brushSize, RED);
DrawText("Use Mouse wheel to increase/decrease brush size", 10, 10, 20, WHITE);
DrawFPS(GetScreenWidth() - 100, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
// Unload shader buffers objects.
rlUnloadShaderBuffer(ssboA);
rlUnloadShaderBuffer(ssboB);
rlUnloadShaderBuffer(ssboTransfert);
// Unload compute shader programs
rlUnloadShaderProgram(golTransfertProgram);
rlUnloadShaderProgram(golLogicProgram);
UnloadTexture(whiteTex); // Unload white texture
UnloadShader(golRenderShader); // Unload rendering fragment shader
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
/*******************************************************************************************
*
* raylib [shaders] example - OpenGL point particle system
*
* Example originally created with raylib 3.8, last time updated with raylib 2.5
*
* Example contributed by Stephan Soller (@arkanis) and reviewed by Ramon Santamaria (@raysan5)
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2021-2024 Stephan Soller (@arkanis) and Ramon Santamaria (@raysan5)
*
********************************************************************************************
*
* Mixes raylib and plain OpenGL code to draw a GL_POINTS based particle system. The
* primary point is to demonstrate raylib and OpenGL interop.
*
* rlgl batched draw operations internally so we have to flush the current batch before
* doing our own OpenGL work (rlDrawRenderBatchActive()).
*
* The example also demonstrates how to get the current model view projection matrix of
* raylib. That way raylib cameras and so on work as expected.
*
********************************************************************************************/
#include "raylib.h"
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_DESKTOP_SDL)
#if defined(GRAPHICS_API_OPENGL_ES2)
#include "glad_gles2.h" // Required for: OpenGL functionality
#define glGenVertexArrays glGenVertexArraysOES
#define glBindVertexArray glBindVertexArrayOES
#define glDeleteVertexArrays glDeleteVertexArraysOES
#define GLSL_VERSION 100
#else
#if defined(__APPLE__)
#define GL_SILENCE_DEPRECATION // Silence Opengl API deprecation warnings
#include <OpenGL/gl3.h> // OpenGL 3 library for OSX
#include <OpenGL/gl3ext.h> // OpenGL 3 extensions library for OSX
#else
#include "glad.h" // Required for: OpenGL functionality
#endif
#define GLSL_VERSION 330
#endif
#else // PLATFORM_ANDROID, PLATFORM_WEB
#define GLSL_VERSION 100
#endif
#include "rlgl.h" // Required for: rlDrawRenderBatchActive(), rlGetMatrixModelview(), rlGetMatrixProjection()
#include "raymath.h" // Required for: MatrixMultiply(), MatrixToFloat()
#define MAX_PARTICLES 1000
// Particle type
typedef struct Particle {
float x;
float y;
float period;
} Particle;
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib - point particles");
Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/point_particle.vs", GLSL_VERSION),
TextFormat("resources/shaders/glsl%i/point_particle.fs", GLSL_VERSION));
int currentTimeLoc = GetShaderLocation(shader, "currentTime");
int colorLoc = GetShaderLocation(shader, "color");
// Initialize the vertex buffer for the particles and assign each particle random values
Particle particles[MAX_PARTICLES] = { 0 };
for (int i = 0; i < MAX_PARTICLES; i++)
{
particles[i].x = (float)GetRandomValue(20, screenWidth - 20);
particles[i].y = (float)GetRandomValue(50, screenHeight - 20);
// Give each particle a slightly different period. But don't spread it to much.
// This way the particles line up every so often and you get a glimps of what is going on.
particles[i].period = (float)GetRandomValue(10, 30)/10.0f;
}
// Create a plain OpenGL vertex buffer with the data and an vertex array object
// that feeds the data from the buffer into the vertexPosition shader attribute.
GLuint vao = 0;
GLuint vbo = 0;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, MAX_PARTICLES*sizeof(Particle), particles, GL_STATIC_DRAW);
// Note: LoadShader() automatically fetches the attribute index of "vertexPosition" and saves it in shader.locs[SHADER_LOC_VERTEX_POSITION]
glVertexAttribPointer(shader.locs[SHADER_LOC_VERTEX_POSITION], 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
// Allows the vertex shader to set the point size of each particle individually
#ifndef GRAPHICS_API_OPENGL_ES2
glEnable(GL_PROGRAM_POINT_SIZE);
#endif
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(WHITE);
DrawRectangle(10, 10, 210, 30, MAROON);
DrawText(TextFormat("%zu particles in one vertex buffer", MAX_PARTICLES), 20, 20, 10, RAYWHITE);
rlDrawRenderBatchActive(); // Draw iternal buffers data (previous draw calls)
// Switch to plain OpenGL
//------------------------------------------------------------------------------
glUseProgram(shader.id);
glUniform1f(currentTimeLoc, GetTime());
Vector4 color = ColorNormalize((Color){ 255, 0, 0, 128 });
glUniform4fv(colorLoc, 1, (float *)&color);
// Get the current modelview and projection matrix so the particle system is displayed and transformed
Matrix modelViewProjection = MatrixMultiply(rlGetMatrixModelview(), rlGetMatrixProjection());
glUniformMatrix4fv(shader.locs[SHADER_LOC_MATRIX_MVP], 1, false, MatrixToFloat(modelViewProjection));
glBindVertexArray(vao);
glDrawArrays(GL_POINTS, 0, MAX_PARTICLES);
glBindVertexArray(0);
glUseProgram(0);
//------------------------------------------------------------------------------
DrawFPS(screenWidth - 100, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
glDeleteBuffers(1, &vbo);
glDeleteVertexArrays(1, &vao);
UnloadShader(shader); // Unload shader
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
rlgl5.h
[edit | edit source]/**********************************************************************************************
*
* rlgl v5.0 - A multi-OpenGL abstraction layer with an immediate-mode style API
*
* DESCRIPTION:
* An abstraction layer for multiple OpenGL versions (1.1, 2.1, 3.3 Core, 4.3 Core, ES 2.0)
* that provides a pseudo-OpenGL 1.1 immediate-mode style API (rlVertex, rlTranslate, rlRotate...)
*
* ADDITIONAL NOTES:
* When choosing an OpenGL backend different than OpenGL 1.1, some internal buffer are
* initialized on rlglInit() to accumulate vertex data.
*
* When an internal state change is required all the stored vertex data is renderer in batch,
* additionally, rlDrawRenderBatchActive() could be called to force flushing of the batch.
*
* Some resources are also loaded for convenience, here the complete list:
* - Default batch (RLGL.defaultBatch): RenderBatch system to accumulate vertex data
* - Default texture (RLGL.defaultTextureId): 1x1 white pixel R8G8B8A8
* - Default shader (RLGL.State.defaultShaderId, RLGL.State.defaultShaderLocs)
*
* Internal buffer (and resources) must be manually unloaded calling rlglClose().
*
* CONFIGURATION:
* #define GRAPHICS_API_OPENGL_11
* #define GRAPHICS_API_OPENGL_21
* #define GRAPHICS_API_OPENGL_33
* #define GRAPHICS_API_OPENGL_43
* #define GRAPHICS_API_OPENGL_ES2
* #define GRAPHICS_API_OPENGL_ES3
* Use selected OpenGL graphics backend, should be supported by platform
* Those preprocessor defines are only used on rlgl module, if OpenGL version is
* required by any other module, use rlGetVersion() to check it
*
* #define RLGL_IMPLEMENTATION
* Generates the implementation of the library into the included file.
* If not defined, the library is in header only mode and can be included in other headers
* or source files without problems. But only ONE file should hold the implementation.
*
* #define RLGL_RENDER_TEXTURES_HINT
* Enable framebuffer objects (fbo) support (enabled by default)
* Some GPUs could not support them despite the OpenGL version
*
* #define RLGL_SHOW_GL_DETAILS_INFO
* Show OpenGL extensions and capabilities detailed logs on init
*
* #define RLGL_ENABLE_OPENGL_DEBUG_CONTEXT
* Enable debug context (only available on OpenGL 4.3)
*
* rlgl capabilities could be customized just defining some internal
* values before library inclusion (default values listed):
*
* #define RL_DEFAULT_BATCH_BUFFER_ELEMENTS 8192 // Default internal render batch elements limits
* #define RL_DEFAULT_BATCH_BUFFERS 1 // Default number of batch buffers (multi-buffering)
* #define RL_DEFAULT_BATCH_DRAWCALLS 256 // Default number of batch draw calls (by state changes: mode, texture)
* #define RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS 4 // Maximum number of textures units that can be activated on batch drawing (SetShaderValueTexture())
*
* #define RL_MAX_MATRIX_STACK_SIZE 32 // Maximum size of internal Matrix stack
* #define RL_MAX_SHADER_LOCATIONS 32 // Maximum number of shader locations supported
* #define RL_CULL_DISTANCE_NEAR 0.01 // Default projection matrix near cull distance
* #define RL_CULL_DISTANCE_FAR 1000.0 // Default projection matrix far cull distance
*
* When loading a shader, the following vertex attributes and uniform
* location names are tried to be set automatically:
*
* #define RL_DEFAULT_SHADER_ATTRIB_NAME_POSITION "vertexPosition" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION
* #define RL_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD "vertexTexCoord" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD
* #define RL_DEFAULT_SHADER_ATTRIB_NAME_NORMAL "vertexNormal" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL
* #define RL_DEFAULT_SHADER_ATTRIB_NAME_COLOR "vertexColor" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_LOCATION_COLOR
* #define RL_DEFAULT_SHADER_ATTRIB_NAME_TANGENT "vertexTangent" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_LOCATION_TANGENT
* #define RL_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD2 "vertexTexCoord2" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD2
* #define RL_DEFAULT_SHADER_UNIFORM_NAME_MVP "mvp" // model-view-projection matrix
* #define RL_DEFAULT_SHADER_UNIFORM_NAME_VIEW "matView" // view matrix
* #define RL_DEFAULT_SHADER_UNIFORM_NAME_PROJECTION "matProjection" // projection matrix
* #define RL_DEFAULT_SHADER_UNIFORM_NAME_MODEL "matModel" // model matrix
* #define RL_DEFAULT_SHADER_UNIFORM_NAME_NORMAL "matNormal" // normal matrix (transpose(inverse(matModelView)))
* #define RL_DEFAULT_SHADER_UNIFORM_NAME_COLOR "colDiffuse" // color diffuse (base tint color, multiplied by texture color)
* #define RL_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE0 "texture0" // texture0 (texture slot active 0)
* #define RL_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE1 "texture1" // texture1 (texture slot active 1)
* #define RL_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE2 "texture2" // texture2 (texture slot active 2)
*
* DEPENDENCIES:
* - OpenGL libraries (depending on platform and OpenGL version selected)
* - GLAD OpenGL extensions loading library (only for OpenGL 3.3 Core, 4.3 Core)
*
*
* LICENSE: zlib/libpng
*
* Copyright (c) 2014-2024 Ramon Santamaria (@raysan5)
*
* This software is provided "as-is", without any express or implied warranty. In no event
* will the authors be held liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose, including commercial
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not claim that you
* wrote the original software. If you use this software in a product, an acknowledgment
* in the product documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
* as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*
**********************************************************************************************/
#ifndef RLGL_H
#define RLGL_H
#define RLGL_VERSION "5.0"
// Function specifiers in case library is build/used as a shared library
// NOTE: Microsoft specifiers to tell compiler that symbols are imported/exported from a .dll
// NOTE: visibility(default) attribute makes symbols "visible" when compiled with -fvisibility=hidden
#if defined(_WIN32) && defined(BUILD_LIBTYPE_SHARED)
#define RLAPI __declspec(dllexport) // We are building the library as a Win32 shared library (.dll)
#elif defined(BUILD_LIBTYPE_SHARED)
#define RLAPI __attribute__((visibility("default"))) // We are building the library as a Unix shared library (.so/.dylib)
#elif defined(_WIN32) && defined(USE_LIBTYPE_SHARED)
#define RLAPI __declspec(dllimport) // We are using the library as a Win32 shared library (.dll)
#endif
// Function specifiers definition
#ifndef RLAPI
#define RLAPI // Functions defined as 'extern' by default (implicit specifiers)
#endif
// Support TRACELOG macros
#ifndef TRACELOG
#define TRACELOG(level, ...) (void)0
#define TRACELOGD(...) (void)0
#endif
// Allow custom memory allocators
#ifndef RL_MALLOC
#define RL_MALLOC(sz) malloc(sz)
#endif
#ifndef RL_CALLOC
#define RL_CALLOC(n,sz) calloc(n,sz)
#endif
#ifndef RL_REALLOC
#define RL_REALLOC(n,sz) realloc(n,sz)
#endif
#ifndef RL_FREE
#define RL_FREE(p) free(p)
#endif
// Security check in case no GRAPHICS_API_OPENGL_* defined
#if !defined(GRAPHICS_API_OPENGL_11) && \
!defined(GRAPHICS_API_OPENGL_21) && \
!defined(GRAPHICS_API_OPENGL_33) && \
!defined(GRAPHICS_API_OPENGL_43) && \
!defined(GRAPHICS_API_OPENGL_ES2) && \
!defined(GRAPHICS_API_OPENGL_ES3)
#define GRAPHICS_API_OPENGL_33
#endif
// Security check in case multiple GRAPHICS_API_OPENGL_* defined
#if defined(GRAPHICS_API_OPENGL_11)
#if defined(GRAPHICS_API_OPENGL_21)
#undef GRAPHICS_API_OPENGL_21
#endif
#if defined(GRAPHICS_API_OPENGL_33)
#undef GRAPHICS_API_OPENGL_33
#endif
#if defined(GRAPHICS_API_OPENGL_43)
#undef GRAPHICS_API_OPENGL_43
#endif
#if defined(GRAPHICS_API_OPENGL_ES2)
#undef GRAPHICS_API_OPENGL_ES2
#endif
#endif
// OpenGL 2.1 uses most of OpenGL 3.3 Core functionality
// WARNING: Specific parts are checked with #if defines
#if defined(GRAPHICS_API_OPENGL_21)
#define GRAPHICS_API_OPENGL_33
#endif
// OpenGL 4.3 uses OpenGL 3.3 Core functionality
#if defined(GRAPHICS_API_OPENGL_43)
#define GRAPHICS_API_OPENGL_33
#endif
// OpenGL ES 3.0 uses OpenGL ES 2.0 functionality (and more)
#if defined(GRAPHICS_API_OPENGL_ES3)
#define GRAPHICS_API_OPENGL_ES2
#endif
// Support framebuffer objects by default
// NOTE: Some driver implementation do not support it, despite they should
#define RLGL_RENDER_TEXTURES_HINT
//----------------------------------------------------------------------------------
// Defines and Macros
//----------------------------------------------------------------------------------
// Default internal render batch elements limits
#ifndef RL_DEFAULT_BATCH_BUFFER_ELEMENTS
#if defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33)
// This is the maximum amount of elements (quads) per batch
// NOTE: Be careful with text, every letter maps to a quad
#define RL_DEFAULT_BATCH_BUFFER_ELEMENTS 8192
#endif
#if defined(GRAPHICS_API_OPENGL_ES2)
// We reduce memory sizes for embedded systems (RPI and HTML5)
// NOTE: On HTML5 (emscripten) this is allocated on heap,
// by default it's only 16MB!...just take care...
#define RL_DEFAULT_BATCH_BUFFER_ELEMENTS 2048
#endif
#endif
#ifndef RL_DEFAULT_BATCH_BUFFERS
#define RL_DEFAULT_BATCH_BUFFERS 1 // Default number of batch buffers (multi-buffering)
#endif
#ifndef RL_DEFAULT_BATCH_DRAWCALLS
#define RL_DEFAULT_BATCH_DRAWCALLS 256 // Default number of batch draw calls (by state changes: mode, texture)
#endif
#ifndef RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS
#define RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS 4 // Maximum number of textures units that can be activated on batch drawing (SetShaderValueTexture())
#endif
// Internal Matrix stack
#ifndef RL_MAX_MATRIX_STACK_SIZE
#define RL_MAX_MATRIX_STACK_SIZE 32 // Maximum size of Matrix stack
#endif
// Shader limits
#ifndef RL_MAX_SHADER_LOCATIONS
#define RL_MAX_SHADER_LOCATIONS 32 // Maximum number of shader locations supported
#endif
// Projection matrix culling
#ifndef RL_CULL_DISTANCE_NEAR
#define RL_CULL_DISTANCE_NEAR 0.01 // Default near cull distance
#endif
#ifndef RL_CULL_DISTANCE_FAR
#define RL_CULL_DISTANCE_FAR 1000.0 // Default far cull distance
#endif
// Texture parameters (equivalent to OpenGL defines)
#define RL_TEXTURE_WRAP_S 0x2802 // GL_TEXTURE_WRAP_S
#define RL_TEXTURE_WRAP_T 0x2803 // GL_TEXTURE_WRAP_T
#define RL_TEXTURE_MAG_FILTER 0x2800 // GL_TEXTURE_MAG_FILTER
#define RL_TEXTURE_MIN_FILTER 0x2801 // GL_TEXTURE_MIN_FILTER
#define RL_TEXTURE_FILTER_NEAREST 0x2600 // GL_NEAREST
#define RL_TEXTURE_FILTER_LINEAR 0x2601 // GL_LINEAR
#define RL_TEXTURE_FILTER_MIP_NEAREST 0x2700 // GL_NEAREST_MIPMAP_NEAREST
#define RL_TEXTURE_FILTER_NEAREST_MIP_LINEAR 0x2702 // GL_NEAREST_MIPMAP_LINEAR
#define RL_TEXTURE_FILTER_LINEAR_MIP_NEAREST 0x2701 // GL_LINEAR_MIPMAP_NEAREST
#define RL_TEXTURE_FILTER_MIP_LINEAR 0x2703 // GL_LINEAR_MIPMAP_LINEAR
#define RL_TEXTURE_FILTER_ANISOTROPIC 0x3000 // Anisotropic filter (custom identifier)
#define RL_TEXTURE_MIPMAP_BIAS_RATIO 0x4000 // Texture mipmap bias, percentage ratio (custom identifier)
#define RL_TEXTURE_WRAP_REPEAT 0x2901 // GL_REPEAT
#define RL_TEXTURE_WRAP_CLAMP 0x812F // GL_CLAMP_TO_EDGE
#define RL_TEXTURE_WRAP_MIRROR_REPEAT 0x8370 // GL_MIRRORED_REPEAT
#define RL_TEXTURE_WRAP_MIRROR_CLAMP 0x8742 // GL_MIRROR_CLAMP_EXT
// Matrix modes (equivalent to OpenGL)
#define RL_MODELVIEW 0x1700 // GL_MODELVIEW
#define RL_PROJECTION 0x1701 // GL_PROJECTION
#define RL_TEXTURE 0x1702 // GL_TEXTURE
// Primitive assembly draw modes
#define RL_LINES 0x0001 // GL_LINES
#define RL_TRIANGLES 0x0004 // GL_TRIANGLES
#define RL_QUADS 0x0007 // GL_QUADS
// GL equivalent data types
#define RL_UNSIGNED_BYTE 0x1401 // GL_UNSIGNED_BYTE
#define RL_FLOAT 0x1406 // GL_FLOAT
// GL buffer usage hint
#define RL_STREAM_DRAW 0x88E0 // GL_STREAM_DRAW
#define RL_STREAM_READ 0x88E1 // GL_STREAM_READ
#define RL_STREAM_COPY 0x88E2 // GL_STREAM_COPY
#define RL_STATIC_DRAW 0x88E4 // GL_STATIC_DRAW
#define RL_STATIC_READ 0x88E5 // GL_STATIC_READ
#define RL_STATIC_COPY 0x88E6 // GL_STATIC_COPY
#define RL_DYNAMIC_DRAW 0x88E8 // GL_DYNAMIC_DRAW
#define RL_DYNAMIC_READ 0x88E9 // GL_DYNAMIC_READ
#define RL_DYNAMIC_COPY 0x88EA // GL_DYNAMIC_COPY
// GL Shader type
#define RL_FRAGMENT_SHADER 0x8B30 // GL_FRAGMENT_SHADER
#define RL_VERTEX_SHADER 0x8B31 // GL_VERTEX_SHADER
#define RL_COMPUTE_SHADER 0x91B9 // GL_COMPUTE_SHADER
// GL blending factors
#define RL_ZERO 0 // GL_ZERO
#define RL_ONE 1 // GL_ONE
#define RL_SRC_COLOR 0x0300 // GL_SRC_COLOR
#define RL_ONE_MINUS_SRC_COLOR 0x0301 // GL_ONE_MINUS_SRC_COLOR
#define RL_SRC_ALPHA 0x0302 // GL_SRC_ALPHA
#define RL_ONE_MINUS_SRC_ALPHA 0x0303 // GL_ONE_MINUS_SRC_ALPHA
#define RL_DST_ALPHA 0x0304 // GL_DST_ALPHA
#define RL_ONE_MINUS_DST_ALPHA 0x0305 // GL_ONE_MINUS_DST_ALPHA
#define RL_DST_COLOR 0x0306 // GL_DST_COLOR
#define RL_ONE_MINUS_DST_COLOR 0x0307 // GL_ONE_MINUS_DST_COLOR
#define RL_SRC_ALPHA_SATURATE 0x0308 // GL_SRC_ALPHA_SATURATE
#define RL_CONSTANT_COLOR 0x8001 // GL_CONSTANT_COLOR
#define RL_ONE_MINUS_CONSTANT_COLOR 0x8002 // GL_ONE_MINUS_CONSTANT_COLOR
#define RL_CONSTANT_ALPHA 0x8003 // GL_CONSTANT_ALPHA
#define RL_ONE_MINUS_CONSTANT_ALPHA 0x8004 // GL_ONE_MINUS_CONSTANT_ALPHA
// GL blending functions/equations
#define RL_FUNC_ADD 0x8006 // GL_FUNC_ADD
#define RL_MIN 0x8007 // GL_MIN
#define RL_MAX 0x8008 // GL_MAX
#define RL_FUNC_SUBTRACT 0x800A // GL_FUNC_SUBTRACT
#define RL_FUNC_REVERSE_SUBTRACT 0x800B // GL_FUNC_REVERSE_SUBTRACT
#define RL_BLEND_EQUATION 0x8009 // GL_BLEND_EQUATION
#define RL_BLEND_EQUATION_RGB 0x8009 // GL_BLEND_EQUATION_RGB // (Same as BLEND_EQUATION)
#define RL_BLEND_EQUATION_ALPHA 0x883D // GL_BLEND_EQUATION_ALPHA
#define RL_BLEND_DST_RGB 0x80C8 // GL_BLEND_DST_RGB
#define RL_BLEND_SRC_RGB 0x80C9 // GL_BLEND_SRC_RGB
#define RL_BLEND_DST_ALPHA 0x80CA // GL_BLEND_DST_ALPHA
#define RL_BLEND_SRC_ALPHA 0x80CB // GL_BLEND_SRC_ALPHA
#define RL_BLEND_COLOR 0x8005 // GL_BLEND_COLOR
#define RL_READ_FRAMEBUFFER 0x8CA8 // GL_READ_FRAMEBUFFER
#define RL_DRAW_FRAMEBUFFER 0x8CA9 // GL_DRAW_FRAMEBUFFER
// Default shader vertex attribute locations
#ifndef RL_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION
#define RL_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION 0
#endif
#ifndef RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD
#define RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD 1
#endif
#ifndef RL_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL
#define RL_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL 2
#endif
#ifndef RL_DEFAULT_SHADER_ATTRIB_LOCATION_COLOR
#define RL_DEFAULT_SHADER_ATTRIB_LOCATION_COLOR 3
#endif
#ifndef RL_DEFAULT_SHADER_ATTRIB_LOCATION_TANGENT
#define RL_DEFAULT_SHADER_ATTRIB_LOCATION_TANGENT 4
#endif
#ifndef RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD2
#define RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD2 5
#endif
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
#if (defined(__STDC__) && __STDC_VERSION__ >= 199901L) || (defined(_MSC_VER) && _MSC_VER >= 1800)
#include <stdbool.h>
#elif !defined(__cplusplus) && !defined(bool) && !defined(RL_BOOL_TYPE)
// Boolean type
typedef enum bool { false = 0, true = !false } bool;
#endif
#if !defined(RL_MATRIX_TYPE)
// Matrix, 4x4 components, column major, OpenGL style, right handed
typedef struct Matrix {
float m0, m4, m8, m12; // Matrix first row (4 components)
float m1, m5, m9, m13; // Matrix second row (4 components)
float m2, m6, m10, m14; // Matrix third row (4 components)
float m3, m7, m11, m15; // Matrix fourth row (4 components)
} Matrix;
#define RL_MATRIX_TYPE
#endif
// Dynamic vertex buffers (position + texcoords + colors + indices arrays)
typedef struct rlVertexBuffer {
int elementCount; // Number of elements in the buffer (QUADS)
float *vertices; // Vertex position (XYZ - 3 components per vertex) (shader-location = 0)
float *texcoords; // Vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1)
float *normals; // Vertex normal (XYZ - 3 components per vertex) (shader-location = 2)
unsigned char *colors; // Vertex colors (RGBA - 4 components per vertex) (shader-location = 3)
#if defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33)
unsigned int *indices; // Vertex indices (in case vertex data comes indexed) (6 indices per quad)
#endif
#if defined(GRAPHICS_API_OPENGL_ES2)
unsigned short *indices; // Vertex indices (in case vertex data comes indexed) (6 indices per quad)
#endif
unsigned int vaoId; // OpenGL Vertex Array Object id
unsigned int vboId[5]; // OpenGL Vertex Buffer Objects id (5 types of vertex data)
} rlVertexBuffer;
// Draw call type
// NOTE: Only texture changes register a new draw, other state-change-related elements are not
// used at this moment (vaoId, shaderId, matrices), raylib just forces a batch draw call if any
// of those state-change happens (this is done in core module)
typedef struct rlDrawCall {
int mode; // Drawing mode: LINES, TRIANGLES, QUADS
int vertexCount; // Number of vertex of the draw
int vertexAlignment; // Number of vertex required for index alignment (LINES, TRIANGLES)
//unsigned int vaoId; // Vertex array id to be used on the draw -> Using RLGL.currentBatch->vertexBuffer.vaoId
//unsigned int shaderId; // Shader id to be used on the draw -> Using RLGL.currentShaderId
unsigned int textureId; // Texture id to be used on the draw -> Use to create new draw call if changes
//Matrix projection; // Projection matrix for this draw -> Using RLGL.projection by default
//Matrix modelview; // Modelview matrix for this draw -> Using RLGL.modelview by default
} rlDrawCall;
// rlRenderBatch type
typedef struct rlRenderBatch {
int bufferCount; // Number of vertex buffers (multi-buffering support)
int currentBuffer; // Current buffer tracking in case of multi-buffering
rlVertexBuffer *vertexBuffer; // Dynamic buffer(s) for vertex data
rlDrawCall *draws; // Draw calls array, depends on textureId
int drawCounter; // Draw calls counter
float currentDepth; // Current depth value for next draw
} rlRenderBatch;
// OpenGL version
typedef enum {
RL_OPENGL_11 = 1, // OpenGL 1.1
RL_OPENGL_21, // OpenGL 2.1 (GLSL 120)
RL_OPENGL_33, // OpenGL 3.3 (GLSL 330)
RL_OPENGL_43, // OpenGL 4.3 (using GLSL 330)
RL_OPENGL_ES_20, // OpenGL ES 2.0 (GLSL 100)
RL_OPENGL_ES_30 // OpenGL ES 3.0 (GLSL 300 es)
} rlGlVersion;
// Trace log level
// NOTE: Organized by priority level
typedef enum {
RL_LOG_ALL = 0, // Display all logs
RL_LOG_TRACE, // Trace logging, intended for internal use only
RL_LOG_DEBUG, // Debug logging, used for internal debugging, it should be disabled on release builds
RL_LOG_INFO, // Info logging, used for program execution info
RL_LOG_WARNING, // Warning logging, used on recoverable failures
RL_LOG_ERROR, // Error logging, used on unrecoverable failures
RL_LOG_FATAL, // Fatal logging, used to abort program: exit(EXIT_FAILURE)
RL_LOG_NONE // Disable logging
} rlTraceLogLevel;
// Texture pixel formats
// NOTE: Support depends on OpenGL version
typedef enum {
RL_PIXELFORMAT_UNCOMPRESSED_GRAYSCALE = 1, // 8 bit per pixel (no alpha)
RL_PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA, // 8*2 bpp (2 channels)
RL_PIXELFORMAT_UNCOMPRESSED_R5G6B5, // 16 bpp
RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8, // 24 bpp
RL_PIXELFORMAT_UNCOMPRESSED_R5G5B5A1, // 16 bpp (1 bit alpha)
RL_PIXELFORMAT_UNCOMPRESSED_R4G4B4A4, // 16 bpp (4 bit alpha)
RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, // 32 bpp
RL_PIXELFORMAT_UNCOMPRESSED_R32, // 32 bpp (1 channel - float)
RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32, // 32*3 bpp (3 channels - float)
RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32A32, // 32*4 bpp (4 channels - float)
RL_PIXELFORMAT_UNCOMPRESSED_R16, // 16 bpp (1 channel - half float)
RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16, // 16*3 bpp (3 channels - half float)
RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16A16, // 16*4 bpp (4 channels - half float)
RL_PIXELFORMAT_COMPRESSED_DXT1_RGB, // 4 bpp (no alpha)
RL_PIXELFORMAT_COMPRESSED_DXT1_RGBA, // 4 bpp (1 bit alpha)
RL_PIXELFORMAT_COMPRESSED_DXT3_RGBA, // 8 bpp
RL_PIXELFORMAT_COMPRESSED_DXT5_RGBA, // 8 bpp
RL_PIXELFORMAT_COMPRESSED_ETC1_RGB, // 4 bpp
RL_PIXELFORMAT_COMPRESSED_ETC2_RGB, // 4 bpp
RL_PIXELFORMAT_COMPRESSED_ETC2_EAC_RGBA, // 8 bpp
RL_PIXELFORMAT_COMPRESSED_PVRT_RGB, // 4 bpp
RL_PIXELFORMAT_COMPRESSED_PVRT_RGBA, // 4 bpp
RL_PIXELFORMAT_COMPRESSED_ASTC_4x4_RGBA, // 8 bpp
RL_PIXELFORMAT_COMPRESSED_ASTC_8x8_RGBA // 2 bpp
} rlPixelFormat;
// Texture parameters: filter mode
// NOTE 1: Filtering considers mipmaps if available in the texture
// NOTE 2: Filter is accordingly set for minification and magnification
typedef enum {
RL_TEXTURE_FILTER_POINT = 0, // No filter, just pixel approximation
RL_TEXTURE_FILTER_BILINEAR, // Linear filtering
RL_TEXTURE_FILTER_TRILINEAR, // Trilinear filtering (linear with mipmaps)
RL_TEXTURE_FILTER_ANISOTROPIC_4X, // Anisotropic filtering 4x
RL_TEXTURE_FILTER_ANISOTROPIC_8X, // Anisotropic filtering 8x
RL_TEXTURE_FILTER_ANISOTROPIC_16X, // Anisotropic filtering 16x
} rlTextureFilter;
// Color blending modes (pre-defined)
typedef enum {
RL_BLEND_ALPHA = 0, // Blend textures considering alpha (default)
RL_BLEND_ADDITIVE, // Blend textures adding colors
RL_BLEND_MULTIPLIED, // Blend textures multiplying colors
RL_BLEND_ADD_COLORS, // Blend textures adding colors (alternative)
RL_BLEND_SUBTRACT_COLORS, // Blend textures subtracting colors (alternative)
RL_BLEND_ALPHA_PREMULTIPLY, // Blend premultiplied textures considering alpha
RL_BLEND_CUSTOM, // Blend textures using custom src/dst factors (use rlSetBlendFactors())
RL_BLEND_CUSTOM_SEPARATE // Blend textures using custom src/dst factors (use rlSetBlendFactorsSeparate())
} rlBlendMode;
// Shader location point type
typedef enum {
RL_SHADER_LOC_VERTEX_POSITION = 0, // Shader location: vertex attribute: position
RL_SHADER_LOC_VERTEX_TEXCOORD01, // Shader location: vertex attribute: texcoord01
RL_SHADER_LOC_VERTEX_TEXCOORD02, // Shader location: vertex attribute: texcoord02
RL_SHADER_LOC_VERTEX_NORMAL, // Shader location: vertex attribute: normal
RL_SHADER_LOC_VERTEX_TANGENT, // Shader location: vertex attribute: tangent
RL_SHADER_LOC_VERTEX_COLOR, // Shader location: vertex attribute: color
RL_SHADER_LOC_MATRIX_MVP, // Shader location: matrix uniform: model-view-projection
RL_SHADER_LOC_MATRIX_VIEW, // Shader location: matrix uniform: view (camera transform)
RL_SHADER_LOC_MATRIX_PROJECTION, // Shader location: matrix uniform: projection
RL_SHADER_LOC_MATRIX_MODEL, // Shader location: matrix uniform: model (transform)
RL_SHADER_LOC_MATRIX_NORMAL, // Shader location: matrix uniform: normal
RL_SHADER_LOC_VECTOR_VIEW, // Shader location: vector uniform: view
RL_SHADER_LOC_COLOR_DIFFUSE, // Shader location: vector uniform: diffuse color
RL_SHADER_LOC_COLOR_SPECULAR, // Shader location: vector uniform: specular color
RL_SHADER_LOC_COLOR_AMBIENT, // Shader location: vector uniform: ambient color
RL_SHADER_LOC_MAP_ALBEDO, // Shader location: sampler2d texture: albedo (same as: RL_SHADER_LOC_MAP_DIFFUSE)
RL_SHADER_LOC_MAP_METALNESS, // Shader location: sampler2d texture: metalness (same as: RL_SHADER_LOC_MAP_SPECULAR)
RL_SHADER_LOC_MAP_NORMAL, // Shader location: sampler2d texture: normal
RL_SHADER_LOC_MAP_ROUGHNESS, // Shader location: sampler2d texture: roughness
RL_SHADER_LOC_MAP_OCCLUSION, // Shader location: sampler2d texture: occlusion
RL_SHADER_LOC_MAP_EMISSION, // Shader location: sampler2d texture: emission
RL_SHADER_LOC_MAP_HEIGHT, // Shader location: sampler2d texture: height
RL_SHADER_LOC_MAP_CUBEMAP, // Shader location: samplerCube texture: cubemap
RL_SHADER_LOC_MAP_IRRADIANCE, // Shader location: samplerCube texture: irradiance
RL_SHADER_LOC_MAP_PREFILTER, // Shader location: samplerCube texture: prefilter
RL_SHADER_LOC_MAP_BRDF // Shader location: sampler2d texture: brdf
} rlShaderLocationIndex;
#define RL_SHADER_LOC_MAP_DIFFUSE RL_SHADER_LOC_MAP_ALBEDO
#define RL_SHADER_LOC_MAP_SPECULAR RL_SHADER_LOC_MAP_METALNESS
// Shader uniform data type
typedef enum {
RL_SHADER_UNIFORM_FLOAT = 0, // Shader uniform type: float
RL_SHADER_UNIFORM_VEC2, // Shader uniform type: vec2 (2 float)
RL_SHADER_UNIFORM_VEC3, // Shader uniform type: vec3 (3 float)
RL_SHADER_UNIFORM_VEC4, // Shader uniform type: vec4 (4 float)
RL_SHADER_UNIFORM_INT, // Shader uniform type: int
RL_SHADER_UNIFORM_IVEC2, // Shader uniform type: ivec2 (2 int)
RL_SHADER_UNIFORM_IVEC3, // Shader uniform type: ivec3 (3 int)
RL_SHADER_UNIFORM_IVEC4, // Shader uniform type: ivec4 (4 int)
RL_SHADER_UNIFORM_SAMPLER2D // Shader uniform type: sampler2d
} rlShaderUniformDataType;
// Shader attribute data types
typedef enum {
RL_SHADER_ATTRIB_FLOAT = 0, // Shader attribute type: float
RL_SHADER_ATTRIB_VEC2, // Shader attribute type: vec2 (2 float)
RL_SHADER_ATTRIB_VEC3, // Shader attribute type: vec3 (3 float)
RL_SHADER_ATTRIB_VEC4 // Shader attribute type: vec4 (4 float)
} rlShaderAttributeDataType;
// Framebuffer attachment type
// NOTE: By default up to 8 color channels defined, but it can be more
typedef enum {
RL_ATTACHMENT_COLOR_CHANNEL0 = 0, // Framebuffer attachment type: color 0
RL_ATTACHMENT_COLOR_CHANNEL1 = 1, // Framebuffer attachment type: color 1
RL_ATTACHMENT_COLOR_CHANNEL2 = 2, // Framebuffer attachment type: color 2
RL_ATTACHMENT_COLOR_CHANNEL3 = 3, // Framebuffer attachment type: color 3
RL_ATTACHMENT_COLOR_CHANNEL4 = 4, // Framebuffer attachment type: color 4
RL_ATTACHMENT_COLOR_CHANNEL5 = 5, // Framebuffer attachment type: color 5
RL_ATTACHMENT_COLOR_CHANNEL6 = 6, // Framebuffer attachment type: color 6
RL_ATTACHMENT_COLOR_CHANNEL7 = 7, // Framebuffer attachment type: color 7
RL_ATTACHMENT_DEPTH = 100, // Framebuffer attachment type: depth
RL_ATTACHMENT_STENCIL = 200, // Framebuffer attachment type: stencil
} rlFramebufferAttachType;
// Framebuffer texture attachment type
typedef enum {
RL_ATTACHMENT_CUBEMAP_POSITIVE_X = 0, // Framebuffer texture attachment type: cubemap, +X side
RL_ATTACHMENT_CUBEMAP_NEGATIVE_X = 1, // Framebuffer texture attachment type: cubemap, -X side
RL_ATTACHMENT_CUBEMAP_POSITIVE_Y = 2, // Framebuffer texture attachment type: cubemap, +Y side
RL_ATTACHMENT_CUBEMAP_NEGATIVE_Y = 3, // Framebuffer texture attachment type: cubemap, -Y side
RL_ATTACHMENT_CUBEMAP_POSITIVE_Z = 4, // Framebuffer texture attachment type: cubemap, +Z side
RL_ATTACHMENT_CUBEMAP_NEGATIVE_Z = 5, // Framebuffer texture attachment type: cubemap, -Z side
RL_ATTACHMENT_TEXTURE2D = 100, // Framebuffer texture attachment type: texture2d
RL_ATTACHMENT_RENDERBUFFER = 200, // Framebuffer texture attachment type: renderbuffer
} rlFramebufferAttachTextureType;
// Face culling mode
typedef enum {
RL_CULL_FACE_FRONT = 0,
RL_CULL_FACE_BACK
} rlCullMode;
//------------------------------------------------------------------------------------
// Functions Declaration - Matrix operations
//------------------------------------------------------------------------------------
#if defined(__cplusplus)
extern "C" { // Prevents name mangling of functions
#endif
RLAPI void rlMatrixMode(int mode); // Choose the current matrix to be transformed
RLAPI void rlPushMatrix(void); // Push the current matrix to stack
RLAPI void rlPopMatrix(void); // Pop latest inserted matrix from stack
RLAPI void rlLoadIdentity(void); // Reset current matrix to identity matrix
RLAPI void rlTranslatef(float x, float y, float z); // Multiply the current matrix by a translation matrix
RLAPI void rlRotatef(float angle, float x, float y, float z); // Multiply the current matrix by a rotation matrix
RLAPI void rlScalef(float x, float y, float z); // Multiply the current matrix by a scaling matrix
RLAPI void rlMultMatrixf(const float *matf); // Multiply the current matrix by another matrix
RLAPI void rlFrustum(double left, double right, double bottom, double top, double znear, double zfar);
RLAPI void rlOrtho(double left, double right, double bottom, double top, double znear, double zfar);
RLAPI void rlViewport(int x, int y, int width, int height); // Set the viewport area
RLAPI void rlSetClipPlanes(double near, double far); // Set clip planes distances
RLAPI double rlGetCullDistanceNear(void); // Get cull plane distance near
RLAPI double rlGetCullDistanceFar(void); // Get cull plane distance far
//------------------------------------------------------------------------------------
// Functions Declaration - Vertex level operations
//------------------------------------------------------------------------------------
RLAPI void rlBegin(int mode); // Initialize drawing mode (how to organize vertex)
RLAPI void rlEnd(void); // Finish vertex providing
RLAPI void rlVertex2i(int x, int y); // Define one vertex (position) - 2 int
RLAPI void rlVertex2f(float x, float y); // Define one vertex (position) - 2 float
RLAPI void rlVertex3f(float x, float y, float z); // Define one vertex (position) - 3 float
RLAPI void rlTexCoord2f(float x, float y); // Define one vertex (texture coordinate) - 2 float
RLAPI void rlNormal3f(float x, float y, float z); // Define one vertex (normal) - 3 float
RLAPI void rlColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a); // Define one vertex (color) - 4 byte
RLAPI void rlColor3f(float x, float y, float z); // Define one vertex (color) - 3 float
RLAPI void rlColor4f(float x, float y, float z, float w); // Define one vertex (color) - 4 float
//------------------------------------------------------------------------------------
// Functions Declaration - OpenGL style functions (common to 1.1, 3.3+, ES2)
// NOTE: This functions are used to completely abstract raylib code from OpenGL layer,
// some of them are direct wrappers over OpenGL calls, some others are custom
//------------------------------------------------------------------------------------
// Vertex buffers state
RLAPI bool rlEnableVertexArray(unsigned int vaoId); // Enable vertex array (VAO, if supported)
RLAPI void rlDisableVertexArray(void); // Disable vertex array (VAO, if supported)
RLAPI void rlEnableVertexBuffer(unsigned int id); // Enable vertex buffer (VBO)
RLAPI void rlDisableVertexBuffer(void); // Disable vertex buffer (VBO)
RLAPI void rlEnableVertexBufferElement(unsigned int id); // Enable vertex buffer element (VBO element)
RLAPI void rlDisableVertexBufferElement(void); // Disable vertex buffer element (VBO element)
RLAPI void rlEnableVertexAttribute(unsigned int index); // Enable vertex attribute index
RLAPI void rlDisableVertexAttribute(unsigned int index); // Disable vertex attribute index
#if defined(GRAPHICS_API_OPENGL_11)
RLAPI void rlEnableStatePointer(int vertexAttribType, void *buffer); // Enable attribute state pointer
RLAPI void rlDisableStatePointer(int vertexAttribType); // Disable attribute state pointer
#endif
// Textures state
RLAPI void rlActiveTextureSlot(int slot); // Select and active a texture slot
RLAPI void rlEnableTexture(unsigned int id); // Enable texture
RLAPI void rlDisableTexture(void); // Disable texture
RLAPI void rlEnableTextureCubemap(unsigned int id); // Enable texture cubemap
RLAPI void rlDisableTextureCubemap(void); // Disable texture cubemap
RLAPI void rlTextureParameters(unsigned int id, int param, int value); // Set texture parameters (filter, wrap)
RLAPI void rlCubemapParameters(unsigned int id, int param, int value); // Set cubemap parameters (filter, wrap)
// Shader state
RLAPI void rlEnableShader(unsigned int id); // Enable shader program
RLAPI void rlDisableShader(void); // Disable shader program
// Framebuffer state
RLAPI void rlEnableFramebuffer(unsigned int id); // Enable render texture (fbo)
RLAPI void rlDisableFramebuffer(void); // Disable render texture (fbo), return to default framebuffer
RLAPI unsigned int rlGetActiveFramebuffer(void); // Get the currently active render texture (fbo), 0 for default framebuffer
RLAPI void rlActiveDrawBuffers(int count); // Activate multiple draw color buffers
RLAPI void rlBlitFramebuffer(int srcX, int srcY, int srcWidth, int srcHeight, int dstX, int dstY, int dstWidth, int dstHeight, int bufferMask); // Blit active framebuffer to main framebuffer
RLAPI void rlBindFramebuffer(unsigned int target, unsigned int framebuffer); // Bind framebuffer (FBO)
// General render state
RLAPI void rlEnableColorBlend(void); // Enable color blending
RLAPI void rlDisableColorBlend(void); // Disable color blending
RLAPI void rlEnableDepthTest(void); // Enable depth test
RLAPI void rlDisableDepthTest(void); // Disable depth test
RLAPI void rlEnableDepthMask(void); // Enable depth write
RLAPI void rlDisableDepthMask(void); // Disable depth write
RLAPI void rlEnableBackfaceCulling(void); // Enable backface culling
RLAPI void rlDisableBackfaceCulling(void); // Disable backface culling
RLAPI void rlColorMask(bool r, bool g, bool b, bool a); // Color mask control
RLAPI void rlSetCullFace(int mode); // Set face culling mode
RLAPI void rlEnableScissorTest(void); // Enable scissor test
RLAPI void rlDisableScissorTest(void); // Disable scissor test
RLAPI void rlScissor(int x, int y, int width, int height); // Scissor test
RLAPI void rlEnableWireMode(void); // Enable wire mode
RLAPI void rlEnablePointMode(void); // Enable point mode
RLAPI void rlDisableWireMode(void); // Disable wire mode ( and point ) maybe rename
RLAPI void rlSetLineWidth(float width); // Set the line drawing width
RLAPI float rlGetLineWidth(void); // Get the line drawing width
RLAPI void rlEnableSmoothLines(void); // Enable line aliasing
RLAPI void rlDisableSmoothLines(void); // Disable line aliasing
RLAPI void rlEnableStereoRender(void); // Enable stereo rendering
RLAPI void rlDisableStereoRender(void); // Disable stereo rendering
RLAPI bool rlIsStereoRenderEnabled(void); // Check if stereo render is enabled
RLAPI void rlClearColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a); // Clear color buffer with color
RLAPI void rlClearScreenBuffers(void); // Clear used screen buffers (color and depth)
RLAPI void rlCheckErrors(void); // Check and log OpenGL error codes
RLAPI void rlSetBlendMode(int mode); // Set blending mode
RLAPI void rlSetBlendFactors(int glSrcFactor, int glDstFactor, int glEquation); // Set blending mode factor and equation (using OpenGL factors)
RLAPI void rlSetBlendFactorsSeparate(int glSrcRGB, int glDstRGB, int glSrcAlpha, int glDstAlpha, int glEqRGB, int glEqAlpha); // Set blending mode factors and equations separately (using OpenGL factors)
//------------------------------------------------------------------------------------
// Functions Declaration - rlgl functionality
//------------------------------------------------------------------------------------
// rlgl initialization functions
RLAPI void rlglInit(int width, int height); // Initialize rlgl (buffers, shaders, textures, states)
RLAPI void rlglClose(void); // De-initialize rlgl (buffers, shaders, textures)
RLAPI void rlLoadExtensions(void *loader); // Load OpenGL extensions (loader function required)
RLAPI int rlGetVersion(void); // Get current OpenGL version
RLAPI void rlSetFramebufferWidth(int width); // Set current framebuffer width
RLAPI int rlGetFramebufferWidth(void); // Get default framebuffer width
RLAPI void rlSetFramebufferHeight(int height); // Set current framebuffer height
RLAPI int rlGetFramebufferHeight(void); // Get default framebuffer height
RLAPI unsigned int rlGetTextureIdDefault(void); // Get default texture id
RLAPI unsigned int rlGetShaderIdDefault(void); // Get default shader id
RLAPI int *rlGetShaderLocsDefault(void); // Get default shader locations
// Render batch management
// NOTE: rlgl provides a default render batch to behave like OpenGL 1.1 immediate mode
// but this render batch API is exposed in case of custom batches are required
RLAPI rlRenderBatch rlLoadRenderBatch(int numBuffers, int bufferElements); // Load a render batch system
RLAPI void rlUnloadRenderBatch(rlRenderBatch batch); // Unload render batch system
RLAPI void rlDrawRenderBatch(rlRenderBatch *batch); // Draw render batch data (Update->Draw->Reset)
RLAPI void rlSetRenderBatchActive(rlRenderBatch *batch); // Set the active render batch for rlgl (NULL for default internal)
RLAPI void rlDrawRenderBatchActive(void); // Update and draw internal render batch
RLAPI bool rlCheckRenderBatchLimit(int vCount); // Check internal buffer overflow for a given number of vertex
RLAPI void rlSetTexture(unsigned int id); // Set current texture for render batch and check buffers limits
//------------------------------------------------------------------------------------------------------------------------
// Vertex buffers management
RLAPI unsigned int rlLoadVertexArray(void); // Load vertex array (vao) if supported
RLAPI unsigned int rlLoadVertexBuffer(const void *buffer, int size, bool dynamic); // Load a vertex buffer object
RLAPI unsigned int rlLoadVertexBufferElement(const void *buffer, int size, bool dynamic); // Load vertex buffer elements object
RLAPI void rlUpdateVertexBuffer(unsigned int bufferId, const void *data, int dataSize, int offset); // Update vertex buffer object data on GPU buffer
RLAPI void rlUpdateVertexBufferElements(unsigned int id, const void *data, int dataSize, int offset); // Update vertex buffer elements data on GPU buffer
RLAPI void rlUnloadVertexArray(unsigned int vaoId); // Unload vertex array (vao)
RLAPI void rlUnloadVertexBuffer(unsigned int vboId); // Unload vertex buffer object
RLAPI void rlSetVertexAttribute(unsigned int index, int compSize, int type, bool normalized, int stride, int offset); // Set vertex attribute data configuration
RLAPI void rlSetVertexAttributeDivisor(unsigned int index, int divisor); // Set vertex attribute data divisor
RLAPI void rlSetVertexAttributeDefault(int locIndex, const void *value, int attribType, int count); // Set vertex attribute default value, when attribute to provided
RLAPI void rlDrawVertexArray(int offset, int count); // Draw vertex array (currently active vao)
RLAPI void rlDrawVertexArrayElements(int offset, int count, const void *buffer); // Draw vertex array elements
RLAPI void rlDrawVertexArrayInstanced(int offset, int count, int instances); // Draw vertex array (currently active vao) with instancing
RLAPI void rlDrawVertexArrayElementsInstanced(int offset, int count, const void *buffer, int instances); // Draw vertex array elements with instancing
// Textures management
RLAPI unsigned int rlLoadTexture(const void *data, int width, int height, int format, int mipmapCount); // Load texture data
RLAPI unsigned int rlLoadTextureDepth(int width, int height, bool useRenderBuffer); // Load depth texture/renderbuffer (to be attached to fbo)
RLAPI unsigned int rlLoadTextureCubemap(const void *data, int size, int format); // Load texture cubemap data
RLAPI void rlUpdateTexture(unsigned int id, int offsetX, int offsetY, int width, int height, int format, const void *data); // Update texture with new data on GPU
RLAPI void rlGetGlTextureFormats(int format, unsigned int *glInternalFormat, unsigned int *glFormat, unsigned int *glType); // Get OpenGL internal formats
RLAPI const char *rlGetPixelFormatName(unsigned int format); // Get name string for pixel format
RLAPI void rlUnloadTexture(unsigned int id); // Unload texture from GPU memory
RLAPI void rlGenTextureMipmaps(unsigned int id, int width, int height, int format, int *mipmaps); // Generate mipmap data for selected texture
RLAPI void *rlReadTexturePixels(unsigned int id, int width, int height, int format); // Read texture pixel data
RLAPI unsigned char *rlReadScreenPixels(int width, int height); // Read screen pixel data (color buffer)
// Framebuffer management (fbo)
RLAPI unsigned int rlLoadFramebuffer(void); // Load an empty framebuffer
RLAPI void rlFramebufferAttach(unsigned int fboId, unsigned int texId, int attachType, int texType, int mipLevel); // Attach texture/renderbuffer to a framebuffer
RLAPI bool rlFramebufferComplete(unsigned int id); // Verify framebuffer is complete
RLAPI void rlUnloadFramebuffer(unsigned int id); // Delete framebuffer from GPU
// Shaders management
RLAPI unsigned int rlLoadShaderCode(const char *vsCode, const char *fsCode); // Load shader from code strings
RLAPI unsigned int rlCompileShader(const char *shaderCode, int type); // Compile custom shader and return shader id (type: RL_VERTEX_SHADER, RL_FRAGMENT_SHADER, RL_COMPUTE_SHADER)
RLAPI unsigned int rlLoadShaderProgram(unsigned int vShaderId, unsigned int fShaderId); // Load custom shader program
RLAPI void rlUnloadShaderProgram(unsigned int id); // Unload shader program
RLAPI int rlGetLocationUniform(unsigned int shaderId, const char *uniformName); // Get shader location uniform
RLAPI int rlGetLocationAttrib(unsigned int shaderId, const char *attribName); // Get shader location attribute
RLAPI void rlSetUniform(int locIndex, const void *value, int uniformType, int count); // Set shader value uniform
RLAPI void rlSetUniformMatrix(int locIndex, Matrix mat); // Set shader value matrix
RLAPI void rlSetUniformSampler(int locIndex, unsigned int textureId); // Set shader value sampler
RLAPI void rlSetShader(unsigned int id, int *locs); // Set shader currently active (id and locations)
// Compute shader management
RLAPI unsigned int rlLoadComputeShaderProgram(unsigned int shaderId); // Load compute shader program
RLAPI void rlComputeShaderDispatch(unsigned int groupX, unsigned int groupY, unsigned int groupZ); // Dispatch compute shader (equivalent to *draw* for graphics pipeline)
// Shader buffer storage object management (ssbo)
RLAPI unsigned int rlLoadShaderBuffer(unsigned int size, const void *data, int usageHint); // Load shader storage buffer object (SSBO)
RLAPI void rlUnloadShaderBuffer(unsigned int ssboId); // Unload shader storage buffer object (SSBO)
RLAPI void rlUpdateShaderBuffer(unsigned int id, const void *data, unsigned int dataSize, unsigned int offset); // Update SSBO buffer data
RLAPI void rlBindShaderBuffer(unsigned int id, unsigned int index); // Bind SSBO buffer
RLAPI void rlReadShaderBuffer(unsigned int id, void *dest, unsigned int count, unsigned int offset); // Read SSBO buffer data (GPU->CPU)
RLAPI void rlCopyShaderBuffer(unsigned int destId, unsigned int srcId, unsigned int destOffset, unsigned int srcOffset, unsigned int count); // Copy SSBO data between buffers
RLAPI unsigned int rlGetShaderBufferSize(unsigned int id); // Get SSBO buffer size
// Buffer management
RLAPI void rlBindImageTexture(unsigned int id, unsigned int index, int format, bool readonly); // Bind image texture
// Matrix state management
RLAPI Matrix rlGetMatrixModelview(void); // Get internal modelview matrix
RLAPI Matrix rlGetMatrixProjection(void); // Get internal projection matrix
RLAPI Matrix rlGetMatrixTransform(void); // Get internal accumulated transform matrix
RLAPI Matrix rlGetMatrixProjectionStereo(int eye); // Get internal projection matrix for stereo render (selected eye)
RLAPI Matrix rlGetMatrixViewOffsetStereo(int eye); // Get internal view offset matrix for stereo render (selected eye)
RLAPI void rlSetMatrixProjection(Matrix proj); // Set a custom projection matrix (replaces internal projection matrix)
RLAPI void rlSetMatrixModelview(Matrix view); // Set a custom modelview matrix (replaces internal modelview matrix)
RLAPI void rlSetMatrixProjectionStereo(Matrix right, Matrix left); // Set eyes projection matrices for stereo rendering
RLAPI void rlSetMatrixViewOffsetStereo(Matrix right, Matrix left); // Set eyes view offsets matrices for stereo rendering
// Quick and dirty cube/quad buffers load->draw->unload
RLAPI void rlLoadDrawCube(void); // Load and draw a cube
RLAPI void rlLoadDrawQuad(void); // Load and draw a quad
#if defined(__cplusplus)
}
#endif
#endif // RLGL_H
/***********************************************************************************
*
* RLGL IMPLEMENTATION
*
************************************************************************************/
#if defined(RLGL_IMPLEMENTATION)
// Expose OpenGL functions from glad in raylib
#if defined(BUILD_LIBTYPE_SHARED)
#define GLAD_API_CALL_EXPORT
#define GLAD_API_CALL_EXPORT_BUILD
#endif
#if defined(GRAPHICS_API_OPENGL_11)
#if defined(__APPLE__)
#include <OpenGL/gl.h> // OpenGL 1.1 library for OSX
#include <OpenGL/glext.h> // OpenGL extensions library
#else
// APIENTRY for OpenGL function pointer declarations is required
#if !defined(APIENTRY)
#if defined(_WIN32)
#define APIENTRY __stdcall
#else
#define APIENTRY
#endif
#endif
// WINGDIAPI definition. Some Windows OpenGL headers need it
#if !defined(WINGDIAPI) && defined(_WIN32)
#define WINGDIAPI __declspec(dllimport)
#endif
#include <GL/gl.h> // OpenGL 1.1 library
#endif
#endif
#if defined(GRAPHICS_API_OPENGL_33)
#define GLAD_MALLOC RL_MALLOC
#define GLAD_FREE RL_FREE
#define GLAD_GL_IMPLEMENTATION
#include "external/glad.h" // GLAD extensions loading library, includes OpenGL headers
#endif
#if defined(GRAPHICS_API_OPENGL_ES3)
#include <GLES3/gl3.h> // OpenGL ES 3.0 library
#define GL_GLEXT_PROTOTYPES
#include <GLES2/gl2ext.h> // OpenGL ES 2.0 extensions library
#elif defined(GRAPHICS_API_OPENGL_ES2)
// NOTE: OpenGL ES 2.0 can be enabled on PLATFORM_DESKTOP,
// in that case, functions are loaded from a custom glad for OpenGL ES 2.0
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_DESKTOP_SDL)
#define GLAD_GLES2_IMPLEMENTATION
#include "external/glad_gles2.h"
#else
#define GL_GLEXT_PROTOTYPES
//#include <EGL/egl.h> // EGL library -> not required, platform layer
#include <GLES2/gl2.h> // OpenGL ES 2.0 library
#include <GLES2/gl2ext.h> // OpenGL ES 2.0 extensions library
#endif
// It seems OpenGL ES 2.0 instancing entry points are not defined on Raspberry Pi
// provided headers (despite being defined in official Khronos GLES2 headers)
#if defined(PLATFORM_DRM)
typedef void (GL_APIENTRYP PFNGLDRAWARRAYSINSTANCEDEXTPROC) (GLenum mode, GLint start, GLsizei count, GLsizei primcount);
typedef void (GL_APIENTRYP PFNGLDRAWELEMENTSINSTANCEDEXTPROC) (GLenum mode, GLsizei count, GLenum type, const void *indices, GLsizei primcount);
typedef void (GL_APIENTRYP PFNGLVERTEXATTRIBDIVISOREXTPROC) (GLuint index, GLuint divisor);
#endif
#endif
#include <stdlib.h> // Required for: malloc(), free()
#include <string.h> // Required for: strcmp(), strlen() [Used in rlglInit(), on extensions loading]
#include <math.h> // Required for: sqrtf(), sinf(), cosf(), floor(), log()
//----------------------------------------------------------------------------------
// Defines and Macros
//----------------------------------------------------------------------------------
#ifndef PI
#define PI 3.14159265358979323846f
#endif
#ifndef DEG2RAD
#define DEG2RAD (PI/180.0f)
#endif
#ifndef RAD2DEG
#define RAD2DEG (180.0f/PI)
#endif
#ifndef GL_SHADING_LANGUAGE_VERSION
#define GL_SHADING_LANGUAGE_VERSION 0x8B8C
#endif
#ifndef GL_COMPRESSED_RGB_S3TC_DXT1_EXT
#define GL_COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0
#endif
#ifndef GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
#define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
#endif
#ifndef GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
#define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
#endif
#ifndef GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
#define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
#endif
#ifndef GL_ETC1_RGB8_OES
#define GL_ETC1_RGB8_OES 0x8D64
#endif
#ifndef GL_COMPRESSED_RGB8_ETC2
#define GL_COMPRESSED_RGB8_ETC2 0x9274
#endif
#ifndef GL_COMPRESSED_RGBA8_ETC2_EAC
#define GL_COMPRESSED_RGBA8_ETC2_EAC 0x9278
#endif
#ifndef GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG
#define GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8C00
#endif
#ifndef GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG
#define GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8C02
#endif
#ifndef GL_COMPRESSED_RGBA_ASTC_4x4_KHR
#define GL_COMPRESSED_RGBA_ASTC_4x4_KHR 0x93b0
#endif
#ifndef GL_COMPRESSED_RGBA_ASTC_8x8_KHR
#define GL_COMPRESSED_RGBA_ASTC_8x8_KHR 0x93b7
#endif
#ifndef GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT
#define GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT 0x84FF
#endif
#ifndef GL_TEXTURE_MAX_ANISOTROPY_EXT
#define GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE
#endif
#ifndef GL_PROGRAM_POINT_SIZE
#define GL_PROGRAM_POINT_SIZE 0x8642
#endif
#ifndef GL_LINE_WIDTH
#define GL_LINE_WIDTH 0x0B21
#endif
#if defined(GRAPHICS_API_OPENGL_11)
#define GL_UNSIGNED_SHORT_5_6_5 0x8363
#define GL_UNSIGNED_SHORT_5_5_5_1 0x8034
#define GL_UNSIGNED_SHORT_4_4_4_4 0x8033
#endif
#if defined(GRAPHICS_API_OPENGL_21)
#define GL_LUMINANCE 0x1909
#define GL_LUMINANCE_ALPHA 0x190A
#endif
#if defined(GRAPHICS_API_OPENGL_ES2)
#define glClearDepth glClearDepthf
#if !defined(GRAPHICS_API_OPENGL_ES3)
#define GL_READ_FRAMEBUFFER GL_FRAMEBUFFER
#define GL_DRAW_FRAMEBUFFER GL_FRAMEBUFFER
#endif
#endif
// Default shader vertex attribute names to set location points
#ifndef RL_DEFAULT_SHADER_ATTRIB_NAME_POSITION
#define RL_DEFAULT_SHADER_ATTRIB_NAME_POSITION "vertexPosition" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_NAME_POSITION
#endif
#ifndef RL_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD
#define RL_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD "vertexTexCoord" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD
#endif
#ifndef RL_DEFAULT_SHADER_ATTRIB_NAME_NORMAL
#define RL_DEFAULT_SHADER_ATTRIB_NAME_NORMAL "vertexNormal" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_NAME_NORMAL
#endif
#ifndef RL_DEFAULT_SHADER_ATTRIB_NAME_COLOR
#define RL_DEFAULT_SHADER_ATTRIB_NAME_COLOR "vertexColor" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_NAME_COLOR
#endif
#ifndef RL_DEFAULT_SHADER_ATTRIB_NAME_TANGENT
#define RL_DEFAULT_SHADER_ATTRIB_NAME_TANGENT "vertexTangent" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_NAME_TANGENT
#endif
#ifndef RL_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD2
#define RL_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD2 "vertexTexCoord2" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD2
#endif
#ifndef RL_DEFAULT_SHADER_UNIFORM_NAME_MVP
#define RL_DEFAULT_SHADER_UNIFORM_NAME_MVP "mvp" // model-view-projection matrix
#endif
#ifndef RL_DEFAULT_SHADER_UNIFORM_NAME_VIEW
#define RL_DEFAULT_SHADER_UNIFORM_NAME_VIEW "matView" // view matrix
#endif
#ifndef RL_DEFAULT_SHADER_UNIFORM_NAME_PROJECTION
#define RL_DEFAULT_SHADER_UNIFORM_NAME_PROJECTION "matProjection" // projection matrix
#endif
#ifndef RL_DEFAULT_SHADER_UNIFORM_NAME_MODEL
#define RL_DEFAULT_SHADER_UNIFORM_NAME_MODEL "matModel" // model matrix
#endif
#ifndef RL_DEFAULT_SHADER_UNIFORM_NAME_NORMAL
#define RL_DEFAULT_SHADER_UNIFORM_NAME_NORMAL "matNormal" // normal matrix (transpose(inverse(matModelView))
#endif
#ifndef RL_DEFAULT_SHADER_UNIFORM_NAME_COLOR
#define RL_DEFAULT_SHADER_UNIFORM_NAME_COLOR "colDiffuse" // color diffuse (base tint color, multiplied by texture color)
#endif
#ifndef RL_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE0
#define RL_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE0 "texture0" // texture0 (texture slot active 0)
#endif
#ifndef RL_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE1
#define RL_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE1 "texture1" // texture1 (texture slot active 1)
#endif
#ifndef RL_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE2
#define RL_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE2 "texture2" // texture2 (texture slot active 2)
#endif
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
typedef struct rlglData {
rlRenderBatch *currentBatch; // Current render batch
rlRenderBatch defaultBatch; // Default internal render batch
struct {
int vertexCounter; // Current active render batch vertex counter (generic, used for all batches)
float texcoordx, texcoordy; // Current active texture coordinate (added on glVertex*())
float normalx, normaly, normalz; // Current active normal (added on glVertex*())
unsigned char colorr, colorg, colorb, colora; // Current active color (added on glVertex*())
int currentMatrixMode; // Current matrix mode
Matrix *currentMatrix; // Current matrix pointer
Matrix modelview; // Default modelview matrix
Matrix projection; // Default projection matrix
Matrix transform; // Transform matrix to be used with rlTranslate, rlRotate, rlScale
bool transformRequired; // Require transform matrix application to current draw-call vertex (if required)
Matrix stack[RL_MAX_MATRIX_STACK_SIZE];// Matrix stack for push/pop
int stackCounter; // Matrix stack counter
unsigned int defaultTextureId; // Default texture used on shapes/poly drawing (required by shader)
unsigned int activeTextureId[RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS]; // Active texture ids to be enabled on batch drawing (0 active by default)
unsigned int defaultVShaderId; // Default vertex shader id (used by default shader program)
unsigned int defaultFShaderId; // Default fragment shader id (used by default shader program)
unsigned int defaultShaderId; // Default shader program id, supports vertex color and diffuse texture
int *defaultShaderLocs; // Default shader locations pointer to be used on rendering
unsigned int currentShaderId; // Current shader id to be used on rendering (by default, defaultShaderId)
int *currentShaderLocs; // Current shader locations pointer to be used on rendering (by default, defaultShaderLocs)
bool stereoRender; // Stereo rendering flag
Matrix projectionStereo[2]; // VR stereo rendering eyes projection matrices
Matrix viewOffsetStereo[2]; // VR stereo rendering eyes view offset matrices
// Blending variables
int currentBlendMode; // Blending mode active
int glBlendSrcFactor; // Blending source factor
int glBlendDstFactor; // Blending destination factor
int glBlendEquation; // Blending equation
int glBlendSrcFactorRGB; // Blending source RGB factor
int glBlendDestFactorRGB; // Blending destination RGB factor
int glBlendSrcFactorAlpha; // Blending source alpha factor
int glBlendDestFactorAlpha; // Blending destination alpha factor
int glBlendEquationRGB; // Blending equation for RGB
int glBlendEquationAlpha; // Blending equation for alpha
bool glCustomBlendModeModified; // Custom blending factor and equation modification status
int framebufferWidth; // Current framebuffer width
int framebufferHeight; // Current framebuffer height
} State; // Renderer state
struct {
bool vao; // VAO support (OpenGL ES2 could not support VAO extension) (GL_ARB_vertex_array_object)
bool instancing; // Instancing supported (GL_ANGLE_instanced_arrays, GL_EXT_draw_instanced + GL_EXT_instanced_arrays)
bool texNPOT; // NPOT textures full support (GL_ARB_texture_non_power_of_two, GL_OES_texture_npot)
bool texDepth; // Depth textures supported (GL_ARB_depth_texture, GL_OES_depth_texture)
bool texDepthWebGL; // Depth textures supported WebGL specific (GL_WEBGL_depth_texture)
bool texFloat32; // float textures support (32 bit per channel) (GL_OES_texture_float)
bool texFloat16; // half float textures support (16 bit per channel) (GL_OES_texture_half_float)
bool texCompDXT; // DDS texture compression support (GL_EXT_texture_compression_s3tc, GL_WEBGL_compressed_texture_s3tc, GL_WEBKIT_WEBGL_compressed_texture_s3tc)
bool texCompETC1; // ETC1 texture compression support (GL_OES_compressed_ETC1_RGB8_texture, GL_WEBGL_compressed_texture_etc1)
bool texCompETC2; // ETC2/EAC texture compression support (GL_ARB_ES3_compatibility)
bool texCompPVRT; // PVR texture compression support (GL_IMG_texture_compression_pvrtc)
bool texCompASTC; // ASTC texture compression support (GL_KHR_texture_compression_astc_hdr, GL_KHR_texture_compression_astc_ldr)
bool texMirrorClamp; // Clamp mirror wrap mode supported (GL_EXT_texture_mirror_clamp)
bool texAnisoFilter; // Anisotropic texture filtering support (GL_EXT_texture_filter_anisotropic)
bool computeShader; // Compute shaders support (GL_ARB_compute_shader)
bool ssbo; // Shader storage buffer object support (GL_ARB_shader_storage_buffer_object)
float maxAnisotropyLevel; // Maximum anisotropy level supported (minimum is 2.0f)
int maxDepthBits; // Maximum bits for depth component
} ExtSupported; // Extensions supported flags
} rlglData;
typedef void *(*rlglLoadProc)(const char *name); // OpenGL extension functions loader signature (same as GLADloadproc)
#endif // GRAPHICS_API_OPENGL_33 || GRAPHICS_API_OPENGL_ES2
//----------------------------------------------------------------------------------
// Global Variables Definition
//----------------------------------------------------------------------------------
static double rlCullDistanceNear = RL_CULL_DISTANCE_NEAR;
static double rlCullDistanceFar = RL_CULL_DISTANCE_FAR;
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
static rlglData RLGL = { 0 };
#endif // GRAPHICS_API_OPENGL_33 || GRAPHICS_API_OPENGL_ES2
#if defined(GRAPHICS_API_OPENGL_ES2) && !defined(GRAPHICS_API_OPENGL_ES3)
// NOTE: VAO functionality is exposed through extensions (OES)
static PFNGLGENVERTEXARRAYSOESPROC glGenVertexArrays = NULL;
static PFNGLBINDVERTEXARRAYOESPROC glBindVertexArray = NULL;
static PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArrays = NULL;
// NOTE: Instancing functionality could also be available through extension
static PFNGLDRAWARRAYSINSTANCEDEXTPROC glDrawArraysInstanced = NULL;
static PFNGLDRAWELEMENTSINSTANCEDEXTPROC glDrawElementsInstanced = NULL;
static PFNGLVERTEXATTRIBDIVISOREXTPROC glVertexAttribDivisor = NULL;
#endif
//----------------------------------------------------------------------------------
// Module specific Functions Declaration
//----------------------------------------------------------------------------------
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
static void rlLoadShaderDefault(void); // Load default shader
static void rlUnloadShaderDefault(void); // Unload default shader
#if defined(RLGL_SHOW_GL_DETAILS_INFO)
static const char *rlGetCompressedFormatName(int format); // Get compressed format official GL identifier name
#endif // RLGL_SHOW_GL_DETAILS_INFO
#endif // GRAPHICS_API_OPENGL_33 || GRAPHICS_API_OPENGL_ES2
static int rlGetPixelDataSize(int width, int height, int format); // Get pixel data size in bytes (image or texture)
// Auxiliar matrix math functions
typedef struct rl_float16 {
float v[16];
} rl_float16;
static rl_float16 rlMatrixToFloatV(Matrix mat); // Get float array of matrix data
#define rlMatrixToFloat(mat) (rlMatrixToFloatV(mat).v) // Get float vector for Matrix
static Matrix rlMatrixIdentity(void); // Get identity matrix
static Matrix rlMatrixMultiply(Matrix left, Matrix right); // Multiply two matrices
static Matrix rlMatrixTranspose(Matrix mat); // Transposes provided matrix
static Matrix rlMatrixInvert(Matrix mat); // Invert provided matrix
//----------------------------------------------------------------------------------
// Module Functions Definition - Matrix operations
//----------------------------------------------------------------------------------
#if defined(GRAPHICS_API_OPENGL_11)
// Fallback to OpenGL 1.1 function calls
//---------------------------------------
void rlMatrixMode(int mode)
{
switch (mode)
{
case RL_PROJECTION: glMatrixMode(GL_PROJECTION); break;
case RL_MODELVIEW: glMatrixMode(GL_MODELVIEW); break;
case RL_TEXTURE: glMatrixMode(GL_TEXTURE); break;
default: break;
}
}
void rlFrustum(double left, double right, double bottom, double top, double znear, double zfar)
{
glFrustum(left, right, bottom, top, znear, zfar);
}
void rlOrtho(double left, double right, double bottom, double top, double znear, double zfar)
{
glOrtho(left, right, bottom, top, znear, zfar);
}
void rlPushMatrix(void) { glPushMatrix(); }
void rlPopMatrix(void) { glPopMatrix(); }
void rlLoadIdentity(void) { glLoadIdentity(); }
void rlTranslatef(float x, float y, float z) { glTranslatef(x, y, z); }
void rlRotatef(float angle, float x, float y, float z) { glRotatef(angle, x, y, z); }
void rlScalef(float x, float y, float z) { glScalef(x, y, z); }
void rlMultMatrixf(const float *matf) { glMultMatrixf(matf); }
#endif
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
// Choose the current matrix to be transformed
void rlMatrixMode(int mode)
{
if (mode == RL_PROJECTION) RLGL.State.currentMatrix = &RLGL.State.projection;
else if (mode == RL_MODELVIEW) RLGL.State.currentMatrix = &RLGL.State.modelview;
//else if (mode == RL_TEXTURE) // Not supported
RLGL.State.currentMatrixMode = mode;
}
// Push the current matrix into RLGL.State.stack
void rlPushMatrix(void)
{
if (RLGL.State.stackCounter >= RL_MAX_MATRIX_STACK_SIZE) TRACELOG(RL_LOG_ERROR, "RLGL: Matrix stack overflow (RL_MAX_MATRIX_STACK_SIZE)");
if (RLGL.State.currentMatrixMode == RL_MODELVIEW)
{
RLGL.State.transformRequired = true;
RLGL.State.currentMatrix = &RLGL.State.transform;
}
RLGL.State.stack[RLGL.State.stackCounter] = *RLGL.State.currentMatrix;
RLGL.State.stackCounter++;
}
// Pop lattest inserted matrix from RLGL.State.stack
void rlPopMatrix(void)
{
if (RLGL.State.stackCounter > 0)
{
Matrix mat = RLGL.State.stack[RLGL.State.stackCounter - 1];
*RLGL.State.currentMatrix = mat;
RLGL.State.stackCounter--;
}
if ((RLGL.State.stackCounter == 0) && (RLGL.State.currentMatrixMode == RL_MODELVIEW))
{
RLGL.State.currentMatrix = &RLGL.State.modelview;
RLGL.State.transformRequired = false;
}
}
// Reset current matrix to identity matrix
void rlLoadIdentity(void)
{
*RLGL.State.currentMatrix = rlMatrixIdentity();
}
// Multiply the current matrix by a translation matrix
void rlTranslatef(float x, float y, float z)
{
Matrix matTranslation = {
1.0f, 0.0f, 0.0f, x,
0.0f, 1.0f, 0.0f, y,
0.0f, 0.0f, 1.0f, z,
0.0f, 0.0f, 0.0f, 1.0f
};
// NOTE: We transpose matrix with multiplication order
*RLGL.State.currentMatrix = rlMatrixMultiply(matTranslation, *RLGL.State.currentMatrix);
}
// Multiply the current matrix by a rotation matrix
// NOTE: The provided angle must be in degrees
void rlRotatef(float angle, float x, float y, float z)
{
Matrix matRotation = rlMatrixIdentity();
// Axis vector (x, y, z) normalization
float lengthSquared = x*x + y*y + z*z;
if ((lengthSquared != 1.0f) && (lengthSquared != 0.0f))
{
float inverseLength = 1.0f/sqrtf(lengthSquared);
x *= inverseLength;
y *= inverseLength;
z *= inverseLength;
}
// Rotation matrix generation
float sinres = sinf(DEG2RAD*angle);
float cosres = cosf(DEG2RAD*angle);
float t = 1.0f - cosres;
matRotation.m0 = x*x*t + cosres;
matRotation.m1 = y*x*t + z*sinres;
matRotation.m2 = z*x*t - y*sinres;
matRotation.m3 = 0.0f;
matRotation.m4 = x*y*t - z*sinres;
matRotation.m5 = y*y*t + cosres;
matRotation.m6 = z*y*t + x*sinres;
matRotation.m7 = 0.0f;
matRotation.m8 = x*z*t + y*sinres;
matRotation.m9 = y*z*t - x*sinres;
matRotation.m10 = z*z*t + cosres;
matRotation.m11 = 0.0f;
matRotation.m12 = 0.0f;
matRotation.m13 = 0.0f;
matRotation.m14 = 0.0f;
matRotation.m15 = 1.0f;
// NOTE: We transpose matrix with multiplication order
*RLGL.State.currentMatrix = rlMatrixMultiply(matRotation, *RLGL.State.currentMatrix);
}
// Multiply the current matrix by a scaling matrix
void rlScalef(float x, float y, float z)
{
Matrix matScale = {
x, 0.0f, 0.0f, 0.0f,
0.0f, y, 0.0f, 0.0f,
0.0f, 0.0f, z, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
// NOTE: We transpose matrix with multiplication order
*RLGL.State.currentMatrix = rlMatrixMultiply(matScale, *RLGL.State.currentMatrix);
}
// Multiply the current matrix by another matrix
void rlMultMatrixf(const float *matf)
{
// Matrix creation from array
Matrix mat = { matf[0], matf[4], matf[8], matf[12],
matf[1], matf[5], matf[9], matf[13],
matf[2], matf[6], matf[10], matf[14],
matf[3], matf[7], matf[11], matf[15] };
*RLGL.State.currentMatrix = rlMatrixMultiply(mat, *RLGL.State.currentMatrix);
}
// Multiply the current matrix by a perspective matrix generated by parameters
void rlFrustum(double left, double right, double bottom, double top, double znear, double zfar)
{
Matrix matFrustum = { 0 };
float rl = (float)(right - left);
float tb = (float)(top - bottom);
float fn = (float)(zfar - znear);
matFrustum.m0 = ((float) znear*2.0f)/rl;
matFrustum.m1 = 0.0f;
matFrustum.m2 = 0.0f;
matFrustum.m3 = 0.0f;
matFrustum.m4 = 0.0f;
matFrustum.m5 = ((float) znear*2.0f)/tb;
matFrustum.m6 = 0.0f;
matFrustum.m7 = 0.0f;
matFrustum.m8 = ((float)right + (float)left)/rl;
matFrustum.m9 = ((float)top + (float)bottom)/tb;
matFrustum.m10 = -((float)zfar + (float)znear)/fn;
matFrustum.m11 = -1.0f;
matFrustum.m12 = 0.0f;
matFrustum.m13 = 0.0f;
matFrustum.m14 = -((float)zfar*(float)znear*2.0f)/fn;
matFrustum.m15 = 0.0f;
*RLGL.State.currentMatrix = rlMatrixMultiply(*RLGL.State.currentMatrix, matFrustum);
}
// Multiply the current matrix by an orthographic matrix generated by parameters
void rlOrtho(double left, double right, double bottom, double top, double znear, double zfar)
{
// NOTE: If left-right and top-botton values are equal it could create a division by zero,
// response to it is platform/compiler dependant
Matrix matOrtho = { 0 };
float rl = (float)(right - left);
float tb = (float)(top - bottom);
float fn = (float)(zfar - znear);
matOrtho.m0 = 2.0f/rl;
matOrtho.m1 = 0.0f;
matOrtho.m2 = 0.0f;
matOrtho.m3 = 0.0f;
matOrtho.m4 = 0.0f;
matOrtho.m5 = 2.0f/tb;
matOrtho.m6 = 0.0f;
matOrtho.m7 = 0.0f;
matOrtho.m8 = 0.0f;
matOrtho.m9 = 0.0f;
matOrtho.m10 = -2.0f/fn;
matOrtho.m11 = 0.0f;
matOrtho.m12 = -((float)left + (float)right)/rl;
matOrtho.m13 = -((float)top + (float)bottom)/tb;
matOrtho.m14 = -((float)zfar + (float)znear)/fn;
matOrtho.m15 = 1.0f;
*RLGL.State.currentMatrix = rlMatrixMultiply(*RLGL.State.currentMatrix, matOrtho);
}
#endif
// Set the viewport area (transformation from normalized device coordinates to window coordinates)
// NOTE: We store current viewport dimensions
void rlViewport(int x, int y, int width, int height)
{
glViewport(x, y, width, height);
}
// Set clip planes distances
void rlSetClipPlanes(double near, double far)
{
rlCullDistanceNear = near;
rlCullDistanceFar = far;
}
// Get cull plane distance near
double rlGetCullDistanceNear(void)
{
return rlCullDistanceNear;
}
// Get cull plane distance far
double rlGetCullDistanceFar(void)
{
return rlCullDistanceFar;
}
//----------------------------------------------------------------------------------
// Module Functions Definition - Vertex level operations
//----------------------------------------------------------------------------------
#if defined(GRAPHICS_API_OPENGL_11)
// Fallback to OpenGL 1.1 function calls
//---------------------------------------
void rlBegin(int mode)
{
switch (mode)
{
case RL_LINES: glBegin(GL_LINES); break;
case RL_TRIANGLES: glBegin(GL_TRIANGLES); break;
case RL_QUADS: glBegin(GL_QUADS); break;
default: break;
}
}
void rlEnd() { glEnd(); }
void rlVertex2i(int x, int y) { glVertex2i(x, y); }
void rlVertex2f(float x, float y) { glVertex2f(x, y); }
void rlVertex3f(float x, float y, float z) { glVertex3f(x, y, z); }
void rlTexCoord2f(float x, float y) { glTexCoord2f(x, y); }
void rlNormal3f(float x, float y, float z) { glNormal3f(x, y, z); }
void rlColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a) { glColor4ub(r, g, b, a); }
void rlColor3f(float x, float y, float z) { glColor3f(x, y, z); }
void rlColor4f(float x, float y, float z, float w) { glColor4f(x, y, z, w); }
#endif
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
// Initialize drawing mode (how to organize vertex)
void rlBegin(int mode)
{
// Draw mode can be RL_LINES, RL_TRIANGLES and RL_QUADS
// NOTE: In all three cases, vertex are accumulated over default internal vertex buffer
if (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode != mode)
{
if (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount > 0)
{
// Make sure current RLGL.currentBatch->draws[i].vertexCount is aligned a multiple of 4,
// that way, following QUADS drawing will keep aligned with index processing
// It implies adding some extra alignment vertex at the end of the draw,
// those vertex are not processed but they are considered as an additional offset
// for the next set of vertex to be drawn
if (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode == RL_LINES) RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexAlignment = ((RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount < 4)? RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount : RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount%4);
else if (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode == RL_TRIANGLES) RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexAlignment = ((RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount < 4)? 1 : (4 - (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount%4)));
else RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexAlignment = 0;
if (!rlCheckRenderBatchLimit(RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexAlignment))
{
RLGL.State.vertexCounter += RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexAlignment;
RLGL.currentBatch->drawCounter++;
}
}
if (RLGL.currentBatch->drawCounter >= RL_DEFAULT_BATCH_DRAWCALLS) rlDrawRenderBatch(RLGL.currentBatch);
RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode = mode;
RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount = 0;
RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].textureId = RLGL.State.defaultTextureId;
}
}
// Finish vertex providing
void rlEnd(void)
{
// NOTE: Depth increment is dependant on rlOrtho(): z-near and z-far values,
// as well as depth buffer bit-depth (16bit or 24bit or 32bit)
// Correct increment formula would be: depthInc = (zfar - znear)/pow(2, bits)
RLGL.currentBatch->currentDepth += (1.0f/20000.0f);
}
// Define one vertex (position)
// NOTE: Vertex position data is the basic information required for drawing
void rlVertex3f(float x, float y, float z)
{
float tx = x;
float ty = y;
float tz = z;
// Transform provided vector if required
if (RLGL.State.transformRequired)
{
tx = RLGL.State.transform.m0*x + RLGL.State.transform.m4*y + RLGL.State.transform.m8*z + RLGL.State.transform.m12;
ty = RLGL.State.transform.m1*x + RLGL.State.transform.m5*y + RLGL.State.transform.m9*z + RLGL.State.transform.m13;
tz = RLGL.State.transform.m2*x + RLGL.State.transform.m6*y + RLGL.State.transform.m10*z + RLGL.State.transform.m14;
}
// WARNING: We can't break primitives when launching a new batch.
// RL_LINES comes in pairs, RL_TRIANGLES come in groups of 3 vertices and RL_QUADS come in groups of 4 vertices.
// We must check current draw.mode when a new vertex is required and finish the batch only if the draw.mode draw.vertexCount is %2, %3 or %4
if (RLGL.State.vertexCounter > (RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].elementCount*4 - 4))
{
if ((RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode == RL_LINES) &&
(RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount%2 == 0))
{
// Reached the maximum number of vertices for RL_LINES drawing
// Launch a draw call but keep current state for next vertices comming
// NOTE: We add +1 vertex to the check for security
rlCheckRenderBatchLimit(2 + 1);
}
else if ((RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode == RL_TRIANGLES) &&
(RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount%3 == 0))
{
rlCheckRenderBatchLimit(3 + 1);
}
else if ((RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode == RL_QUADS) &&
(RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount%4 == 0))
{
rlCheckRenderBatchLimit(4 + 1);
}
}
// Add vertices
RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].vertices[3*RLGL.State.vertexCounter] = tx;
RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].vertices[3*RLGL.State.vertexCounter + 1] = ty;
RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].vertices[3*RLGL.State.vertexCounter + 2] = tz;
// Add current texcoord
RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].texcoords[2*RLGL.State.vertexCounter] = RLGL.State.texcoordx;
RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].texcoords[2*RLGL.State.vertexCounter + 1] = RLGL.State.texcoordy;
// Add current normal
RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].normals[3*RLGL.State.vertexCounter] = RLGL.State.normalx;
RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].normals[3*RLGL.State.vertexCounter + 1] = RLGL.State.normaly;
RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].normals[3*RLGL.State.vertexCounter + 2] = RLGL.State.normalz;
// Add current color
RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].colors[4*RLGL.State.vertexCounter] = RLGL.State.colorr;
RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].colors[4*RLGL.State.vertexCounter + 1] = RLGL.State.colorg;
RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].colors[4*RLGL.State.vertexCounter + 2] = RLGL.State.colorb;
RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].colors[4*RLGL.State.vertexCounter + 3] = RLGL.State.colora;
RLGL.State.vertexCounter++;
RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount++;
}
// Define one vertex (position)
void rlVertex2f(float x, float y)
{
rlVertex3f(x, y, RLGL.currentBatch->currentDepth);
}
// Define one vertex (position)
void rlVertex2i(int x, int y)
{
rlVertex3f((float)x, (float)y, RLGL.currentBatch->currentDepth);
}
// Define one vertex (texture coordinate)
// NOTE: Texture coordinates are limited to QUADS only
void rlTexCoord2f(float x, float y)
{
RLGL.State.texcoordx = x;
RLGL.State.texcoordy = y;
}
// Define one vertex (normal)
// NOTE: Normals limited to TRIANGLES only?
void rlNormal3f(float x, float y, float z)
{
float normalx = x;
float normaly = y;
float normalz = z;
if (RLGL.State.transformRequired)
{
normalx = RLGL.State.transform.m0*x + RLGL.State.transform.m4*y + RLGL.State.transform.m8*z;
normaly = RLGL.State.transform.m1*x + RLGL.State.transform.m5*y + RLGL.State.transform.m9*z;
normalz = RLGL.State.transform.m2*x + RLGL.State.transform.m6*y + RLGL.State.transform.m10*z;
}
float length = sqrtf(normalx*normalx + normaly*normaly + normalz*normalz);
if (length != 0.0f)
{
float ilength = 1.0f / length;
normalx *= ilength;
normaly *= ilength;
normalz *= ilength;
}
RLGL.State.normalx = normalx;
RLGL.State.normaly = normaly;
RLGL.State.normalz = normalz;
}
// Define one vertex (color)
void rlColor4ub(unsigned char x, unsigned char y, unsigned char z, unsigned char w)
{
RLGL.State.colorr = x;
RLGL.State.colorg = y;
RLGL.State.colorb = z;
RLGL.State.colora = w;
}
// Define one vertex (color)
void rlColor4f(float r, float g, float b, float a)
{
rlColor4ub((unsigned char)(r*255), (unsigned char)(g*255), (unsigned char)(b*255), (unsigned char)(a*255));
}
// Define one vertex (color)
void rlColor3f(float x, float y, float z)
{
rlColor4ub((unsigned char)(x*255), (unsigned char)(y*255), (unsigned char)(z*255), 255);
}
#endif
//--------------------------------------------------------------------------------------
// Module Functions Definition - OpenGL style functions (common to 1.1, 3.3+, ES2)
//--------------------------------------------------------------------------------------
// Set current texture to use
void rlSetTexture(unsigned int id)
{
if (id == 0)
{
#if defined(GRAPHICS_API_OPENGL_11)
rlDisableTexture();
#else
// NOTE: If quads batch limit is reached, we force a draw call and next batch starts
if (RLGL.State.vertexCounter >=
RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].elementCount*4)
{
rlDrawRenderBatch(RLGL.currentBatch);
}
#endif
}
else
{
#if defined(GRAPHICS_API_OPENGL_11)
rlEnableTexture(id);
#else
if (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].textureId != id)
{
if (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount > 0)
{
// Make sure current RLGL.currentBatch->draws[i].vertexCount is aligned a multiple of 4,
// that way, following QUADS drawing will keep aligned with index processing
// It implies adding some extra alignment vertex at the end of the draw,
// those vertex are not processed but they are considered as an additional offset
// for the next set of vertex to be drawn
if (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode == RL_LINES) RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexAlignment = ((RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount < 4)? RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount : RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount%4);
else if (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode == RL_TRIANGLES) RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexAlignment = ((RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount < 4)? 1 : (4 - (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount%4)));
else RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexAlignment = 0;
if (!rlCheckRenderBatchLimit(RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexAlignment))
{
RLGL.State.vertexCounter += RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexAlignment;
RLGL.currentBatch->drawCounter++;
}
}
if (RLGL.currentBatch->drawCounter >= RL_DEFAULT_BATCH_DRAWCALLS) rlDrawRenderBatch(RLGL.currentBatch);
RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].textureId = id;
RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount = 0;
}
#endif
}
}
// Select and active a texture slot
void rlActiveTextureSlot(int slot)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
glActiveTexture(GL_TEXTURE0 + slot);
#endif
}
// Enable texture
void rlEnableTexture(unsigned int id)
{
#if defined(GRAPHICS_API_OPENGL_11)
glEnable(GL_TEXTURE_2D);
#endif
glBindTexture(GL_TEXTURE_2D, id);
}
// Disable texture
void rlDisableTexture(void)
{
#if defined(GRAPHICS_API_OPENGL_11)
glDisable(GL_TEXTURE_2D);
#endif
glBindTexture(GL_TEXTURE_2D, 0);
}
// Enable texture cubemap
void rlEnableTextureCubemap(unsigned int id)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
glBindTexture(GL_TEXTURE_CUBE_MAP, id);
#endif
}
// Disable texture cubemap
void rlDisableTextureCubemap(void)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
#endif
}
// Set texture parameters (wrap mode/filter mode)
void rlTextureParameters(unsigned int id, int param, int value)
{
glBindTexture(GL_TEXTURE_2D, id);
#if !defined(GRAPHICS_API_OPENGL_11)
// Reset anisotropy filter, in case it was set
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f);
#endif
switch (param)
{
case RL_TEXTURE_WRAP_S:
case RL_TEXTURE_WRAP_T:
{
if (value == RL_TEXTURE_WRAP_MIRROR_CLAMP)
{
#if !defined(GRAPHICS_API_OPENGL_11)
if (RLGL.ExtSupported.texMirrorClamp) glTexParameteri(GL_TEXTURE_2D, param, value);
else TRACELOG(RL_LOG_WARNING, "GL: Clamp mirror wrap mode not supported (GL_MIRROR_CLAMP_EXT)");
#endif
}
else glTexParameteri(GL_TEXTURE_2D, param, value);
} break;
case RL_TEXTURE_MAG_FILTER:
case RL_TEXTURE_MIN_FILTER: glTexParameteri(GL_TEXTURE_2D, param, value); break;
case RL_TEXTURE_FILTER_ANISOTROPIC:
{
#if !defined(GRAPHICS_API_OPENGL_11)
if (value <= RLGL.ExtSupported.maxAnisotropyLevel) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, (float)value);
else if (RLGL.ExtSupported.maxAnisotropyLevel > 0.0f)
{
TRACELOG(RL_LOG_WARNING, "GL: Maximum anisotropic filter level supported is %iX", id, (int)RLGL.ExtSupported.maxAnisotropyLevel);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, (float)value);
}
else TRACELOG(RL_LOG_WARNING, "GL: Anisotropic filtering not supported");
#endif
} break;
#if defined(GRAPHICS_API_OPENGL_33)
case RL_TEXTURE_MIPMAP_BIAS_RATIO: glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, value/100.0f);
#endif
default: break;
}
glBindTexture(GL_TEXTURE_2D, 0);
}
// Set cubemap parameters (wrap mode/filter mode)
void rlCubemapParameters(unsigned int id, int param, int value)
{
#if !defined(GRAPHICS_API_OPENGL_11)
glBindTexture(GL_TEXTURE_CUBE_MAP, id);
// Reset anisotropy filter, in case it was set
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f);
switch (param)
{
case RL_TEXTURE_WRAP_S:
case RL_TEXTURE_WRAP_T:
{
if (value == RL_TEXTURE_WRAP_MIRROR_CLAMP)
{
if (RLGL.ExtSupported.texMirrorClamp) glTexParameteri(GL_TEXTURE_CUBE_MAP, param, value);
else TRACELOG(RL_LOG_WARNING, "GL: Clamp mirror wrap mode not supported (GL_MIRROR_CLAMP_EXT)");
}
else glTexParameteri(GL_TEXTURE_CUBE_MAP, param, value);
} break;
case RL_TEXTURE_MAG_FILTER:
case RL_TEXTURE_MIN_FILTER: glTexParameteri(GL_TEXTURE_CUBE_MAP, param, value); break;
case RL_TEXTURE_FILTER_ANISOTROPIC:
{
if (value <= RLGL.ExtSupported.maxAnisotropyLevel) glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_ANISOTROPY_EXT, (float)value);
else if (RLGL.ExtSupported.maxAnisotropyLevel > 0.0f)
{
TRACELOG(RL_LOG_WARNING, "GL: Maximum anisotropic filter level supported is %iX", id, (int)RLGL.ExtSupported.maxAnisotropyLevel);
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_ANISOTROPY_EXT, (float)value);
}
else TRACELOG(RL_LOG_WARNING, "GL: Anisotropic filtering not supported");
} break;
#if defined(GRAPHICS_API_OPENGL_33)
case RL_TEXTURE_MIPMAP_BIAS_RATIO: glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_LOD_BIAS, value/100.0f);
#endif
default: break;
}
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
#endif
}
// Enable shader program
void rlEnableShader(unsigned int id)
{
#if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2))
glUseProgram(id);
#endif
}
// Disable shader program
void rlDisableShader(void)
{
#if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2))
glUseProgram(0);
#endif
}
// Enable rendering to texture (fbo)
void rlEnableFramebuffer(unsigned int id)
{
#if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)) && defined(RLGL_RENDER_TEXTURES_HINT)
glBindFramebuffer(GL_FRAMEBUFFER, id);
#endif
}
// return the active render texture (fbo)
unsigned int rlGetActiveFramebuffer(void)
{
GLint fboId = 0;
#if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES3)) && defined(RLGL_RENDER_TEXTURES_HINT)
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &fboId);
#endif
return fboId;
}
// Disable rendering to texture
void rlDisableFramebuffer(void)
{
#if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)) && defined(RLGL_RENDER_TEXTURES_HINT)
glBindFramebuffer(GL_FRAMEBUFFER, 0);
#endif
}
// Blit active framebuffer to main framebuffer
void rlBlitFramebuffer(int srcX, int srcY, int srcWidth, int srcHeight, int dstX, int dstY, int dstWidth, int dstHeight, int bufferMask)
{
#if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES3)) && defined(RLGL_RENDER_TEXTURES_HINT)
glBlitFramebuffer(srcX, srcY, srcWidth, srcHeight, dstX, dstY, dstWidth, dstHeight, bufferMask, GL_NEAREST);
#endif
}
// Bind framebuffer object (fbo)
void rlBindFramebuffer(unsigned int target, unsigned int framebuffer)
{
#if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)) && defined(RLGL_RENDER_TEXTURES_HINT)
glBindFramebuffer(target, framebuffer);
#endif
}
// Activate multiple draw color buffers
// NOTE: One color buffer is always active by default
void rlActiveDrawBuffers(int count)
{
#if ((defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES3)) && defined(RLGL_RENDER_TEXTURES_HINT))
// NOTE: Maximum number of draw buffers supported is implementation dependant,
// it can be queried with glGet*() but it must be at least 8
//GLint maxDrawBuffers = 0;
//glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxDrawBuffers);
if (count > 0)
{
if (count > 8) TRACELOG(LOG_WARNING, "GL: Max color buffers limited to 8");
else
{
unsigned int buffers[8] = {
#if defined(GRAPHICS_API_OPENGL_ES3)
GL_COLOR_ATTACHMENT0_EXT,
GL_COLOR_ATTACHMENT1_EXT,
GL_COLOR_ATTACHMENT2_EXT,
GL_COLOR_ATTACHMENT3_EXT,
GL_COLOR_ATTACHMENT4_EXT,
GL_COLOR_ATTACHMENT5_EXT,
GL_COLOR_ATTACHMENT6_EXT,
GL_COLOR_ATTACHMENT7_EXT,
#else
GL_COLOR_ATTACHMENT0,
GL_COLOR_ATTACHMENT1,
GL_COLOR_ATTACHMENT2,
GL_COLOR_ATTACHMENT3,
GL_COLOR_ATTACHMENT4,
GL_COLOR_ATTACHMENT5,
GL_COLOR_ATTACHMENT6,
GL_COLOR_ATTACHMENT7,
#endif
};
#if defined(GRAPHICS_API_OPENGL_ES3)
glDrawBuffersEXT(count, buffers);
#else
glDrawBuffers(count, buffers);
#endif
}
}
else TRACELOG(LOG_WARNING, "GL: One color buffer active by default");
#endif
}
//----------------------------------------------------------------------------------
// General render state configuration
//----------------------------------------------------------------------------------
// Enable color blending
void rlEnableColorBlend(void) { glEnable(GL_BLEND); }
// Disable color blending
void rlDisableColorBlend(void) { glDisable(GL_BLEND); }
// Enable depth test
void rlEnableDepthTest(void) { glEnable(GL_DEPTH_TEST); }
// Disable depth test
void rlDisableDepthTest(void) { glDisable(GL_DEPTH_TEST); }
// Enable depth write
void rlEnableDepthMask(void) { glDepthMask(GL_TRUE); }
// Disable depth write
void rlDisableDepthMask(void) { glDepthMask(GL_FALSE); }
// Enable backface culling
void rlEnableBackfaceCulling(void) { glEnable(GL_CULL_FACE); }
// Disable backface culling
void rlDisableBackfaceCulling(void) { glDisable(GL_CULL_FACE); }
// Set color mask active for screen read/draw
void rlColorMask(bool r, bool g, bool b, bool a) { glColorMask(r, g, b, a); }
// Set face culling mode
void rlSetCullFace(int mode)
{
switch (mode)
{
case RL_CULL_FACE_BACK: glCullFace(GL_BACK); break;
case RL_CULL_FACE_FRONT: glCullFace(GL_FRONT); break;
default: break;
}
}
// Enable scissor test
void rlEnableScissorTest(void) { glEnable(GL_SCISSOR_TEST); }
// Disable scissor test
void rlDisableScissorTest(void) { glDisable(GL_SCISSOR_TEST); }
// Scissor test
void rlScissor(int x, int y, int width, int height) { glScissor(x, y, width, height); }
// Enable wire mode
void rlEnableWireMode(void)
{
#if defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33)
// NOTE: glPolygonMode() not available on OpenGL ES
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
#endif
}
void rlEnablePointMode(void)
{
#if defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33)
// NOTE: glPolygonMode() not available on OpenGL ES
glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
glEnable(GL_PROGRAM_POINT_SIZE);
#endif
}
// Disable wire mode
void rlDisableWireMode(void)
{
#if defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33)
// NOTE: glPolygonMode() not available on OpenGL ES
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
#endif
}
// Set the line drawing width
void rlSetLineWidth(float width) { glLineWidth(width); }
// Get the line drawing width
float rlGetLineWidth(void)
{
float width = 0;
glGetFloatv(GL_LINE_WIDTH, &width);
return width;
}
// Enable line aliasing
void rlEnableSmoothLines(void)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_11)
glEnable(GL_LINE_SMOOTH);
#endif
}
// Disable line aliasing
void rlDisableSmoothLines(void)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_11)
glDisable(GL_LINE_SMOOTH);
#endif
}
// Enable stereo rendering
void rlEnableStereoRender(void)
{
#if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2))
RLGL.State.stereoRender = true;
#endif
}
// Disable stereo rendering
void rlDisableStereoRender(void)
{
#if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2))
RLGL.State.stereoRender = false;
#endif
}
// Check if stereo render is enabled
bool rlIsStereoRenderEnabled(void)
{
#if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2))
return RLGL.State.stereoRender;
#else
return false;
#endif
}
// Clear color buffer with color
void rlClearColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{
// Color values clamp to 0.0f(0) and 1.0f(255)
float cr = (float)r/255;
float cg = (float)g/255;
float cb = (float)b/255;
float ca = (float)a/255;
glClearColor(cr, cg, cb, ca);
}
// Clear used screen buffers (color and depth)
void rlClearScreenBuffers(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear used buffers: Color and Depth (Depth is used for 3D)
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // Stencil buffer not used...
}
// Check and log OpenGL error codes
void rlCheckErrors(void)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
int check = 1;
while (check)
{
const GLenum err = glGetError();
switch (err)
{
case GL_NO_ERROR: check = 0; break;
case 0x0500: TRACELOG(RL_LOG_WARNING, "GL: Error detected: GL_INVALID_ENUM"); break;
case 0x0501: TRACELOG(RL_LOG_WARNING, "GL: Error detected: GL_INVALID_VALUE"); break;
case 0x0502: TRACELOG(RL_LOG_WARNING, "GL: Error detected: GL_INVALID_OPERATION"); break;
case 0x0503: TRACELOG(RL_LOG_WARNING, "GL: Error detected: GL_STACK_OVERFLOW"); break;
case 0x0504: TRACELOG(RL_LOG_WARNING, "GL: Error detected: GL_STACK_UNDERFLOW"); break;
case 0x0505: TRACELOG(RL_LOG_WARNING, "GL: Error detected: GL_OUT_OF_MEMORY"); break;
case 0x0506: TRACELOG(RL_LOG_WARNING, "GL: Error detected: GL_INVALID_FRAMEBUFFER_OPERATION"); break;
default: TRACELOG(RL_LOG_WARNING, "GL: Error detected: Unknown error code: %x", err); break;
}
}
#endif
}
// Set blend mode
void rlSetBlendMode(int mode)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
if ((RLGL.State.currentBlendMode != mode) || ((mode == RL_BLEND_CUSTOM || mode == RL_BLEND_CUSTOM_SEPARATE) && RLGL.State.glCustomBlendModeModified))
{
rlDrawRenderBatch(RLGL.currentBatch);
switch (mode)
{
case RL_BLEND_ALPHA: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendEquation(GL_FUNC_ADD); break;
case RL_BLEND_ADDITIVE: glBlendFunc(GL_SRC_ALPHA, GL_ONE); glBlendEquation(GL_FUNC_ADD); break;
case RL_BLEND_MULTIPLIED: glBlendFunc(GL_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA); glBlendEquation(GL_FUNC_ADD); break;
case RL_BLEND_ADD_COLORS: glBlendFunc(GL_ONE, GL_ONE); glBlendEquation(GL_FUNC_ADD); break;
case RL_BLEND_SUBTRACT_COLORS: glBlendFunc(GL_ONE, GL_ONE); glBlendEquation(GL_FUNC_SUBTRACT); break;
case RL_BLEND_ALPHA_PREMULTIPLY: glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); glBlendEquation(GL_FUNC_ADD); break;
case RL_BLEND_CUSTOM:
{
// NOTE: Using GL blend src/dst factors and GL equation configured with rlSetBlendFactors()
glBlendFunc(RLGL.State.glBlendSrcFactor, RLGL.State.glBlendDstFactor); glBlendEquation(RLGL.State.glBlendEquation);
} break;
case RL_BLEND_CUSTOM_SEPARATE:
{
// NOTE: Using GL blend src/dst factors and GL equation configured with rlSetBlendFactorsSeparate()
glBlendFuncSeparate(RLGL.State.glBlendSrcFactorRGB, RLGL.State.glBlendDestFactorRGB, RLGL.State.glBlendSrcFactorAlpha, RLGL.State.glBlendDestFactorAlpha);
glBlendEquationSeparate(RLGL.State.glBlendEquationRGB, RLGL.State.glBlendEquationAlpha);
} break;
default: break;
}
RLGL.State.currentBlendMode = mode;
RLGL.State.glCustomBlendModeModified = false;
}
#endif
}
// Set blending mode factor and equation
void rlSetBlendFactors(int glSrcFactor, int glDstFactor, int glEquation)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
if ((RLGL.State.glBlendSrcFactor != glSrcFactor) ||
(RLGL.State.glBlendDstFactor != glDstFactor) ||
(RLGL.State.glBlendEquation != glEquation))
{
RLGL.State.glBlendSrcFactor = glSrcFactor;
RLGL.State.glBlendDstFactor = glDstFactor;
RLGL.State.glBlendEquation = glEquation;
RLGL.State.glCustomBlendModeModified = true;
}
#endif
}
// Set blending mode factor and equation separately for RGB and alpha
void rlSetBlendFactorsSeparate(int glSrcRGB, int glDstRGB, int glSrcAlpha, int glDstAlpha, int glEqRGB, int glEqAlpha)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
if ((RLGL.State.glBlendSrcFactorRGB != glSrcRGB) ||
(RLGL.State.glBlendDestFactorRGB != glDstRGB) ||
(RLGL.State.glBlendSrcFactorAlpha != glSrcAlpha) ||
(RLGL.State.glBlendDestFactorAlpha != glDstAlpha) ||
(RLGL.State.glBlendEquationRGB != glEqRGB) ||
(RLGL.State.glBlendEquationAlpha != glEqAlpha))
{
RLGL.State.glBlendSrcFactorRGB = glSrcRGB;
RLGL.State.glBlendDestFactorRGB = glDstRGB;
RLGL.State.glBlendSrcFactorAlpha = glSrcAlpha;
RLGL.State.glBlendDestFactorAlpha = glDstAlpha;
RLGL.State.glBlendEquationRGB = glEqRGB;
RLGL.State.glBlendEquationAlpha = glEqAlpha;
RLGL.State.glCustomBlendModeModified = true;
}
#endif
}
//----------------------------------------------------------------------------------
// Module Functions Definition - OpenGL Debug
//----------------------------------------------------------------------------------
#if defined(RLGL_ENABLE_OPENGL_DEBUG_CONTEXT) && defined(GRAPHICS_API_OPENGL_43)
static void GLAPIENTRY rlDebugMessageCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam)
{
// Ignore non-significant error/warning codes (NVidia drivers)
// NOTE: Here there are the details with a sample output:
// - #131169 - Framebuffer detailed info: The driver allocated storage for renderbuffer 2. (severity: low)
// - #131185 - Buffer detailed info: Buffer object 1 (bound to GL_ELEMENT_ARRAY_BUFFER_ARB, usage hint is GL_ENUM_88e4)
// will use VIDEO memory as the source for buffer object operations. (severity: low)
// - #131218 - Program/shader state performance warning: Vertex shader in program 7 is being recompiled based on GL state. (severity: medium)
// - #131204 - Texture state usage warning: The texture object (0) bound to texture image unit 0 does not have
// a defined base level and cannot be used for texture mapping. (severity: low)
if ((id == 131169) || (id == 131185) || (id == 131218) || (id == 131204)) return;
const char *msgSource = NULL;
switch (source)
{
case GL_DEBUG_SOURCE_API: msgSource = "API"; break;
case GL_DEBUG_SOURCE_WINDOW_SYSTEM: msgSource = "WINDOW_SYSTEM"; break;
case GL_DEBUG_SOURCE_SHADER_COMPILER: msgSource = "SHADER_COMPILER"; break;
case GL_DEBUG_SOURCE_THIRD_PARTY: msgSource = "THIRD_PARTY"; break;
case GL_DEBUG_SOURCE_APPLICATION: msgSource = "APPLICATION"; break;
case GL_DEBUG_SOURCE_OTHER: msgSource = "OTHER"; break;
default: break;
}
const char *msgType = NULL;
switch (type)
{
case GL_DEBUG_TYPE_ERROR: msgType = "ERROR"; break;
case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: msgType = "DEPRECATED_BEHAVIOR"; break;
case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: msgType = "UNDEFINED_BEHAVIOR"; break;
case GL_DEBUG_TYPE_PORTABILITY: msgType = "PORTABILITY"; break;
case GL_DEBUG_TYPE_PERFORMANCE: msgType = "PERFORMANCE"; break;
case GL_DEBUG_TYPE_MARKER: msgType = "MARKER"; break;
case GL_DEBUG_TYPE_PUSH_GROUP: msgType = "PUSH_GROUP"; break;
case GL_DEBUG_TYPE_POP_GROUP: msgType = "POP_GROUP"; break;
case GL_DEBUG_TYPE_OTHER: msgType = "OTHER"; break;
default: break;
}
const char *msgSeverity = "DEFAULT";
switch (severity)
{
case GL_DEBUG_SEVERITY_LOW: msgSeverity = "LOW"; break;
case GL_DEBUG_SEVERITY_MEDIUM: msgSeverity = "MEDIUM"; break;
case GL_DEBUG_SEVERITY_HIGH: msgSeverity = "HIGH"; break;
case GL_DEBUG_SEVERITY_NOTIFICATION: msgSeverity = "NOTIFICATION"; break;
default: break;
}
TRACELOG(LOG_WARNING, "GL: OpenGL debug message: %s", message);
TRACELOG(LOG_WARNING, " > Type: %s", msgType);
TRACELOG(LOG_WARNING, " > Source = %s", msgSource);
TRACELOG(LOG_WARNING, " > Severity = %s", msgSeverity);
}
#endif
//----------------------------------------------------------------------------------
// Module Functions Definition - rlgl functionality
//----------------------------------------------------------------------------------
// Initialize rlgl: OpenGL extensions, default buffers/shaders/textures, OpenGL states
void rlglInit(int width, int height)
{
// Enable OpenGL debug context if required
#if defined(RLGL_ENABLE_OPENGL_DEBUG_CONTEXT) && defined(GRAPHICS_API_OPENGL_43)
if ((glDebugMessageCallback != NULL) && (glDebugMessageControl != NULL))
{
glDebugMessageCallback(rlDebugMessageCallback, 0);
// glDebugMessageControl(GL_DEBUG_SOURCE_API, GL_DEBUG_TYPE_ERROR, GL_DEBUG_SEVERITY_HIGH, 0, 0, GL_TRUE);
// Debug context options:
// - GL_DEBUG_OUTPUT - Faster version but not useful for breakpoints
// - GL_DEBUG_OUTPUT_SYNCHRONUS - Callback is in sync with errors, so a breakpoint can be placed on the callback in order to get a stacktrace for the GL error
glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
}
#endif
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
// Init default white texture
unsigned char pixels[4] = { 255, 255, 255, 255 }; // 1 pixel RGBA (4 bytes)
RLGL.State.defaultTextureId = rlLoadTexture(pixels, 1, 1, RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, 1);
if (RLGL.State.defaultTextureId != 0) TRACELOG(RL_LOG_INFO, "TEXTURE: [ID %i] Default texture loaded successfully", RLGL.State.defaultTextureId);
else TRACELOG(RL_LOG_WARNING, "TEXTURE: Failed to load default texture");
// Init default Shader (customized for GL 3.3 and ES2)
// Loaded: RLGL.State.defaultShaderId + RLGL.State.defaultShaderLocs
rlLoadShaderDefault();
RLGL.State.currentShaderId = RLGL.State.defaultShaderId;
RLGL.State.currentShaderLocs = RLGL.State.defaultShaderLocs;
// Init default vertex arrays buffers
// Simulate that the default shader has the location RL_SHADER_LOC_VERTEX_NORMAL to bind the normal buffer for the default render batch
RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_NORMAL] = RL_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL;
RLGL.defaultBatch = rlLoadRenderBatch(RL_DEFAULT_BATCH_BUFFERS, RL_DEFAULT_BATCH_BUFFER_ELEMENTS);
RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_NORMAL] = -1;
RLGL.currentBatch = &RLGL.defaultBatch;
// Init stack matrices (emulating OpenGL 1.1)
for (int i = 0; i < RL_MAX_MATRIX_STACK_SIZE; i++) RLGL.State.stack[i] = rlMatrixIdentity();
// Init internal matrices
RLGL.State.transform = rlMatrixIdentity();
RLGL.State.projection = rlMatrixIdentity();
RLGL.State.modelview = rlMatrixIdentity();
RLGL.State.currentMatrix = &RLGL.State.modelview;
#endif // GRAPHICS_API_OPENGL_33 || GRAPHICS_API_OPENGL_ES2
// Initialize OpenGL default states
//----------------------------------------------------------
// Init state: Depth test
glDepthFunc(GL_LEQUAL); // Type of depth testing to apply
glDisable(GL_DEPTH_TEST); // Disable depth testing for 2D (only used for 3D)
// Init state: Blending mode
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Color blending function (how colors are mixed)
glEnable(GL_BLEND); // Enable color blending (required to work with transparencies)
// Init state: Culling
// NOTE: All shapes/models triangles are drawn CCW
glCullFace(GL_BACK); // Cull the back face (default)
glFrontFace(GL_CCW); // Front face are defined counter clockwise (default)
glEnable(GL_CULL_FACE); // Enable backface culling
// Init state: Cubemap seamless
#if defined(GRAPHICS_API_OPENGL_33)
glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS); // Seamless cubemaps (not supported on OpenGL ES 2.0)
#endif
#if defined(GRAPHICS_API_OPENGL_11)
// Init state: Color hints (deprecated in OpenGL 3.0+)
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Improve quality of color and texture coordinate interpolation
glShadeModel(GL_SMOOTH); // Smooth shading between vertex (vertex colors interpolation)
#endif
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
// Store screen size into global variables
RLGL.State.framebufferWidth = width;
RLGL.State.framebufferHeight = height;
TRACELOG(RL_LOG_INFO, "RLGL: Default OpenGL state initialized successfully");
//----------------------------------------------------------
#endif
// Init state: Color/Depth buffers clear
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set clear color (black)
glClearDepth(1.0f); // Set clear depth value (default)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers (depth buffer required for 3D)
}
// Vertex Buffer Object deinitialization (memory free)
void rlglClose(void)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
rlUnloadRenderBatch(RLGL.defaultBatch);
rlUnloadShaderDefault(); // Unload default shader
glDeleteTextures(1, &RLGL.State.defaultTextureId); // Unload default texture
TRACELOG(RL_LOG_INFO, "TEXTURE: [ID %i] Default texture unloaded successfully", RLGL.State.defaultTextureId);
#endif
}
// Load OpenGL extensions
// NOTE: External loader function must be provided
void rlLoadExtensions(void *loader)
{
#if defined(GRAPHICS_API_OPENGL_33) // Also defined for GRAPHICS_API_OPENGL_21
// NOTE: glad is generated and contains only required OpenGL 3.3 Core extensions (and lower versions)
if (gladLoadGL((GLADloadfunc)loader) == 0) TRACELOG(RL_LOG_WARNING, "GLAD: Cannot load OpenGL extensions");
else TRACELOG(RL_LOG_INFO, "GLAD: OpenGL extensions loaded successfully");
// Get number of supported extensions
GLint numExt = 0;
glGetIntegerv(GL_NUM_EXTENSIONS, &numExt);
TRACELOG(RL_LOG_INFO, "GL: Supported extensions count: %i", numExt);
#if defined(RLGL_SHOW_GL_DETAILS_INFO)
// Get supported extensions list
// WARNING: glGetStringi() not available on OpenGL 2.1
TRACELOG(RL_LOG_INFO, "GL: OpenGL extensions:");
for (int i = 0; i < numExt; i++) TRACELOG(RL_LOG_INFO, " %s", glGetStringi(GL_EXTENSIONS, i));
#endif
#if defined(GRAPHICS_API_OPENGL_21)
// Register supported extensions flags
// Optional OpenGL 2.1 extensions
RLGL.ExtSupported.vao = GLAD_GL_ARB_vertex_array_object;
RLGL.ExtSupported.instancing = (GLAD_GL_EXT_draw_instanced && GLAD_GL_ARB_instanced_arrays);
RLGL.ExtSupported.texNPOT = GLAD_GL_ARB_texture_non_power_of_two;
RLGL.ExtSupported.texFloat32 = GLAD_GL_ARB_texture_float;
RLGL.ExtSupported.texFloat16 = GLAD_GL_ARB_texture_float;
RLGL.ExtSupported.texDepth = GLAD_GL_ARB_depth_texture;
RLGL.ExtSupported.maxDepthBits = 32;
RLGL.ExtSupported.texAnisoFilter = GLAD_GL_EXT_texture_filter_anisotropic;
RLGL.ExtSupported.texMirrorClamp = GLAD_GL_EXT_texture_mirror_clamp;
#else
// Register supported extensions flags
// OpenGL 3.3 extensions supported by default (core)
RLGL.ExtSupported.vao = true;
RLGL.ExtSupported.instancing = true;
RLGL.ExtSupported.texNPOT = true;
RLGL.ExtSupported.texFloat32 = true;
RLGL.ExtSupported.texFloat16 = true;
RLGL.ExtSupported.texDepth = true;
RLGL.ExtSupported.maxDepthBits = 32;
RLGL.ExtSupported.texAnisoFilter = true;
RLGL.ExtSupported.texMirrorClamp = true;
#endif
// Optional OpenGL 3.3 extensions
RLGL.ExtSupported.texCompASTC = GLAD_GL_KHR_texture_compression_astc_hdr && GLAD_GL_KHR_texture_compression_astc_ldr;
RLGL.ExtSupported.texCompDXT = GLAD_GL_EXT_texture_compression_s3tc; // Texture compression: DXT
RLGL.ExtSupported.texCompETC2 = GLAD_GL_ARB_ES3_compatibility; // Texture compression: ETC2/EAC
#if defined(GRAPHICS_API_OPENGL_43)
RLGL.ExtSupported.computeShader = GLAD_GL_ARB_compute_shader;
RLGL.ExtSupported.ssbo = GLAD_GL_ARB_shader_storage_buffer_object;
#endif
#endif // GRAPHICS_API_OPENGL_33
#if defined(GRAPHICS_API_OPENGL_ES3)
// Register supported extensions flags
// OpenGL ES 3.0 extensions supported by default (or it should be)
RLGL.ExtSupported.vao = true;
RLGL.ExtSupported.instancing = true;
RLGL.ExtSupported.texNPOT = true;
RLGL.ExtSupported.texFloat32 = true;
RLGL.ExtSupported.texFloat16 = true;
RLGL.ExtSupported.texDepth = true;
RLGL.ExtSupported.texDepthWebGL = true;
RLGL.ExtSupported.maxDepthBits = 24;
RLGL.ExtSupported.texAnisoFilter = true;
RLGL.ExtSupported.texMirrorClamp = true;
// TODO: Check for additional OpenGL ES 3.0 supported extensions:
//RLGL.ExtSupported.texCompDXT = true;
//RLGL.ExtSupported.texCompETC1 = true;
//RLGL.ExtSupported.texCompETC2 = true;
//RLGL.ExtSupported.texCompPVRT = true;
//RLGL.ExtSupported.texCompASTC = true;
//RLGL.ExtSupported.maxAnisotropyLevel = true;
//RLGL.ExtSupported.computeShader = true;
//RLGL.ExtSupported.ssbo = true;
#elif defined(GRAPHICS_API_OPENGL_ES2)
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_DESKTOP_SDL)
// TODO: Support GLAD loader for OpenGL ES 3.0
if (gladLoadGLES2((GLADloadfunc)loader) == 0) TRACELOG(RL_LOG_WARNING, "GLAD: Cannot load OpenGL ES2.0 functions");
else TRACELOG(RL_LOG_INFO, "GLAD: OpenGL ES 2.0 loaded successfully");
#endif
// Get supported extensions list
GLint numExt = 0;
const char **extList = RL_MALLOC(512*sizeof(const char *)); // Allocate 512 strings pointers (2 KB)
const char *extensions = (const char *)glGetString(GL_EXTENSIONS); // One big const string
// NOTE: We have to duplicate string because glGetString() returns a const string
int size = strlen(extensions) + 1; // Get extensions string size in bytes
char *extensionsDup = (char *)RL_CALLOC(size, sizeof(char));
strcpy(extensionsDup, extensions);
extList[numExt] = extensionsDup;
for (int i = 0; i < size; i++)
{
if (extensionsDup[i] == ' ')
{
extensionsDup[i] = '\0';
numExt++;
extList[numExt] = &extensionsDup[i + 1];
}
}
TRACELOG(RL_LOG_INFO, "GL: Supported extensions count: %i", numExt);
#if defined(RLGL_SHOW_GL_DETAILS_INFO)
TRACELOG(RL_LOG_INFO, "GL: OpenGL extensions:");
for (int i = 0; i < numExt; i++) TRACELOG(RL_LOG_INFO, " %s", extList[i]);
#endif
// Check required extensions
for (int i = 0; i < numExt; i++)
{
// Check VAO support
// NOTE: Only check on OpenGL ES, OpenGL 3.3 has VAO support as core feature
if (strcmp(extList[i], (const char *)"GL_OES_vertex_array_object") == 0)
{
// The extension is supported by our hardware and driver, try to get related functions pointers
// NOTE: emscripten does not support VAOs natively, it uses emulation and it reduces overall performance...
glGenVertexArrays = (PFNGLGENVERTEXARRAYSOESPROC)((rlglLoadProc)loader)("glGenVertexArraysOES");
glBindVertexArray = (PFNGLBINDVERTEXARRAYOESPROC)((rlglLoadProc)loader)("glBindVertexArrayOES");
glDeleteVertexArrays = (PFNGLDELETEVERTEXARRAYSOESPROC)((rlglLoadProc)loader)("glDeleteVertexArraysOES");
//glIsVertexArray = (PFNGLISVERTEXARRAYOESPROC)loader("glIsVertexArrayOES"); // NOTE: Fails in WebGL, omitted
if ((glGenVertexArrays != NULL) && (glBindVertexArray != NULL) && (glDeleteVertexArrays != NULL)) RLGL.ExtSupported.vao = true;
}
// Check instanced rendering support
if (strcmp(extList[i], (const char *)"GL_ANGLE_instanced_arrays") == 0) // Web ANGLE
{
glDrawArraysInstanced = (PFNGLDRAWARRAYSINSTANCEDEXTPROC)((rlglLoadProc)loader)("glDrawArraysInstancedANGLE");
glDrawElementsInstanced = (PFNGLDRAWELEMENTSINSTANCEDEXTPROC)((rlglLoadProc)loader)("glDrawElementsInstancedANGLE");
glVertexAttribDivisor = (PFNGLVERTEXATTRIBDIVISOREXTPROC)((rlglLoadProc)loader)("glVertexAttribDivisorANGLE");
if ((glDrawArraysInstanced != NULL) && (glDrawElementsInstanced != NULL) && (glVertexAttribDivisor != NULL)) RLGL.ExtSupported.instancing = true;
}
else
{
if ((strcmp(extList[i], (const char *)"GL_EXT_draw_instanced") == 0) && // Standard EXT
(strcmp(extList[i], (const char *)"GL_EXT_instanced_arrays") == 0))
{
glDrawArraysInstanced = (PFNGLDRAWARRAYSINSTANCEDEXTPROC)((rlglLoadProc)loader)("glDrawArraysInstancedEXT");
glDrawElementsInstanced = (PFNGLDRAWELEMENTSINSTANCEDEXTPROC)((rlglLoadProc)loader)("glDrawElementsInstancedEXT");
glVertexAttribDivisor = (PFNGLVERTEXATTRIBDIVISOREXTPROC)((rlglLoadProc)loader)("glVertexAttribDivisorEXT");
if ((glDrawArraysInstanced != NULL) && (glDrawElementsInstanced != NULL) && (glVertexAttribDivisor != NULL)) RLGL.ExtSupported.instancing = true;
}
}
// Check NPOT textures support
// NOTE: Only check on OpenGL ES, OpenGL 3.3 has NPOT textures full support as core feature
if (strcmp(extList[i], (const char *)"GL_OES_texture_npot") == 0) RLGL.ExtSupported.texNPOT = true;
// Check texture float support
if (strcmp(extList[i], (const char *)"GL_OES_texture_float") == 0) RLGL.ExtSupported.texFloat32 = true;
if (strcmp(extList[i], (const char *)"GL_OES_texture_half_float") == 0) RLGL.ExtSupported.texFloat16 = true;
// Check depth texture support
if (strcmp(extList[i], (const char *)"GL_OES_depth_texture") == 0) RLGL.ExtSupported.texDepth = true;
if (strcmp(extList[i], (const char *)"GL_WEBGL_depth_texture") == 0) RLGL.ExtSupported.texDepthWebGL = true; // WebGL requires unsized internal format
if (RLGL.ExtSupported.texDepthWebGL) RLGL.ExtSupported.texDepth = true;
if (strcmp(extList[i], (const char *)"GL_OES_depth24") == 0) RLGL.ExtSupported.maxDepthBits = 24; // Not available on WebGL
if (strcmp(extList[i], (const char *)"GL_OES_depth32") == 0) RLGL.ExtSupported.maxDepthBits = 32; // Not available on WebGL
// Check texture compression support: DXT
if ((strcmp(extList[i], (const char *)"GL_EXT_texture_compression_s3tc") == 0) ||
(strcmp(extList[i], (const char *)"GL_WEBGL_compressed_texture_s3tc") == 0) ||
(strcmp(extList[i], (const char *)"GL_WEBKIT_WEBGL_compressed_texture_s3tc") == 0)) RLGL.ExtSupported.texCompDXT = true;
// Check texture compression support: ETC1
if ((strcmp(extList[i], (const char *)"GL_OES_compressed_ETC1_RGB8_texture") == 0) ||
(strcmp(extList[i], (const char *)"GL_WEBGL_compressed_texture_etc1") == 0)) RLGL.ExtSupported.texCompETC1 = true;
// Check texture compression support: ETC2/EAC
if (strcmp(extList[i], (const char *)"GL_ARB_ES3_compatibility") == 0) RLGL.ExtSupported.texCompETC2 = true;
// Check texture compression support: PVR
if (strcmp(extList[i], (const char *)"GL_IMG_texture_compression_pvrtc") == 0) RLGL.ExtSupported.texCompPVRT = true;
// Check texture compression support: ASTC
if (strcmp(extList[i], (const char *)"GL_KHR_texture_compression_astc_hdr") == 0) RLGL.ExtSupported.texCompASTC = true;
// Check anisotropic texture filter support
if (strcmp(extList[i], (const char *)"GL_EXT_texture_filter_anisotropic") == 0) RLGL.ExtSupported.texAnisoFilter = true;
// Check clamp mirror wrap mode support
if (strcmp(extList[i], (const char *)"GL_EXT_texture_mirror_clamp") == 0) RLGL.ExtSupported.texMirrorClamp = true;
}
// Free extensions pointers
RL_FREE(extList);
RL_FREE(extensionsDup); // Duplicated string must be deallocated
#endif // GRAPHICS_API_OPENGL_ES2
// Check OpenGL information and capabilities
//------------------------------------------------------------------------------
// Show current OpenGL and GLSL version
TRACELOG(RL_LOG_INFO, "GL: OpenGL device information:");
TRACELOG(RL_LOG_INFO, " > Vendor: %s", glGetString(GL_VENDOR));
TRACELOG(RL_LOG_INFO, " > Renderer: %s", glGetString(GL_RENDERER));
TRACELOG(RL_LOG_INFO, " > Version: %s", glGetString(GL_VERSION));
TRACELOG(RL_LOG_INFO, " > GLSL: %s", glGetString(GL_SHADING_LANGUAGE_VERSION));
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
// NOTE: Anisotropy levels capability is an extension
#ifndef GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT
#define GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT 0x84FF
#endif
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &RLGL.ExtSupported.maxAnisotropyLevel);
#if defined(RLGL_SHOW_GL_DETAILS_INFO)
// Show some OpenGL GPU capabilities
TRACELOG(RL_LOG_INFO, "GL: OpenGL capabilities:");
GLint capability = 0;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &capability);
TRACELOG(RL_LOG_INFO, " GL_MAX_TEXTURE_SIZE: %i", capability);
glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, &capability);
TRACELOG(RL_LOG_INFO, " GL_MAX_CUBE_MAP_TEXTURE_SIZE: %i", capability);
glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &capability);
TRACELOG(RL_LOG_INFO, " GL_MAX_TEXTURE_IMAGE_UNITS: %i", capability);
glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &capability);
TRACELOG(RL_LOG_INFO, " GL_MAX_VERTEX_ATTRIBS: %i", capability);
#if !defined(GRAPHICS_API_OPENGL_ES2)
glGetIntegerv(GL_MAX_UNIFORM_BLOCK_SIZE, &capability);
TRACELOG(RL_LOG_INFO, " GL_MAX_UNIFORM_BLOCK_SIZE: %i", capability);
glGetIntegerv(GL_MAX_DRAW_BUFFERS, &capability);
TRACELOG(RL_LOG_INFO, " GL_MAX_DRAW_BUFFERS: %i", capability);
if (RLGL.ExtSupported.texAnisoFilter) TRACELOG(RL_LOG_INFO, " GL_MAX_TEXTURE_MAX_ANISOTROPY: %.0f", RLGL.ExtSupported.maxAnisotropyLevel);
#endif
glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &capability);
TRACELOG(RL_LOG_INFO, " GL_NUM_COMPRESSED_TEXTURE_FORMATS: %i", capability);
GLint *compFormats = (GLint *)RL_CALLOC(capability, sizeof(GLint));
glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, compFormats);
for (int i = 0; i < capability; i++) TRACELOG(RL_LOG_INFO, " %s", rlGetCompressedFormatName(compFormats[i]));
RL_FREE(compFormats);
#if defined(GRAPHICS_API_OPENGL_43)
glGetIntegerv(GL_MAX_VERTEX_ATTRIB_BINDINGS, &capability);
TRACELOG(RL_LOG_INFO, " GL_MAX_VERTEX_ATTRIB_BINDINGS: %i", capability);
glGetIntegerv(GL_MAX_UNIFORM_LOCATIONS, &capability);
TRACELOG(RL_LOG_INFO, " GL_MAX_UNIFORM_LOCATIONS: %i", capability);
#endif // GRAPHICS_API_OPENGL_43
#else // RLGL_SHOW_GL_DETAILS_INFO
// Show some basic info about GL supported features
if (RLGL.ExtSupported.vao) TRACELOG(RL_LOG_INFO, "GL: VAO extension detected, VAO functions loaded successfully");
else TRACELOG(RL_LOG_WARNING, "GL: VAO extension not found, VAO not supported");
if (RLGL.ExtSupported.texNPOT) TRACELOG(RL_LOG_INFO, "GL: NPOT textures extension detected, full NPOT textures supported");
else TRACELOG(RL_LOG_WARNING, "GL: NPOT textures extension not found, limited NPOT support (no-mipmaps, no-repeat)");
if (RLGL.ExtSupported.texCompDXT) TRACELOG(RL_LOG_INFO, "GL: DXT compressed textures supported");
if (RLGL.ExtSupported.texCompETC1) TRACELOG(RL_LOG_INFO, "GL: ETC1 compressed textures supported");
if (RLGL.ExtSupported.texCompETC2) TRACELOG(RL_LOG_INFO, "GL: ETC2/EAC compressed textures supported");
if (RLGL.ExtSupported.texCompPVRT) TRACELOG(RL_LOG_INFO, "GL: PVRT compressed textures supported");
if (RLGL.ExtSupported.texCompASTC) TRACELOG(RL_LOG_INFO, "GL: ASTC compressed textures supported");
if (RLGL.ExtSupported.computeShader) TRACELOG(RL_LOG_INFO, "GL: Compute shaders supported");
if (RLGL.ExtSupported.ssbo) TRACELOG(RL_LOG_INFO, "GL: Shader storage buffer objects supported");
#endif // RLGL_SHOW_GL_DETAILS_INFO
#endif // GRAPHICS_API_OPENGL_33 || GRAPHICS_API_OPENGL_ES2
}
// Get current OpenGL version
int rlGetVersion(void)
{
int glVersion = 0;
#if defined(GRAPHICS_API_OPENGL_11)
glVersion = RL_OPENGL_11;
#endif
#if defined(GRAPHICS_API_OPENGL_21)
glVersion = RL_OPENGL_21;
#elif defined(GRAPHICS_API_OPENGL_43)
glVersion = RL_OPENGL_43;
#elif defined(GRAPHICS_API_OPENGL_33)
glVersion = RL_OPENGL_33;
#endif
#if defined(GRAPHICS_API_OPENGL_ES3)
glVersion = RL_OPENGL_ES_30;
#elif defined(GRAPHICS_API_OPENGL_ES2)
glVersion = RL_OPENGL_ES_20;
#endif
return glVersion;
}
// Set current framebuffer width
void rlSetFramebufferWidth(int width)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
RLGL.State.framebufferWidth = width;
#endif
}
// Set current framebuffer height
void rlSetFramebufferHeight(int height)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
RLGL.State.framebufferHeight = height;
#endif
}
// Get default framebuffer width
int rlGetFramebufferWidth(void)
{
int width = 0;
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
width = RLGL.State.framebufferWidth;
#endif
return width;
}
// Get default framebuffer height
int rlGetFramebufferHeight(void)
{
int height = 0;
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
height = RLGL.State.framebufferHeight;
#endif
return height;
}
// Get default internal texture (white texture)
// NOTE: Default texture is a 1x1 pixel UNCOMPRESSED_R8G8B8A8
unsigned int rlGetTextureIdDefault(void)
{
unsigned int id = 0;
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
id = RLGL.State.defaultTextureId;
#endif
return id;
}
// Get default shader id
unsigned int rlGetShaderIdDefault(void)
{
unsigned int id = 0;
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
id = RLGL.State.defaultShaderId;
#endif
return id;
}
// Get default shader locs
int *rlGetShaderLocsDefault(void)
{
int *locs = NULL;
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
locs = RLGL.State.defaultShaderLocs;
#endif
return locs;
}
// Render batch management
//------------------------------------------------------------------------------------------------
// Load render batch
rlRenderBatch rlLoadRenderBatch(int numBuffers, int bufferElements)
{
rlRenderBatch batch = { 0 };
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
// Initialize CPU (RAM) vertex buffers (position, texcoord, color data and indexes)
//--------------------------------------------------------------------------------------------
batch.vertexBuffer = (rlVertexBuffer *)RL_MALLOC(numBuffers*sizeof(rlVertexBuffer));
for (int i = 0; i < numBuffers; i++)
{
batch.vertexBuffer[i].elementCount = bufferElements;
batch.vertexBuffer[i].vertices = (float *)RL_MALLOC(bufferElements*3*4*sizeof(float)); // 3 float by vertex, 4 vertex by quad
batch.vertexBuffer[i].texcoords = (float *)RL_MALLOC(bufferElements*2*4*sizeof(float)); // 2 float by texcoord, 4 texcoord by quad
batch.vertexBuffer[i].normals = (float *)RL_MALLOC(bufferElements*3*4*sizeof(float)); // 3 float by vertex, 4 vertex by quad
batch.vertexBuffer[i].colors = (unsigned char *)RL_MALLOC(bufferElements*4*4*sizeof(unsigned char)); // 4 float by color, 4 colors by quad
#if defined(GRAPHICS_API_OPENGL_33)
batch.vertexBuffer[i].indices = (unsigned int *)RL_MALLOC(bufferElements*6*sizeof(unsigned int)); // 6 int by quad (indices)
#endif
#if defined(GRAPHICS_API_OPENGL_ES2)
batch.vertexBuffer[i].indices = (unsigned short *)RL_MALLOC(bufferElements*6*sizeof(unsigned short)); // 6 int by quad (indices)
#endif
for (int j = 0; j < (3*4*bufferElements); j++) batch.vertexBuffer[i].vertices[j] = 0.0f;
for (int j = 0; j < (2*4*bufferElements); j++) batch.vertexBuffer[i].texcoords[j] = 0.0f;
for (int j = 0; j < (3*4*bufferElements); j++) batch.vertexBuffer[i].normals[j] = 0.0f;
for (int j = 0; j < (4*4*bufferElements); j++) batch.vertexBuffer[i].colors[j] = 0;
int k = 0;
// Indices can be initialized right now
for (int j = 0; j < (6*bufferElements); j += 6)
{
batch.vertexBuffer[i].indices[j] = 4*k;
batch.vertexBuffer[i].indices[j + 1] = 4*k + 1;
batch.vertexBuffer[i].indices[j + 2] = 4*k + 2;
batch.vertexBuffer[i].indices[j + 3] = 4*k;
batch.vertexBuffer[i].indices[j + 4] = 4*k + 2;
batch.vertexBuffer[i].indices[j + 5] = 4*k + 3;
k++;
}
RLGL.State.vertexCounter = 0;
}
TRACELOG(RL_LOG_INFO, "RLGL: Render batch vertex buffers loaded successfully in RAM (CPU)");
//--------------------------------------------------------------------------------------------
// Upload to GPU (VRAM) vertex data and initialize VAOs/VBOs
//--------------------------------------------------------------------------------------------
for (int i = 0; i < numBuffers; i++)
{
if (RLGL.ExtSupported.vao)
{
// Initialize Quads VAO
glGenVertexArrays(1, &batch.vertexBuffer[i].vaoId);
glBindVertexArray(batch.vertexBuffer[i].vaoId);
}
// Quads - Vertex buffers binding and attributes enable
// Vertex position buffer (shader-location = 0)
glGenBuffers(1, &batch.vertexBuffer[i].vboId[0]);
glBindBuffer(GL_ARRAY_BUFFER, batch.vertexBuffer[i].vboId[0]);
glBufferData(GL_ARRAY_BUFFER, bufferElements*3*4*sizeof(float), batch.vertexBuffer[i].vertices, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_POSITION]);
glVertexAttribPointer(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_POSITION], 3, GL_FLOAT, 0, 0, 0);
// Vertex texcoord buffer (shader-location = 1)
glGenBuffers(1, &batch.vertexBuffer[i].vboId[1]);
glBindBuffer(GL_ARRAY_BUFFER, batch.vertexBuffer[i].vboId[1]);
glBufferData(GL_ARRAY_BUFFER, bufferElements*2*4*sizeof(float), batch.vertexBuffer[i].texcoords, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_TEXCOORD01]);
glVertexAttribPointer(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_TEXCOORD01], 2, GL_FLOAT, 0, 0, 0);
// Vertex normal buffer (shader-location = 2)
glGenBuffers(1, &batch.vertexBuffer[i].vboId[2]);
glBindBuffer(GL_ARRAY_BUFFER, batch.vertexBuffer[i].vboId[2]);
glBufferData(GL_ARRAY_BUFFER, bufferElements*3*4*sizeof(float), batch.vertexBuffer[i].normals, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_NORMAL]);
glVertexAttribPointer(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_NORMAL], 3, GL_FLOAT, 0, 0, 0);
// Vertex color buffer (shader-location = 3)
glGenBuffers(1, &batch.vertexBuffer[i].vboId[3]);
glBindBuffer(GL_ARRAY_BUFFER, batch.vertexBuffer[i].vboId[3]);
glBufferData(GL_ARRAY_BUFFER, bufferElements*4*4*sizeof(unsigned char), batch.vertexBuffer[i].colors, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_COLOR]);
glVertexAttribPointer(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_COLOR], 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0);
// Fill index buffer
glGenBuffers(1, &batch.vertexBuffer[i].vboId[4]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, batch.vertexBuffer[i].vboId[4]);
#if defined(GRAPHICS_API_OPENGL_33)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, bufferElements*6*sizeof(int), batch.vertexBuffer[i].indices, GL_STATIC_DRAW);
#endif
#if defined(GRAPHICS_API_OPENGL_ES2)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, bufferElements*6*sizeof(short), batch.vertexBuffer[i].indices, GL_STATIC_DRAW);
#endif
}
TRACELOG(RL_LOG_INFO, "RLGL: Render batch vertex buffers loaded successfully in VRAM (GPU)");
// Unbind the current VAO
if (RLGL.ExtSupported.vao) glBindVertexArray(0);
//--------------------------------------------------------------------------------------------
// Init draw calls tracking system
//--------------------------------------------------------------------------------------------
batch.draws = (rlDrawCall *)RL_MALLOC(RL_DEFAULT_BATCH_DRAWCALLS*sizeof(rlDrawCall));
for (int i = 0; i < RL_DEFAULT_BATCH_DRAWCALLS; i++)
{
batch.draws[i].mode = RL_QUADS;
batch.draws[i].vertexCount = 0;
batch.draws[i].vertexAlignment = 0;
//batch.draws[i].vaoId = 0;
//batch.draws[i].shaderId = 0;
batch.draws[i].textureId = RLGL.State.defaultTextureId;
//batch.draws[i].RLGL.State.projection = rlMatrixIdentity();
//batch.draws[i].RLGL.State.modelview = rlMatrixIdentity();
}
batch.bufferCount = numBuffers; // Record buffer count
batch.drawCounter = 1; // Reset draws counter
batch.currentDepth = -1.0f; // Reset depth value
//--------------------------------------------------------------------------------------------
#endif
return batch;
}
// Unload default internal buffers vertex data from CPU and GPU
void rlUnloadRenderBatch(rlRenderBatch batch)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
// Unbind everything
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
// Unload all vertex buffers data
for (int i = 0; i < batch.bufferCount; i++)
{
// Unbind VAO attribs data
if (RLGL.ExtSupported.vao)
{
glBindVertexArray(batch.vertexBuffer[i].vaoId);
glDisableVertexAttribArray(RL_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION);
glDisableVertexAttribArray(RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD);
glDisableVertexAttribArray(RL_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL);
glDisableVertexAttribArray(RL_DEFAULT_SHADER_ATTRIB_LOCATION_COLOR);
glBindVertexArray(0);
}
// Delete VBOs from GPU (VRAM)
glDeleteBuffers(1, &batch.vertexBuffer[i].vboId[0]);
glDeleteBuffers(1, &batch.vertexBuffer[i].vboId[1]);
glDeleteBuffers(1, &batch.vertexBuffer[i].vboId[2]);
glDeleteBuffers(1, &batch.vertexBuffer[i].vboId[3]);
glDeleteBuffers(1, &batch.vertexBuffer[i].vboId[4]);
// Delete VAOs from GPU (VRAM)
if (RLGL.ExtSupported.vao) glDeleteVertexArrays(1, &batch.vertexBuffer[i].vaoId);
// Free vertex arrays memory from CPU (RAM)
RL_FREE(batch.vertexBuffer[i].vertices);
RL_FREE(batch.vertexBuffer[i].texcoords);
RL_FREE(batch.vertexBuffer[i].normals);
RL_FREE(batch.vertexBuffer[i].colors);
RL_FREE(batch.vertexBuffer[i].indices);
}
// Unload arrays
RL_FREE(batch.vertexBuffer);
RL_FREE(batch.draws);
#endif
}
// Draw render batch
// NOTE: We require a pointer to reset batch and increase current buffer (multi-buffer)
void rlDrawRenderBatch(rlRenderBatch *batch)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
// Update batch vertex buffers
//------------------------------------------------------------------------------------------------------------
// NOTE: If there is not vertex data, buffers doesn't need to be updated (vertexCount > 0)
// TODO: If no data changed on the CPU arrays --> No need to re-update GPU arrays (use a change detector flag?)
if (RLGL.State.vertexCounter > 0)
{
// Activate elements VAO
if (RLGL.ExtSupported.vao) glBindVertexArray(batch->vertexBuffer[batch->currentBuffer].vaoId);
// Vertex positions buffer
glBindBuffer(GL_ARRAY_BUFFER, batch->vertexBuffer[batch->currentBuffer].vboId[0]);
glBufferSubData(GL_ARRAY_BUFFER, 0, RLGL.State.vertexCounter*3*sizeof(float), batch->vertexBuffer[batch->currentBuffer].vertices);
//glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*4*batch->vertexBuffer[batch->currentBuffer].elementCount, batch->vertexBuffer[batch->currentBuffer].vertices, GL_DYNAMIC_DRAW); // Update all buffer
// Texture coordinates buffer
glBindBuffer(GL_ARRAY_BUFFER, batch->vertexBuffer[batch->currentBuffer].vboId[1]);
glBufferSubData(GL_ARRAY_BUFFER, 0, RLGL.State.vertexCounter*2*sizeof(float), batch->vertexBuffer[batch->currentBuffer].texcoords);
//glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*4*batch->vertexBuffer[batch->currentBuffer].elementCount, batch->vertexBuffer[batch->currentBuffer].texcoords, GL_DYNAMIC_DRAW); // Update all buffer
// Normals buffer
glBindBuffer(GL_ARRAY_BUFFER, batch->vertexBuffer[batch->currentBuffer].vboId[2]);
glBufferSubData(GL_ARRAY_BUFFER, 0, RLGL.State.vertexCounter*3*sizeof(float), batch->vertexBuffer[batch->currentBuffer].normals);
//glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*4*batch->vertexBuffer[batch->currentBuffer].elementCount, batch->vertexBuffer[batch->currentBuffer].normals, GL_DYNAMIC_DRAW); // Update all buffer
// Colors buffer
glBindBuffer(GL_ARRAY_BUFFER, batch->vertexBuffer[batch->currentBuffer].vboId[3]);
glBufferSubData(GL_ARRAY_BUFFER, 0, RLGL.State.vertexCounter*4*sizeof(unsigned char), batch->vertexBuffer[batch->currentBuffer].colors);
//glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*4*batch->vertexBuffer[batch->currentBuffer].elementCount, batch->vertexBuffer[batch->currentBuffer].colors, GL_DYNAMIC_DRAW); // Update all buffer
// NOTE: glMapBuffer() causes sync issue.
// If GPU is working with this buffer, glMapBuffer() will wait(stall) until GPU to finish its job.
// To avoid waiting (idle), you can call first glBufferData() with NULL pointer before glMapBuffer().
// If you do that, the previous data in PBO will be discarded and glMapBuffer() returns a new
// allocated pointer immediately even if GPU is still working with the previous data.
// Another option: map the buffer object into client's memory
// Probably this code could be moved somewhere else...
// batch->vertexBuffer[batch->currentBuffer].vertices = (float *)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
// if (batch->vertexBuffer[batch->currentBuffer].vertices)
// {
// Update vertex data
// }
// glUnmapBuffer(GL_ARRAY_BUFFER);
// Unbind the current VAO
if (RLGL.ExtSupported.vao) glBindVertexArray(0);
}
//------------------------------------------------------------------------------------------------------------
// Draw batch vertex buffers (considering VR stereo if required)
//------------------------------------------------------------------------------------------------------------
Matrix matProjection = RLGL.State.projection;
Matrix matModelView = RLGL.State.modelview;
int eyeCount = 1;
if (RLGL.State.stereoRender) eyeCount = 2;
for (int eye = 0; eye < eyeCount; eye++)
{
if (eyeCount == 2)
{
// Setup current eye viewport (half screen width)
rlViewport(eye*RLGL.State.framebufferWidth/2, 0, RLGL.State.framebufferWidth/2, RLGL.State.framebufferHeight);
// Set current eye view offset to modelview matrix
rlSetMatrixModelview(rlMatrixMultiply(matModelView, RLGL.State.viewOffsetStereo[eye]));
// Set current eye projection matrix
rlSetMatrixProjection(RLGL.State.projectionStereo[eye]);
}
// Draw buffers
if (RLGL.State.vertexCounter > 0)
{
// Set current shader and upload current MVP matrix
glUseProgram(RLGL.State.currentShaderId);
// Create modelview-projection matrix and upload to shader
Matrix matMVP = rlMatrixMultiply(RLGL.State.modelview, RLGL.State.projection);
glUniformMatrix4fv(RLGL.State.currentShaderLocs[RL_SHADER_LOC_MATRIX_MVP], 1, false, rlMatrixToFloat(matMVP));
if (RLGL.State.currentShaderLocs[RL_SHADER_LOC_MATRIX_PROJECTION] != -1)
{
glUniformMatrix4fv(RLGL.State.currentShaderLocs[RL_SHADER_LOC_MATRIX_PROJECTION], 1, false, rlMatrixToFloat(RLGL.State.projection));
}
// WARNING: For the following setup of the view, model, and normal matrices, it is expected that
// transformations and rendering occur between rlPushMatrix and rlPopMatrix.
if (RLGL.State.currentShaderLocs[RL_SHADER_LOC_MATRIX_VIEW] != -1)
{
glUniformMatrix4fv(RLGL.State.currentShaderLocs[RL_SHADER_LOC_MATRIX_VIEW], 1, false, rlMatrixToFloat(RLGL.State.modelview));
}
if (RLGL.State.currentShaderLocs[RL_SHADER_LOC_MATRIX_MODEL] != -1)
{
glUniformMatrix4fv(RLGL.State.currentShaderLocs[RL_SHADER_LOC_MATRIX_MODEL], 1, false, rlMatrixToFloat(RLGL.State.transform));
}
if (RLGL.State.currentShaderLocs[RL_SHADER_LOC_MATRIX_NORMAL] != -1)
{
glUniformMatrix4fv(RLGL.State.currentShaderLocs[RL_SHADER_LOC_MATRIX_NORMAL], 1, false, rlMatrixToFloat(rlMatrixTranspose(rlMatrixInvert(RLGL.State.transform))));
}
if (RLGL.ExtSupported.vao) glBindVertexArray(batch->vertexBuffer[batch->currentBuffer].vaoId);
else
{
// Bind vertex attrib: position (shader-location = 0)
glBindBuffer(GL_ARRAY_BUFFER, batch->vertexBuffer[batch->currentBuffer].vboId[0]);
glVertexAttribPointer(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_POSITION], 3, GL_FLOAT, 0, 0, 0);
glEnableVertexAttribArray(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_POSITION]);
// Bind vertex attrib: texcoord (shader-location = 1)
glBindBuffer(GL_ARRAY_BUFFER, batch->vertexBuffer[batch->currentBuffer].vboId[1]);
glVertexAttribPointer(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_TEXCOORD01], 2, GL_FLOAT, 0, 0, 0);
glEnableVertexAttribArray(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_TEXCOORD01]);
// Bind vertex attrib: normal (shader-location = 2)
glBindBuffer(GL_ARRAY_BUFFER, batch->vertexBuffer[batch->currentBuffer].vboId[2]);
glVertexAttribPointer(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_NORMAL], 3, GL_FLOAT, 0, 0, 0);
glEnableVertexAttribArray(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_NORMAL]);
// Bind vertex attrib: color (shader-location = 3)
glBindBuffer(GL_ARRAY_BUFFER, batch->vertexBuffer[batch->currentBuffer].vboId[3]);
glVertexAttribPointer(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_COLOR], 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0);
glEnableVertexAttribArray(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_COLOR]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, batch->vertexBuffer[batch->currentBuffer].vboId[4]);
}
// Setup some default shader values
glUniform4f(RLGL.State.currentShaderLocs[RL_SHADER_LOC_COLOR_DIFFUSE], 1.0f, 1.0f, 1.0f, 1.0f);
glUniform1i(RLGL.State.currentShaderLocs[RL_SHADER_LOC_MAP_DIFFUSE], 0); // Active default sampler2D: texture0
// Activate additional sampler textures
// Those additional textures will be common for all draw calls of the batch
for (int i = 0; i < RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS; i++)
{
if (RLGL.State.activeTextureId[i] > 0)
{
glActiveTexture(GL_TEXTURE0 + 1 + i);
glBindTexture(GL_TEXTURE_2D, RLGL.State.activeTextureId[i]);
}
}
// Activate default sampler2D texture0 (one texture is always active for default batch shader)
// NOTE: Batch system accumulates calls by texture0 changes, additional textures are enabled for all the draw calls
glActiveTexture(GL_TEXTURE0);
for (int i = 0, vertexOffset = 0; i < batch->drawCounter; i++)
{
// Bind current draw call texture, activated as GL_TEXTURE0 and Bound to sampler2D texture0 by default
glBindTexture(GL_TEXTURE_2D, batch->draws[i].textureId);
if ((batch->draws[i].mode == RL_LINES) || (batch->draws[i].mode == RL_TRIANGLES)) glDrawArrays(batch->draws[i].mode, vertexOffset, batch->draws[i].vertexCount);
else
{
#if defined(GRAPHICS_API_OPENGL_33)
// We need to define the number of indices to be processed: elementCount*6
// NOTE: The final parameter tells the GPU the offset in bytes from the
// start of the index buffer to the location of the first index to process
glDrawElements(GL_TRIANGLES, batch->draws[i].vertexCount/4*6, GL_UNSIGNED_INT, (GLvoid *)(vertexOffset/4*6*sizeof(GLuint)));
#endif
#if defined(GRAPHICS_API_OPENGL_ES2)
glDrawElements(GL_TRIANGLES, batch->draws[i].vertexCount/4*6, GL_UNSIGNED_SHORT, (GLvoid *)(vertexOffset/4*6*sizeof(GLushort)));
#endif
}
vertexOffset += (batch->draws[i].vertexCount + batch->draws[i].vertexAlignment);
}
if (!RLGL.ExtSupported.vao)
{
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
glBindTexture(GL_TEXTURE_2D, 0); // Unbind textures
}
if (RLGL.ExtSupported.vao) glBindVertexArray(0); // Unbind VAO
glUseProgram(0); // Unbind shader program
}
// Restore viewport to default measures
if (eyeCount == 2) rlViewport(0, 0, RLGL.State.framebufferWidth, RLGL.State.framebufferHeight);
//------------------------------------------------------------------------------------------------------------
// Reset batch buffers
//------------------------------------------------------------------------------------------------------------
// Reset vertex counter for next frame
RLGL.State.vertexCounter = 0;
// Reset depth for next draw
batch->currentDepth = -1.0f;
// Restore projection/modelview matrices
RLGL.State.projection = matProjection;
RLGL.State.modelview = matModelView;
// Reset RLGL.currentBatch->draws array
for (int i = 0; i < RL_DEFAULT_BATCH_DRAWCALLS; i++)
{
batch->draws[i].mode = RL_QUADS;
batch->draws[i].vertexCount = 0;
batch->draws[i].textureId = RLGL.State.defaultTextureId;
}
// Reset active texture units for next batch
for (int i = 0; i < RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS; i++) RLGL.State.activeTextureId[i] = 0;
// Reset draws counter to one draw for the batch
batch->drawCounter = 1;
//------------------------------------------------------------------------------------------------------------
// Change to next buffer in the list (in case of multi-buffering)
batch->currentBuffer++;
if (batch->currentBuffer >= batch->bufferCount) batch->currentBuffer = 0;
#endif
}
// Set the active render batch for rlgl
void rlSetRenderBatchActive(rlRenderBatch *batch)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
rlDrawRenderBatch(RLGL.currentBatch);
if (batch != NULL) RLGL.currentBatch = batch;
else RLGL.currentBatch = &RLGL.defaultBatch;
#endif
}
// Update and draw internal render batch
void rlDrawRenderBatchActive(void)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
rlDrawRenderBatch(RLGL.currentBatch); // NOTE: Stereo rendering is checked inside
#endif
}
// Check internal buffer overflow for a given number of vertex
// and force a rlRenderBatch draw call if required
bool rlCheckRenderBatchLimit(int vCount)
{
bool overflow = false;
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
if ((RLGL.State.vertexCounter + vCount) >=
(RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].elementCount*4))
{
overflow = true;
// Store current primitive drawing mode and texture id
int currentMode = RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode;
int currentTexture = RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].textureId;
rlDrawRenderBatch(RLGL.currentBatch); // NOTE: Stereo rendering is checked inside
// Restore state of last batch so we can continue adding vertices
RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode = currentMode;
RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].textureId = currentTexture;
}
#endif
return overflow;
}
// Textures data management
//-----------------------------------------------------------------------------------------
// Convert image data to OpenGL texture (returns OpenGL valid Id)
unsigned int rlLoadTexture(const void *data, int width, int height, int format, int mipmapCount)
{
unsigned int id = 0;
glBindTexture(GL_TEXTURE_2D, 0); // Free any old binding
// Check texture format support by OpenGL 1.1 (compressed textures not supported)
#if defined(GRAPHICS_API_OPENGL_11)
if (format >= RL_PIXELFORMAT_COMPRESSED_DXT1_RGB)
{
TRACELOG(RL_LOG_WARNING, "GL: OpenGL 1.1 does not support GPU compressed texture formats");
return id;
}
#else
if ((!RLGL.ExtSupported.texCompDXT) && ((format == RL_PIXELFORMAT_COMPRESSED_DXT1_RGB) || (format == RL_PIXELFORMAT_COMPRESSED_DXT1_RGBA) ||
(format == RL_PIXELFORMAT_COMPRESSED_DXT3_RGBA) || (format == RL_PIXELFORMAT_COMPRESSED_DXT5_RGBA)))
{
TRACELOG(RL_LOG_WARNING, "GL: DXT compressed texture format not supported");
return id;
}
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
if ((!RLGL.ExtSupported.texCompETC1) && (format == RL_PIXELFORMAT_COMPRESSED_ETC1_RGB))
{
TRACELOG(RL_LOG_WARNING, "GL: ETC1 compressed texture format not supported");
return id;
}
if ((!RLGL.ExtSupported.texCompETC2) && ((format == RL_PIXELFORMAT_COMPRESSED_ETC2_RGB) || (format == RL_PIXELFORMAT_COMPRESSED_ETC2_EAC_RGBA)))
{
TRACELOG(RL_LOG_WARNING, "GL: ETC2 compressed texture format not supported");
return id;
}
if ((!RLGL.ExtSupported.texCompPVRT) && ((format == RL_PIXELFORMAT_COMPRESSED_PVRT_RGB) || (format == RL_PIXELFORMAT_COMPRESSED_PVRT_RGBA)))
{
TRACELOG(RL_LOG_WARNING, "GL: PVRT compressed texture format not supported");
return id;
}
if ((!RLGL.ExtSupported.texCompASTC) && ((format == RL_PIXELFORMAT_COMPRESSED_ASTC_4x4_RGBA) || (format == RL_PIXELFORMAT_COMPRESSED_ASTC_8x8_RGBA)))
{
TRACELOG(RL_LOG_WARNING, "GL: ASTC compressed texture format not supported");
return id;
}
#endif
#endif // GRAPHICS_API_OPENGL_11
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &id); // Generate texture id
glBindTexture(GL_TEXTURE_2D, id);
int mipWidth = width;
int mipHeight = height;
int mipOffset = 0; // Mipmap data offset, only used for tracelog
// NOTE: Added pointer math separately from function to avoid UBSAN complaining
unsigned char *dataPtr = NULL;
if (data != NULL) dataPtr = (unsigned char *)data;
// Load the different mipmap levels
for (int i = 0; i < mipmapCount; i++)
{
unsigned int mipSize = rlGetPixelDataSize(mipWidth, mipHeight, format);
unsigned int glInternalFormat, glFormat, glType;
rlGetGlTextureFormats(format, &glInternalFormat, &glFormat, &glType);
TRACELOGD("TEXTURE: Load mipmap level %i (%i x %i), size: %i, offset: %i", i, mipWidth, mipHeight, mipSize, mipOffset);
if (glInternalFormat != 0)
{
if (format < RL_PIXELFORMAT_COMPRESSED_DXT1_RGB) glTexImage2D(GL_TEXTURE_2D, i, glInternalFormat, mipWidth, mipHeight, 0, glFormat, glType, dataPtr);
#if !defined(GRAPHICS_API_OPENGL_11)
else glCompressedTexImage2D(GL_TEXTURE_2D, i, glInternalFormat, mipWidth, mipHeight, 0, mipSize, dataPtr);
#endif
#if defined(GRAPHICS_API_OPENGL_33)
if (format == RL_PIXELFORMAT_UNCOMPRESSED_GRAYSCALE)
{
GLint swizzleMask[] = { GL_RED, GL_RED, GL_RED, GL_ONE };
glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask);
}
else if (format == RL_PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA)
{
#if defined(GRAPHICS_API_OPENGL_21)
GLint swizzleMask[] = { GL_RED, GL_RED, GL_RED, GL_ALPHA };
#elif defined(GRAPHICS_API_OPENGL_33)
GLint swizzleMask[] = { GL_RED, GL_RED, GL_RED, GL_GREEN };
#endif
glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask);
}
#endif
}
mipWidth /= 2;
mipHeight /= 2;
mipOffset += mipSize; // Increment offset position to next mipmap
if (data != NULL) dataPtr += mipSize; // Increment data pointer to next mipmap
// Security check for NPOT textures
if (mipWidth < 1) mipWidth = 1;
if (mipHeight < 1) mipHeight = 1;
}
// Texture parameters configuration
// NOTE: glTexParameteri does NOT affect texture uploading, just the way it's used
#if defined(GRAPHICS_API_OPENGL_ES2)
// 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
if (RLGL.ExtSupported.texNPOT)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // Set texture to repeat on x-axis
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // Set texture to repeat on y-axis
}
else
{
// NOTE: If using negative texture coordinates (LoadOBJ()), it does not work!
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // Set texture to clamp on x-axis
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // Set texture to clamp on y-axis
}
#else
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // Set texture to repeat on x-axis
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // Set texture to repeat on y-axis
#endif
// Magnification and minification filters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // Alternative: GL_LINEAR
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // Alternative: GL_LINEAR
#if defined(GRAPHICS_API_OPENGL_33)
if (mipmapCount > 1)
{
// Activate Trilinear filtering if mipmaps are available
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
}
#endif
// At this point we have the texture loaded in GPU and texture parameters configured
// NOTE: If mipmaps were not in data, they are not generated automatically
// Unbind current texture
glBindTexture(GL_TEXTURE_2D, 0);
if (id > 0) TRACELOG(RL_LOG_INFO, "TEXTURE: [ID %i] Texture loaded successfully (%ix%i | %s | %i mipmaps)", id, width, height, rlGetPixelFormatName(format), mipmapCount);
else TRACELOG(RL_LOG_WARNING, "TEXTURE: Failed to load texture");
return id;
}
// Load depth texture/renderbuffer (to be attached to fbo)
// WARNING: OpenGL ES 2.0 requires GL_OES_depth_texture and WebGL requires WEBGL_depth_texture extensions
unsigned int rlLoadTextureDepth(int width, int height, bool useRenderBuffer)
{
unsigned int id = 0;
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
// In case depth textures not supported, we force renderbuffer usage
if (!RLGL.ExtSupported.texDepth) useRenderBuffer = true;
// NOTE: We let the implementation to choose the best bit-depth
// Possible formats: GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT32 and GL_DEPTH_COMPONENT32F
unsigned int glInternalFormat = GL_DEPTH_COMPONENT;
#if (defined(GRAPHICS_API_OPENGL_ES2) || defined(GRAPHICS_API_OPENGL_ES3))
// WARNING: WebGL platform requires unsized internal format definition (GL_DEPTH_COMPONENT)
// while other platforms using OpenGL ES 2.0 require/support sized internal formats depending on the GPU capabilities
if (!RLGL.ExtSupported.texDepthWebGL || useRenderBuffer)
{
if (RLGL.ExtSupported.maxDepthBits == 32) glInternalFormat = GL_DEPTH_COMPONENT32_OES;
else if (RLGL.ExtSupported.maxDepthBits == 24) glInternalFormat = GL_DEPTH_COMPONENT24_OES;
else glInternalFormat = GL_DEPTH_COMPONENT16;
}
#endif
if (!useRenderBuffer && RLGL.ExtSupported.texDepth)
{
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glTexImage2D(GL_TEXTURE_2D, 0, glInternalFormat, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, 0);
TRACELOG(RL_LOG_INFO, "TEXTURE: Depth texture loaded successfully");
}
else
{
// Create the renderbuffer that will serve as the depth attachment for the framebuffer
// NOTE: A renderbuffer is simpler than a texture and could offer better performance on embedded devices
glGenRenderbuffers(1, &id);
glBindRenderbuffer(GL_RENDERBUFFER, id);
glRenderbufferStorage(GL_RENDERBUFFER, glInternalFormat, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
TRACELOG(RL_LOG_INFO, "TEXTURE: [ID %i] Depth renderbuffer loaded successfully (%i bits)", id, (RLGL.ExtSupported.maxDepthBits >= 24)? RLGL.ExtSupported.maxDepthBits : 16);
}
#endif
return id;
}
// Load texture cubemap
// NOTE: Cubemap data is expected to be 6 images in a single data array (one after the other),
// expected the following convention: +X, -X, +Y, -Y, +Z, -Z
unsigned int rlLoadTextureCubemap(const void *data, int size, int format)
{
unsigned int id = 0;
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
unsigned int dataSize = rlGetPixelDataSize(size, size, format);
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_CUBE_MAP, id);
unsigned int glInternalFormat, glFormat, glType;
rlGetGlTextureFormats(format, &glInternalFormat, &glFormat, &glType);
if (glInternalFormat != 0)
{
// Load cubemap faces
for (unsigned int i = 0; i < 6; i++)
{
if (data == NULL)
{
if (format < RL_PIXELFORMAT_COMPRESSED_DXT1_RGB)
{
if ((format == RL_PIXELFORMAT_UNCOMPRESSED_R32) || (format == RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32A32)
|| (format == RL_PIXELFORMAT_UNCOMPRESSED_R16) || (format == RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16A16))
TRACELOG(RL_LOG_WARNING, "TEXTURES: Cubemap requested format not supported");
else glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glInternalFormat, size, size, 0, glFormat, glType, NULL);
}
else TRACELOG(RL_LOG_WARNING, "TEXTURES: Empty cubemap creation does not support compressed format");
}
else
{
if (format < RL_PIXELFORMAT_COMPRESSED_DXT1_RGB) glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glInternalFormat, size, size, 0, glFormat, glType, (unsigned char *)data + i*dataSize);
else glCompressedTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glInternalFormat, size, size, 0, dataSize, (unsigned char *)data + i*dataSize);
}
#if defined(GRAPHICS_API_OPENGL_33)
if (format == RL_PIXELFORMAT_UNCOMPRESSED_GRAYSCALE)
{
GLint swizzleMask[] = { GL_RED, GL_RED, GL_RED, GL_ONE };
glTexParameteriv(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask);
}
else if (format == RL_PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA)
{
#if defined(GRAPHICS_API_OPENGL_21)
GLint swizzleMask[] = { GL_RED, GL_RED, GL_RED, GL_ALPHA };
#elif defined(GRAPHICS_API_OPENGL_33)
GLint swizzleMask[] = { GL_RED, GL_RED, GL_RED, GL_GREEN };
#endif
glTexParameteriv(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask);
}
#endif
}
}
// Set cubemap texture sampling parameters
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
#if defined(GRAPHICS_API_OPENGL_33)
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); // Flag not supported on OpenGL ES 2.0
#endif
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
#endif
if (id > 0) TRACELOG(RL_LOG_INFO, "TEXTURE: [ID %i] Cubemap texture loaded successfully (%ix%i)", id, size, size);
else TRACELOG(RL_LOG_WARNING, "TEXTURE: Failed to load cubemap texture");
return id;
}
// Update already loaded texture in GPU with new data
// NOTE: We don't know safely if internal texture format is the expected one...
void rlUpdateTexture(unsigned int id, int offsetX, int offsetY, int width, int height, int format, const void *data)
{
glBindTexture(GL_TEXTURE_2D, id);
unsigned int glInternalFormat, glFormat, glType;
rlGetGlTextureFormats(format, &glInternalFormat, &glFormat, &glType);
if ((glInternalFormat != 0) && (format < RL_PIXELFORMAT_COMPRESSED_DXT1_RGB))
{
glTexSubImage2D(GL_TEXTURE_2D, 0, offsetX, offsetY, width, height, glFormat, glType, data);
}
else TRACELOG(RL_LOG_WARNING, "TEXTURE: [ID %i] Failed to update for current texture format (%i)", id, format);
}
// Get OpenGL internal formats and data type from raylib PixelFormat
void rlGetGlTextureFormats(int format, unsigned int *glInternalFormat, unsigned int *glFormat, unsigned int *glType)
{
*glInternalFormat = 0;
*glFormat = 0;
*glType = 0;
switch (format)
{
#if defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_21) || defined(GRAPHICS_API_OPENGL_ES2)
// NOTE: on OpenGL ES 2.0 (WebGL), internalFormat must match format and options allowed are: GL_LUMINANCE, GL_RGB, GL_RGBA
case RL_PIXELFORMAT_UNCOMPRESSED_GRAYSCALE: *glInternalFormat = GL_LUMINANCE; *glFormat = GL_LUMINANCE; *glType = GL_UNSIGNED_BYTE; break;
case RL_PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA: *glInternalFormat = GL_LUMINANCE_ALPHA; *glFormat = GL_LUMINANCE_ALPHA; *glType = GL_UNSIGNED_BYTE; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R5G6B5: *glInternalFormat = GL_RGB; *glFormat = GL_RGB; *glType = GL_UNSIGNED_SHORT_5_6_5; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8: *glInternalFormat = GL_RGB; *glFormat = GL_RGB; *glType = GL_UNSIGNED_BYTE; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R5G5B5A1: *glInternalFormat = GL_RGBA; *glFormat = GL_RGBA; *glType = GL_UNSIGNED_SHORT_5_5_5_1; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R4G4B4A4: *glInternalFormat = GL_RGBA; *glFormat = GL_RGBA; *glType = GL_UNSIGNED_SHORT_4_4_4_4; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8A8: *glInternalFormat = GL_RGBA; *glFormat = GL_RGBA; *glType = GL_UNSIGNED_BYTE; break;
#if !defined(GRAPHICS_API_OPENGL_11)
#if defined(GRAPHICS_API_OPENGL_ES3)
case RL_PIXELFORMAT_UNCOMPRESSED_R32: if (RLGL.ExtSupported.texFloat32) *glInternalFormat = GL_R32F_EXT; *glFormat = GL_RED_EXT; *glType = GL_FLOAT; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32: if (RLGL.ExtSupported.texFloat32) *glInternalFormat = GL_RGB32F_EXT; *glFormat = GL_RGB; *glType = GL_FLOAT; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32A32: if (RLGL.ExtSupported.texFloat32) *glInternalFormat = GL_RGBA32F_EXT; *glFormat = GL_RGBA; *glType = GL_FLOAT; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_R16F_EXT; *glFormat = GL_RED_EXT; *glType = GL_HALF_FLOAT; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_RGB16F_EXT; *glFormat = GL_RGB; *glType = GL_HALF_FLOAT; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16A16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_RGBA16F_EXT; *glFormat = GL_RGBA; *glType = GL_HALF_FLOAT; break;
#else
case RL_PIXELFORMAT_UNCOMPRESSED_R32: if (RLGL.ExtSupported.texFloat32) *glInternalFormat = GL_LUMINANCE; *glFormat = GL_LUMINANCE; *glType = GL_FLOAT; break; // NOTE: Requires extension OES_texture_float
case RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32: if (RLGL.ExtSupported.texFloat32) *glInternalFormat = GL_RGB; *glFormat = GL_RGB; *glType = GL_FLOAT; break; // NOTE: Requires extension OES_texture_float
case RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32A32: if (RLGL.ExtSupported.texFloat32) *glInternalFormat = GL_RGBA; *glFormat = GL_RGBA; *glType = GL_FLOAT; break; // NOTE: Requires extension OES_texture_float
#if defined(GRAPHICS_API_OPENGL_21)
case RL_PIXELFORMAT_UNCOMPRESSED_R16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_LUMINANCE; *glFormat = GL_LUMINANCE; *glType = GL_HALF_FLOAT_ARB; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_RGB; *glFormat = GL_RGB; *glType = GL_HALF_FLOAT_ARB; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16A16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_RGBA; *glFormat = GL_RGBA; *glType = GL_HALF_FLOAT_ARB; break;
#else // defined(GRAPHICS_API_OPENGL_ES2)
case RL_PIXELFORMAT_UNCOMPRESSED_R16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_LUMINANCE; *glFormat = GL_LUMINANCE; *glType = GL_HALF_FLOAT_OES; break; // NOTE: Requires extension OES_texture_half_float
case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_RGB; *glFormat = GL_RGB; *glType = GL_HALF_FLOAT_OES; break; // NOTE: Requires extension OES_texture_half_float
case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16A16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_RGBA; *glFormat = GL_RGBA; *glType = GL_HALF_FLOAT_OES; break; // NOTE: Requires extension OES_texture_half_float
#endif
#endif
#endif
#elif defined(GRAPHICS_API_OPENGL_33)
case RL_PIXELFORMAT_UNCOMPRESSED_GRAYSCALE: *glInternalFormat = GL_R8; *glFormat = GL_RED; *glType = GL_UNSIGNED_BYTE; break;
case RL_PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA: *glInternalFormat = GL_RG8; *glFormat = GL_RG; *glType = GL_UNSIGNED_BYTE; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R5G6B5: *glInternalFormat = GL_RGB565; *glFormat = GL_RGB; *glType = GL_UNSIGNED_SHORT_5_6_5; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8: *glInternalFormat = GL_RGB8; *glFormat = GL_RGB; *glType = GL_UNSIGNED_BYTE; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R5G5B5A1: *glInternalFormat = GL_RGB5_A1; *glFormat = GL_RGBA; *glType = GL_UNSIGNED_SHORT_5_5_5_1; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R4G4B4A4: *glInternalFormat = GL_RGBA4; *glFormat = GL_RGBA; *glType = GL_UNSIGNED_SHORT_4_4_4_4; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8A8: *glInternalFormat = GL_RGBA8; *glFormat = GL_RGBA; *glType = GL_UNSIGNED_BYTE; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R32: if (RLGL.ExtSupported.texFloat32) *glInternalFormat = GL_R32F; *glFormat = GL_RED; *glType = GL_FLOAT; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32: if (RLGL.ExtSupported.texFloat32) *glInternalFormat = GL_RGB32F; *glFormat = GL_RGB; *glType = GL_FLOAT; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32A32: if (RLGL.ExtSupported.texFloat32) *glInternalFormat = GL_RGBA32F; *glFormat = GL_RGBA; *glType = GL_FLOAT; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_R16F; *glFormat = GL_RED; *glType = GL_HALF_FLOAT; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_RGB16F; *glFormat = GL_RGB; *glType = GL_HALF_FLOAT; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16A16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_RGBA16F; *glFormat = GL_RGBA; *glType = GL_HALF_FLOAT; break;
#endif
#if !defined(GRAPHICS_API_OPENGL_11)
case RL_PIXELFORMAT_COMPRESSED_DXT1_RGB: if (RLGL.ExtSupported.texCompDXT) *glInternalFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT; break;
case RL_PIXELFORMAT_COMPRESSED_DXT1_RGBA: if (RLGL.ExtSupported.texCompDXT) *glInternalFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; break;
case RL_PIXELFORMAT_COMPRESSED_DXT3_RGBA: if (RLGL.ExtSupported.texCompDXT) *glInternalFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; break;
case RL_PIXELFORMAT_COMPRESSED_DXT5_RGBA: if (RLGL.ExtSupported.texCompDXT) *glInternalFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; break;
case RL_PIXELFORMAT_COMPRESSED_ETC1_RGB: if (RLGL.ExtSupported.texCompETC1) *glInternalFormat = GL_ETC1_RGB8_OES; break; // NOTE: Requires OpenGL ES 2.0 or OpenGL 4.3
case RL_PIXELFORMAT_COMPRESSED_ETC2_RGB: if (RLGL.ExtSupported.texCompETC2) *glInternalFormat = GL_COMPRESSED_RGB8_ETC2; break; // NOTE: Requires OpenGL ES 3.0 or OpenGL 4.3
case RL_PIXELFORMAT_COMPRESSED_ETC2_EAC_RGBA: if (RLGL.ExtSupported.texCompETC2) *glInternalFormat = GL_COMPRESSED_RGBA8_ETC2_EAC; break; // NOTE: Requires OpenGL ES 3.0 or OpenGL 4.3
case RL_PIXELFORMAT_COMPRESSED_PVRT_RGB: if (RLGL.ExtSupported.texCompPVRT) *glInternalFormat = GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG; break; // NOTE: Requires PowerVR GPU
case RL_PIXELFORMAT_COMPRESSED_PVRT_RGBA: if (RLGL.ExtSupported.texCompPVRT) *glInternalFormat = GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG; break; // NOTE: Requires PowerVR GPU
case RL_PIXELFORMAT_COMPRESSED_ASTC_4x4_RGBA: if (RLGL.ExtSupported.texCompASTC) *glInternalFormat = GL_COMPRESSED_RGBA_ASTC_4x4_KHR; break; // NOTE: Requires OpenGL ES 3.1 or OpenGL 4.3
case RL_PIXELFORMAT_COMPRESSED_ASTC_8x8_RGBA: if (RLGL.ExtSupported.texCompASTC) *glInternalFormat = GL_COMPRESSED_RGBA_ASTC_8x8_KHR; break; // NOTE: Requires OpenGL ES 3.1 or OpenGL 4.3
#endif
default: TRACELOG(RL_LOG_WARNING, "TEXTURE: Current format not supported (%i)", format); break;
}
}
// Unload texture from GPU memory
void rlUnloadTexture(unsigned int id)
{
glDeleteTextures(1, &id);
}
// Generate mipmap data for selected texture
// NOTE: Only supports GPU mipmap generation
void rlGenTextureMipmaps(unsigned int id, int width, int height, int format, int *mipmaps)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
glBindTexture(GL_TEXTURE_2D, id);
// Check if texture is power-of-two (POT)
bool texIsPOT = false;
if (((width > 0) && ((width & (width - 1)) == 0)) &&
((height > 0) && ((height & (height - 1)) == 0))) texIsPOT = true;
if ((texIsPOT) || (RLGL.ExtSupported.texNPOT))
{
//glHint(GL_GENERATE_MIPMAP_HINT, GL_DONT_CARE); // Hint for mipmaps generation algorithm: GL_FASTEST, GL_NICEST, GL_DONT_CARE
glGenerateMipmap(GL_TEXTURE_2D); // Generate mipmaps automatically
#define MIN(a,b) (((a)<(b))? (a):(b))
#define MAX(a,b) (((a)>(b))? (a):(b))
*mipmaps = 1 + (int)floor(log(MAX(width, height))/log(2));
TRACELOG(RL_LOG_INFO, "TEXTURE: [ID %i] Mipmaps generated automatically, total: %i", id, *mipmaps);
}
else TRACELOG(RL_LOG_WARNING, "TEXTURE: [ID %i] Failed to generate mipmaps", id);
glBindTexture(GL_TEXTURE_2D, 0);
#else
TRACELOG(RL_LOG_WARNING, "TEXTURE: [ID %i] GPU mipmap generation not supported", id);
#endif
}
// Read texture pixel data
void *rlReadTexturePixels(unsigned int id, int width, int height, int format)
{
void *pixels = NULL;
#if defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33)
glBindTexture(GL_TEXTURE_2D, id);
// NOTE: Using texture id, we can retrieve some texture info (but not on OpenGL ES 2.0)
// Possible texture info: GL_TEXTURE_RED_SIZE, GL_TEXTURE_GREEN_SIZE, GL_TEXTURE_BLUE_SIZE, GL_TEXTURE_ALPHA_SIZE
//int width, height, format;
//glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
//glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
//glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &format);
// 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.
// Use glPixelStorei to modify padding with the GL_[UN]PACK_ALIGNMENT setting.
// GL_PACK_ALIGNMENT affects operations that read from OpenGL memory (glReadPixels, glGetTexImage, etc.)
// GL_UNPACK_ALIGNMENT affects operations that write to OpenGL memory (glTexImage, etc.)
glPixelStorei(GL_PACK_ALIGNMENT, 1);
unsigned int glInternalFormat, glFormat, glType;
rlGetGlTextureFormats(format, &glInternalFormat, &glFormat, &glType);
unsigned int size = rlGetPixelDataSize(width, height, format);
if ((glInternalFormat != 0) && (format < RL_PIXELFORMAT_COMPRESSED_DXT1_RGB))
{
pixels = RL_MALLOC(size);
glGetTexImage(GL_TEXTURE_2D, 0, glFormat, glType, pixels);
}
else TRACELOG(RL_LOG_WARNING, "TEXTURE: [ID %i] Data retrieval not suported for pixel format (%i)", id, format);
glBindTexture(GL_TEXTURE_2D, 0);
#endif
#if defined(GRAPHICS_API_OPENGL_ES2)
// glGetTexImage() is not available on OpenGL ES 2.0
// Texture width and height are required on OpenGL ES 2.0. There is no way to get it from texture id.
// Two possible Options:
// 1 - Bind texture to color fbo attachment and glReadPixels()
// 2 - Create an fbo, activate it, render quad with texture, glReadPixels()
// We are using Option 1, just need to care for texture format on retrieval
// NOTE: This behaviour could be conditioned by graphic driver...
unsigned int fboId = rlLoadFramebuffer();
glBindFramebuffer(GL_FRAMEBUFFER, fboId);
glBindTexture(GL_TEXTURE_2D, 0);
// Attach our texture to FBO
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, id, 0);
// We read data as RGBA because FBO texture is configured as RGBA, despite binding another texture format
pixels = (unsigned char *)RL_MALLOC(rlGetPixelDataSize(width, height, RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8A8));
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// Clean up temporal fbo
rlUnloadFramebuffer(fboId);
#endif
return pixels;
}
// Read screen pixel data (color buffer)
unsigned char *rlReadScreenPixels(int width, int height)
{
unsigned char *screenData = (unsigned char *)RL_CALLOC(width*height*4, sizeof(unsigned char));
// NOTE 1: glReadPixels returns image flipped vertically -> (0,0) is the bottom left corner of the framebuffer
// NOTE 2: We are getting alpha channel! Be careful, it can be transparent if not cleared properly!
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, screenData);
// Flip image vertically!
unsigned char *imgData = (unsigned char *)RL_MALLOC(width*height*4*sizeof(unsigned char));
for (int y = height - 1; y >= 0; y--)
{
for (int x = 0; x < (width*4); x++)
{
imgData[((height - 1) - y)*width*4 + x] = screenData[(y*width*4) + x]; // Flip line
// Set alpha component value to 255 (no trasparent image retrieval)
// NOTE: Alpha value has already been applied to RGB in framebuffer, we don't need it!
if (((x + 1)%4) == 0) imgData[((height - 1) - y)*width*4 + x] = 255;
}
}
RL_FREE(screenData);
return imgData; // NOTE: image data should be freed
}
// Framebuffer management (fbo)
//-----------------------------------------------------------------------------------------
// Load a framebuffer to be used for rendering
// NOTE: No textures attached
unsigned int rlLoadFramebuffer(void)
{
unsigned int fboId = 0;
#if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)) && defined(RLGL_RENDER_TEXTURES_HINT)
glGenFramebuffers(1, &fboId); // Create the framebuffer object
glBindFramebuffer(GL_FRAMEBUFFER, 0); // Unbind any framebuffer
#endif
return fboId;
}
// Attach color buffer texture to an fbo (unloads previous attachment)
// NOTE: Attach type: 0-Color, 1-Depth renderbuffer, 2-Depth texture
void rlFramebufferAttach(unsigned int fboId, unsigned int texId, int attachType, int texType, int mipLevel)
{
#if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)) && defined(RLGL_RENDER_TEXTURES_HINT)
glBindFramebuffer(GL_FRAMEBUFFER, fboId);
switch (attachType)
{
case RL_ATTACHMENT_COLOR_CHANNEL0:
case RL_ATTACHMENT_COLOR_CHANNEL1:
case RL_ATTACHMENT_COLOR_CHANNEL2:
case RL_ATTACHMENT_COLOR_CHANNEL3:
case RL_ATTACHMENT_COLOR_CHANNEL4:
case RL_ATTACHMENT_COLOR_CHANNEL5:
case RL_ATTACHMENT_COLOR_CHANNEL6:
case RL_ATTACHMENT_COLOR_CHANNEL7:
{
if (texType == RL_ATTACHMENT_TEXTURE2D) glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + attachType, GL_TEXTURE_2D, texId, mipLevel);
else if (texType == RL_ATTACHMENT_RENDERBUFFER) glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + attachType, GL_RENDERBUFFER, texId);
else if (texType >= RL_ATTACHMENT_CUBEMAP_POSITIVE_X) glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + attachType, GL_TEXTURE_CUBE_MAP_POSITIVE_X + texType, texId, mipLevel);
} break;
case RL_ATTACHMENT_DEPTH:
{
if (texType == RL_ATTACHMENT_TEXTURE2D) glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, texId, mipLevel);
else if (texType == RL_ATTACHMENT_RENDERBUFFER) glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, texId);
} break;
case RL_ATTACHMENT_STENCIL:
{
if (texType == RL_ATTACHMENT_TEXTURE2D) glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, texId, mipLevel);
else if (texType == RL_ATTACHMENT_RENDERBUFFER) glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, texId);
} break;
default: break;
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
#endif
}
// Verify render texture is complete
bool rlFramebufferComplete(unsigned int id)
{
bool result = false;
#if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)) && defined(RLGL_RENDER_TEXTURES_HINT)
glBindFramebuffer(GL_FRAMEBUFFER, id);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE)
{
switch (status)
{
case GL_FRAMEBUFFER_UNSUPPORTED: TRACELOG(RL_LOG_WARNING, "FBO: [ID %i] Framebuffer is unsupported", id); break;
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: TRACELOG(RL_LOG_WARNING, "FBO: [ID %i] Framebuffer has incomplete attachment", id); break;
#if defined(GRAPHICS_API_OPENGL_ES2)
case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS: TRACELOG(RL_LOG_WARNING, "FBO: [ID %i] Framebuffer has incomplete dimensions", id); break;
#endif
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: TRACELOG(RL_LOG_WARNING, "FBO: [ID %i] Framebuffer has a missing attachment", id); break;
default: break;
}
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
result = (status == GL_FRAMEBUFFER_COMPLETE);
#endif
return result;
}
// Unload framebuffer from GPU memory
// NOTE: All attached textures/cubemaps/renderbuffers are also deleted
void rlUnloadFramebuffer(unsigned int id)
{
#if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)) && defined(RLGL_RENDER_TEXTURES_HINT)
// Query depth attachment to automatically delete texture/renderbuffer
int depthType = 0, depthId = 0;
glBindFramebuffer(GL_FRAMEBUFFER, id); // Bind framebuffer to query depth texture type
glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &depthType);
// TODO: Review warning retrieving object name in WebGL
// WARNING: WebGL: INVALID_ENUM: getFramebufferAttachmentParameter: invalid parameter name
// https://registry.khronos.org/webgl/specs/latest/1.0/
glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &depthId);
unsigned int depthIdU = (unsigned int)depthId;
if (depthType == GL_RENDERBUFFER) glDeleteRenderbuffers(1, &depthIdU);
else if (depthType == GL_TEXTURE) glDeleteTextures(1, &depthIdU);
// NOTE: If a texture object is deleted while its image is attached to the *currently bound* framebuffer,
// the texture image is automatically detached from the currently bound framebuffer.
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDeleteFramebuffers(1, &id);
TRACELOG(RL_LOG_INFO, "FBO: [ID %i] Unloaded framebuffer from VRAM (GPU)", id);
#endif
}
// Vertex data management
//-----------------------------------------------------------------------------------------
// Load a new attributes buffer
unsigned int rlLoadVertexBuffer(const void *buffer, int size, bool dynamic)
{
unsigned int id = 0;
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
glGenBuffers(1, &id);
glBindBuffer(GL_ARRAY_BUFFER, id);
glBufferData(GL_ARRAY_BUFFER, size, buffer, dynamic? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
#endif
return id;
}
// Load a new attributes element buffer
unsigned int rlLoadVertexBufferElement(const void *buffer, int size, bool dynamic)
{
unsigned int id = 0;
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
glGenBuffers(1, &id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, buffer, dynamic? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
#endif
return id;
}
// Enable vertex buffer (VBO)
void rlEnableVertexBuffer(unsigned int id)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
glBindBuffer(GL_ARRAY_BUFFER, id);
#endif
}
// Disable vertex buffer (VBO)
void rlDisableVertexBuffer(void)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
glBindBuffer(GL_ARRAY_BUFFER, 0);
#endif
}
// Enable vertex buffer element (VBO element)
void rlEnableVertexBufferElement(unsigned int id)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
#endif
}
// Disable vertex buffer element (VBO element)
void rlDisableVertexBufferElement(void)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
#endif
}
// Update vertex buffer with new data
// NOTE: dataSize and offset must be provided in bytes
void rlUpdateVertexBuffer(unsigned int id, const void *data, int dataSize, int offset)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
glBindBuffer(GL_ARRAY_BUFFER, id);
glBufferSubData(GL_ARRAY_BUFFER, offset, dataSize, data);
#endif
}
// Update vertex buffer elements with new data
// NOTE: dataSize and offset must be provided in bytes
void rlUpdateVertexBufferElements(unsigned int id, const void *data, int dataSize, int offset)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, offset, dataSize, data);
#endif
}
// Enable vertex array object (VAO)
bool rlEnableVertexArray(unsigned int vaoId)
{
bool result = false;
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
if (RLGL.ExtSupported.vao)
{
glBindVertexArray(vaoId);
result = true;
}
#endif
return result;
}
// Disable vertex array object (VAO)
void rlDisableVertexArray(void)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
if (RLGL.ExtSupported.vao) glBindVertexArray(0);
#endif
}
// Enable vertex attribute index
void rlEnableVertexAttribute(unsigned int index)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
glEnableVertexAttribArray(index);
#endif
}
// Disable vertex attribute index
void rlDisableVertexAttribute(unsigned int index)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
glDisableVertexAttribArray(index);
#endif
}
// Draw vertex array
void rlDrawVertexArray(int offset, int count)
{
glDrawArrays(GL_TRIANGLES, offset, count);
}
// Draw vertex array elements
void rlDrawVertexArrayElements(int offset, int count, const void *buffer)
{
// NOTE: Added pointer math separately from function to avoid UBSAN complaining
unsigned short *bufferPtr = (unsigned short *)buffer;
if (offset > 0) bufferPtr += offset;
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_SHORT, (const unsigned short *)bufferPtr);
}
// Draw vertex array instanced
void rlDrawVertexArrayInstanced(int offset, int count, int instances)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
glDrawArraysInstanced(GL_TRIANGLES, 0, count, instances);
#endif
}
// Draw vertex array elements instanced
void rlDrawVertexArrayElementsInstanced(int offset, int count, const void *buffer, int instances)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
// NOTE: Added pointer math separately from function to avoid UBSAN complaining
unsigned short *bufferPtr = (unsigned short *)buffer;
if (offset > 0) bufferPtr += offset;
glDrawElementsInstanced(GL_TRIANGLES, count, GL_UNSIGNED_SHORT, (const unsigned short *)bufferPtr, instances);
#endif
}
#if defined(GRAPHICS_API_OPENGL_11)
// Enable vertex state pointer
void rlEnableStatePointer(int vertexAttribType, void *buffer)
{
if (buffer != NULL) glEnableClientState(vertexAttribType);
switch (vertexAttribType)
{
case GL_VERTEX_ARRAY: glVertexPointer(3, GL_FLOAT, 0, buffer); break;
case GL_TEXTURE_COORD_ARRAY: glTexCoordPointer(2, GL_FLOAT, 0, buffer); break;
case GL_NORMAL_ARRAY: if (buffer != NULL) glNormalPointer(GL_FLOAT, 0, buffer); break;
case GL_COLOR_ARRAY: if (buffer != NULL) glColorPointer(4, GL_UNSIGNED_BYTE, 0, buffer); break;
//case GL_INDEX_ARRAY: if (buffer != NULL) glIndexPointer(GL_SHORT, 0, buffer); break; // Indexed colors
default: break;
}
}
// Disable vertex state pointer
void rlDisableStatePointer(int vertexAttribType)
{
glDisableClientState(vertexAttribType);
}
#endif
// Load vertex array object (VAO)
unsigned int rlLoadVertexArray(void)
{
unsigned int vaoId = 0;
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
if (RLGL.ExtSupported.vao)
{
glGenVertexArrays(1, &vaoId);
}
#endif
return vaoId;
}
// Set vertex attribute
void rlSetVertexAttribute(unsigned int index, int compSize, int type, bool normalized, int stride, int offset)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
// NOTE: Data type could be: GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_INT, GL_UNSIGNED_INT
// Additional types (depends on OpenGL version or extensions):
// - GL_HALF_FLOAT, GL_FLOAT, GL_DOUBLE, GL_FIXED,
// - GL_INT_2_10_10_10_REV, GL_UNSIGNED_INT_2_10_10_10_REV, GL_UNSIGNED_INT_10F_11F_11F_REV
glVertexAttribPointer(index, compSize, type, normalized, stride, (void *)offset);
#endif
}
// Set vertex attribute divisor
void rlSetVertexAttributeDivisor(unsigned int index, int divisor)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
glVertexAttribDivisor(index, divisor);
#endif
}
// Unload vertex array object (VAO)
void rlUnloadVertexArray(unsigned int vaoId)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
if (RLGL.ExtSupported.vao)
{
glBindVertexArray(0);
glDeleteVertexArrays(1, &vaoId);
TRACELOG(RL_LOG_INFO, "VAO: [ID %i] Unloaded vertex array data from VRAM (GPU)", vaoId);
}
#endif
}
// Unload vertex buffer (VBO)
void rlUnloadVertexBuffer(unsigned int vboId)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
glDeleteBuffers(1, &vboId);
//TRACELOG(RL_LOG_INFO, "VBO: Unloaded vertex data from VRAM (GPU)");
#endif
}
// Shaders management
//-----------------------------------------------------------------------------------------------
// Load shader from code strings
// NOTE: If shader string is NULL, using default vertex/fragment shaders
unsigned int rlLoadShaderCode(const char *vsCode, const char *fsCode)
{
unsigned int id = 0;
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
unsigned int vertexShaderId = 0;
unsigned int fragmentShaderId = 0;
// Compile vertex shader (if provided)
if (vsCode != NULL) vertexShaderId = rlCompileShader(vsCode, GL_VERTEX_SHADER);
// In case no vertex shader was provided or compilation failed, we use default vertex shader
if (vertexShaderId == 0) vertexShaderId = RLGL.State.defaultVShaderId;
// Compile fragment shader (if provided)
if (fsCode != NULL) fragmentShaderId = rlCompileShader(fsCode, GL_FRAGMENT_SHADER);
// In case no fragment shader was provided or compilation failed, we use default fragment shader
if (fragmentShaderId == 0) fragmentShaderId = RLGL.State.defaultFShaderId;
// In case vertex and fragment shader are the default ones, no need to recompile, we can just assign the default shader program id
if ((vertexShaderId == RLGL.State.defaultVShaderId) && (fragmentShaderId == RLGL.State.defaultFShaderId)) id = RLGL.State.defaultShaderId;
else
{
// One of or both shader are new, we need to compile a new shader program
id = rlLoadShaderProgram(vertexShaderId, fragmentShaderId);
// We can detach and delete vertex/fragment shaders (if not default ones)
// NOTE: We detach shader before deletion to make sure memory is freed
if (vertexShaderId != RLGL.State.defaultVShaderId)
{
// WARNING: Shader program linkage could fail and returned id is 0
if (id > 0) glDetachShader(id, vertexShaderId);
glDeleteShader(vertexShaderId);
}
if (fragmentShaderId != RLGL.State.defaultFShaderId)
{
// WARNING: Shader program linkage could fail and returned id is 0
if (id > 0) glDetachShader(id, fragmentShaderId);
glDeleteShader(fragmentShaderId);
}
// In case shader program loading failed, we assign default shader
if (id == 0)
{
// In case shader loading fails, we return the default shader
TRACELOG(RL_LOG_WARNING, "SHADER: Failed to load custom shader code, using default shader");
id = RLGL.State.defaultShaderId;
}
/*
else
{
// Get available shader uniforms
// NOTE: This information is useful for debug...
int uniformCount = -1;
glGetProgramiv(id, GL_ACTIVE_UNIFORMS, &uniformCount);
for (int i = 0; i < uniformCount; i++)
{
int namelen = -1;
int num = -1;
char name[256] = { 0 }; // Assume no variable names longer than 256
GLenum type = GL_ZERO;
// Get the name of the uniforms
glGetActiveUniform(id, i, sizeof(name) - 1, &namelen, &num, &type, name);
name[namelen] = 0;
TRACELOGD("SHADER: [ID %i] Active uniform (%s) set at location: %i", id, name, glGetUniformLocation(id, name));
}
}
*/
}
#endif
return id;
}
// Compile custom shader and return shader id
unsigned int rlCompileShader(const char *shaderCode, int type)
{
unsigned int shader = 0;
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
shader = glCreateShader(type);
glShaderSource(shader, 1, &shaderCode, NULL);
GLint success = 0;
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (success == GL_FALSE)
{
switch (type)
{
case GL_VERTEX_SHADER: TRACELOG(RL_LOG_WARNING, "SHADER: [ID %i] Failed to compile vertex shader code", shader); break;
case GL_FRAGMENT_SHADER: TRACELOG(RL_LOG_WARNING, "SHADER: [ID %i] Failed to compile fragment shader code", shader); break;
//case GL_GEOMETRY_SHADER:
#if defined(GRAPHICS_API_OPENGL_43)
case GL_COMPUTE_SHADER: TRACELOG(RL_LOG_WARNING, "SHADER: [ID %i] Failed to compile compute shader code", shader); break;
#endif
default: break;
}
int maxLength = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
if (maxLength > 0)
{
int length = 0;
char *log = (char *)RL_CALLOC(maxLength, sizeof(char));
glGetShaderInfoLog(shader, maxLength, &length, log);
TRACELOG(RL_LOG_WARNING, "SHADER: [ID %i] Compile error: %s", shader, log);
RL_FREE(log);
}
}
else
{
switch (type)
{
case GL_VERTEX_SHADER: TRACELOG(RL_LOG_INFO, "SHADER: [ID %i] Vertex shader compiled successfully", shader); break;
case GL_FRAGMENT_SHADER: TRACELOG(RL_LOG_INFO, "SHADER: [ID %i] Fragment shader compiled successfully", shader); break;
//case GL_GEOMETRY_SHADER:
#if defined(GRAPHICS_API_OPENGL_43)
case GL_COMPUTE_SHADER: TRACELOG(RL_LOG_INFO, "SHADER: [ID %i] Compute shader compiled successfully", shader); break;
#endif
default: break;
}
}
#endif
return shader;
}
// Load custom shader strings and return program id
unsigned int rlLoadShaderProgram(unsigned int vShaderId, unsigned int fShaderId)
{
unsigned int program = 0;
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
GLint success = 0;
program = glCreateProgram();
glAttachShader(program, vShaderId);
glAttachShader(program, fShaderId);
// NOTE: Default attribute shader locations must be Bound before linking
glBindAttribLocation(program, RL_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION, RL_DEFAULT_SHADER_ATTRIB_NAME_POSITION);
glBindAttribLocation(program, RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD, RL_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD);
glBindAttribLocation(program, RL_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL, RL_DEFAULT_SHADER_ATTRIB_NAME_NORMAL);
glBindAttribLocation(program, RL_DEFAULT_SHADER_ATTRIB_LOCATION_COLOR, RL_DEFAULT_SHADER_ATTRIB_NAME_COLOR);
glBindAttribLocation(program, RL_DEFAULT_SHADER_ATTRIB_LOCATION_TANGENT, RL_DEFAULT_SHADER_ATTRIB_NAME_TANGENT);
glBindAttribLocation(program, RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD2, RL_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD2);
// NOTE: If some attrib name is no found on the shader, it locations becomes -1
glLinkProgram(program);
// NOTE: All uniform variables are intitialised to 0 when a program links
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (success == GL_FALSE)
{
TRACELOG(RL_LOG_WARNING, "SHADER: [ID %i] Failed to link shader program", program);
int maxLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength);
if (maxLength > 0)
{
int length = 0;
char *log = (char *)RL_CALLOC(maxLength, sizeof(char));
glGetProgramInfoLog(program, maxLength, &length, log);
TRACELOG(RL_LOG_WARNING, "SHADER: [ID %i] Link error: %s", program, log);
RL_FREE(log);
}
glDeleteProgram(program);
program = 0;
}
else
{
// Get the size of compiled shader program (not available on OpenGL ES 2.0)
// NOTE: If GL_LINK_STATUS is GL_FALSE, program binary length is zero.
//GLint binarySize = 0;
//glGetProgramiv(id, GL_PROGRAM_BINARY_LENGTH, &binarySize);
TRACELOG(RL_LOG_INFO, "SHADER: [ID %i] Program shader loaded successfully", program);
}
#endif
return program;
}
// Unload shader program
void rlUnloadShaderProgram(unsigned int id)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
glDeleteProgram(id);
TRACELOG(RL_LOG_INFO, "SHADER: [ID %i] Unloaded shader program data from VRAM (GPU)", id);
#endif
}
// Get shader location uniform
int rlGetLocationUniform(unsigned int shaderId, const char *uniformName)
{
int location = -1;
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
location = glGetUniformLocation(shaderId, uniformName);
//if (location == -1) TRACELOG(RL_LOG_WARNING, "SHADER: [ID %i] Failed to find shader uniform: %s", shaderId, uniformName);
//else TRACELOG(RL_LOG_INFO, "SHADER: [ID %i] Shader uniform (%s) set at location: %i", shaderId, uniformName, location);
#endif
return location;
}
// Get shader location attribute
int rlGetLocationAttrib(unsigned int shaderId, const char *attribName)
{
int location = -1;
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
location = glGetAttribLocation(shaderId, attribName);
//if (location == -1) TRACELOG(RL_LOG_WARNING, "SHADER: [ID %i] Failed to find shader attribute: %s", shaderId, attribName);
//else TRACELOG(RL_LOG_INFO, "SHADER: [ID %i] Shader attribute (%s) set at location: %i", shaderId, attribName, location);
#endif
return location;
}
// Set shader value uniform
void rlSetUniform(int locIndex, const void *value, int uniformType, int count)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
switch (uniformType)
{
case RL_SHADER_UNIFORM_FLOAT: glUniform1fv(locIndex, count, (float *)value); break;
case RL_SHADER_UNIFORM_VEC2: glUniform2fv(locIndex, count, (float *)value); break;
case RL_SHADER_UNIFORM_VEC3: glUniform3fv(locIndex, count, (float *)value); break;
case RL_SHADER_UNIFORM_VEC4: glUniform4fv(locIndex, count, (float *)value); break;
case RL_SHADER_UNIFORM_INT: glUniform1iv(locIndex, count, (int *)value); break;
case RL_SHADER_UNIFORM_IVEC2: glUniform2iv(locIndex, count, (int *)value); break;
case RL_SHADER_UNIFORM_IVEC3: glUniform3iv(locIndex, count, (int *)value); break;
case RL_SHADER_UNIFORM_IVEC4: glUniform4iv(locIndex, count, (int *)value); break;
case RL_SHADER_UNIFORM_SAMPLER2D: glUniform1iv(locIndex, count, (int *)value); break;
default: TRACELOG(RL_LOG_WARNING, "SHADER: Failed to set uniform value, data type not recognized");
}
#endif
}
// Set shader value attribute
void rlSetVertexAttributeDefault(int locIndex, const void *value, int attribType, int count)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
switch (attribType)
{
case RL_SHADER_ATTRIB_FLOAT: if (count == 1) glVertexAttrib1fv(locIndex, (float *)value); break;
case RL_SHADER_ATTRIB_VEC2: if (count == 2) glVertexAttrib2fv(locIndex, (float *)value); break;
case RL_SHADER_ATTRIB_VEC3: if (count == 3) glVertexAttrib3fv(locIndex, (float *)value); break;
case RL_SHADER_ATTRIB_VEC4: if (count == 4) glVertexAttrib4fv(locIndex, (float *)value); break;
default: TRACELOG(RL_LOG_WARNING, "SHADER: Failed to set attrib default value, data type not recognized");
}
#endif
}
// Set shader value uniform matrix
void rlSetUniformMatrix(int locIndex, Matrix mat)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
float matfloat[16] = {
mat.m0, mat.m1, mat.m2, mat.m3,
mat.m4, mat.m5, mat.m6, mat.m7,
mat.m8, mat.m9, mat.m10, mat.m11,
mat.m12, mat.m13, mat.m14, mat.m15
};
glUniformMatrix4fv(locIndex, 1, false, matfloat);
#endif
}
// Set shader value uniform sampler
void rlSetUniformSampler(int locIndex, unsigned int textureId)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
// Check if texture is already active
for (int i = 0; i < RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS; i++)
{
if (RLGL.State.activeTextureId[i] == textureId)
{
glUniform1i(locIndex, 1 + i);
return;
}
}
// Register a new active texture for the internal batch system
// NOTE: Default texture is always activated as GL_TEXTURE0
for (int i = 0; i < RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS; i++)
{
if (RLGL.State.activeTextureId[i] == 0)
{
glUniform1i(locIndex, 1 + i); // Activate new texture unit
RLGL.State.activeTextureId[i] = textureId; // Save texture id for binding on drawing
break;
}
}
#endif
}
// Set shader currently active (id and locations)
void rlSetShader(unsigned int id, int *locs)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
if (RLGL.State.currentShaderId != id)
{
rlDrawRenderBatch(RLGL.currentBatch);
RLGL.State.currentShaderId = id;
RLGL.State.currentShaderLocs = locs;
}
#endif
}
// Load compute shader program
unsigned int rlLoadComputeShaderProgram(unsigned int shaderId)
{
unsigned int program = 0;
#if defined(GRAPHICS_API_OPENGL_43)
GLint success = 0;
program = glCreateProgram();
glAttachShader(program, shaderId);
glLinkProgram(program);
// NOTE: All uniform variables are intitialised to 0 when a program links
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (success == GL_FALSE)
{
TRACELOG(RL_LOG_WARNING, "SHADER: [ID %i] Failed to link compute shader program", program);
int maxLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength);
if (maxLength > 0)
{
int length = 0;
char *log = (char *)RL_CALLOC(maxLength, sizeof(char));
glGetProgramInfoLog(program, maxLength, &length, log);
TRACELOG(RL_LOG_WARNING, "SHADER: [ID %i] Link error: %s", program, log);
RL_FREE(log);
}
glDeleteProgram(program);
program = 0;
}
else
{
// Get the size of compiled shader program (not available on OpenGL ES 2.0)
// NOTE: If GL_LINK_STATUS is GL_FALSE, program binary length is zero.
//GLint binarySize = 0;
//glGetProgramiv(id, GL_PROGRAM_BINARY_LENGTH, &binarySize);
TRACELOG(RL_LOG_INFO, "SHADER: [ID %i] Compute shader program loaded successfully", program);
}
#endif
return program;
}
// Dispatch compute shader (equivalent to *draw* for graphics pilepine)
void rlComputeShaderDispatch(unsigned int groupX, unsigned int groupY, unsigned int groupZ)
{
#if defined(GRAPHICS_API_OPENGL_43)
glDispatchCompute(groupX, groupY, groupZ);
#endif
}
// Load shader storage buffer object (SSBO)
unsigned int rlLoadShaderBuffer(unsigned int size, const void *data, int usageHint)
{
unsigned int ssbo = 0;
#if defined(GRAPHICS_API_OPENGL_43)
glGenBuffers(1, &ssbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, size, data, usageHint? usageHint : RL_STREAM_COPY);
if (data == NULL) glClearBufferData(GL_SHADER_STORAGE_BUFFER, GL_R8UI, GL_RED_INTEGER, GL_UNSIGNED_BYTE, NULL); // Clear buffer data to 0
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
#endif
return ssbo;
}
// Unload shader storage buffer object (SSBO)
void rlUnloadShaderBuffer(unsigned int ssboId)
{
#if defined(GRAPHICS_API_OPENGL_43)
glDeleteBuffers(1, &ssboId);
#endif
}
// Update SSBO buffer data
void rlUpdateShaderBuffer(unsigned int id, const void *data, unsigned int dataSize, unsigned int offset)
{
#if defined(GRAPHICS_API_OPENGL_43)
glBindBuffer(GL_SHADER_STORAGE_BUFFER, id);
glBufferSubData(GL_SHADER_STORAGE_BUFFER, offset, dataSize, data);
#endif
}
// Get SSBO buffer size
unsigned int rlGetShaderBufferSize(unsigned int id)
{
long long size = 0;
#if defined(GRAPHICS_API_OPENGL_43)
glBindBuffer(GL_SHADER_STORAGE_BUFFER, id);
glGetInteger64v(GL_SHADER_STORAGE_BUFFER_SIZE, &size);
#endif
return (size > 0)? (unsigned int)size : 0;
}
// Read SSBO buffer data (GPU->CPU)
void rlReadShaderBuffer(unsigned int id, void *dest, unsigned int count, unsigned int offset)
{
#if defined(GRAPHICS_API_OPENGL_43)
glBindBuffer(GL_SHADER_STORAGE_BUFFER, id);
glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, offset, count, dest);
#endif
}
// Bind SSBO buffer
void rlBindShaderBuffer(unsigned int id, unsigned int index)
{
#if defined(GRAPHICS_API_OPENGL_43)
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, index, id);
#endif
}
// Copy SSBO buffer data
void rlCopyShaderBuffer(unsigned int destId, unsigned int srcId, unsigned int destOffset, unsigned int srcOffset, unsigned int count)
{
#if defined(GRAPHICS_API_OPENGL_43)
glBindBuffer(GL_COPY_READ_BUFFER, srcId);
glBindBuffer(GL_COPY_WRITE_BUFFER, destId);
glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, srcOffset, destOffset, count);
#endif
}
// Bind image texture
void rlBindImageTexture(unsigned int id, unsigned int index, int format, bool readonly)
{
#if defined(GRAPHICS_API_OPENGL_43)
unsigned int glInternalFormat = 0, glFormat = 0, glType = 0;
rlGetGlTextureFormats(format, &glInternalFormat, &glFormat, &glType);
glBindImageTexture(index, id, 0, 0, 0, readonly? GL_READ_ONLY : GL_READ_WRITE, glInternalFormat);
#endif
}
// Matrix state management
//-----------------------------------------------------------------------------------------
// Get internal modelview matrix
Matrix rlGetMatrixModelview(void)
{
Matrix matrix = rlMatrixIdentity();
#if defined(GRAPHICS_API_OPENGL_11)
float mat[16];
glGetFloatv(GL_MODELVIEW_MATRIX, mat);
matrix.m0 = mat[0];
matrix.m1 = mat[1];
matrix.m2 = mat[2];
matrix.m3 = mat[3];
matrix.m4 = mat[4];
matrix.m5 = mat[5];
matrix.m6 = mat[6];
matrix.m7 = mat[7];
matrix.m8 = mat[8];
matrix.m9 = mat[9];
matrix.m10 = mat[10];
matrix.m11 = mat[11];
matrix.m12 = mat[12];
matrix.m13 = mat[13];
matrix.m14 = mat[14];
matrix.m15 = mat[15];
#else
matrix = RLGL.State.modelview;
#endif
return matrix;
}
// Get internal projection matrix
Matrix rlGetMatrixProjection(void)
{
#if defined(GRAPHICS_API_OPENGL_11)
float mat[16];
glGetFloatv(GL_PROJECTION_MATRIX,mat);
Matrix m;
m.m0 = mat[0];
m.m1 = mat[1];
m.m2 = mat[2];
m.m3 = mat[3];
m.m4 = mat[4];
m.m5 = mat[5];
m.m6 = mat[6];
m.m7 = mat[7];
m.m8 = mat[8];
m.m9 = mat[9];
m.m10 = mat[10];
m.m11 = mat[11];
m.m12 = mat[12];
m.m13 = mat[13];
m.m14 = mat[14];
m.m15 = mat[15];
return m;
#else
return RLGL.State.projection;
#endif
}
// Get internal accumulated transform matrix
Matrix rlGetMatrixTransform(void)
{
Matrix mat = rlMatrixIdentity();
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
// TODO: Consider possible transform matrices in the RLGL.State.stack
// Is this the right order? or should we start with the first stored matrix instead of the last one?
//Matrix matStackTransform = rlMatrixIdentity();
//for (int i = RLGL.State.stackCounter; i > 0; i--) matStackTransform = rlMatrixMultiply(RLGL.State.stack[i], matStackTransform);
mat = RLGL.State.transform;
#endif
return mat;
}
// Get internal projection matrix for stereo render (selected eye)
RLAPI Matrix rlGetMatrixProjectionStereo(int eye)
{
Matrix mat = rlMatrixIdentity();
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
mat = RLGL.State.projectionStereo[eye];
#endif
return mat;
}
// Get internal view offset matrix for stereo render (selected eye)
RLAPI Matrix rlGetMatrixViewOffsetStereo(int eye)
{
Matrix mat = rlMatrixIdentity();
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
mat = RLGL.State.viewOffsetStereo[eye];
#endif
return mat;
}
// Set a custom modelview matrix (replaces internal modelview matrix)
void rlSetMatrixModelview(Matrix view)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
RLGL.State.modelview = view;
#endif
}
// Set a custom projection matrix (replaces internal projection matrix)
void rlSetMatrixProjection(Matrix projection)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
RLGL.State.projection = projection;
#endif
}
// Set eyes projection matrices for stereo rendering
void rlSetMatrixProjectionStereo(Matrix right, Matrix left)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
RLGL.State.projectionStereo[0] = right;
RLGL.State.projectionStereo[1] = left;
#endif
}
// Set eyes view offsets matrices for stereo rendering
void rlSetMatrixViewOffsetStereo(Matrix right, Matrix left)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
RLGL.State.viewOffsetStereo[0] = right;
RLGL.State.viewOffsetStereo[1] = left;
#endif
}
// Load and draw a quad in NDC
void rlLoadDrawQuad(void)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
unsigned int quadVAO = 0;
unsigned int quadVBO = 0;
float vertices[] = {
// Positions Texcoords
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
};
// Gen VAO to contain VBO
glGenVertexArrays(1, &quadVAO);
glBindVertexArray(quadVAO);
// Gen and fill vertex buffer (VBO)
glGenBuffers(1, &quadVBO);
glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices, GL_STATIC_DRAW);
// Bind vertex attributes (position, texcoords)
glEnableVertexAttribArray(RL_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION);
glVertexAttribPointer(RL_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION, 3, GL_FLOAT, GL_FALSE, 5*sizeof(float), (void *)0); // Positions
glEnableVertexAttribArray(RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD);
glVertexAttribPointer(RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 5*sizeof(float), (void *)(3*sizeof(float))); // Texcoords
// Draw quad
glBindVertexArray(quadVAO);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0);
// Delete buffers (VBO and VAO)
glDeleteBuffers(1, &quadVBO);
glDeleteVertexArrays(1, &quadVAO);
#endif
}
// Load and draw a cube in NDC
void rlLoadDrawCube(void)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
unsigned int cubeVAO = 0;
unsigned int cubeVBO = 0;
float vertices[] = {
// Positions Normals Texcoords
-1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f,
1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
-1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
-1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
-1.0f, 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
-1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
-1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
-1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
-1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
-1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f,
-1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
-1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
-1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
-1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f
};
// Gen VAO to contain VBO
glGenVertexArrays(1, &cubeVAO);
glBindVertexArray(cubeVAO);
// Gen and fill vertex buffer (VBO)
glGenBuffers(1, &cubeVBO);
glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Bind vertex attributes (position, normals, texcoords)
glBindVertexArray(cubeVAO);
glEnableVertexAttribArray(RL_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION);
glVertexAttribPointer(RL_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION, 3, GL_FLOAT, GL_FALSE, 8*sizeof(float), (void *)0); // Positions
glEnableVertexAttribArray(RL_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL);
glVertexAttribPointer(RL_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL, 3, GL_FLOAT, GL_FALSE, 8*sizeof(float), (void *)(3*sizeof(float))); // Normals
glEnableVertexAttribArray(RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD);
glVertexAttribPointer(RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 8*sizeof(float), (void *)(6*sizeof(float))); // Texcoords
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
// Draw cube
glBindVertexArray(cubeVAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
// Delete VBO and VAO
glDeleteBuffers(1, &cubeVBO);
glDeleteVertexArrays(1, &cubeVAO);
#endif
}
// Get name string for pixel format
const char *rlGetPixelFormatName(unsigned int format)
{
switch (format)
{
case RL_PIXELFORMAT_UNCOMPRESSED_GRAYSCALE: return "GRAYSCALE"; break; // 8 bit per pixel (no alpha)
case RL_PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA: return "GRAY_ALPHA"; break; // 8*2 bpp (2 channels)
case RL_PIXELFORMAT_UNCOMPRESSED_R5G6B5: return "R5G6B5"; break; // 16 bpp
case RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8: return "R8G8B8"; break; // 24 bpp
case RL_PIXELFORMAT_UNCOMPRESSED_R5G5B5A1: return "R5G5B5A1"; break; // 16 bpp (1 bit alpha)
case RL_PIXELFORMAT_UNCOMPRESSED_R4G4B4A4: return "R4G4B4A4"; break; // 16 bpp (4 bit alpha)
case RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8A8: return "R8G8B8A8"; break; // 32 bpp
case RL_PIXELFORMAT_UNCOMPRESSED_R32: return "R32"; break; // 32 bpp (1 channel - float)
case RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32: return "R32G32B32"; break; // 32*3 bpp (3 channels - float)
case RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32A32: return "R32G32B32A32"; break; // 32*4 bpp (4 channels - float)
case RL_PIXELFORMAT_UNCOMPRESSED_R16: return "R16"; break; // 16 bpp (1 channel - half float)
case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16: return "R16G16B16"; break; // 16*3 bpp (3 channels - half float)
case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16A16: return "R16G16B16A16"; break; // 16*4 bpp (4 channels - half float)
case RL_PIXELFORMAT_COMPRESSED_DXT1_RGB: return "DXT1_RGB"; break; // 4 bpp (no alpha)
case RL_PIXELFORMAT_COMPRESSED_DXT1_RGBA: return "DXT1_RGBA"; break; // 4 bpp (1 bit alpha)
case RL_PIXELFORMAT_COMPRESSED_DXT3_RGBA: return "DXT3_RGBA"; break; // 8 bpp
case RL_PIXELFORMAT_COMPRESSED_DXT5_RGBA: return "DXT5_RGBA"; break; // 8 bpp
case RL_PIXELFORMAT_COMPRESSED_ETC1_RGB: return "ETC1_RGB"; break; // 4 bpp
case RL_PIXELFORMAT_COMPRESSED_ETC2_RGB: return "ETC2_RGB"; break; // 4 bpp
case RL_PIXELFORMAT_COMPRESSED_ETC2_EAC_RGBA: return "ETC2_RGBA"; break; // 8 bpp
case RL_PIXELFORMAT_COMPRESSED_PVRT_RGB: return "PVRT_RGB"; break; // 4 bpp
case RL_PIXELFORMAT_COMPRESSED_PVRT_RGBA: return "PVRT_RGBA"; break; // 4 bpp
case RL_PIXELFORMAT_COMPRESSED_ASTC_4x4_RGBA: return "ASTC_4x4_RGBA"; break; // 8 bpp
case RL_PIXELFORMAT_COMPRESSED_ASTC_8x8_RGBA: return "ASTC_8x8_RGBA"; break; // 2 bpp
default: return "UNKNOWN"; break;
}
}
//----------------------------------------------------------------------------------
// Module specific Functions Definition
//----------------------------------------------------------------------------------
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
// Load default shader (just vertex positioning and texture coloring)
// NOTE: This shader program is used for internal buffers
// NOTE: Loaded: RLGL.State.defaultShaderId, RLGL.State.defaultShaderLocs
static void rlLoadShaderDefault(void)
{
RLGL.State.defaultShaderLocs = (int *)RL_CALLOC(RL_MAX_SHADER_LOCATIONS, sizeof(int));
// NOTE: All locations must be reseted to -1 (no location)
for (int i = 0; i < RL_MAX_SHADER_LOCATIONS; i++) RLGL.State.defaultShaderLocs[i] = -1;
// Vertex shader directly defined, no external file required
const char *defaultVShaderCode =
#if defined(GRAPHICS_API_OPENGL_21)
"#version 120 \n"
"attribute vec3 vertexPosition; \n"
"attribute vec2 vertexTexCoord; \n"
"attribute vec4 vertexColor; \n"
"varying vec2 fragTexCoord; \n"
"varying vec4 fragColor; \n"
#elif defined(GRAPHICS_API_OPENGL_33)
"#version 330 \n"
"in vec3 vertexPosition; \n"
"in vec2 vertexTexCoord; \n"
"in vec4 vertexColor; \n"
"out vec2 fragTexCoord; \n"
"out vec4 fragColor; \n"
#endif
#if defined(GRAPHICS_API_OPENGL_ES2)
"#version 100 \n"
"precision mediump float; \n" // Precision required for OpenGL ES2 (WebGL) (on some browsers)
"attribute vec3 vertexPosition; \n"
"attribute vec2 vertexTexCoord; \n"
"attribute vec4 vertexColor; \n"
"varying vec2 fragTexCoord; \n"
"varying vec4 fragColor; \n"
#endif
"uniform mat4 mvp; \n"
"void main() \n"
"{ \n"
" fragTexCoord = vertexTexCoord; \n"
" fragColor = vertexColor; \n"
" gl_Position = mvp*vec4(vertexPosition, 1.0); \n"
"} \n";
// Fragment shader directly defined, no external file required
const char *defaultFShaderCode =
#if defined(GRAPHICS_API_OPENGL_21)
"#version 120 \n"
"varying vec2 fragTexCoord; \n"
"varying vec4 fragColor; \n"
"uniform sampler2D texture0; \n"
"uniform vec4 colDiffuse; \n"
"void main() \n"
"{ \n"
" vec4 texelColor = texture2D(texture0, fragTexCoord); \n"
" gl_FragColor = texelColor*colDiffuse*fragColor; \n"
"} \n";
#elif defined(GRAPHICS_API_OPENGL_33)
"#version 330 \n"
"in vec2 fragTexCoord; \n"
"in vec4 fragColor; \n"
"out vec4 finalColor; \n"
"uniform sampler2D texture0; \n"
"uniform vec4 colDiffuse; \n"
"void main() \n"
"{ \n"
" vec4 texelColor = texture(texture0, fragTexCoord); \n"
" finalColor = texelColor*colDiffuse*fragColor; \n"
"} \n";
#endif
#if defined(GRAPHICS_API_OPENGL_ES2)
"#version 100 \n"
"precision mediump float; \n" // Precision required for OpenGL ES2 (WebGL)
"varying vec2 fragTexCoord; \n"
"varying vec4 fragColor; \n"
"uniform sampler2D texture0; \n"
"uniform vec4 colDiffuse; \n"
"void main() \n"
"{ \n"
" vec4 texelColor = texture2D(texture0, fragTexCoord); \n"
" gl_FragColor = texelColor*colDiffuse*fragColor; \n"
"} \n";
#endif
// NOTE: Compiled vertex/fragment shaders are not deleted,
// they are kept for re-use as default shaders in case some shader loading fails
RLGL.State.defaultVShaderId = rlCompileShader(defaultVShaderCode, GL_VERTEX_SHADER); // Compile default vertex shader
RLGL.State.defaultFShaderId = rlCompileShader(defaultFShaderCode, GL_FRAGMENT_SHADER); // Compile default fragment shader
RLGL.State.defaultShaderId = rlLoadShaderProgram(RLGL.State.defaultVShaderId, RLGL.State.defaultFShaderId);
if (RLGL.State.defaultShaderId > 0)
{
TRACELOG(RL_LOG_INFO, "SHADER: [ID %i] Default shader loaded successfully", RLGL.State.defaultShaderId);
// Set default shader locations: attributes locations
RLGL.State.defaultShaderLocs[RL_SHADER_LOC_VERTEX_POSITION] = glGetAttribLocation(RLGL.State.defaultShaderId, RL_DEFAULT_SHADER_ATTRIB_NAME_POSITION);
RLGL.State.defaultShaderLocs[RL_SHADER_LOC_VERTEX_TEXCOORD01] = glGetAttribLocation(RLGL.State.defaultShaderId, RL_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD);
RLGL.State.defaultShaderLocs[RL_SHADER_LOC_VERTEX_COLOR] = glGetAttribLocation(RLGL.State.defaultShaderId, RL_DEFAULT_SHADER_ATTRIB_NAME_COLOR);
// Set default shader locations: uniform locations
RLGL.State.defaultShaderLocs[RL_SHADER_LOC_MATRIX_MVP] = glGetUniformLocation(RLGL.State.defaultShaderId, RL_DEFAULT_SHADER_UNIFORM_NAME_MVP);
RLGL.State.defaultShaderLocs[RL_SHADER_LOC_COLOR_DIFFUSE] = glGetUniformLocation(RLGL.State.defaultShaderId, RL_DEFAULT_SHADER_UNIFORM_NAME_COLOR);
RLGL.State.defaultShaderLocs[RL_SHADER_LOC_MAP_DIFFUSE] = glGetUniformLocation(RLGL.State.defaultShaderId, RL_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE0);
}
else TRACELOG(RL_LOG_WARNING, "SHADER: [ID %i] Failed to load default shader", RLGL.State.defaultShaderId);
}
// Unload default shader
// NOTE: Unloads: RLGL.State.defaultShaderId, RLGL.State.defaultShaderLocs
static void rlUnloadShaderDefault(void)
{
glUseProgram(0);
glDetachShader(RLGL.State.defaultShaderId, RLGL.State.defaultVShaderId);
glDetachShader(RLGL.State.defaultShaderId, RLGL.State.defaultFShaderId);
glDeleteShader(RLGL.State.defaultVShaderId);
glDeleteShader(RLGL.State.defaultFShaderId);
glDeleteProgram(RLGL.State.defaultShaderId);
RL_FREE(RLGL.State.defaultShaderLocs);
TRACELOG(RL_LOG_INFO, "SHADER: [ID %i] Default shader unloaded successfully", RLGL.State.defaultShaderId);
}
#if defined(RLGL_SHOW_GL_DETAILS_INFO)
// Get compressed format official GL identifier name
static const char *rlGetCompressedFormatName(int format)
{
switch (format)
{
// GL_EXT_texture_compression_s3tc
case 0x83F0: return "GL_COMPRESSED_RGB_S3TC_DXT1_EXT"; break;
case 0x83F1: return "GL_COMPRESSED_RGBA_S3TC_DXT1_EXT"; break;
case 0x83F2: return "GL_COMPRESSED_RGBA_S3TC_DXT3_EXT"; break;
case 0x83F3: return "GL_COMPRESSED_RGBA_S3TC_DXT5_EXT"; break;
// GL_3DFX_texture_compression_FXT1
case 0x86B0: return "GL_COMPRESSED_RGB_FXT1_3DFX"; break;
case 0x86B1: return "GL_COMPRESSED_RGBA_FXT1_3DFX"; break;
// GL_IMG_texture_compression_pvrtc
case 0x8C00: return "GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG"; break;
case 0x8C01: return "GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG"; break;
case 0x8C02: return "GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG"; break;
case 0x8C03: return "GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG"; break;
// GL_OES_compressed_ETC1_RGB8_texture
case 0x8D64: return "GL_ETC1_RGB8_OES"; break;
// GL_ARB_texture_compression_rgtc
case 0x8DBB: return "GL_COMPRESSED_RED_RGTC1"; break;
case 0x8DBC: return "GL_COMPRESSED_SIGNED_RED_RGTC1"; break;
case 0x8DBD: return "GL_COMPRESSED_RG_RGTC2"; break;
case 0x8DBE: return "GL_COMPRESSED_SIGNED_RG_RGTC2"; break;
// GL_ARB_texture_compression_bptc
case 0x8E8C: return "GL_COMPRESSED_RGBA_BPTC_UNORM_ARB"; break;
case 0x8E8D: return "GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB"; break;
case 0x8E8E: return "GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB"; break;
case 0x8E8F: return "GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB"; break;
// GL_ARB_ES3_compatibility
case 0x9274: return "GL_COMPRESSED_RGB8_ETC2"; break;
case 0x9275: return "GL_COMPRESSED_SRGB8_ETC2"; break;
case 0x9276: return "GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2"; break;
case 0x9277: return "GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2"; break;
case 0x9278: return "GL_COMPRESSED_RGBA8_ETC2_EAC"; break;
case 0x9279: return "GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC"; break;
case 0x9270: return "GL_COMPRESSED_R11_EAC"; break;
case 0x9271: return "GL_COMPRESSED_SIGNED_R11_EAC"; break;
case 0x9272: return "GL_COMPRESSED_RG11_EAC"; break;
case 0x9273: return "GL_COMPRESSED_SIGNED_RG11_EAC"; break;
// GL_KHR_texture_compression_astc_hdr
case 0x93B0: return "GL_COMPRESSED_RGBA_ASTC_4x4_KHR"; break;
case 0x93B1: return "GL_COMPRESSED_RGBA_ASTC_5x4_KHR"; break;
case 0x93B2: return "GL_COMPRESSED_RGBA_ASTC_5x5_KHR"; break;
case 0x93B3: return "GL_COMPRESSED_RGBA_ASTC_6x5_KHR"; break;
case 0x93B4: return "GL_COMPRESSED_RGBA_ASTC_6x6_KHR"; break;
case 0x93B5: return "GL_COMPRESSED_RGBA_ASTC_8x5_KHR"; break;
case 0x93B6: return "GL_COMPRESSED_RGBA_ASTC_8x6_KHR"; break;
case 0x93B7: return "GL_COMPRESSED_RGBA_ASTC_8x8_KHR"; break;
case 0x93B8: return "GL_COMPRESSED_RGBA_ASTC_10x5_KHR"; break;
case 0x93B9: return "GL_COMPRESSED_RGBA_ASTC_10x6_KHR"; break;
case 0x93BA: return "GL_COMPRESSED_RGBA_ASTC_10x8_KHR"; break;
case 0x93BB: return "GL_COMPRESSED_RGBA_ASTC_10x10_KHR"; break;
case 0x93BC: return "GL_COMPRESSED_RGBA_ASTC_12x10_KHR"; break;
case 0x93BD: return "GL_COMPRESSED_RGBA_ASTC_12x12_KHR"; break;
case 0x93D0: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR"; break;
case 0x93D1: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR"; break;
case 0x93D2: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR"; break;
case 0x93D3: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR"; break;
case 0x93D4: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR"; break;
case 0x93D5: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR"; break;
case 0x93D6: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR"; break;
case 0x93D7: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR"; break;
case 0x93D8: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR"; break;
case 0x93D9: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR"; break;
case 0x93DA: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR"; break;
case 0x93DB: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR"; break;
case 0x93DC: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR"; break;
case 0x93DD: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR"; break;
default: return "GL_COMPRESSED_UNKNOWN"; break;
}
}
#endif // RLGL_SHOW_GL_DETAILS_INFO
#endif // GRAPHICS_API_OPENGL_33 || GRAPHICS_API_OPENGL_ES2
// Get pixel data size in bytes (image or texture)
// NOTE: Size depends on pixel format
static int rlGetPixelDataSize(int width, int height, int format)
{
int dataSize = 0; // Size in bytes
int bpp = 0; // Bits per pixel
switch (format)
{
case RL_PIXELFORMAT_UNCOMPRESSED_GRAYSCALE: bpp = 8; break;
case RL_PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA:
case RL_PIXELFORMAT_UNCOMPRESSED_R5G6B5:
case RL_PIXELFORMAT_UNCOMPRESSED_R5G5B5A1:
case RL_PIXELFORMAT_UNCOMPRESSED_R4G4B4A4: bpp = 16; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8A8: bpp = 32; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8: bpp = 24; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R32: bpp = 32; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32: bpp = 32*3; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32A32: bpp = 32*4; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R16: bpp = 16; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16: bpp = 16*3; break;
case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16A16: bpp = 16*4; break;
case RL_PIXELFORMAT_COMPRESSED_DXT1_RGB:
case RL_PIXELFORMAT_COMPRESSED_DXT1_RGBA:
case RL_PIXELFORMAT_COMPRESSED_ETC1_RGB:
case RL_PIXELFORMAT_COMPRESSED_ETC2_RGB:
case RL_PIXELFORMAT_COMPRESSED_PVRT_RGB:
case RL_PIXELFORMAT_COMPRESSED_PVRT_RGBA: bpp = 4; break;
case RL_PIXELFORMAT_COMPRESSED_DXT3_RGBA:
case RL_PIXELFORMAT_COMPRESSED_DXT5_RGBA:
case RL_PIXELFORMAT_COMPRESSED_ETC2_EAC_RGBA:
case RL_PIXELFORMAT_COMPRESSED_ASTC_4x4_RGBA: bpp = 8; break;
case RL_PIXELFORMAT_COMPRESSED_ASTC_8x8_RGBA: bpp = 2; break;
default: break;
}
dataSize = width*height*bpp/8; // Total data size in bytes
// Most compressed formats works on 4x4 blocks,
// if texture is smaller, minimum dataSize is 8 or 16
if ((width < 4) && (height < 4))
{
if ((format >= RL_PIXELFORMAT_COMPRESSED_DXT1_RGB) && (format < RL_PIXELFORMAT_COMPRESSED_DXT3_RGBA)) dataSize = 8;
else if ((format >= RL_PIXELFORMAT_COMPRESSED_DXT3_RGBA) && (format < RL_PIXELFORMAT_COMPRESSED_ASTC_8x8_RGBA)) dataSize = 16;
}
return dataSize;
}
// Auxiliar math functions
// Get float array of matrix data
static rl_float16 rlMatrixToFloatV(Matrix mat)
{
rl_float16 result = { 0 };
result.v[0] = mat.m0;
result.v[1] = mat.m1;
result.v[2] = mat.m2;
result.v[3] = mat.m3;
result.v[4] = mat.m4;
result.v[5] = mat.m5;
result.v[6] = mat.m6;
result.v[7] = mat.m7;
result.v[8] = mat.m8;
result.v[9] = mat.m9;
result.v[10] = mat.m10;
result.v[11] = mat.m11;
result.v[12] = mat.m12;
result.v[13] = mat.m13;
result.v[14] = mat.m14;
result.v[15] = mat.m15;
return result;
}
// Get identity matrix
static Matrix rlMatrixIdentity(void)
{
Matrix result = {
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
return result;
}
// Get two matrix multiplication
// NOTE: When multiplying matrices... the order matters!
static Matrix rlMatrixMultiply(Matrix left, Matrix right)
{
Matrix result = { 0 };
result.m0 = left.m0*right.m0 + left.m1*right.m4 + left.m2*right.m8 + left.m3*right.m12;
result.m1 = left.m0*right.m1 + left.m1*right.m5 + left.m2*right.m9 + left.m3*right.m13;
result.m2 = left.m0*right.m2 + left.m1*right.m6 + left.m2*right.m10 + left.m3*right.m14;
result.m3 = left.m0*right.m3 + left.m1*right.m7 + left.m2*right.m11 + left.m3*right.m15;
result.m4 = left.m4*right.m0 + left.m5*right.m4 + left.m6*right.m8 + left.m7*right.m12;
result.m5 = left.m4*right.m1 + left.m5*right.m5 + left.m6*right.m9 + left.m7*right.m13;
result.m6 = left.m4*right.m2 + left.m5*right.m6 + left.m6*right.m10 + left.m7*right.m14;
result.m7 = left.m4*right.m3 + left.m5*right.m7 + left.m6*right.m11 + left.m7*right.m15;
result.m8 = left.m8*right.m0 + left.m9*right.m4 + left.m10*right.m8 + left.m11*right.m12;
result.m9 = left.m8*right.m1 + left.m9*right.m5 + left.m10*right.m9 + left.m11*right.m13;
result.m10 = left.m8*right.m2 + left.m9*right.m6 + left.m10*right.m10 + left.m11*right.m14;
result.m11 = left.m8*right.m3 + left.m9*right.m7 + left.m10*right.m11 + left.m11*right.m15;
result.m12 = left.m12*right.m0 + left.m13*right.m4 + left.m14*right.m8 + left.m15*right.m12;
result.m13 = left.m12*right.m1 + left.m13*right.m5 + left.m14*right.m9 + left.m15*right.m13;
result.m14 = left.m12*right.m2 + left.m13*right.m6 + left.m14*right.m10 + left.m15*right.m14;
result.m15 = left.m12*right.m3 + left.m13*right.m7 + left.m14*right.m11 + left.m15*right.m15;
return result;
}
// Transposes provided matrix
static Matrix rlMatrixTranspose(Matrix mat)
{
Matrix result = { 0 };
result.m0 = mat.m0;
result.m1 = mat.m4;
result.m2 = mat.m8;
result.m3 = mat.m12;
result.m4 = mat.m1;
result.m5 = mat.m5;
result.m6 = mat.m9;
result.m7 = mat.m13;
result.m8 = mat.m2;
result.m9 = mat.m6;
result.m10 = mat.m10;
result.m11 = mat.m14;
result.m12 = mat.m3;
result.m13 = mat.m7;
result.m14 = mat.m11;
result.m15 = mat.m15;
return result;
}
// Invert provided matrix
static Matrix rlMatrixInvert(Matrix mat)
{
Matrix result = { 0 };
// Cache the matrix values (speed optimization)
float a00 = mat.m0, a01 = mat.m1, a02 = mat.m2, a03 = mat.m3;
float a10 = mat.m4, a11 = mat.m5, a12 = mat.m6, a13 = mat.m7;
float a20 = mat.m8, a21 = mat.m9, a22 = mat.m10, a23 = mat.m11;
float a30 = mat.m12, a31 = mat.m13, a32 = mat.m14, a33 = mat.m15;
float b00 = a00*a11 - a01*a10;
float b01 = a00*a12 - a02*a10;
float b02 = a00*a13 - a03*a10;
float b03 = a01*a12 - a02*a11;
float b04 = a01*a13 - a03*a11;
float b05 = a02*a13 - a03*a12;
float b06 = a20*a31 - a21*a30;
float b07 = a20*a32 - a22*a30;
float b08 = a20*a33 - a23*a30;
float b09 = a21*a32 - a22*a31;
float b10 = a21*a33 - a23*a31;
float b11 = a22*a33 - a23*a32;
// Calculate the invert determinant (inlined to avoid double-caching)
float invDet = 1.0f/(b00*b11 - b01*b10 + b02*b09 + b03*b08 - b04*b07 + b05*b06);
result.m0 = (a11*b11 - a12*b10 + a13*b09)*invDet;
result.m1 = (-a01*b11 + a02*b10 - a03*b09)*invDet;
result.m2 = (a31*b05 - a32*b04 + a33*b03)*invDet;
result.m3 = (-a21*b05 + a22*b04 - a23*b03)*invDet;
result.m4 = (-a10*b11 + a12*b08 - a13*b07)*invDet;
result.m5 = (a00*b11 - a02*b08 + a03*b07)*invDet;
result.m6 = (-a30*b05 + a32*b02 - a33*b01)*invDet;
result.m7 = (a20*b05 - a22*b02 + a23*b01)*invDet;
result.m8 = (a10*b10 - a11*b08 + a13*b06)*invDet;
result.m9 = (-a00*b10 + a01*b08 - a03*b06)*invDet;
result.m10 = (a30*b04 - a31*b02 + a33*b00)*invDet;
result.m11 = (-a20*b04 + a21*b02 - a23*b00)*invDet;
result.m12 = (-a10*b09 + a11*b07 - a12*b06)*invDet;
result.m13 = (a00*b09 - a01*b07 + a02*b06)*invDet;
result.m14 = (-a30*b03 + a31*b01 - a32*b00)*invDet;
result.m15 = (a20*b03 - a21*b01 + a22*b00)*invDet;
return result;
}
#endif // RLGL_IMPLEMENTATION
Midless 3D Voxel, Simple M*necraft, 3dship,
Misc
[edit | edit source]slowdown when running multiple 3d apps: this happens because of the GL semaphore "ping pong" between the tasks which can cause like >10000 task switches (and GL context switches) per second.
Semaphores are evil! It you have two or more tasks (3d apps) competing for sem then they get into ping pong state when a task is preempted while he owns the sem which is very likely. From then on (until a task switch happens while the sem is *not* locked) what will happen is that only one (GL) call can be made by task #1, then task switch to task #2 happens, which also can only make one (GL) call, then task switch to task #1 happens, which can only make one (GL) call, then task switch to task #2 happens, etc.
One solution to prevent this would be to have some GrabGL/UngrabGL (or ObtainGL,ReleaseGL if that sounds better) functions to be used in 3D apps and then enclose big parts of GL rendering function calls with it. GrabGL would lock the GL sem and make sure context is correct. ReleaseGL would unlock the GL sem. Even better would be if GrabGL switched all the GL functions (through function table) to versions which which don't use semlock/contextcheck/semunlock at all.
you must pass SDL_OPENGL to SDL_SetVideoMode, you must specify several GL attributes (depth buffer size, framebuffer sizes) using SDL_GL_SetAttribute and finally, if you wish to use double buffering you must specify it as a GL attribute, not by passing the SDL_DOUBLEBUF flag to SDL_SetVideoMode
using a double-buffered display, then you must use SDL_GL_SwapBuffers() to swap the buffers and update the display. To request double-buffering with OpenGL, use SDL_GL_SetAttribute with SDL_GL_DOUBLEBUFFER, and use SDL_GL_GetAttribute to see if you actually got
Loaders may be required to help with different versions needs Lua based loading libraries with extensions, extensions,
Open Scene Graph claims OGL 3.x and 4.x support, but has some kind of weird LGPL based hybrid license. It's C++ based and they consider subclassing to be outside of the scope of the license. The hybrid LGPL-derived license has an exception which allows the shipment of statically linked binaries, so that eliminates one of the major objections to using the LGPL in commercial work.
Ogre has a new 3.3 renderer that seems to be a work in progress. Forum entries seem to indicate that "modern" shader architecture stuff for both OpenGL and DX11 are WIP. MIT license.
G3D has OpenGL 3.3 but only for Windows and OS X. They used to have Linux support but discontinued it due to too many distros to deal with. A recent mailing list post says they want to reinstate the support. BSD license.
ClanLib seems to support 3.3..4.3 but may not build. Zlib license.
Irrlicht claims OpenGL 3.x on their features page. Zlib license.
Cube 2 looks like OpenGL 2.x. Someone's working on something called Tesseract which might have more "dynamic lighting" capabilities, but it's unclear that this means 3.x+. Zlib license.
3D Physics – Qu3e, [ Bullet],
3D Game Engine – List of,
the bare minimum of a "modernized" engine are
Conditional rendering support (if the engine relies on occlusion queries, which many engines do, it should use conditional rendering as much as possible to avoid CPU-GPU sync points)
GPU based skeletal animation (many engines still use CPU side skinning, despite the technology for GPU based animation is there for a decade now)
Instancing support (especially for particles, vegetation and other similar stuff)
Uniform buffer support (remove all glUniform* calls, despite they are still in core, allows also for batching)
Texture array support (use them as much as possible, batch draw commands together)
The most important take away here is to batch. GL 3.x+ provides dozens of different tech to support this, unfortunately they go unused 99% of the time.
procedural map generation principles
[edit | edit source]Simple room placement, BSP room placement, BSP interiors, cellular automata, drunkard's walk, mazes, diffusion-limited aggregation, voronoi, wave function collapse wfc (e.g 2D sprite sheets, tiles placed in terrain maps to 3d objects into 3d spaces) with valid path placed beforehand to save backtracking, and prefabs
Symmetry, layering/combining generators, different corridor algorithms, and doors placement
Domain warping with perturb
#pragma once #include "util/types.hpp" #include "util/std.hpp" #include "util/ndarray.hpp" #include "util/collections.hpp" #include "util/rand.hpp" #include "util/hash.hpp" #include "util/assert.hpp" #include "util/bitset.hpp" #include "util/result.hpp" #include "util/omp.hpp" namespace wfc { enum Flags { // add rotations of default patterns to pattern set FLAGS_ROTATE = 1 << 0, // add reflections of default patterns to pattern set FLAGS_REFLECT = 1 << 1 }; // which pattern selection function to use enum class PatternFunction { WEIGHTED }; // which cell selection function to use enum class NextCellFunction { MIN_ENTROPY }; // behavior of borders for pattern calculation // EXCLUDE: any pattern which would include a border is not used // ZERO: borders have the value T(0) // CLAMP: borders are clamped to their nearest defined value // WRAP: borders are wrapped around the input data enum BorderBehavior { EXCLUDE, ZERO, CLAMP, WRAP }; // permute n-dimensional rotations of an array template <usize N> struct Rotator {}; template <> struct Rotator<2> { using V = ivec2; // permutes rotations of src template <typename T, usize S> static inline std::array<std::array<T, S * S>, 4> permute( const std::array<T, S * S> &src) { std::array<std::array<T, S * S>, 4> dst; dst[0] = src; dst[1] = rotate_ccw<T, S>(dst[0]); dst[2] = rotate_ccw<T, S>(dst[1]); dst[3] = rotate_ccw<T, S>(dst[2]); return dst; } private: template <typename T, usize S> static inline std::array<T, S * S> rotate( const std::array<T, S * S> &src) { std::array<T, S * S> dst; for (usize i = 0; i < S; i++) { for (usize j = 0; j < S; j++) { ndarray::at(V(S), &dst[0], { i, j }) = ndarray::at(V(S), &src[0], { S - j - 1, i }); } } return dst; } }; // permutes n-dimensional reflections of an array template <usize N> struct Reflector { using V = math::vec<N, int, math::defaultp>; template <typename T, usize S, usize VOL = math::cexp::pow(S, N)> static inline auto permute( const std::array<T, S * S> &src) { std::array<std::array<T, VOL>, math::cexp::pow(2, N)> dst; // iterate over possible permutations of axes usize i = 0; ndarray::each( V(2), [&](const V &v) { const auto which = math::vec<N, bool, math::defaultp>(v); dst[i] = src; for (usize j = 0; j < N; j++) { if (which[j]) { dst[i] = reflect_axis<T, S>(dst[i], j); } } i++; }); return dst; } private: template <typename T, usize S, usize VOL = math::cexp::pow(S, N)> static inline auto reflect_axis( const std::array<T, VOL> &src, usize axis) { std::array<T, VOL> dst; ndarray::each( V(S), [&](const V &v) { V u = v; u[axis] = S - u[axis] - 1; ndarray::at(V(S), &dst[0], v) = ndarray::at(V(S), &src[0], u); }); return dst; } }; // neighbors in N dimensions template <usize N> struct Neighbors {}; template <> struct Neighbors<2> { static constexpr std::array<ivec2, 4> neighbors = { ivec2(-1, 0), ivec2(1, 0), ivec2(0, -1), ivec2(0, 1), }; }; template <> struct Neighbors<3> { static constexpr std::array<ivec3, 9> neighbors = { ivec3(-1, 0, 0), ivec3(1, 0, 0), ivec3(0, -1, 0), ivec3(0, 1, 0), ivec3(0, 0, -1), ivec3(0, 0, 1), }; }; // implements n-dimensional wave function collapse with the "overlapping model" // T: pattern data type // N: number of dimension // S: size of patterns (MUST BE ODD!) // D: bitset size // V (defaulted): ivecN template < typename T, usize N, usize S, usize D, typename V = math::vec<N, int, math::defaultp>> requires ((S % 2) == 1) struct WFC { // forward declarations struct Pattern; struct Element; struct Wave; // function which takes current wave and returns next cell to collapse using NextCellFn = std::function<Element&(Wave&)>; // function which chooses the pattern to collapse a cell to using PatternFn = std::function<usize(Wave&, Element&)>; // function used for optional callbacks during collapse process using CallbackFn = std::function<void(const Wave&)>; // S ** N (volume of one pattern) static constexpr auto VOL = math::cexp::pow(S, N); // pattern derived from input data struct Pattern { // unique pattern ID usize id; // normalized frequency of this pattern in input data f32 frequency; // value at the center of this pattern T value; // pattern data std::array<T, VOL> data; // valid neighbors on each side of this pattern std::array<Bitset<D>, 2 * N> valid; explicit Pattern(const std::array<T, VOL> &data) : data(data), value(ndarray::at(V(S), &data[0], V(S) / 2)) {} // NOTE: comparison is on data only auto operator<=>(const Pattern &other) const { return (*this) == other ? std::strong_ordering::equal : this->hash() <=> other.hash(); } // NOTE: equality comparison is on data only bool operator==(const Pattern &other) const { return this->hash() == other.hash() && this->data == other.data; } inline u64 hash() const { if (this->_hash) { return this->_hash; } u64 v = 0x12345; for (const auto &x : this->data) { v ^= x + 0x9e3779b9 + (v << 6) + (v >> 2); } return this->_hash = v; } private: // stored hash, only calculated once mutable u64 _hash = 0; }; // wave element struct Element { // position in output space V pos; // coefficient, marked bits are VALID choices Bitset<D> c; // number of valid bits remaining usize popcnt; // value post-collapse (std::nullopt if not collapsed) std::optional<T> value = std::nullopt; // memoized entropy values f32 sum_weights = 0.0f; f32 sum_weight_log_weights = 0.0f; f32 entropy = 0.0f; // intialize entropy values, coefficient void init(const Wave &w, const Bitset<D> &mask) { this->c = mask; this->popcnt = this->c.popcnt(); for ( auto it = this->c.begin_on(); it != this->c.end_on(); it++) { const auto weight = w.wfc.patterns[*it].frequency; this->sum_weights += weight; this->sum_weight_log_weights += weight * std::log(weight); } } // applies a mask to this element's coefficient, updating memoized // entropy values // returns false on failure (0 popcnt/contradiction) bool apply(const Wave &w, const Bitset<D> &mask) { // get bits which are on in the current coefficient but off in the // new mask. exit early if nothing changes. const auto diff = this->c & mask; if (diff.popcnt() == 0) { return true; } this->c &= mask; for ( auto it = diff.begin_on(); it != diff.end_on(); it++) { const auto weight = w.wfc.patterns[*it].frequency; this->sum_weights -= weight; this->sum_weight_log_weights -= weight * std::log(weight); } this->entropy = std::log(this->sum_weights) - (this->sum_weight_log_weights / this->sum_weights); this->popcnt = this->c.popcnt(); return this->popcnt != 0; } // collapse this element to pattern n bool collapse(usize n, const T &value) { if (!this->c[n]) { return false; } this->value = value; this->c.reset(); this->c.set(n); this->popcnt = 1; this->entropy = 0.0f; this->sum_weights = 0.0f; this->sum_weight_log_weights = 0.0f; return true; } // returns true if this element is collapsed bool collapsed() const { return static_cast<bool>(this->value); } }; // wave to collapse struct Wave { const WFC &wfc; // size of output wave V size_wave; // output wave elements std::vector<Element> wave; // optional preset values const std::optional<T> *preset; // total number of collapsed elements usize num_collapsed = 0; Wave( const WFC &wfc, const V &size_wave, const std::optional<T> *preset = nullptr) : wfc(wfc), size_wave(size_wave), preset(preset) {} // collapses the specified wave element to the only remaining // possibility, or n if specified result::Result<void, V> collapse( Element &e, usize n = std::numeric_limits<usize>::max()) { n = (n == std::numeric_limits<usize>::max()) ? e.c.nth_set(0) : n; const auto &p = this->wfc.patterns[n]; if (!e.collapse(n, p.value)) { return result::Err(e.pos); } this->num_collapsed++; return result::Ok(); } // observe specified wave element, collapsing it to one of its possible // values result::Result<void, V> observe(Element &e) { const auto n = this->wfc.pattern_fn(*this, e); return this->collapse(e, n); } // propagate the current value of the specified wave element // returns Ok on success, erroneous position V on failure // (contradiction) result::Result<void, V> propagate(Element &to_propagate) { // DFS elements to update std::stack<Element*> es; es.push(to_propagate); // if not std::nullopt, there is an unresolvable contradiction in // the wave std::optional<V> contradiction = std::nullopt; // propagate each stack entry to neighbors while (!es.empty()) { auto &e = *es.top(); es.pop(); // get only valid non-collapsed neighbors std::array<Element*, N * 2> neighbors; for (usize i = 0; i < N * 2; i++) { neighbors[i] = nullptr; const auto &n = Neighbors<N>::neighbors[i]; const auto pos_n = e.pos + n; if (!ndarray::in_bounds(this->size_wave, pos_n)) { continue; } auto &e_n = ndarray::at( this->size_wave, &this->wave[0], pos_n); if (e_n.collapsed()) { continue; } neighbors[i] = &e_n; } // compute superpatterns for valid neighbors std::array<Bitset<D>, N * 2> neighbor_patterns; for ( auto it = e.c.begin_on(); it != e.c.end_on(); it++) { const auto &p = this->wfc.patterns[*it]; for (usize i = 0; i < N * 2; i++) { if (neighbors[i]) { neighbor_patterns[i] |= p.valid[i]; } } } // apply superpatterns for (usize i = 0; i < N * 2; i++) { if (!neighbors[i]) { continue; } auto &e_n = *neighbors[i]; const auto popcnt_old = e_n.popcnt; e_n.apply(*this, neighbor_patterns[i]); if (e_n.popcnt != popcnt_old) { if (e_n.popcnt == 0) { // zero popcount = failure/contradiction this->collapse(e_n, 0); contradiction = e_n.pos; break; } else if (e_n.popcnt == 1) { // one popcount = one remaining possibility/collapse auto res = this->collapse(e_n); if (res.isErr()) { return res; } } // propagate changed value es.push(&e_n); } } } if (contradiction) { return result::Err(*contradiction); } if (this->wfc.on_propagate) { (*this->wfc.on_propagate)(*this); } return result::Ok(); } // collapse the wave result::Result<void, V> collapse() { // initialize the wave, all patterns are valid for each element this->wave = std::vector<Element>(math::prod(this->size_wave)); // safe to parallelize, only one element modified at a time #pragma omp parallel for for (usize i = 0; i < this->wave.size(); i++) { auto &e = this->wave[i]; e.pos = ndarray::unravel_index(this->size_wave, i); e.init(*this, this->wfc.mask_used); } // load preset values if present if (this->preset) { ndarray::each( this->size_wave, [&](const V &pos) { const auto &p = ndarray::at(this->size_wave, &this->preset[0], pos); if (p) { auto &e = ndarray::at( this->size_wave, &this->wave[0], pos); e.value = *p; } }); } // collapse the wave while (this->num_collapsed != this->wave.size()) { auto &e = this->wfc.next_cell_fn(*this); const auto res_observe = this->observe(e); if (res_observe.isErr()) { return res_observe; } const auto res_prop = this->propagate(e); if (res_prop.isErr()) { return res_prop; } } // success! return result::Ok(); } }; // size of input data V size_in; // input data const T *in; // possible patterns std::vector<Pattern> patterns; // function to select pattern to collapse to PatternFn pattern_fn; // function to select next cell to collapse NextCellFn next_cell_fn; // behavior of patterns on borders BorderBehavior border_behavior; // mask of used bits of coefficient bitsets // bits are also zeroed for disallowed patterns (patterns without valid // neighbors) Bitset<D> mask_used; // random generator Rand rand; // options usize flags; // optional callback to be called after propagation of each collapsed wave // element std::optional<CallbackFn> on_propagate = std::nullopt; explicit WFC( const V &size_in, const T *in, PatternFunction pattern_function, NextCellFunction next_cell_function, BorderBehavior border_behavior, Rand &&rand, usize flags) : size_in(size_in), in(in), border_behavior(border_behavior), rand(std::move(rand)), flags(flags) { // get pattern data from input at specified location // returns std::nullopt if pattern is made illegal by border behavior const auto data_at = [&](const V ¢er) -> std::optional<std::array<T, VOL>> { const auto base = center - (V(S) / 2); if (border_behavior == BorderBehavior::EXCLUDE) { if (!ndarray::in_bounds(size_in, base) || !ndarray::in_bounds(size_in, base + V(S) - 1)) { return std::nullopt; } } std::array<T, VOL> dst; ndarray::each( V(S), [&](const V &offset) { std::optional<T> override = std::nullopt; V pos = base + offset; switch (border_behavior) { case BorderBehavior::EXCLUDE: break; case BorderBehavior::ZERO: if (!ndarray::in_bounds(size_in, pos)) { override = T(0); } break; case BorderBehavior::CLAMP: pos = math::clamp(pos, V(0), size_in - V(1)); break; case BorderBehavior::WRAP: pos = ((pos % size_in) + size_in) % size_in; break; }; ndarray::at(V(S), &dst[0], offset) = override ? *override : ndarray::at(size_in, in, pos); }); return std::make_optional(dst); }; // slide along input data creating patterns ndarray::each( size_in, [&](const V &p) { if (const auto data = data_at(p)) { this->patterns.emplace_back(Pattern(data)); } }); // calculate per-pattern frequencies (normalization occurs later once // patterns have been deduplicated) std::unordered_map<u64, usize> pattern_hash_to_freq; for (const auto &p : this->patterns) { pattern_hash_to_freq[p.hash()]++; } // assign base frequencies (again, not normalized) for (auto &p : this->patterns) { p.frequency = static_cast<f32>(pattern_hash_to_freq[p.hash()]); } // removes duplicate patterns from this->patterns const auto deduplicate = [&]() { std::sort(this->patterns.begin(), this->patterns.end()); this->patterns.erase( std::unique(this->patterns.begin(), this->patterns.end()), this->patterns.end()); }; // remove duplicate patterns deduplicate(); // add rotations, reflections const auto base_patterns = this->patterns; this->patterns.clear(); for (const auto &p : base_patterns) { this->patterns.push_back(p); if (flags & FLAGS_ROTATE) { ASSERT(N == 2, "can only rotate patterns in 2 dimensions"); if constexpr (N == 2) { const auto permutations = Rotator<N>::template permute<T, S>(p.data); for (usize i = 1; i < permutations.size(); i++) { auto &q = this->patterns.emplace_back( pattern(permutations[i])); q.frequency = p.frequency; } } } if (flags & FLAGS_REFLECT) { const auto permutations = Reflector<N>::template permute<T, S>(p.data); for (usize i = 1; i < permutations.size(); i++) { auto &q = this->patterns.emplace_back( pattern(permutations[i])); q.frequency = p.frequency; } } } // deduplicate again to remove duplicates added by reflection/rotation deduplicate(); // normalize pattern frequencies, pattern set will no longer change f32 frequency_total = 0.0f; for (const auto &p : this->patterns) { frequency_total += p.frequency; } for (auto &p : this->patterns) { p.frequency /= frequency_total; } // compute used mask, assign IDs this->mask_used.reset(); for (usize i = 0; i < this->patterns.size(); i++) { this->patterns[i].id = i; this->mask_used.set(i); } // compuate valid patterns around each pattern (adjacency) // check against all overlapping slots for each pattern pair (p, q) for // every side // // safe to parallelize on p because only p is being written to and not // read from :) #pragma omp parallel for for (auto &p : this->patterns) { for (const auto &q : this->patterns) { for (usize i = 0; i < 2 * N; i++) { const auto &n = Neighbors<N>::neighbors[i]; bool valid = true; ndarray::each( V(S), [&](const auto &offset_q) { if (!valid) { return; } // compute offset into p's data const auto offset_p = n + offset_q; if (!ndarray::in_bounds(V(S), offset_p)) { return; } const auto v_p = ndarray::at(V(S), &p.data[0], offset_p), v_q = ndarray::at(V(S), &q.data[0], offset_q); // data must be equal at each offset for patterns to // match (be valid neighbors) if (v_p != v_q) { valid = false; } }); if (valid) { p.valid[i].set(q.id); } } } } switch (pattern_function) { case PatternFunction::WEIGHTED: this->pattern_fn = this->pattern_weighted(); break; } switch (next_cell_fn) { case NextCellFunction::MIN_ENTROPY: this->next_cell_fn = this->next_cell_min_entropy(); break; } } // collapses a wave into the specified output // returns true on success bool collapse( const V &size_out, T *out, const std::optional<T> *preset = nullptr) const { auto w = Wave(*this, size_out, preset); const auto res = w.collapse(); if (res.isErr()) { return false; } // map elements to output ndarray::each( w.size_wave, [&](const V &pos) { ndarray::at(size_out, out, pos) = *ndarray::at(size_out, &w.wave, pos).value; }); return true; } // returns a function which selects a pattern based on the distribution of // patterns in the input data PatternFn pattern_weighted() { return [&](Wave &w, Element &e) -> usize { f32 sum_cs = 0.0f; std::vector<f32> cs(this->patterns.size()); for ( auto it = e.c.begin_on(); it != e.c.end_on(); it++) { cs[*it] = this->patterns[*it].frequency; sum_cs += cs[*it]; } const auto r = this->rand.template next<f32>(0.0f, sum_cs); f32 acc = 0.0f; for (usize i = 0; i < cs.size(); i++) { acc += cs[i]; if (acc >= r) { return i; } } ASSERT(false, "failed to select pattern for {}", e.pos); return e.c.nth_set(0); }; } // returns a function which selects the next cell based on finding the cell // with the minimum entropy in those remaining NextCellFn next_cell_min_entropy() { return [&](Wave &w) -> Element& { f32 min = 1e4; Element *argmin = nullptr; // min entropy with a bit of noise for (auto &e : w.wave) { if (!e.collapsed() && e.entropy < min && e.entropy + rand.next<f32>(0.0f, 1e-6) < min) { argmin = &e; } } ASSERT(argmin); return *argmin; }; } }; }
isometric grid view
[edit | edit source]Grid is rotated 45 deg and height reduced 50%
func cartesian_to_isometric(vector): return Vector2(vector.x - vector.y, (vector.x + vector.y) / 2)
When a character moves in one direction in the isometric system, always moves both on the X and Y axis in the game.
// These are the four numbers that define the transform, i hat and j hat const i_x = 1; const i_y = 0.5; const j_x = -1; const j_y = 0.5; // Sprite size const w = 32; const h = 32; function to_screen_coordinate(tile: Vector2) { // Without accounting for sprite size return { x: tile.x * i_x + tile.y * j_x, y: tile.x * i_y + tile.y * j_y, } // Accounting for sprite size return { x: tile.x * i_x * 0.5 * w + tile.y * j_x * 0.5 * w, y: tile.x * i_y * 0.5 * h + tile.y * j_y * 0.5 * h, } } // Going from screen coordinate to grid coordinate function invert_matrix(a, b, c, d) { // Determinant const det = (1 / (a * d - b * c)); return { a: det * d, b: det * -b, c: det * -c, d: det * a, } } function to_grid_coordinate(screen: Vector2) { const a = i_x * 0.5 * w; const b = j_x * 0.5 * w; const c = i_y * 0.5 * h; const d = j_y * 0.5 * h; const inv = invert_matrix(a, b, c, d); return { x: screen.x * inv.a + screen.y * inv.b, y: screen.x * inv.c + screen.y * inv.d, } }
You have to manage the game data as if you were creating a top-down title, and you must then convert all the positions and motion vectors to render the characters and the levels properly
#include <raylib.h> #include <raymath.h> #define GRID_WIDTH 8 #define GRID_HEIGHT 8 #define TILE_SIZE 64 #define PLAYER_SPEED 1 #define SCREEN_WIDTH 640 #define SCREEN_HEIGHT 480 // Define tile types (add more as needed) enum TileType { TILE_FLOOR = 0, TILE_WALL, TILE_TREE, TILE_WATER, TILE_ELEVATION, // New tile type for elevation TILE_GRASS, }; int playerX = 0; int playerY = 0; // 3D array representing the isometric grid with tile types and elevation int grid[GRID_WIDTH][GRID_HEIGHT][2] = {0}; // Initialize with all floor tiles at elevation 0 // Function to convert grid coordinates to screen coordinates Vector2 GridToScreen(int x, int y, int elevation) { Vector2 screenPos; screenPos.x = (x - y) * (TILE_SIZE / 2); screenPos.y = (x + y) * (TILE_SIZE / 4) - elevation * (TILE_SIZE / 2); return screenPos; } // Function to check if the player can move to the specified grid position bool CanMoveTo(int x, int y) { if (x < 0 || x >= GRID_WIDTH || y < 0 || y >= GRID_HEIGHT) { // Out of bounds return false; } // Check the tile type and elevation at the specified grid position int tileType = grid[x][y][0]; int elevation = grid[x][y][1]; // Check if the target tile has an elevation of 1 or is flat ground (elevation 0) if (elevation == 1 || elevation == 0) { // If the tile is a wall or tree, return false if (tileType == TILE_WALL || tileType == TILE_TREE) { return false; } return true; // Otherwise, the player can move to this tile } // Check if the player is moving from elevation 1 to elevation 2 or vice versa int currentElevation = grid[playerX][playerY][1]; if ((currentElevation == 1 && elevation == 2) || (currentElevation == 2 && elevation == 1)){ return true; } return false; // Elevation is 2 or higher, and it's not a valid move from elevation 0 } int main() { InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Isometric Grid with Raylib"); SetTargetFPS(60); // Place wall tiles at specific locations with elevation 1 (you can change these as needed) grid[2][5][0] = TILE_WALL; grid[3][1][0] = TILE_WALL; grid[4][3][0] = TILE_WALL; grid[2][4][0] = TILE_WALL; grid[3][3][0] = TILE_WALL; // Add other tile types (e.g., tree, water, grass) at specific locations and elevations grid[1][3][0] = TILE_TREE; grid[6][4][0] = TILE_TREE; grid[4][1][0] = TILE_WATER; grid[0][7][0] = TILE_ELEVATION; // Elevation at (0, 7) represented by the rock tile grid[7][0][0] = TILE_GRASS; // Set the elevation value for the tile representing the rock elevation grid[0][7][1] = 1; // Elevation level of 1 for the rock tile grid[1][7][1] = 2; // Elevation level of 1 for the rock tile // Load textures Texture2D floorTexture = LoadTexture("images/floorTile.png"); Texture2D wallTexture = LoadTexture("images/wallTile.png"); Texture2D playerTexture = LoadTexture("images/playerTile.png"); Texture2D treeTexture = LoadTexture("images/treeTile.png"); Texture2D waterTexture = LoadTexture("images/waterTile.png"); Texture2D elevationTexture = LoadTexture("images/rockTile.png"); // Texture for elevation (rock tile) Texture2D grassTexture = LoadTexture("images/grassTile.png"); while (!WindowShouldClose()) { // Handle player movement if (IsKeyPressed(KEY_W) && CanMoveTo(playerX, playerY - 1)) { playerY -= PLAYER_SPEED; } if (IsKeyPressed(KEY_A) && CanMoveTo(playerX - 1, playerY)) { playerX -= PLAYER_SPEED; } if (IsKeyPressed(KEY_S) && CanMoveTo(playerX, playerY + 1)) { playerY += PLAYER_SPEED; } if (IsKeyPressed(KEY_D) && CanMoveTo(playerX + 1, playerY)) { playerX += PLAYER_SPEED; } // Ensure the player stays within the grid boundaries playerX = Clamp(playerX, 0, GRID_WIDTH - 1); playerY = Clamp(playerY, 0, GRID_HEIGHT - 1); BeginDrawing(); ClearBackground(BLACK); // Render tiles from back to front (lower y-axis first) for (int y = 0; y < GRID_HEIGHT; y++) { for (int x = 0; x < GRID_WIDTH; x++) { Vector2 screenPos = GridToScreen(x, y, grid[x][y][1]); // Use the elevation value // Render the tile at grid[x][y] at (screenPos.x, screenPos.y) switch (grid[x][y][0]) { case TILE_WALL: DrawTexture(wallTexture, screenPos.x + SCREEN_WIDTH / 2, screenPos.y, WHITE); break; case TILE_TREE: DrawTexture(treeTexture, screenPos.x + SCREEN_WIDTH / 2, screenPos.y, WHITE); break; case TILE_WATER: DrawTexture(waterTexture, screenPos.x + SCREEN_WIDTH / 2, screenPos.y, WHITE); break; case TILE_ELEVATION: DrawTexture(elevationTexture, screenPos.x + SCREEN_WIDTH / 2, screenPos.y, WHITE); break; case TILE_GRASS: DrawTexture(grassTexture, screenPos.x + SCREEN_WIDTH / 2, screenPos.y, WHITE); break; default: DrawTexture(floorTexture, screenPos.x + SCREEN_WIDTH / 2, screenPos.y, WHITE); break; } // Render the player if (x == playerX && y == playerY) { DrawTexture(playerTexture, screenPos.x + SCREEN_WIDTH / 2, screenPos.y, WHITE); } } } EndDrawing(); } // Unload textures UnloadTexture(floorTexture); UnloadTexture(wallTexture); UnloadTexture(playerTexture); UnloadTexture(treeTexture); UnloadTexture(waterTexture); UnloadTexture(elevationTexture); UnloadTexture(grassTexture); CloseWindow(); return 0; }
#include <SDL2/SDL.h> #include <SDL2/SDL_image.h> #include <SDL2/SDL_ttf.h> #include <iostream> #define WIDTH 920 #define HEIGHT 620 #define FONT_SIZE 32 #define TILE_SIZE 64 struct object { SDL_Rect dest, src; SDL_Texture* img; }; object grid[12][12]; object selected; bool running; SDL_Renderer* renderer; SDL_Window* window; TTF_Font *font; SDL_Color fcolor; SDL_Point mouse; int frameCount, timerFPS, lastFrame, fps; SDL_Texture* setImage(std::string filename) { return IMG_LoadTexture(renderer, filename.c_str()); } void draw(object o) { SDL_RenderCopyEx(renderer, o.img, &o.src, &o.dest, 0, NULL, SDL_FLIP_NONE); } SDL_Surface *surface; SDL_Texture *texture; SDL_Rect wrect; void write(std::string text, int x, int y) { if (font == NULL) { fprintf(stderr, "error: font not found\n"); exit(EXIT_FAILURE); } fcolor.r = 0; fcolor.g = 0; fcolor.b = 0; const char* t = text.c_str(); surface = TTF_RenderText_Solid(font, t, fcolor); texture = SDL_CreateTextureFromSurface(renderer, surface); wrect.w = surface->w; wrect.h = surface->h; wrect.x = x-wrect.w; wrect.y = y-wrect.h; SDL_FreeSurface(surface); SDL_RenderCopy(renderer, texture, NULL, &wrect); SDL_DestroyTexture(texture); } void update() { } const Uint8 *keystates; void input() { SDL_Event e; keystates = SDL_GetKeyboardState(NULL); while(SDL_PollEvent(&e)) { if(e.type == SDL_QUIT) running=false; } if(keystates[SDL_SCANCODE_ESCAPE]) running=false; SDL_GetMouseState(&mouse.x, &mouse.y); } bool touching; std::string m; void render() { SDL_SetRenderDrawColor(renderer, 240, 240, 240, 255); SDL_RenderClear(renderer); frameCount++; int timerFPS = SDL_GetTicks()-lastFrame; if(timerFPS<(1000/60)) { SDL_Delay((1000/60)-timerFPS); } touching=0; for(int i=0; i<12; i++) { for(int j=0; j<12; j++) { draw(grid[i][j]); if(SDL_PointInRect(&mouse, &grid[i][j].dest)) { m = std::to_string(i) + " " + std::to_string(j); selected.dest = grid[i][j].dest; touching=1; //SDL_RenderDrawRect(renderer, &grid[i][j].dest); } } } if(touching) { draw(selected); write(m, mouse.x, mouse.y); } SDL_RenderPresent(renderer); } void init() { object tile; tile.img=setImage("res/block.png"); tile.src.w=32;tile.src.h=32; tile.src.x=tile.src.y=0; tile.dest.w=TILE_SIZE; tile.dest.h=TILE_SIZE; for(int i=0; i<12; i++) { for(int j=0; j<12; j++) { tile.dest.x=(i*TILE_SIZE - j*TILE_SIZE)/2 + 426; tile.dest.y=(j*TILE_SIZE + i*TILE_SIZE)/4 + 140; grid[i][j]=tile; } } selected=tile; selected.img=setImage("res/select.png"); } int main() { running=1; static int lastTime=0; SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0"); if(SDL_Init(SDL_INIT_EVERYTHING) < 0) std::cout << "Failed at SDL_Init()" << std::endl; if(SDL_CreateWindowAndRenderer(WIDTH, HEIGHT, 0, &window, &renderer) < 0) std::cout << "Failed at SDL_CreateWindowAndRenderer()" << std::endl; SDL_SetWindowTitle(window, "Isometric Grid"); TTF_Init(); font = TTF_OpenFont("res/BebasNeue.ttf", FONT_SIZE); if(font == NULL) std::cout << "failed to load font" << std::endl; init(); while(running) { lastFrame=SDL_GetTicks(); if(lastFrame>=(lastTime+1000)) { lastTime=lastFrame; fps=frameCount; frameCount=0; } update(); input(); render(); } TTF_CloseFont(font); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); }
invert matrix
// These are the four numbers that define the transform, i hat and j hat const i_x = 1; const i_y = 0.5; const j_x = -1; const j_y = 0.5; // Sprite size const w = 32; const h = 32; function to_screen_coordinate(tile: Vector2) { // Without accounting for sprite size return { x: tile.x * i_x + tile.y * j_x, y: tile.x * i_y + tile.y * j_y, } // Accounting for sprite size return { x: tile.x * i_x * 0.5 * w + tile.y * j_x * 0.5 * w, y: tile.x * i_y * 0.5 * h + tile.y * j_y * 0.5 * h, } } // Going from screen coordinate to grid coordinate function invert_matrix(a, b, c, d) { // Determinant const det = (1 / (a * d - b * c)); return { a: det * d, b: det * -b, c: det * -c, d: det * a, } } function to_grid_coordinate(screen: Vector2) { const a = i_x * 0.5 * w; const b = j_x * 0.5 * w; const c = i_y * 0.5 * h; const d = j_y * 0.5 * h; const inv = invert_matrix(a, b, c, d); return { x: screen.x * inv.a + screen.y * inv.b, y: screen.x * inv.c + screen.y * inv.d, } }
rhythm
[edit | edit source]Simple scrolling in C, [], [],
breakout clones
[edit | edit source]3D in C++, [], [],
pathfinding
[edit | edit source]From [3],
/*
OneLoneCoder.com - PathFinding A*
"No more getting lost..." - @Javidx9
License
~~~~~~~
Copyright (C) 2018 Javidx9
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions; See license for details.
Original works located at:
https://www.github.com/onelonecoder
https://www.onelonecoder.com
https://www.youtube.com/javidx9
GNU GPLv3
https://github.com/OneLoneCoder/videos/blob/master/LICENSE
From Javidx9 :)
~~~~~~~~~~~~~~~
Hello! Ultimately I don't care what you use this for. It's intended to be
educational, and perhaps to the oddly minded - a little bit of fun.
Please hack this, change it and use it in any way you see fit. You acknowledge
that I am not responsible for anything bad that happens as a result of
your actions. However this code is protected by GNU GPLv3, see the license in the
github repo. This means you must attribute me if you use it. You can view this
license here: https://github.com/OneLoneCoder/videos/blob/master/LICENSE
Cheers!
Background
~~~~~~~~~~
The A* path finding algorithm is a widely used and powerful shortest path
finding node traversal algorithm. A heuristic is used to bias the algorithm
towards success. This code is probably more interesting than the video. :-/
Author
~~~~~~
Twitter: @javidx9
Blog: www.onelonecoder.com
Video:
~~~~~~
https://youtu.be/icZj67PTFhc
Last Updated: 08/10/2017
*/
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
#include "olcConsoleGameEngine.h"
class OneLoneCoder_PathFinding : public olcConsoleGameEngine
{
public:
OneLoneCoder_PathFinding()
{
m_sAppName = L"Path Finding";
}
private:
struct sNode
{
bool bObstacle = false; // Is the node an obstruction?
bool bVisited = false; // Have we searched this node before?
float fGlobalGoal; // Distance to goal so far
float fLocalGoal; // Distance to goal if we took the alternative route
int x; // Nodes position in 2D space
int y;
vector<sNode*> vecNeighbours; // Connections to neighbours
sNode* parent; // Node connecting to this node that offers shortest parent
};
sNode *nodes = nullptr;
int nMapWidth = 16;
int nMapHeight = 16;
sNode *nodeStart = nullptr;
sNode *nodeEnd = nullptr;
protected:
virtual bool OnUserCreate()
{
// Create a 2D array of nodes - this is for convenience of rendering and construction
// and is not required for the algorithm to work - the nodes could be placed anywhere
// in any space, in multiple dimensions...
nodes = new sNode[nMapWidth * nMapHeight];
for (int x = 0; x < nMapWidth; x++)
for (int y = 0; y < nMapHeight; y++)
{
nodes[y * nMapWidth + x].x = x; // ...because we give each node its own coordinates
nodes[y * nMapWidth + x].y = y;
nodes[y * nMapWidth + x].bObstacle = false;
nodes[y * nMapWidth + x].parent = nullptr;
nodes[y * nMapWidth + x].bVisited = false;
}
// Create connections - in this case nodes are on a regular grid
for (int x = 0; x < nMapWidth; x++)
for (int y = 0; y < nMapHeight; y++)
{
if(y>0)
nodes[y*nMapWidth + x].vecNeighbours.push_back(&nodes[(y - 1) * nMapWidth + (x + 0)]);
if(y<nMapHeight-1)
nodes[y*nMapWidth + x].vecNeighbours.push_back(&nodes[(y + 1) * nMapWidth + (x + 0)]);
if (x>0)
nodes[y*nMapWidth + x].vecNeighbours.push_back(&nodes[(y + 0) * nMapWidth + (x - 1)]);
if(x<nMapWidth-1)
nodes[y*nMapWidth + x].vecNeighbours.push_back(&nodes[(y + 0) * nMapWidth + (x + 1)]);
// We can also connect diagonally
/*if (y>0 && x>0)
nodes[y*nMapWidth + x].vecNeighbours.push_back(&nodes[(y - 1) * nMapWidth + (x - 1)]);
if (y<nMapHeight-1 && x>0)
nodes[y*nMapWidth + x].vecNeighbours.push_back(&nodes[(y + 1) * nMapWidth + (x - 1)]);
if (y>0 && x<nMapWidth-1)
nodes[y*nMapWidth + x].vecNeighbours.push_back(&nodes[(y - 1) * nMapWidth + (x + 1)]);
if (y<nMapHeight - 1 && x<nMapWidth-1)
nodes[y*nMapWidth + x].vecNeighbours.push_back(&nodes[(y + 1) * nMapWidth + (x + 1)]);
*/
}
// Manually positio the start and end markers so they are not nullptr
nodeStart = &nodes[(nMapHeight / 2) * nMapWidth + 1];
nodeEnd = &nodes[(nMapHeight / 2) * nMapWidth + nMapWidth-2];
return true;
}
bool Solve_AStar()
{
// Reset Navigation Graph - default all node states
for (int x = 0; x < nMapWidth; x++)
for (int y = 0; y < nMapHeight; y++)
{
nodes[y*nMapWidth + x].bVisited = false;
nodes[y*nMapWidth + x].fGlobalGoal = INFINITY;
nodes[y*nMapWidth + x].fLocalGoal = INFINITY;
nodes[y*nMapWidth + x].parent = nullptr; // No parents
}
auto distance = [](sNode* a, sNode* b) // For convenience
{
return sqrtf((a->x - b->x)*(a->x - b->x) + (a->y - b->y)*(a->y - b->y));
};
auto heuristic = [distance](sNode* a, sNode* b) // So we can experiment with heuristic
{
return distance(a, b);
};
// Setup starting conditions
sNode *nodeCurrent = nodeStart;
nodeStart->fLocalGoal = 0.0f;
nodeStart->fGlobalGoal = heuristic(nodeStart, nodeEnd);
// Add start node to not tested list - this will ensure it gets tested.
// As the algorithm progresses, newly discovered nodes get added to this
// list, and will themselves be tested later
list<sNode*> listNotTestedNodes;
listNotTestedNodes.push_back(nodeStart);
// if the not tested list contains nodes, there may be better paths
// which have not yet been explored. However, we will also stop
// searching when we reach the target - there may well be better
// paths but this one will do - it wont be the longest.
while (!listNotTestedNodes.empty() && nodeCurrent != nodeEnd)// Find absolutely shortest path // && nodeCurrent != nodeEnd)
{
// Sort Untested nodes by global goal, so lowest is first
listNotTestedNodes.sort([](const sNode* lhs, const sNode* rhs){ return lhs->fGlobalGoal < rhs->fGlobalGoal; } );
// Front of listNotTestedNodes is potentially the lowest distance node. Our
// list may also contain nodes that have been visited, so ditch these...
while(!listNotTestedNodes.empty() && listNotTestedNodes.front()->bVisited)
listNotTestedNodes.pop_front();
// ...or abort because there are no valid nodes left to test
if (listNotTestedNodes.empty())
break;
nodeCurrent = listNotTestedNodes.front();
nodeCurrent->bVisited = true; // We only explore a node once
// Check each of this node's neighbours...
for (auto nodeNeighbour : nodeCurrent->vecNeighbours)
{
// ... and only if the neighbour is not visited and is
// not an obstacle, add it to NotTested List
if (!nodeNeighbour->bVisited && nodeNeighbour->bObstacle == 0)
listNotTestedNodes.push_back(nodeNeighbour);
// Calculate the neighbours potential lowest parent distance
float fPossiblyLowerGoal = nodeCurrent->fLocalGoal + distance(nodeCurrent, nodeNeighbour);
// If choosing to path through this node is a lower distance than what
// the neighbour currently has set, update the neighbour to use this node
// as the path source, and set its distance scores as necessary
if (fPossiblyLowerGoal < nodeNeighbour->fLocalGoal)
{
nodeNeighbour->parent = nodeCurrent;
nodeNeighbour->fLocalGoal = fPossiblyLowerGoal;
// The best path length to the neighbour being tested has changed, so
// update the neighbour's score. The heuristic is used to globally bias
// the path algorithm, so it knows if its getting better or worse. At some
// point the algo will realise this path is worse and abandon it, and then go
// and search along the next best path.
nodeNeighbour->fGlobalGoal = nodeNeighbour->fLocalGoal + heuristic(nodeNeighbour, nodeEnd);
}
}
}
return true;
}
virtual bool OnUserUpdate(float fElapsedTime)
{
int nNodeSize = 9;
int nNodeBorder = 2;
// Use integer division to nicely get cursor position in node space
int nSelectedNodeX = m_mousePosX / nNodeSize;
int nSelectedNodeY = m_mousePosY / nNodeSize;
if (m_mouse[0].bReleased) // Use mouse to draw maze, shift and ctrl to place start and end
{
if(nSelectedNodeX >=0 && nSelectedNodeX < nMapWidth)
if (nSelectedNodeY >= 0 && nSelectedNodeY < nMapHeight)
{
if (m_keys[VK_SHIFT].bHeld)
nodeStart = &nodes[nSelectedNodeY * nMapWidth + nSelectedNodeX];
else if (m_keys[VK_CONTROL].bHeld)
nodeEnd = &nodes[nSelectedNodeY * nMapWidth + nSelectedNodeX];
else
nodes[nSelectedNodeY * nMapWidth + nSelectedNodeX].bObstacle = !nodes[nSelectedNodeY * nMapWidth + nSelectedNodeX].bObstacle;
Solve_AStar(); // Solve in "real-time" gives a nice effect
}
}
// Draw Connections First - lines from this nodes position to its
// connected neighbour node positions
Fill(0, 0, ScreenWidth(), ScreenHeight(), L' ');
for (int x = 0; x < nMapWidth; x++)
for (int y = 0; y < nMapHeight; y++)
{
for (auto n : nodes[y*nMapWidth + x].vecNeighbours)
{
DrawLine(x*nNodeSize + nNodeSize / 2, y*nNodeSize + nNodeSize / 2,
n->x*nNodeSize + nNodeSize / 2, n->y*nNodeSize + nNodeSize / 2, PIXEL_SOLID, FG_DARK_BLUE);
}
}
// Draw Nodes on top
for (int x = 0; x < nMapWidth; x++)
for (int y = 0; y < nMapHeight; y++)
{
Fill(x*nNodeSize + nNodeBorder, y*nNodeSize + nNodeBorder,
(x + 1)*nNodeSize - nNodeBorder, (y + 1)*nNodeSize - nNodeBorder,
PIXEL_HALF, nodes[y * nMapWidth + x].bObstacle ? FG_WHITE : FG_BLUE);
if (nodes[y * nMapWidth + x].bVisited)
Fill(x*nNodeSize + nNodeBorder, y*nNodeSize + nNodeBorder, (x + 1)*nNodeSize - nNodeBorder, (y + 1)*nNodeSize - nNodeBorder, PIXEL_SOLID, FG_BLUE);
if(&nodes[y * nMapWidth + x] == nodeStart)
Fill(x*nNodeSize + nNodeBorder, y*nNodeSize + nNodeBorder, (x + 1)*nNodeSize - nNodeBorder, (y + 1)*nNodeSize - nNodeBorder, PIXEL_SOLID, FG_GREEN);
if(&nodes[y * nMapWidth + x] == nodeEnd)
Fill(x*nNodeSize + nNodeBorder, y*nNodeSize + nNodeBorder, (x + 1)*nNodeSize - nNodeBorder, (y + 1)*nNodeSize - nNodeBorder, PIXEL_SOLID, FG_RED);
}
// Draw Path by starting ath the end, and following the parent node trail
// back to the start - the start node will not have a parent path to follow
if (nodeEnd != nullptr)
{
sNode *p = nodeEnd;
while (p->parent != nullptr)
{
DrawLine(p->x*nNodeSize + nNodeSize / 2, p->y*nNodeSize + nNodeSize / 2,
p->parent->x*nNodeSize + nNodeSize / 2, p->parent->y*nNodeSize + nNodeSize / 2, PIXEL_SOLID, FG_YELLOW);
// Set next node to this node's parent
p = p->parent;
}
}
return true;
}
};
int main()
{
OneLoneCoder_PathFinding game;
game.ConstructConsole(160, 160, 6, 6);
game.Start();
return 0;
}
metroidvania
[edit | edit source]Topology - Abilities and Upgrades - Secrets and Misc - Locks and Keys
Macro Top down design - Overworld - Area - Room
- Overworld, critical points and necessary destinations
- Area,
- Main routes, Landmarks, POI,
- Storyboard and events
- Landscapes and Lore
Game design document GDD
[edit | edit source]Top 100 games have over 90% market share
95% of indie games make very little money
Concept (marketing hook in order of difficulty) - gamestyle with gamestyle but ...
arcade, catch, platformer, endless runner, mazes, boss rush, tower defense, puzzle, side scrolling, city builder, rhythm, text, visual novel, adventure, party, shoot 'em up shmups, metrovania, bullet hell, shooting gallery, beat 'em up, walking sim on rails, card deck builder, cozy life cooking, racing, fighter, tactical turn based, real time strategy rts long term, cosy relationships, survival horror, collectothon, fps, sandbox, management simulator, trading, sports, betting, rogue, roguelite, roguelike, dungeon crawler, rpg, horror, souls, soulslite, soulslike, battle royale, arena, co-op,
Similar games - same ... look / feel / genre / scope / target avatar audience
Measurement (Scale) - how the character / enemies interact, like reach, swing, grab, etc
Prototype - ugly grey box version, mock ups, concepts, screenshots, color schemes, logo, fonts, etc
10min demo - vertical slice for funding, getting initial impressions about good bits, poor ideas, missing parts, etc i.e playtesting
Fun like overcoming obstacles challenges, progression (quests logs to do lists) and rewards, sense of wonder
Design Patterns - finite state machines (behaviors through states entities transitions), event bus singleton (manage signals from objects), entity component patterns (building blocks), Behavior Trees, Goal Action Planners, Hierarchical Tasks, Crowd Pathfinding, Collision Awareness, NPC Attacks
Game Play Loop - , extraction, looter shooter,
Mechanics
- Walls, Spikes, , etc
Story - Narrative, storyline,
Shipping
- game jam to limit scope and allow completion within 1 week with clean code
- then expand to 3,4 and 6 month cycles later as you gain experience and knowledge, later 9 or 12 months max to gain experience and stay focused
design process - Empathize: Research user needs - Define: State user needs and problems - Refine: Challenge assumptions and create ideas - Prototype: Start to create solutions - Test: Try out solutions
- Design
- Programming
- Art
- Audio
Examples in C C++
[edit | edit source]Languages in 3 Hours, [], [],
Raylib Pong Video with, Pong Tutorial C++, [],
C game template, game premake, Asteroids in C raylib, Catch items in C, [],
bullet hell, [], [],
[4], Co-ordinates, [5], [],
C++ RPG simple, C++ RPG simple, c++ dungeon crawler, [],
2D and 3D examples, Stars c++, [], [],
Car physics rally, pdf, [],
Simple AI Maze runner, [], [],
wfc in c, [],
GL_VERSION: 1.4 Mesa 7.11 GL_EXTENSIONS: GL_ARB_multisample GL_EXT_abgr GL_EXT_bgra GL_EXT_blend_color GL_EXT_blend_logic_op GL_EXT_blend_minmax GL_EXT_blend_subtract GL_EXT_copy_texture GL_EXT_polygon_offset GL_EXT_subtexture GL_EXT_texture_object GL_EXT_vertex_array GL_EXT_compiled_vertex_array GL_EXT_texture GL_EXT_texture3D GL_IBM_rasterpos_clip GL_ARB_point_parameters GL_EXT_draw_range_elements GL_EXT_packed_pixels GL_EXT_point_parameters GL_EXT_rescale_normal GL_EXT_separate_specular_color GL_EXT_texture_edge_clamp GL_SGIS_generate_mipmap GL_SGIS_texture_border_clamp GL_SGIS_texture_edge_clamp GL_SGIS_texture_lod GL_ARB_multitexture GL_IBM_multimode_draw_arrays GL_IBM_texture_mirrored_repeat GL_ARB_texture_cube_map GL_ARB_texture_env_add GL_ARB_transpose_matrix GL_EXT_blend_func_separate GL_EXT_fog_coord GL_EXT_multi_draw_arrays GL_EXT_secondary_color GL_EXT_texture_env_add GL_EXT_texture_filter_anisotropic GL_EXT_texture_lod_bias GL_INGR_blend_func_separate GL_NV_blend_square GL_NV_light_max_exponent GL_NV_texgen_reflection GL_NV_texture_env_combine4 GL_SUN_multi_draw_arrays GL_ARB_texture_border_clamp GL_ARB_texture_compression GL_EXT_framebuffer_object GL_EXT_texture_env_dot3 GL_MESA_window_pos GL_NV_packed_depth_stencil GL_NV_texture_rectangle GL_ARB_depth_texture GL_ARB_shadow GL_ARB_texture_env_combine GL_ARB_texture_env_crossbar GL_ARB_texture_env_dot3 GL_ARB_texture_mirrored_repeat GL_ARB_window_pos GL_EXT_stencil_two_side GL_EXT_texture_cube_map GL_APPLE_packed_pixels GL_APPLE_vertex_array_object GL_ARB_draw_buffers GL_ARB_fragment_program GL_ARB_vertex_program GL_ATI_draw_buffers GL_ATI_texture_env_combine3 GL_EXT_shadow_funcs GL_EXT_stencil_wrap GL_MESA_pack_invert GL_MESA_ycbcr_texture GL_NV_primitive_restart GL_ARB_fragment_program_shadow GL_ARB_half_float_pixel GL_ARB_point_sprite GL_ARB_sync GL_ARB_texture_non_power_of_two GL_ARB_vertex_buffer_object GL_OES_read_format GL_ARB_color_buffer_float GL_ARB_pixel_buffer_object GL_ARB_texture_rectangle GL_EXT_pixel_buffer_object GL_EXT_texture_rectangle GL_ARB_framebuffer_object GL_EXT_framebuffer_blit GL_EXT_framebuffer_multisample GL_EXT_packed_depth_stencil GL_ARB_vertex_array_object GL_ATI_separate_stencil GL_EXT_gpu_program_parameters GL_EXT_texture_env_combine GL_OES_EGL_image GL_ARB_copy_buffer GL_ARB_map_buffer_range GL_ARB_vertex_array_bgra GL_EXT_vertex_array_bgra GL_ARB_draw_elements_base_vertex GL_ARB_fragment_coord_conventions GL_ARB_provoking_vertex GL_ARB_sampler_objects GL_EXT_provoking_vertex GL_ARB_robustness GL_RENDERER: Gallium 0.4 on i915 (chipset: 915GM) GL_VENDOR: VMware, Inc. GLU_VERSION: 1.3 GLU_EXTENSIONS: GLU_EXT_nurbs_tessellator GLU_EXT_object_space_tess GLUT_API_VERSION: 5 GLUT_XLIB_IMPLEMENTATION: 15