diff --git a/CHANGELOG b/CHANGELOG index 47d900693..d388a678e 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -13,11 +13,13 @@ Release: raylib 1.2.2 (31 December 2014) [core] Corrected bug on input handling (keyboard and mouse) [textures] Renamed function CreateTexture() to LoadTextureFromImage() [textures] Added function ConvertToPOT() +[rlgl] Added support for color tint on models on GL 3.3+ and ES2 +[rlgl] Added support for normals on models [models] Corrected bug on DrawBillboard() [models] Corrected bug on DrawHeightmap() [models] Renamed LoadCubesmap() to LoadCubicmap() [audio] Added function LoadSoundFromWave() -[makefile] Added support for Linux compiling +[makefile] Added support for Linux and OSX compiling [stb] Updated to latest headers versions [*] Lots of tweaks around diff --git a/examples/audio_music_stream.c b/examples/audio_music_stream.c index 5fd4a0549..560347e6a 100644 --- a/examples/audio_music_stream.c +++ b/examples/audio_music_stream.c @@ -27,8 +27,8 @@ int main() PlayMusicStream("resources/audio/guitar_noodling.ogg"); // Play music stream int framesCounter = 0; - float volume = 1.0; float timePlayed = 0; + //float volume = 1.0; SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- diff --git a/examples/core_basic_window.c b/examples/core_basic_window.c index 505d8df47..c6ad8445c 100644 --- a/examples/core_basic_window.c +++ b/examples/core_basic_window.c @@ -2,6 +2,16 @@ * * raylib [core] example - Basic window * +* Welcome to raylib! +* +* To test examples, just press F6 and execute raylib_compile_execute script +* Note that compiled executable is placed in the same folder as .c file +* +* You can find all basic examples on C:\raylib\raylib\examples folder or +* raylib official webpage: www.raylib.com +* +* Enjoy using raylib. :) +* * This example has been created using raylib 1.0 (www.raylib.com) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) * diff --git a/examples/makefile b/examples/makefile index b9cdd15d5..9763b2335 100644 --- a/examples/makefile +++ b/examples/makefile @@ -26,6 +26,23 @@ # WARNING: To compile examples to HTML5, they must be redesigned to use emscripten.h and emscripten_set_main_loop() PLATFORM ?= PLATFORM_DESKTOP +# determine SUBPLATFORM 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) + SUBPLATFORM=WINDOWS + else + UNAMEOS:=$(shell uname) + ifeq ($(UNAMEOS),Linux) + SUBPLATFORM=LINUX + else + ifeq ($(UNAMEOS),Darwin) + SUBPLATFORM=OSX + endif + endif + endif +endif + # define compiler: gcc for C program, define as g++ for C++ ifeq ($(PLATFORM),PLATFORM_WEB) # define emscripten compiler @@ -44,14 +61,14 @@ ifeq ($(PLATFORM),PLATFORM_RPI) else CFLAGS = -O2 -Wall -std=c99 endif -#CFLAGSEXTRA = -Wextra -Wmissing-prototypes -Wstrict-prototypes - ifeq ($(PLATFORM),PLATFORM_WEB) CFLAGS = -O1 -Wall -std=c99 -s USE_GLFW=3 -s ASSERTIONS=1 --preload-file resources #-s ALLOW_MEMORY_GROWTH=1 # to allow memory resizing #-s TOTAL_MEMORY=16777216 # to specify heap memory size (default = 16MB) endif +#CFLAGSEXTRA = -Wextra -Wmissing-prototypes -Wstrict-prototypes + # define any directories containing required header files ifeq ($(PLATFORM),PLATFORM_RPI) INCLUDES = -I. -I../src -I/opt/vc/include -I/opt/vc/include/interface/vcos/pthreads @@ -63,28 +80,35 @@ endif ifeq ($(PLATFORM),PLATFORM_RPI) LFLAGS = -L. -L../src -L/opt/vc/lib else - LFLAGS = -L. -L../src + LFLAGS = -L. -L../src -L../external/glfw3/lib/ -I../external/openal_soft/lib/ endif # define any libraries to link into executable # if you want to link libraries (libname.so or libname.a), use the -lname +ifeq ($(PLATFORM),PLATFORM_DESKTOP) + ifeq ($(SUBPLATFORM),LINUX) + # libraries for Debian GNU/Linux desktop compiling + # requires the following packages: + # libglfw3-dev libopenal-dev libglew-dev libegl1-mesa-dev + LIBS = -lraylib -lglfw -lGLEW -lGL -lopenal + endif + ifeq ($(SUBPLATFORM),OSX) + # libraries for OS X 10.9 desktop compiling + # requires the following packages: + # libglfw3-dev libopenal-dev libglew-dev libegl1-mesa-dev + LIBS = -lraylib -lglfw -framework OpenGL -framework OpenAl -framework Cocoa + + else + # libraries for Windows desktop compiling + # NOTE: GLFW3 and OpenAL Soft libraries should be installed + LIBS = -lraylib -lglfw3 -lglew32 -lopengl32 -lopenal32 -lgdi32 + endif +endif ifeq ($(PLATFORM),PLATFORM_RPI) # libraries for Raspberry Pi compiling # NOTE: OpenAL Soft library should be installed (libopenal1 package) LIBS = -lraylib -lGLESv2 -lEGL -lpthread -lrt -lm -lbcm_host -lopenal -else -ifeq ($(PLATFORM),PLATFORM_DESKTOP_LINUX) - # libraries for Debian GNU/Linux desktop compiling - # requires the following packages: - # libglfw3-dev libopenal-dev libglew-dev libegl1-mesa-dev - LIBS = -lraylib -lglfw -lGLEW -lGL -lopenal -else - # libraries for Windows desktop compiling - # NOTE: GLFW3 and OpenAL Soft libraries should be installed - LIBS = -lraylib -lglfw3 -lglew32 -lopengl32 -lopenal32 -lgdi32 endif -endif - ifeq ($(PLATFORM),PLATFORM_WEB) LIBS = ../src/libraylib.bc endif @@ -265,20 +289,24 @@ audio_music_stream: audio_music_stream.c # clean everything clean: +ifeq ($(PLATFORM),PLATFORM_DESKTOP) + ifeq ($(SUBPLATFORM),OSX) + rm -f *.o + else + ifeq ($(SUBPLATFORM),LINUX) + find . -type f -executable -delete + rm -f *.o + else + del *.o *.exe + endif + endif +endif ifeq ($(PLATFORM),PLATFORM_RPI) - rm -f *.o -# find . -executable -delete -else -ifeq ($(PLATFORM),PLATFORM_DESKTOP_LINUX) find . -type f -executable -delete rm -f *.o -else +endif ifeq ($(PLATFORM),PLATFORM_WEB) del *.o *.html *.js -else - del *.o *.exe -endif -endif endif @echo Cleaning done diff --git a/examples/models_cubesmap.c b/examples/models_cubicmap.c similarity index 84% rename from examples/models_cubesmap.c rename to examples/models_cubicmap.c index 580d67e66..60e93c6da 100644 --- a/examples/models_cubesmap.c +++ b/examples/models_cubicmap.c @@ -1,6 +1,6 @@ /******************************************************************************************* * -* raylib [models] example - Cubesmap loading and drawing +* raylib [models] example - Cubicmap loading and drawing * * This example has been created using raylib 1.2 (www.raylib.com) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) @@ -23,11 +23,11 @@ int main() // Define the camera to look into our 3d world Camera camera = {{ 7.0, 7.0, 7.0 }, { 0.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }}; - Image img = LoadImage("resources/cubesmap.png"); // Load cubesmap image (RAM) - Texture2D texture = CreateTexture(img, false); // Convert image to texture (VRAM) - Model map = LoadCubesmap(img); // Load cubesmap model - SetModelTexture(&map, texture); // Bind texture to model - Vector3 mapPosition = { -1, 0.0, -1 }; // Set model position + Image img = LoadImage("resources/cubicmap.png"); // Load cubesmap image (RAM) + Texture2D texture = LoadTextureFromImage(img, false); // Convert image to texture (VRAM) + Model map = LoadCubicmap(img); // Load cubicmap model + SetModelTexture(&map, texture); // Bind texture to model + Vector3 mapPosition = { -1, 0.0, -1 }; // Set model position UnloadImage(img); // Unload cubesmap image from RAM, already uploaded to VRAM @@ -77,4 +77,4 @@ int main() //-------------------------------------------------------------------------------------- return 0; -} \ No newline at end of file +} diff --git a/examples/models_cubesmap.png b/examples/models_cubicmap.png similarity index 100% rename from examples/models_cubesmap.png rename to examples/models_cubicmap.png diff --git a/examples/models_heightmap.c b/examples/models_heightmap.c index 6e807b8c5..7121c2615 100644 --- a/examples/models_heightmap.c +++ b/examples/models_heightmap.c @@ -23,11 +23,11 @@ int main() // Define the camera to look into our 3d world Camera camera = {{ 10.0, 12.0, 10.0 }, { 0.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }}; - Image img = LoadImage("resources/heightmap.png"); // Load heightmap image (RAM) - Texture2D texture = CreateTexture(img, false); // Convert image to texture (VRAM) - Model map = LoadHeightmap(img, 4); // Load heightmap model - SetModelTexture(&map, texture); // Bind texture to model - Vector3 mapPosition = { -4, 0.0, -4 }; // Set model position + Image img = LoadImage("resources/heightmap.png"); // Load heightmap image (RAM) + Texture2D texture = LoadTextureFromImage(img, false); // Convert image to texture (VRAM) + Model map = LoadHeightmap(img, 4); // Load heightmap model + SetModelTexture(&map, texture); // Bind texture to model + Vector3 mapPosition = { -4, 0.0, -4 }; // Set model position UnloadImage(img); // Unload heightmap image from RAM, already uploaded to VRAM diff --git a/examples/resources/cubesmap.png b/examples/resources/cubicmap.png similarity index 100% rename from examples/resources/cubesmap.png rename to examples/resources/cubicmap.png diff --git a/examples/textures_image_loading.c b/examples/textures_image_loading.c index 179cf84c9..b7fc2cfca 100644 --- a/examples/textures_image_loading.c +++ b/examples/textures_image_loading.c @@ -24,8 +24,8 @@ int main() // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required) - Image img = LoadImage("resources/raylib_logo.png"); // Loaded in CPU memory (RAM) - Texture2D texture = CreateTexture(img, false); // Image converted to texture, GPU memory (VRAM) + Image img = LoadImage("resources/raylib_logo.png"); // Loaded in CPU memory (RAM) + Texture2D texture = LoadTextureFromImage(img, false); // Image converted to texture, GPU memory (VRAM) UnloadImage(img); // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM //--------------------------------------------------------------------------------------- diff --git a/examples/textures_mipmaps.c b/examples/textures_mipmaps.c index ee79f5133..d3ba1708f 100644 --- a/examples/textures_mipmaps.c +++ b/examples/textures_mipmaps.c @@ -27,7 +27,7 @@ int main() // with mipmaps option set to true on CreateTexture() Image image = LoadImage("resources/raylib_logo.png"); // Load image to CPU memory (RAM) - Texture2D texture = CreateTexture(image, true); // Create texture and generate mipmaps + Texture2D texture = LoadTextureFromImage(image, true); // Create texture and generate mipmaps UnloadImage(image); // Once texture has been created, we can unload image data from RAM //-------------------------------------------------------------------------------------- diff --git a/external/glfw3/lib/libglfw.3.0.dylib b/external/glfw3/lib/libglfw.3.0.dylib new file mode 100755 index 000000000..963d716d1 Binary files /dev/null and b/external/glfw3/lib/libglfw.3.0.dylib differ diff --git a/external/glfw3/lib/libglfw.3.dylib b/external/glfw3/lib/libglfw.3.dylib new file mode 120000 index 000000000..cd20112e3 --- /dev/null +++ b/external/glfw3/lib/libglfw.3.dylib @@ -0,0 +1 @@ +libglfw.3.0.dylib \ No newline at end of file diff --git a/external/glfw3/lib/libglfw.dylib b/external/glfw3/lib/libglfw.dylib new file mode 120000 index 000000000..d4bd51e18 --- /dev/null +++ b/external/glfw3/lib/libglfw.dylib @@ -0,0 +1 @@ +libglfw.3.dylib \ No newline at end of file diff --git a/src/core.c b/src/core.c index 5ff44f89c..26b1dd6d6 100644 --- a/src/core.c +++ b/src/core.c @@ -170,7 +170,6 @@ static Matrix downscaleView; // Matrix to downscale view (in case #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) || defined(PLATFORM_WEB) static const char *windowTitle; // Window text title... -static char configFlags = 0; static bool customCursor = false; // Tracks if custom cursor has been set static bool cursorOnScreen = false; // Tracks if cursor is inside client area @@ -204,6 +203,7 @@ static double updateTime, drawTime; // Time measures for update and draw static double frameTime; // Time measure for one frame static double targetTime = 0.0; // Desired time for one frame, if 0 not applied +static char configFlags = 0; static bool showLogo = false; //---------------------------------------------------------------------------------- @@ -261,6 +261,8 @@ static void CommandCallback(struct android_app *app, int32_t cmd); // // Initialize Window and Graphics Context (OpenGL) void InitWindow(int width, int height, const char *title) { + TraceLog(INFO, "Initializing raylib..."); + // Store window title (could be useful...) windowTitle = title; @@ -298,6 +300,8 @@ void InitWindow(int width, int height, const char *title) // Android activity initialization void InitWindow(int width, int height, struct android_app *state) { + TraceLog(INFO, "Initializing raylib..."); + app_dummy(); screenWidth = width; @@ -451,12 +455,6 @@ int GetScreenHeight(void) return screenHeight; } -// Get the last key pressed -int GetKeyPressed(void) -{ - return lastKeyPressed; -} - // Sets Background Color void ClearBackground(Color color) { @@ -670,6 +668,12 @@ bool IsKeyUp(int key) else return false; } +// Get the last key pressed +int GetKeyPressed(void) +{ + return lastKeyPressed; +} + // Detect if a mouse button has been pressed once bool IsMouseButtonPressed(int button) { diff --git a/src/makefile b/src/makefile index 501bd0c91..8566abe4f 100644 --- a/src/makefile +++ b/src/makefile @@ -25,6 +25,23 @@ # possible platforms: PLATFORM_DESKTOP PLATFORM_RPI PLATFORM_WEB PLATFORM ?= PLATFORM_DESKTOP +# determine SUBPLATFORM 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) + SUBPLATFORM=WINDOWS + else + UNAMEOS:=$(shell uname) + ifeq ($(UNAMEOS),Linux) + SUBPLATFORM=LINUX + else + ifeq ($(UNAMEOS),Darwin) + SUBPLATFORM=OSX + endif + endif + endif +endif + # define raylib graphics api depending on selected platform ifeq ($(PLATFORM),PLATFORM_RPI) # define raylib graphics api to use (on RPI, OpenGL ES 2.0 must be used) @@ -34,7 +51,6 @@ else GRAPHICS ?= GRAPHICS_API_OPENGL_11 #GRAPHICS = GRAPHICS_API_OPENGL_33 # Uncomment to use OpenGL 3.3 endif - ifeq ($(PLATFORM),PLATFORM_WEB) GRAPHICS = GRAPHICS_API_OPENGL_ES2 endif @@ -66,7 +82,7 @@ endif ifeq ($(PLATFORM),PLATFORM_RPI) INCLUDES = -I. -I/opt/vc/include -I/opt/vc/include/interface/vcos/pthreads else - INCLUDES = -I. + INCLUDES = -I. -I../external/glfw3/include/ -I../external/openal_soft/include/ endif # define all object files required @@ -127,19 +143,23 @@ stb_vorbis.o: stb_vorbis.c # clean everything clean: +ifeq ($(PLATFORM),PLATFORM_DESKTOP) + ifeq ($(SUBPLATFORM),OSX) + rm -f *.o libraylib.a + else + ifeq ($(SUBPLATFORM),LINUX) + find . -type f -executable -delete + rm -f *.o libraylib.a + else + del *.o libraylib.a + endif + endif +endif ifeq ($(PLATFORM),PLATFORM_RPI) rm -f *.o libraylib.a -else -ifeq ($(PLATFORM),PLATFORM_DESKTOP_LINUX) - find . -type f -executable -delete - rm -f *.o libraylib.a -else +endif ifeq ($(PLATFORM),PLATFORM_WEB) del *.o libraylib.bc -else - del *.o libraylib.a -endif -endif endif @echo Cleaning done diff --git a/src/models.c b/src/models.c index e8e4f635a..f61f79f51 100644 --- a/src/models.c +++ b/src/models.c @@ -711,7 +711,7 @@ Model LoadHeightmap(Image heightmap, float maxHeight) vData.vertices = (float *)malloc(vData.vertexCount * 3 * sizeof(float)); vData.normals = (float *)malloc(vData.vertexCount * 3 * sizeof(float)); vData.texcoords = (float *)malloc(vData.vertexCount * 2 * sizeof(float)); - vData.colors = (unsigned char *)malloc(vData.vertexCount * 4 * sizeof(unsigned char)); + vData.colors = (unsigned char *)malloc(vData.vertexCount * 4 * sizeof(unsigned char)); // Not used... int vCounter = 0; // Used to count vertices float by float int tcCounter = 0; // Used to count texcoords float by float @@ -795,9 +795,9 @@ Model LoadHeightmap(Image heightmap, float maxHeight) } // Fill color data + // NOTE: Not used any more... just one plain color defined at DrawModel() for (int i = 0; i < (4*vData.vertexCount); i++) vData.colors[i] = 255; - // NOTE: At this point we have all vertex, texcoord, normal data for the model in vData struct Model model = rlglLoadModel(vData); @@ -1071,9 +1071,10 @@ Model LoadCubicmap(Image cubesmap) vData.vertices = (float *)malloc(vData.vertexCount * 3 * sizeof(float)); vData.normals = (float *)malloc(vData.vertexCount * 3 * sizeof(float)); vData.texcoords = (float *)malloc(vData.vertexCount * 2 * sizeof(float)); - vData.colors = (unsigned char *)malloc(vData.vertexCount * 4 * sizeof(unsigned char)); + vData.colors = (unsigned char *)malloc(vData.vertexCount * 4 * sizeof(unsigned char)); // Not used... // Fill color data + // NOTE: Not used any more... just one plain color defined at DrawModel() for (int i = 0; i < (4*vData.vertexCount); i++) vData.colors[i] = 255; int fCounter = 0; @@ -1510,6 +1511,7 @@ static VertexData LoadOBJ(const char *fileName) if (numTexCoords == 0) for (int i = 0; i < (2*vData.vertexCount); i++) vData.texcoords[i] = 0.0f; // NOTE: We set all vertex colors to white + // NOTE: Not used any more... just one plain color defined at DrawModel() for (int i = 0; i < (4*vData.vertexCount); i++) vData.colors[i] = 255; // Now we can free temp mid* arrays diff --git a/src/rlgl.c b/src/rlgl.c index 6f2027c77..50ef1efd3 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -32,14 +32,21 @@ #include // Declares malloc() and free() for memory management, rand() #if defined(GRAPHICS_API_OPENGL_11) - #include // Basic OpenGL include - //#include // Basic OpenGL include (OSX) + #ifdef __APPLE__ // OpenGL include for OSX + #include + #else + #include // Basic OpenGL include + #endif #endif #if defined(GRAPHICS_API_OPENGL_33) #define GLEW_STATIC - #include // Extensions loading lib - //#include "glad.h" // TODO: Other extensions loading lib? --> REVIEW + #ifdef __APPLE__ // OpenGL include for OSX + #include + #else + #include // Extensions loading lib + //#include "glad.h" // TODO: Other extensions loading lib? --> REVIEW + #endif #endif #if defined(GRAPHICS_API_OPENGL_ES2) @@ -145,12 +152,17 @@ static VertexPositionColorBuffer triangles; // No texture support static VertexPositionColorTextureIndexBuffer quads; // Vetex-Fragment Shader Program ID -static GLuint shaderProgram; +static GLuint defaultShaderProgram, simpleShaderProgram; -// Shader program attibutes binding locations -static GLuint vertexLoc, texcoordLoc, colorLoc; -static GLuint projectionMatrixLoc, modelviewMatrixLoc; -static GLuint textureLoc; +// Default Shader program attibutes binding locations +static GLuint defaultVertexLoc, defaultTexcoordLoc, defaultColorLoc; +static GLuint defaultProjectionMatrixLoc, defaultModelviewMatrixLoc; +static GLuint defaultTextureLoc; + +// Simple Shader program attibutes binding locations +static GLuint simpleVertexLoc, simpleTexcoordLoc, simpleNormalLoc, simpleColorLoc; +static GLuint simpleProjectionMatrixLoc, simpleModelviewMatrixLoc; +static GLuint simpleTextureLoc; // Vertex Array Objects (VAO) static GLuint vaoLines, vaoTriangles, vaoQuads; @@ -189,13 +201,14 @@ unsigned int whiteTexture; // Module specific Functions Declaration //---------------------------------------------------------------------------------- #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) -static GLuint LoadDefaultShaders(void); +static GLuint LoadDefaultShader(void); +static GLuint LoadSimpleShader(void); static void InitializeBuffers(void); static void InitializeBuffersGPU(void); static void UpdateBuffers(void); -// Shader files loading (external) - Not used but useful... -static GLuint LoadShaders(char *vertexFileName, char *fragmentFileName); +// Custom shader files loading (external) +static GLuint LoadCustomShader(char *vertexFileName, char *fragmentFileName); static char *TextFileRead(char *fn); #endif @@ -836,20 +849,38 @@ void rlglInit(void) for (int i = 0; i < MATRIX_STACK_SIZE; i++) stack[i] = MatrixIdentity(); // Init default Shader (GLSL 110) -> Common for GL 3.3+ and ES2 - shaderProgram = LoadDefaultShaders(); - //shaderProgram = LoadShaders("simple150.vert", "simple150.frag"); + defaultShaderProgram = LoadDefaultShader(); + simpleShaderProgram = LoadSimpleShader(); + //customShaderProgram = LoadShaders("simple150.vert", "simple150.frag"); - // Get handles to GLSL input vars locations - vertexLoc = glGetAttribLocation(shaderProgram, "vertexPosition"); - texcoordLoc = glGetAttribLocation(shaderProgram, "vertexTexCoord"); - colorLoc = glGetAttribLocation(shaderProgram, "vertexColor"); + // Get handles to GLSL input vars locations for defaultShaderProgram + //------------------------------------------------------------------- + defaultVertexLoc = glGetAttribLocation(defaultShaderProgram, "vertexPosition"); + defaultTexcoordLoc = glGetAttribLocation(defaultShaderProgram, "vertexTexCoord"); + defaultColorLoc = glGetAttribLocation(defaultShaderProgram, "vertexColor"); // Get handles to GLSL uniform vars locations (vertex-shader) - modelviewMatrixLoc = glGetUniformLocation(shaderProgram, "modelviewMatrix"); - projectionMatrixLoc = glGetUniformLocation(shaderProgram, "projectionMatrix"); + defaultModelviewMatrixLoc = glGetUniformLocation(defaultShaderProgram, "modelviewMatrix"); + defaultProjectionMatrixLoc = glGetUniformLocation(defaultShaderProgram, "projectionMatrix"); // Get handles to GLSL uniform vars locations (fragment-shader) - textureLoc = glGetUniformLocation(shaderProgram, "texture0"); + defaultTextureLoc = glGetUniformLocation(defaultShaderProgram, "texture0"); + //-------------------------------------------------------------------- + + // Get handles to GLSL input vars locations for simpleShaderProgram + //------------------------------------------------------------------- + simpleVertexLoc = glGetAttribLocation(simpleShaderProgram, "vertexPosition"); + simpleTexcoordLoc = glGetAttribLocation(simpleShaderProgram, "vertexTexCoord"); + simpleNormalLoc = glGetAttribLocation(defaultShaderProgram, "vertexNormal"); + + // Get handles to GLSL uniform vars locations (vertex-shader) + simpleModelviewMatrixLoc = glGetUniformLocation(simpleShaderProgram, "modelviewMatrix"); + simpleProjectionMatrixLoc = glGetUniformLocation(simpleShaderProgram, "projectionMatrix"); + + // Get handles to GLSL uniform vars locations (fragment-shader) + simpleTextureLoc = glGetUniformLocation(simpleShaderProgram, "texture0"); + simpleColorLoc = glGetUniformLocation(simpleShaderProgram, "fragColor"); + //-------------------------------------------------------------------- InitializeBuffers(); // Init vertex arrays InitializeBuffersGPU(); // Init VBO and VAO @@ -914,11 +945,11 @@ void rlglClose(void) glDeleteVertexArrays(1, &vaoQuads); } - //glDetachShader(shaderProgram, v); - //glDetachShader(shaderProgram, f); + //glDetachShader(defaultShaderProgram, v); + //glDetachShader(defaultShaderProgram, f); //glDeleteShader(v); //glDeleteShader(f); - glDeleteProgram(shaderProgram); + glDeleteProgram(defaultShaderProgram); // Free vertex arrays memory free(lines.vertices); @@ -944,12 +975,15 @@ void rlglDraw(void) #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) UpdateBuffers(); - glUseProgram(shaderProgram); // Use our shader - - glUniformMatrix4fv(projectionMatrixLoc, 1, false, GetMatrixVector(projection)); - glUniformMatrix4fv(modelviewMatrixLoc, 1, false, GetMatrixVector(modelview)); - glUniform1i(textureLoc, 0); + if ((lines.vCounter > 0) || (triangles.vCounter > 0) || (quads.vCounter > 0)) + { + glUseProgram(defaultShaderProgram); // Use our shader + glUniformMatrix4fv(defaultProjectionMatrixLoc, 1, false, GetMatrixVector(projection)); + glUniformMatrix4fv(defaultModelviewMatrixLoc, 1, false, GetMatrixVector(modelview)); + glUniform1i(defaultTextureLoc, 0); + } + // NOTE: We draw in this order: triangle shapes, textured quads and lines if (triangles.vCounter > 0) @@ -963,12 +997,12 @@ void rlglDraw(void) else { glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[0]); - glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, 0, 0, 0); - glEnableVertexAttribArray(vertexLoc); + glVertexAttribPointer(defaultVertexLoc, 3, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(defaultVertexLoc); glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[1]); - glVertexAttribPointer(colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); - glEnableVertexAttribArray(colorLoc); + glVertexAttribPointer(defaultColorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); + glEnableVertexAttribArray(defaultColorLoc); } glDrawArrays(GL_TRIANGLES, 0, triangles.vCounter); @@ -991,16 +1025,16 @@ void rlglDraw(void) { // Enable vertex attributes glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[0]); - glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, 0, 0, 0); - glEnableVertexAttribArray(vertexLoc); + glVertexAttribPointer(defaultVertexLoc, 3, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(defaultVertexLoc); glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[1]); - glVertexAttribPointer(texcoordLoc, 2, GL_FLOAT, 0, 0, 0); - glEnableVertexAttribArray(texcoordLoc); + glVertexAttribPointer(defaultTexcoordLoc, 2, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(defaultTexcoordLoc); glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[2]); - glVertexAttribPointer(colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); - glEnableVertexAttribArray(colorLoc); + glVertexAttribPointer(defaultColorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); + glEnableVertexAttribArray(defaultColorLoc); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quadsBuffer[3]); } @@ -1048,12 +1082,12 @@ void rlglDraw(void) else { glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[0]); - glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, 0, 0, 0); - glEnableVertexAttribArray(vertexLoc); + glVertexAttribPointer(defaultVertexLoc, 3, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(defaultVertexLoc); glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[1]); - glVertexAttribPointer(colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); - glEnableVertexAttribArray(colorLoc); + glVertexAttribPointer(defaultColorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); + glEnableVertexAttribArray(defaultColorLoc); } glDrawArrays(GL_LINES, 0, lines.vCounter); @@ -1104,8 +1138,6 @@ void rlglDrawModel(Model model, Vector3 position, Vector3 rotation, Vector3 scal glNormalPointer(GL_FLOAT, 0, model.mesh.normals); // Pointer to normals array //glColorPointer(4, GL_UNSIGNED_BYTE, 0, model.mesh.colors); // Pointer to colors array (NOT USED) - //TraceLog(DEBUG, "Drawing model.mesh, VertexCount: %i", model.mesh.vertexCount); - rlPushMatrix(); rlTranslatef(position.x, position.y, position.z); rlScalef(scale.x, scale.y, scale.z); @@ -1127,7 +1159,7 @@ void rlglDrawModel(Model model, Vector3 position, Vector3 rotation, Vector3 scal #endif #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - glUseProgram(shaderProgram); // Use our shader + glUseProgram(simpleShaderProgram); // Use our simple shader VectorScale(&rotation, DEG2RAD); @@ -1136,37 +1168,14 @@ void rlglDrawModel(Model model, Vector3 position, Vector3 rotation, Vector3 scal Matrix modelviewworld = MatrixMultiply(transform, modelview); // NOTE: Drawing in OpenGL 3.3+, transform is passed to shader - glUniformMatrix4fv(projectionMatrixLoc, 1, false, GetMatrixVector(projection)); - glUniformMatrix4fv(modelviewMatrixLoc, 1, false, GetMatrixVector(modelviewworld)); - glUniform1i(textureLoc, 0); + glUniformMatrix4fv(simpleProjectionMatrixLoc, 1, false, GetMatrixVector(projection)); + glUniformMatrix4fv(simpleModelviewMatrixLoc, 1, false, GetMatrixVector(modelviewworld)); + glUniform1i(simpleTextureLoc, 0); - // Apply color tinting to model: 2 OPTIONS -/* - // OPTION 1 - // Update colors array (model.mesh.colors) with color - int j = 0; - for (int i = 0; i < model.mesh.vertexCount; i++) - { - model.mesh.colors[j] = color.r; - model.mesh.colors[j+1] = color.g; - model.mesh.colors[j+2] = color.b; - model.mesh.colors[j+3] = color.a; - j += 4; - } - - // Update colors buffer in CPU (using Shader) - if (vaoSupported) glBindVertexArray(model.vaoId); - GLuint colorVboId; - glGetVertexAttribIuiv(2, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &colorVboId); // NOTE: Color VBO is buffer index 2 - glBindBuffer(GL_ARRAY_BUFFER, colorVboId); - glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(unsigned char)*4*model.mesh.vertexCount, model.mesh.colors); - - // OPTION 2: Just update one uniform on fragment shader - // NOTE: It requires shader modification to add uniform (fragment shader) and create location point - //glUniform4f(fragmentUniformColorLoc, (float)color.r/255, (float)color.g/255, (float)color.b/255, (float)color.a/255); -*/ - - //TraceLog(DEBUG, "ShaderProgram: %i, VAO ID: %i, VertexCount: %i", shaderProgram, model.vaoId, model.mesh.vertexCount); + // Apply color tinting to model + // NOTE: Just update one uniform on fragment shader + float vColor[4] = { (float)color.r/255, (float)color.g/255, (float)color.b/255, (float)color.a/255 }; + glUniform4fv(simpleColorLoc, 1, vColor); if (vaoSupported) { @@ -1176,16 +1185,17 @@ void rlglDrawModel(Model model, Vector3 position, Vector3 rotation, Vector3 scal { // Bind model VBOs data glBindBuffer(GL_ARRAY_BUFFER, model.vboId[0]); - glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, 0, 0, 0); - glEnableVertexAttribArray(vertexLoc); + glVertexAttribPointer(simpleVertexLoc, 3, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(simpleVertexLoc); glBindBuffer(GL_ARRAY_BUFFER, model.vboId[1]); - glVertexAttribPointer(texcoordLoc, 2, GL_FLOAT, 0, 0, 0); - glEnableVertexAttribArray(texcoordLoc); + glVertexAttribPointer(simpleTexcoordLoc, 2, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(simpleTexcoordLoc); + // Add normals support glBindBuffer(GL_ARRAY_BUFFER, model.vboId[2]); - glVertexAttribPointer(colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); - glEnableVertexAttribArray(colorLoc); + glVertexAttribPointer(simpleNormalLoc, 3, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(simpleNormalLoc); } glBindTexture(GL_TEXTURE_2D, model.textureId); @@ -1196,6 +1206,8 @@ void rlglDrawModel(Model model, Vector3 position, Vector3 rotation, Vector3 scal if (vaoSupported) glBindVertexArray(0); // Unbind VAO else glBindBuffer(GL_ARRAY_BUFFER, 0); // Unbind VBOs + + glUseProgram(0); #endif #if defined (GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33) @@ -1360,8 +1372,7 @@ Model rlglLoadModel(VertexData mesh) model.vaoId = 0; // Vertex Array Object model.vboId[0] = 0; // Vertex position VBO model.vboId[1] = 0; // Texcoords VBO - //model.vboId[2] = 0; // Normals VBO (not used) - model.vboId[2] = 0; // Colors VBO + model.vboId[2] = 0; // Normals VBO #elif defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) model.textureId = 1; // Default whiteTexture @@ -1382,29 +1393,25 @@ Model rlglLoadModel(VertexData mesh) // Enable vertex attributes: position glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh.vertexCount, mesh.vertices, GL_STATIC_DRAW); - glEnableVertexAttribArray(vertexLoc); - glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(simpleVertexLoc); + glVertexAttribPointer(simpleVertexLoc, 3, GL_FLOAT, 0, 0, 0); // Enable vertex attributes: texcoords glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[1]); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*mesh.vertexCount, mesh.texcoords, GL_STATIC_DRAW); - glEnableVertexAttribArray(texcoordLoc); - glVertexAttribPointer(texcoordLoc, 2, GL_FLOAT, 0, 0, 0); - - // TODO: Normals support -> Lighting + glEnableVertexAttribArray(simpleTexcoordLoc); + glVertexAttribPointer(simpleTexcoordLoc, 2, GL_FLOAT, 0, 0, 0); // Enable vertex attributes: normals - //glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[2]); - //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh.vertexCount, mesh.normals, GL_STATIC_DRAW); - //glEnableVertexAttribArray(normalLoc); - //glVertexAttribPointer(normalLoc, 3, GL_FLOAT, 0, 0, 0); - - // Enable vertex attributes: colors glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[2]); - glBufferData(GL_ARRAY_BUFFER, sizeof(unsigned char)*4*mesh.vertexCount, mesh.colors, GL_STATIC_DRAW); - glEnableVertexAttribArray(colorLoc); - glVertexAttribPointer(colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); + glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh.vertexCount, mesh.normals, GL_STATIC_DRAW); + glEnableVertexAttribArray(simpleNormalLoc); + glVertexAttribPointer(simpleNormalLoc, 3, GL_FLOAT, 0, 0, 0); + model.vboId[0] = vertexBuffer[0]; // Vertex position VBO + model.vboId[1] = vertexBuffer[1]; // Texcoords VBO + model.vboId[2] = vertexBuffer[2]; // Normals VBO + if (vaoSupported) { if (vaoModel > 0) @@ -1416,11 +1423,6 @@ Model rlglLoadModel(VertexData mesh) } else { - model.vboId[0] = vertexBuffer[0]; // Vertex position VBO - model.vboId[1] = vertexBuffer[1]; // Texcoords VBO - //model.vboId[2] = 0; // Normals VBO (not used) - model.vboId[2] = vertexBuffer[2]; // Colors VBO - TraceLog(INFO, "[VBO ID %i][VBO ID %i][VBO ID %i] Model uploaded successfully to VRAM (GPU)", model.vboId[0], model.vboId[1], model.vboId[2]); } #endif @@ -1551,8 +1553,9 @@ void PrintModelviewMatrix() #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) -// Load Shaders (Vertex and Fragment) -static GLuint LoadDefaultShaders(void) +// Load Shader (Vertex and Fragment) +// NOTE: This shader program is used only for batch buffers (lines, triangles, quads) +static GLuint LoadDefaultShader(void) { // NOTE: Shaders are written using GLSL 110 (desktop), that is equivalent to GLSL 100 on ES2 @@ -1650,9 +1653,106 @@ static GLuint LoadDefaultShaders(void) return program; } +// Load Simple Shader (Vertex and Fragment) +// NOTE: This shader program is used to render models +static GLuint LoadSimpleShader(void) +{ + // NOTE: Shaders are written using GLSL 110 (desktop), that is equivalent to GLSL 100 on ES2 -// Load Shaders -static GLuint LoadShaders(char *vertexFileName, char *fragmentFileName) + // Vertex shader directly defined, no external file required +#if defined(GRAPHICS_API_OPENGL_33) + char vShaderStr[] = " #version 110 \n" // NOTE: Equivalent to version 100 on ES2 +#elif defined(GRAPHICS_API_OPENGL_ES2) + char vShaderStr[] = " #version 100 \n" // NOTE: Must be defined this way! 110 doesn't work! +#endif + "uniform mat4 projectionMatrix; \n" + "uniform mat4 modelviewMatrix; \n" + "attribute vec3 vertexPosition; \n" + "attribute vec2 vertexTexCoord; \n" + "attribute vec3 vertexNormal; \n" + "varying vec2 fragTexCoord; \n" + "void main() \n" + "{ \n" + " fragTexCoord = vertexTexCoord; \n" + " gl_Position = projectionMatrix * modelviewMatrix * vec4(vertexPosition, 1.0); \n" + "} \n"; + + // Fragment shader directly defined, no external file required +#if defined(GRAPHICS_API_OPENGL_33) + char fShaderStr[] = " #version 110 \n" // NOTE: Equivalent to version 100 on ES2 +#elif defined(GRAPHICS_API_OPENGL_ES2) + char fShaderStr[] = " #version 100 \n" // NOTE: Must be defined this way! 110 doesn't work! + "precision mediump float; \n" // WebGL, required for emscripten +#endif + "uniform sampler2D texture0; \n" + "varying vec2 fragTexCoord; \n" + "uniform vec4 fragColor; \n" + "void main() \n" + "{ \n" + " gl_FragColor = texture2D(texture0, fragTexCoord) * fragColor; \n" + "} \n"; + + GLuint program; + GLuint vertexShader; + GLuint fragmentShader; + + vertexShader = glCreateShader(GL_VERTEX_SHADER); + fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); + + const char *pvs = vShaderStr; + const char *pfs = fShaderStr; + + glShaderSource(vertexShader, 1, &pvs, NULL); + glShaderSource(fragmentShader, 1, &pfs, NULL); + + GLint success = 0; + + glCompileShader(vertexShader); + + glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); + + if (success != GL_TRUE) TraceLog(WARNING, "[VSHDR ID %i] Failed to compile simple vertex shader...", vertexShader); + else TraceLog(INFO, "[VSHDR ID %i] Simple vertex shader compiled successfully", vertexShader); + + glCompileShader(fragmentShader); + + glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); + + if (success != GL_TRUE) TraceLog(WARNING, "[FSHDR ID %i] Failed to compile simple fragment shader...", fragmentShader); + else TraceLog(INFO, "[FSHDR ID %i] Simple fragment shader compiled successfully", fragmentShader); + + program = glCreateProgram(); + + glAttachShader(program, vertexShader); + glAttachShader(program, fragmentShader); + + glLinkProgram(program); + + glGetProgramiv(program, GL_LINK_STATUS, &success); + + if (success == GL_FALSE) + { + int maxLength; + int length; + + glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength); + + char log[maxLength]; + + glGetProgramInfoLog(program, maxLength, &length, log); + + TraceLog(INFO, "Shader program fail log: %s", log); + } + else TraceLog(INFO, "[SHDR ID %i] Simple shader program loaded successfully", program); + + glDeleteShader(vertexShader); + glDeleteShader(fragmentShader); + + return program; +} + +// Load shaders from text files +static GLuint LoadCustomShader(char *vertexFileName, char *fragmentFileName) { // Shaders loading from external text file char *vShaderStr = TextFileRead(vertexFileName); @@ -1686,6 +1786,9 @@ static GLuint LoadShaders(char *vertexFileName, char *fragmentFileName) glDeleteShader(vertexShader); glDeleteShader(fragmentShader); + + free(vShaderStr); + free(fShaderStr); TraceLog(INFO, "[SHDR ID %i] Shader program loaded successfully", program); @@ -1693,6 +1796,7 @@ static GLuint LoadShaders(char *vertexFileName, char *fragmentFileName) } // Read shader text file +// NOTE: text chars array should be freed manually static char *TextFileRead(char *fileName) { FILE *textFile; @@ -1801,14 +1905,14 @@ static void InitializeBuffersGPU(void) // Lines - Vertex positions buffer binding and attributes enable glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*2*MAX_LINES_BATCH, lines.vertices, GL_DYNAMIC_DRAW); - glEnableVertexAttribArray(vertexLoc); - glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(defaultVertexLoc); + glVertexAttribPointer(defaultVertexLoc, 3, GL_FLOAT, 0, 0, 0); // Lines - colors buffer glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[1]); glBufferData(GL_ARRAY_BUFFER, sizeof(unsigned char)*4*2*MAX_LINES_BATCH, lines.colors, GL_DYNAMIC_DRAW); - glEnableVertexAttribArray(colorLoc); - glVertexAttribPointer(colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); + glEnableVertexAttribArray(defaultColorLoc); + glVertexAttribPointer(defaultColorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); if (vaoSupported) TraceLog(INFO, "[VAO ID %i] Lines VAO initialized successfully", vaoLines); else TraceLog(INFO, "[VBO ID %i][VBO ID %i] Lines VBOs initialized successfully", linesBuffer[0], linesBuffer[1]); @@ -1827,13 +1931,13 @@ static void InitializeBuffersGPU(void) // Enable vertex attributes glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*3*MAX_TRIANGLES_BATCH, triangles.vertices, GL_DYNAMIC_DRAW); - glEnableVertexAttribArray(vertexLoc); - glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(defaultVertexLoc); + glVertexAttribPointer(defaultVertexLoc, 3, GL_FLOAT, 0, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[1]); glBufferData(GL_ARRAY_BUFFER, sizeof(unsigned char)*4*3*MAX_TRIANGLES_BATCH, triangles.colors, GL_DYNAMIC_DRAW); - glEnableVertexAttribArray(colorLoc); - glVertexAttribPointer(colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); + glEnableVertexAttribArray(defaultColorLoc); + glVertexAttribPointer(defaultColorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); if (vaoSupported) TraceLog(INFO, "[VAO ID %i] Triangles VAO initialized successfully", vaoTriangles); else TraceLog(INFO, "[VBO ID %i][VBO ID %i] Triangles VBOs initialized successfully", trianglesBuffer[0], trianglesBuffer[1]); @@ -1852,18 +1956,18 @@ static void InitializeBuffersGPU(void) // Enable vertex attributes glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*4*MAX_QUADS_BATCH, quads.vertices, GL_DYNAMIC_DRAW); - glEnableVertexAttribArray(vertexLoc); - glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(defaultVertexLoc); + glVertexAttribPointer(defaultVertexLoc, 3, GL_FLOAT, 0, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[1]); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*4*MAX_QUADS_BATCH, quads.texcoords, GL_DYNAMIC_DRAW); - glEnableVertexAttribArray(texcoordLoc); - glVertexAttribPointer(texcoordLoc, 2, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(defaultTexcoordLoc); + glVertexAttribPointer(defaultTexcoordLoc, 2, GL_FLOAT, 0, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[2]); glBufferData(GL_ARRAY_BUFFER, sizeof(unsigned char)*4*4*MAX_QUADS_BATCH, quads.colors, GL_DYNAMIC_DRAW); - glEnableVertexAttribArray(colorLoc); - glVertexAttribPointer(colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); + glEnableVertexAttribArray(defaultColorLoc); + glVertexAttribPointer(defaultColorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); // Fill index buffer glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quadsBuffer[3]); @@ -1885,59 +1989,65 @@ static void InitializeBuffersGPU(void) // TODO: If no data changed on the CPU arrays --> No need to update GPU arrays every frame! static void UpdateBuffers(void) { - // Activate Lines VAO - if (vaoSupported) glBindVertexArray(vaoLines); - - // Lines - vertex positions buffer - glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[0]); - //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*2*MAX_LINES_BATCH, lines.vertices, GL_DYNAMIC_DRAW); - glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*3*lines.vCounter, lines.vertices); // target - offset (in bytes) - size (in bytes) - data pointer - - // Lines - colors buffer - glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[1]); - //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*2*MAX_LINES_BATCH, lines.colors, GL_DYNAMIC_DRAW); - glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(unsigned char)*4*lines.cCounter, lines.colors); + if (lines.vCounter > 0) + { + // Activate Lines VAO + if (vaoSupported) glBindVertexArray(vaoLines); + + // Lines - vertex positions buffer + glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[0]); + //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*2*MAX_LINES_BATCH, lines.vertices, GL_DYNAMIC_DRAW); + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*3*lines.vCounter, lines.vertices); // target - offset (in bytes) - size (in bytes) - data pointer + // Lines - colors buffer + glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[1]); + //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*2*MAX_LINES_BATCH, lines.colors, GL_DYNAMIC_DRAW); + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(unsigned char)*4*lines.cCounter, lines.colors); + } //-------------------------------------------------------------- - // Activate Triangles VAO - if (vaoSupported) glBindVertexArray(vaoTriangles); - - // Triangles - vertex positions buffer - glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[0]); - //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*3*MAX_TRIANGLES_BATCH, triangles.vertices, GL_DYNAMIC_DRAW); - glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*3*triangles.vCounter, triangles.vertices); - - // Triangles - colors buffer - glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[1]); - //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*3*MAX_TRIANGLES_BATCH, triangles.colors, GL_DYNAMIC_DRAW); - glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(unsigned char)*4*triangles.cCounter, triangles.colors); + if (triangles.vCounter > 0) + { + // Activate Triangles VAO + if (vaoSupported) glBindVertexArray(vaoTriangles); + + // Triangles - vertex positions buffer + glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[0]); + //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*3*MAX_TRIANGLES_BATCH, triangles.vertices, GL_DYNAMIC_DRAW); + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*3*triangles.vCounter, triangles.vertices); + // Triangles - colors buffer + glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[1]); + //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*3*MAX_TRIANGLES_BATCH, triangles.colors, GL_DYNAMIC_DRAW); + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(unsigned char)*4*triangles.cCounter, triangles.colors); + } //-------------------------------------------------------------- - // Activate Quads VAO - if (vaoSupported) glBindVertexArray(vaoQuads); + if (quads.vCounter > 0) + { + // Activate Quads VAO + if (vaoSupported) glBindVertexArray(vaoQuads); - // Quads - vertex positions buffer - glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[0]); - //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*4*MAX_QUADS_BATCH, quads.vertices, GL_DYNAMIC_DRAW); - glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*3*quads.vCounter, quads.vertices); + // Quads - vertex positions buffer + glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[0]); + //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*4*MAX_QUADS_BATCH, quads.vertices, GL_DYNAMIC_DRAW); + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*3*quads.vCounter, quads.vertices); - // Quads - texture coordinates buffer - glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[1]); - //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*4*MAX_QUADS_BATCH, quads.texcoords, GL_DYNAMIC_DRAW); - glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*2*quads.vCounter, quads.texcoords); + // Quads - texture coordinates buffer + glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[1]); + //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*4*MAX_QUADS_BATCH, quads.texcoords, GL_DYNAMIC_DRAW); + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*2*quads.vCounter, quads.texcoords); - // Quads - colors buffer - glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[2]); - //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*4*MAX_QUADS_BATCH, quads.colors, GL_DYNAMIC_DRAW); - glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(unsigned char)*4*quads.vCounter, quads.colors); - - // Another option would be using buffer mapping... - //triangles.vertices = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE); - // Now we can modify vertices - //glUnmapBuffer(GL_ARRAY_BUFFER); + // Quads - colors buffer + glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[2]); + //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*4*MAX_QUADS_BATCH, quads.colors, GL_DYNAMIC_DRAW); + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(unsigned char)*4*quads.vCounter, quads.colors); + // Another option would be using buffer mapping... + //triangles.vertices = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE); + // Now we can modify vertices + //glUnmapBuffer(GL_ARRAY_BUFFER); + } //-------------------------------------------------------------- // Unbind the current VAO diff --git a/src/rlgl.h b/src/rlgl.h index 5fa9c4862..b42b388b0 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -5,7 +5,7 @@ * raylib now uses OpenGL 1.1 style functions (rlVertex) that are mapped to selected OpenGL version: * OpenGL 1.1 - Direct map rl* -> gl* * OpenGL 3.3+ - Vertex data is stored in VAOs, call rlglDraw() to render -* OpenGL ES 2 - Same behaviour as OpenGL 3.3+ +* OpenGL ES 2 - Vertex data is stored in VBOs or VAOs (when available), call rlglDraw() to render * * Copyright (c) 2014 Ramon Santamaria (Ray San - raysan@raysanweb.com) * diff --git a/src_android/jni/Android.mk b/src_android/jni/Android.mk index 56316b174..73a2cc7f0 100644 --- a/src_android/jni/Android.mk +++ b/src_android/jni/Android.mk @@ -40,7 +40,6 @@ LOCAL_SRC_FILES :=\ ../../src/core.c \ ../../src/rlgl.c \ ../../src/raymath.c \ - ../../src/stb_image.c \ ../../src/textures.c \ ../../src/text.c \ ../../src/shapes.c \