Merge pull request #14 from raysan5/master

Integrate master into develop branch
This commit is contained in:
Ray 2015-01-08 20:32:30 +01:00
commit e37ef10595
19 changed files with 406 additions and 229 deletions

View file

@ -13,11 +13,13 @@ Release: raylib 1.2.2 (31 December 2014)
[core] Corrected bug on input handling (keyboard and mouse) [core] Corrected bug on input handling (keyboard and mouse)
[textures] Renamed function CreateTexture() to LoadTextureFromImage() [textures] Renamed function CreateTexture() to LoadTextureFromImage()
[textures] Added function ConvertToPOT() [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 DrawBillboard()
[models] Corrected bug on DrawHeightmap() [models] Corrected bug on DrawHeightmap()
[models] Renamed LoadCubesmap() to LoadCubicmap() [models] Renamed LoadCubesmap() to LoadCubicmap()
[audio] Added function LoadSoundFromWave() [audio] Added function LoadSoundFromWave()
[makefile] Added support for Linux compiling [makefile] Added support for Linux and OSX compiling
[stb] Updated to latest headers versions [stb] Updated to latest headers versions
[*] Lots of tweaks around [*] Lots of tweaks around

View file

@ -27,8 +27,8 @@ int main()
PlayMusicStream("resources/audio/guitar_noodling.ogg"); // Play music stream PlayMusicStream("resources/audio/guitar_noodling.ogg"); // Play music stream
int framesCounter = 0; int framesCounter = 0;
float volume = 1.0;
float timePlayed = 0; float timePlayed = 0;
//float volume = 1.0;
SetTargetFPS(60); // Set our game to run at 60 frames-per-second SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------

View file

@ -2,6 +2,16 @@
* *
* raylib [core] example - Basic window * 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) * 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) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* *

View file

@ -26,6 +26,23 @@
# WARNING: To compile examples to HTML5, they must be redesigned to use emscripten.h and emscripten_set_main_loop() # WARNING: To compile examples to HTML5, they must be redesigned to use emscripten.h and emscripten_set_main_loop()
PLATFORM ?= PLATFORM_DESKTOP 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++ # define compiler: gcc for C program, define as g++ for C++
ifeq ($(PLATFORM),PLATFORM_WEB) ifeq ($(PLATFORM),PLATFORM_WEB)
# define emscripten compiler # define emscripten compiler
@ -44,14 +61,14 @@ ifeq ($(PLATFORM),PLATFORM_RPI)
else else
CFLAGS = -O2 -Wall -std=c99 CFLAGS = -O2 -Wall -std=c99
endif endif
#CFLAGSEXTRA = -Wextra -Wmissing-prototypes -Wstrict-prototypes
ifeq ($(PLATFORM),PLATFORM_WEB) ifeq ($(PLATFORM),PLATFORM_WEB)
CFLAGS = -O1 -Wall -std=c99 -s USE_GLFW=3 -s ASSERTIONS=1 --preload-file resources 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 ALLOW_MEMORY_GROWTH=1 # to allow memory resizing
#-s TOTAL_MEMORY=16777216 # to specify heap memory size (default = 16MB) #-s TOTAL_MEMORY=16777216 # to specify heap memory size (default = 16MB)
endif endif
#CFLAGSEXTRA = -Wextra -Wmissing-prototypes -Wstrict-prototypes
# define any directories containing required header files # define any directories containing required header files
ifeq ($(PLATFORM),PLATFORM_RPI) ifeq ($(PLATFORM),PLATFORM_RPI)
INCLUDES = -I. -I../src -I/opt/vc/include -I/opt/vc/include/interface/vcos/pthreads INCLUDES = -I. -I../src -I/opt/vc/include -I/opt/vc/include/interface/vcos/pthreads
@ -63,28 +80,35 @@ endif
ifeq ($(PLATFORM),PLATFORM_RPI) ifeq ($(PLATFORM),PLATFORM_RPI)
LFLAGS = -L. -L../src -L/opt/vc/lib LFLAGS = -L. -L../src -L/opt/vc/lib
else else
LFLAGS = -L. -L../src LFLAGS = -L. -L../src -L../external/glfw3/lib/ -I../external/openal_soft/lib/
endif endif
# define any libraries to link into executable # define any libraries to link into executable
# if you want to link libraries (libname.so or libname.a), use the -lname # 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) ifeq ($(PLATFORM),PLATFORM_RPI)
# libraries for Raspberry Pi compiling # libraries for Raspberry Pi compiling
# NOTE: OpenAL Soft library should be installed (libopenal1 package) # NOTE: OpenAL Soft library should be installed (libopenal1 package)
LIBS = -lraylib -lGLESv2 -lEGL -lpthread -lrt -lm -lbcm_host -lopenal 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
endif
ifeq ($(PLATFORM),PLATFORM_WEB) ifeq ($(PLATFORM),PLATFORM_WEB)
LIBS = ../src/libraylib.bc LIBS = ../src/libraylib.bc
endif endif
@ -265,20 +289,24 @@ audio_music_stream: audio_music_stream.c
# clean everything # clean everything
clean: 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) ifeq ($(PLATFORM),PLATFORM_RPI)
rm -f *.o
# find . -executable -delete
else
ifeq ($(PLATFORM),PLATFORM_DESKTOP_LINUX)
find . -type f -executable -delete find . -type f -executable -delete
rm -f *.o rm -f *.o
else endif
ifeq ($(PLATFORM),PLATFORM_WEB) ifeq ($(PLATFORM),PLATFORM_WEB)
del *.o *.html *.js del *.o *.html *.js
else
del *.o *.exe
endif
endif
endif endif
@echo Cleaning done @echo Cleaning done

View file

@ -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) * 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) * 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 // 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 }}; 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) Image img = LoadImage("resources/cubicmap.png"); // Load cubesmap image (RAM)
Texture2D texture = CreateTexture(img, false); // Convert image to texture (VRAM) Texture2D texture = LoadTextureFromImage(img, false); // Convert image to texture (VRAM)
Model map = LoadCubesmap(img); // Load cubesmap model Model map = LoadCubicmap(img); // Load cubicmap model
SetModelTexture(&map, texture); // Bind texture to model SetModelTexture(&map, texture); // Bind texture to model
Vector3 mapPosition = { -1, 0.0, -1 }; // Set model position Vector3 mapPosition = { -1, 0.0, -1 }; // Set model position
UnloadImage(img); // Unload cubesmap image from RAM, already uploaded to VRAM UnloadImage(img); // Unload cubesmap image from RAM, already uploaded to VRAM
@ -77,4 +77,4 @@ int main()
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
return 0; return 0;
} }

View file

Before

Width:  |  Height:  |  Size: 27 KiB

After

Width:  |  Height:  |  Size: 27 KiB

Before After
Before After

View file

@ -23,11 +23,11 @@ int main()
// Define the camera to look into our 3d world // 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 }}; 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) Image img = LoadImage("resources/heightmap.png"); // Load heightmap image (RAM)
Texture2D texture = CreateTexture(img, false); // Convert image to texture (VRAM) Texture2D texture = LoadTextureFromImage(img, false); // Convert image to texture (VRAM)
Model map = LoadHeightmap(img, 4); // Load heightmap model Model map = LoadHeightmap(img, 4); // Load heightmap model
SetModelTexture(&map, texture); // Bind texture to model SetModelTexture(&map, texture); // Bind texture to model
Vector3 mapPosition = { -4, 0.0, -4 }; // Set model position Vector3 mapPosition = { -4, 0.0, -4 }; // Set model position
UnloadImage(img); // Unload heightmap image from RAM, already uploaded to VRAM UnloadImage(img); // Unload heightmap image from RAM, already uploaded to VRAM

View file

Before

Width:  |  Height:  |  Size: 173 B

After

Width:  |  Height:  |  Size: 173 B

Before After
Before After

View file

@ -24,8 +24,8 @@ int main()
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required) // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
Image img = LoadImage("resources/raylib_logo.png"); // Loaded in CPU memory (RAM) Image img = LoadImage("resources/raylib_logo.png"); // Loaded in CPU memory (RAM)
Texture2D texture = CreateTexture(img, false); // Image converted to texture, GPU memory (VRAM) 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 UnloadImage(img); // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM
//--------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------

View file

@ -27,7 +27,7 @@ int main()
// with mipmaps option set to true on CreateTexture() // with mipmaps option set to true on CreateTexture()
Image image = LoadImage("resources/raylib_logo.png"); // Load image to CPU memory (RAM) 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 UnloadImage(image); // Once texture has been created, we can unload image data from RAM
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------

BIN
external/glfw3/lib/libglfw.3.0.dylib vendored Executable file

Binary file not shown.

1
external/glfw3/lib/libglfw.3.dylib vendored Symbolic link
View file

@ -0,0 +1 @@
libglfw.3.0.dylib

1
external/glfw3/lib/libglfw.dylib vendored Symbolic link
View file

@ -0,0 +1 @@
libglfw.3.dylib

View file

@ -170,7 +170,6 @@ static Matrix downscaleView; // Matrix to downscale view (in case
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) || defined(PLATFORM_WEB) #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) || defined(PLATFORM_WEB)
static const char *windowTitle; // Window text title... static const char *windowTitle; // Window text title...
static char configFlags = 0;
static bool customCursor = false; // Tracks if custom cursor has been set static bool customCursor = false; // Tracks if custom cursor has been set
static bool cursorOnScreen = false; // Tracks if cursor is inside client area 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 frameTime; // Time measure for one frame
static double targetTime = 0.0; // Desired time for one frame, if 0 not applied static double targetTime = 0.0; // Desired time for one frame, if 0 not applied
static char configFlags = 0;
static bool showLogo = false; static bool showLogo = false;
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -261,6 +261,8 @@ static void CommandCallback(struct android_app *app, int32_t cmd); //
// Initialize Window and Graphics Context (OpenGL) // Initialize Window and Graphics Context (OpenGL)
void InitWindow(int width, int height, const char *title) void InitWindow(int width, int height, const char *title)
{ {
TraceLog(INFO, "Initializing raylib...");
// Store window title (could be useful...) // Store window title (could be useful...)
windowTitle = title; windowTitle = title;
@ -298,6 +300,8 @@ void InitWindow(int width, int height, const char *title)
// Android activity initialization // Android activity initialization
void InitWindow(int width, int height, struct android_app *state) void InitWindow(int width, int height, struct android_app *state)
{ {
TraceLog(INFO, "Initializing raylib...");
app_dummy(); app_dummy();
screenWidth = width; screenWidth = width;
@ -451,12 +455,6 @@ int GetScreenHeight(void)
return screenHeight; return screenHeight;
} }
// Get the last key pressed
int GetKeyPressed(void)
{
return lastKeyPressed;
}
// Sets Background Color // Sets Background Color
void ClearBackground(Color color) void ClearBackground(Color color)
{ {
@ -670,6 +668,12 @@ bool IsKeyUp(int key)
else return false; else return false;
} }
// Get the last key pressed
int GetKeyPressed(void)
{
return lastKeyPressed;
}
// Detect if a mouse button has been pressed once // Detect if a mouse button has been pressed once
bool IsMouseButtonPressed(int button) bool IsMouseButtonPressed(int button)
{ {

View file

@ -25,6 +25,23 @@
# possible platforms: PLATFORM_DESKTOP PLATFORM_RPI PLATFORM_WEB # possible platforms: PLATFORM_DESKTOP PLATFORM_RPI PLATFORM_WEB
PLATFORM ?= PLATFORM_DESKTOP 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 # define raylib graphics api depending on selected platform
ifeq ($(PLATFORM),PLATFORM_RPI) ifeq ($(PLATFORM),PLATFORM_RPI)
# define raylib graphics api to use (on RPI, OpenGL ES 2.0 must be used) # 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_11
#GRAPHICS = GRAPHICS_API_OPENGL_33 # Uncomment to use OpenGL 3.3 #GRAPHICS = GRAPHICS_API_OPENGL_33 # Uncomment to use OpenGL 3.3
endif endif
ifeq ($(PLATFORM),PLATFORM_WEB) ifeq ($(PLATFORM),PLATFORM_WEB)
GRAPHICS = GRAPHICS_API_OPENGL_ES2 GRAPHICS = GRAPHICS_API_OPENGL_ES2
endif endif
@ -66,7 +82,7 @@ endif
ifeq ($(PLATFORM),PLATFORM_RPI) ifeq ($(PLATFORM),PLATFORM_RPI)
INCLUDES = -I. -I/opt/vc/include -I/opt/vc/include/interface/vcos/pthreads INCLUDES = -I. -I/opt/vc/include -I/opt/vc/include/interface/vcos/pthreads
else else
INCLUDES = -I. INCLUDES = -I. -I../external/glfw3/include/ -I../external/openal_soft/include/
endif endif
# define all object files required # define all object files required
@ -127,19 +143,23 @@ stb_vorbis.o: stb_vorbis.c
# clean everything # clean everything
clean: 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) ifeq ($(PLATFORM),PLATFORM_RPI)
rm -f *.o libraylib.a rm -f *.o libraylib.a
else endif
ifeq ($(PLATFORM),PLATFORM_DESKTOP_LINUX)
find . -type f -executable -delete
rm -f *.o libraylib.a
else
ifeq ($(PLATFORM),PLATFORM_WEB) ifeq ($(PLATFORM),PLATFORM_WEB)
del *.o libraylib.bc del *.o libraylib.bc
else
del *.o libraylib.a
endif
endif
endif endif
@echo Cleaning done @echo Cleaning done

View file

@ -711,7 +711,7 @@ Model LoadHeightmap(Image heightmap, float maxHeight)
vData.vertices = (float *)malloc(vData.vertexCount * 3 * sizeof(float)); vData.vertices = (float *)malloc(vData.vertexCount * 3 * sizeof(float));
vData.normals = (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.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 vCounter = 0; // Used to count vertices float by float
int tcCounter = 0; // Used to count texcoords 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 // 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; 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 // NOTE: At this point we have all vertex, texcoord, normal data for the model in vData struct
Model model = rlglLoadModel(vData); Model model = rlglLoadModel(vData);
@ -1071,9 +1071,10 @@ Model LoadCubicmap(Image cubesmap)
vData.vertices = (float *)malloc(vData.vertexCount * 3 * sizeof(float)); vData.vertices = (float *)malloc(vData.vertexCount * 3 * sizeof(float));
vData.normals = (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.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 // 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; for (int i = 0; i < (4*vData.vertexCount); i++) vData.colors[i] = 255;
int fCounter = 0; 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; 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: 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; for (int i = 0; i < (4*vData.vertexCount); i++) vData.colors[i] = 255;
// Now we can free temp mid* arrays // Now we can free temp mid* arrays

View file

@ -32,14 +32,21 @@
#include <stdlib.h> // Declares malloc() and free() for memory management, rand() #include <stdlib.h> // Declares malloc() and free() for memory management, rand()
#if defined(GRAPHICS_API_OPENGL_11) #if defined(GRAPHICS_API_OPENGL_11)
#include <GL/gl.h> // Basic OpenGL include #ifdef __APPLE__ // OpenGL include for OSX
//#include <OpenGL/gl.h> // Basic OpenGL include (OSX) #include <OpenGL/gl.h>
#else
#include <GL/gl.h> // Basic OpenGL include
#endif
#endif #endif
#if defined(GRAPHICS_API_OPENGL_33) #if defined(GRAPHICS_API_OPENGL_33)
#define GLEW_STATIC #define GLEW_STATIC
#include <GL/glew.h> // Extensions loading lib #ifdef __APPLE__ // OpenGL include for OSX
//#include "glad.h" // TODO: Other extensions loading lib? --> REVIEW #include <OpenGL/gl3.h>
#else
#include <GL/glew.h> // Extensions loading lib
//#include "glad.h" // TODO: Other extensions loading lib? --> REVIEW
#endif
#endif #endif
#if defined(GRAPHICS_API_OPENGL_ES2) #if defined(GRAPHICS_API_OPENGL_ES2)
@ -145,12 +152,17 @@ static VertexPositionColorBuffer triangles; // No texture support
static VertexPositionColorTextureIndexBuffer quads; static VertexPositionColorTextureIndexBuffer quads;
// Vetex-Fragment Shader Program ID // Vetex-Fragment Shader Program ID
static GLuint shaderProgram; static GLuint defaultShaderProgram, simpleShaderProgram;
// Shader program attibutes binding locations // Default Shader program attibutes binding locations
static GLuint vertexLoc, texcoordLoc, colorLoc; static GLuint defaultVertexLoc, defaultTexcoordLoc, defaultColorLoc;
static GLuint projectionMatrixLoc, modelviewMatrixLoc; static GLuint defaultProjectionMatrixLoc, defaultModelviewMatrixLoc;
static GLuint textureLoc; 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) // Vertex Array Objects (VAO)
static GLuint vaoLines, vaoTriangles, vaoQuads; static GLuint vaoLines, vaoTriangles, vaoQuads;
@ -189,13 +201,14 @@ unsigned int whiteTexture;
// Module specific Functions Declaration // Module specific Functions Declaration
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) #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 InitializeBuffers(void);
static void InitializeBuffersGPU(void); static void InitializeBuffersGPU(void);
static void UpdateBuffers(void); static void UpdateBuffers(void);
// Shader files loading (external) - Not used but useful... // Custom shader files loading (external)
static GLuint LoadShaders(char *vertexFileName, char *fragmentFileName); static GLuint LoadCustomShader(char *vertexFileName, char *fragmentFileName);
static char *TextFileRead(char *fn); static char *TextFileRead(char *fn);
#endif #endif
@ -836,20 +849,38 @@ void rlglInit(void)
for (int i = 0; i < MATRIX_STACK_SIZE; i++) stack[i] = MatrixIdentity(); for (int i = 0; i < MATRIX_STACK_SIZE; i++) stack[i] = MatrixIdentity();
// Init default Shader (GLSL 110) -> Common for GL 3.3+ and ES2 // Init default Shader (GLSL 110) -> Common for GL 3.3+ and ES2
shaderProgram = LoadDefaultShaders(); defaultShaderProgram = LoadDefaultShader();
//shaderProgram = LoadShaders("simple150.vert", "simple150.frag"); simpleShaderProgram = LoadSimpleShader();
//customShaderProgram = LoadShaders("simple150.vert", "simple150.frag");
// Get handles to GLSL input vars locations // Get handles to GLSL input vars locations for defaultShaderProgram
vertexLoc = glGetAttribLocation(shaderProgram, "vertexPosition"); //-------------------------------------------------------------------
texcoordLoc = glGetAttribLocation(shaderProgram, "vertexTexCoord"); defaultVertexLoc = glGetAttribLocation(defaultShaderProgram, "vertexPosition");
colorLoc = glGetAttribLocation(shaderProgram, "vertexColor"); defaultTexcoordLoc = glGetAttribLocation(defaultShaderProgram, "vertexTexCoord");
defaultColorLoc = glGetAttribLocation(defaultShaderProgram, "vertexColor");
// Get handles to GLSL uniform vars locations (vertex-shader) // Get handles to GLSL uniform vars locations (vertex-shader)
modelviewMatrixLoc = glGetUniformLocation(shaderProgram, "modelviewMatrix"); defaultModelviewMatrixLoc = glGetUniformLocation(defaultShaderProgram, "modelviewMatrix");
projectionMatrixLoc = glGetUniformLocation(shaderProgram, "projectionMatrix"); defaultProjectionMatrixLoc = glGetUniformLocation(defaultShaderProgram, "projectionMatrix");
// Get handles to GLSL uniform vars locations (fragment-shader) // 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 InitializeBuffers(); // Init vertex arrays
InitializeBuffersGPU(); // Init VBO and VAO InitializeBuffersGPU(); // Init VBO and VAO
@ -914,11 +945,11 @@ void rlglClose(void)
glDeleteVertexArrays(1, &vaoQuads); glDeleteVertexArrays(1, &vaoQuads);
} }
//glDetachShader(shaderProgram, v); //glDetachShader(defaultShaderProgram, v);
//glDetachShader(shaderProgram, f); //glDetachShader(defaultShaderProgram, f);
//glDeleteShader(v); //glDeleteShader(v);
//glDeleteShader(f); //glDeleteShader(f);
glDeleteProgram(shaderProgram); glDeleteProgram(defaultShaderProgram);
// Free vertex arrays memory // Free vertex arrays memory
free(lines.vertices); free(lines.vertices);
@ -944,12 +975,15 @@ void rlglDraw(void)
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
UpdateBuffers(); UpdateBuffers();
glUseProgram(shaderProgram); // Use our shader if ((lines.vCounter > 0) || (triangles.vCounter > 0) || (quads.vCounter > 0))
{
glUniformMatrix4fv(projectionMatrixLoc, 1, false, GetMatrixVector(projection)); glUseProgram(defaultShaderProgram); // Use our shader
glUniformMatrix4fv(modelviewMatrixLoc, 1, false, GetMatrixVector(modelview));
glUniform1i(textureLoc, 0);
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 // NOTE: We draw in this order: triangle shapes, textured quads and lines
if (triangles.vCounter > 0) if (triangles.vCounter > 0)
@ -963,12 +997,12 @@ void rlglDraw(void)
else else
{ {
glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[0]); glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[0]);
glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, 0, 0, 0); glVertexAttribPointer(defaultVertexLoc, 3, GL_FLOAT, 0, 0, 0);
glEnableVertexAttribArray(vertexLoc); glEnableVertexAttribArray(defaultVertexLoc);
glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[1]); glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[1]);
glVertexAttribPointer(colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); glVertexAttribPointer(defaultColorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0);
glEnableVertexAttribArray(colorLoc); glEnableVertexAttribArray(defaultColorLoc);
} }
glDrawArrays(GL_TRIANGLES, 0, triangles.vCounter); glDrawArrays(GL_TRIANGLES, 0, triangles.vCounter);
@ -991,16 +1025,16 @@ void rlglDraw(void)
{ {
// Enable vertex attributes // Enable vertex attributes
glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[0]); glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[0]);
glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, 0, 0, 0); glVertexAttribPointer(defaultVertexLoc, 3, GL_FLOAT, 0, 0, 0);
glEnableVertexAttribArray(vertexLoc); glEnableVertexAttribArray(defaultVertexLoc);
glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[1]); glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[1]);
glVertexAttribPointer(texcoordLoc, 2, GL_FLOAT, 0, 0, 0); glVertexAttribPointer(defaultTexcoordLoc, 2, GL_FLOAT, 0, 0, 0);
glEnableVertexAttribArray(texcoordLoc); glEnableVertexAttribArray(defaultTexcoordLoc);
glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[2]); glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[2]);
glVertexAttribPointer(colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); glVertexAttribPointer(defaultColorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0);
glEnableVertexAttribArray(colorLoc); glEnableVertexAttribArray(defaultColorLoc);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quadsBuffer[3]); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quadsBuffer[3]);
} }
@ -1048,12 +1082,12 @@ void rlglDraw(void)
else else
{ {
glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[0]); glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[0]);
glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, 0, 0, 0); glVertexAttribPointer(defaultVertexLoc, 3, GL_FLOAT, 0, 0, 0);
glEnableVertexAttribArray(vertexLoc); glEnableVertexAttribArray(defaultVertexLoc);
glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[1]); glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[1]);
glVertexAttribPointer(colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); glVertexAttribPointer(defaultColorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0);
glEnableVertexAttribArray(colorLoc); glEnableVertexAttribArray(defaultColorLoc);
} }
glDrawArrays(GL_LINES, 0, lines.vCounter); 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 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) //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(); rlPushMatrix();
rlTranslatef(position.x, position.y, position.z); rlTranslatef(position.x, position.y, position.z);
rlScalef(scale.x, scale.y, scale.z); rlScalef(scale.x, scale.y, scale.z);
@ -1127,7 +1159,7 @@ void rlglDrawModel(Model model, Vector3 position, Vector3 rotation, Vector3 scal
#endif #endif
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) #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); VectorScale(&rotation, DEG2RAD);
@ -1136,37 +1168,14 @@ void rlglDrawModel(Model model, Vector3 position, Vector3 rotation, Vector3 scal
Matrix modelviewworld = MatrixMultiply(transform, modelview); Matrix modelviewworld = MatrixMultiply(transform, modelview);
// NOTE: Drawing in OpenGL 3.3+, transform is passed to shader // NOTE: Drawing in OpenGL 3.3+, transform is passed to shader
glUniformMatrix4fv(projectionMatrixLoc, 1, false, GetMatrixVector(projection)); glUniformMatrix4fv(simpleProjectionMatrixLoc, 1, false, GetMatrixVector(projection));
glUniformMatrix4fv(modelviewMatrixLoc, 1, false, GetMatrixVector(modelviewworld)); glUniformMatrix4fv(simpleModelviewMatrixLoc, 1, false, GetMatrixVector(modelviewworld));
glUniform1i(textureLoc, 0); glUniform1i(simpleTextureLoc, 0);
// Apply color tinting to model: 2 OPTIONS // Apply color tinting to model
/* // NOTE: Just update one uniform on fragment shader
// OPTION 1 float vColor[4] = { (float)color.r/255, (float)color.g/255, (float)color.b/255, (float)color.a/255 };
// Update colors array (model.mesh.colors) with color glUniform4fv(simpleColorLoc, 1, vColor);
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);
if (vaoSupported) if (vaoSupported)
{ {
@ -1176,16 +1185,17 @@ void rlglDrawModel(Model model, Vector3 position, Vector3 rotation, Vector3 scal
{ {
// Bind model VBOs data // Bind model VBOs data
glBindBuffer(GL_ARRAY_BUFFER, model.vboId[0]); glBindBuffer(GL_ARRAY_BUFFER, model.vboId[0]);
glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, 0, 0, 0); glVertexAttribPointer(simpleVertexLoc, 3, GL_FLOAT, 0, 0, 0);
glEnableVertexAttribArray(vertexLoc); glEnableVertexAttribArray(simpleVertexLoc);
glBindBuffer(GL_ARRAY_BUFFER, model.vboId[1]); glBindBuffer(GL_ARRAY_BUFFER, model.vboId[1]);
glVertexAttribPointer(texcoordLoc, 2, GL_FLOAT, 0, 0, 0); glVertexAttribPointer(simpleTexcoordLoc, 2, GL_FLOAT, 0, 0, 0);
glEnableVertexAttribArray(texcoordLoc); glEnableVertexAttribArray(simpleTexcoordLoc);
// Add normals support
glBindBuffer(GL_ARRAY_BUFFER, model.vboId[2]); glBindBuffer(GL_ARRAY_BUFFER, model.vboId[2]);
glVertexAttribPointer(colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); glVertexAttribPointer(simpleNormalLoc, 3, GL_FLOAT, 0, 0, 0);
glEnableVertexAttribArray(colorLoc); glEnableVertexAttribArray(simpleNormalLoc);
} }
glBindTexture(GL_TEXTURE_2D, model.textureId); 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 if (vaoSupported) glBindVertexArray(0); // Unbind VAO
else glBindBuffer(GL_ARRAY_BUFFER, 0); // Unbind VBOs else glBindBuffer(GL_ARRAY_BUFFER, 0); // Unbind VBOs
glUseProgram(0);
#endif #endif
#if defined (GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33) #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.vaoId = 0; // Vertex Array Object
model.vboId[0] = 0; // Vertex position VBO model.vboId[0] = 0; // Vertex position VBO
model.vboId[1] = 0; // Texcoords VBO model.vboId[1] = 0; // Texcoords VBO
//model.vboId[2] = 0; // Normals VBO (not used) model.vboId[2] = 0; // Normals VBO
model.vboId[2] = 0; // Colors VBO
#elif defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) #elif defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
model.textureId = 1; // Default whiteTexture model.textureId = 1; // Default whiteTexture
@ -1382,29 +1393,25 @@ Model rlglLoadModel(VertexData mesh)
// Enable vertex attributes: position // Enable vertex attributes: position
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[0]); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh.vertexCount, mesh.vertices, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh.vertexCount, mesh.vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(vertexLoc); glEnableVertexAttribArray(simpleVertexLoc);
glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, 0, 0, 0); glVertexAttribPointer(simpleVertexLoc, 3, GL_FLOAT, 0, 0, 0);
// Enable vertex attributes: texcoords // Enable vertex attributes: texcoords
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[1]); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*mesh.vertexCount, mesh.texcoords, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*mesh.vertexCount, mesh.texcoords, GL_STATIC_DRAW);
glEnableVertexAttribArray(texcoordLoc); glEnableVertexAttribArray(simpleTexcoordLoc);
glVertexAttribPointer(texcoordLoc, 2, GL_FLOAT, 0, 0, 0); glVertexAttribPointer(simpleTexcoordLoc, 2, GL_FLOAT, 0, 0, 0);
// TODO: Normals support -> Lighting
// Enable vertex attributes: normals // 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]); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(unsigned char)*4*mesh.vertexCount, mesh.colors, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh.vertexCount, mesh.normals, GL_STATIC_DRAW);
glEnableVertexAttribArray(colorLoc); glEnableVertexAttribArray(simpleNormalLoc);
glVertexAttribPointer(colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); 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 (vaoSupported)
{ {
if (vaoModel > 0) if (vaoModel > 0)
@ -1416,11 +1423,6 @@ Model rlglLoadModel(VertexData mesh)
} }
else 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]); 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 #endif
@ -1551,8 +1553,9 @@ void PrintModelviewMatrix()
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
// Load Shaders (Vertex and Fragment) // Load Shader (Vertex and Fragment)
static GLuint LoadDefaultShaders(void) // 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 // 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; 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 // Vertex shader directly defined, no external file required
static GLuint LoadShaders(char *vertexFileName, char *fragmentFileName) #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 // Shaders loading from external text file
char *vShaderStr = TextFileRead(vertexFileName); char *vShaderStr = TextFileRead(vertexFileName);
@ -1686,6 +1786,9 @@ static GLuint LoadShaders(char *vertexFileName, char *fragmentFileName)
glDeleteShader(vertexShader); glDeleteShader(vertexShader);
glDeleteShader(fragmentShader); glDeleteShader(fragmentShader);
free(vShaderStr);
free(fShaderStr);
TraceLog(INFO, "[SHDR ID %i] Shader program loaded successfully", program); 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 // Read shader text file
// NOTE: text chars array should be freed manually
static char *TextFileRead(char *fileName) static char *TextFileRead(char *fileName)
{ {
FILE *textFile; FILE *textFile;
@ -1801,14 +1905,14 @@ static void InitializeBuffersGPU(void)
// Lines - Vertex positions buffer binding and attributes enable // Lines - Vertex positions buffer binding and attributes enable
glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[0]); glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*2*MAX_LINES_BATCH, lines.vertices, GL_DYNAMIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*2*MAX_LINES_BATCH, lines.vertices, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(vertexLoc); glEnableVertexAttribArray(defaultVertexLoc);
glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, 0, 0, 0); glVertexAttribPointer(defaultVertexLoc, 3, GL_FLOAT, 0, 0, 0);
// Lines - colors buffer // Lines - colors buffer
glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[1]); glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(unsigned char)*4*2*MAX_LINES_BATCH, lines.colors, GL_DYNAMIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(unsigned char)*4*2*MAX_LINES_BATCH, lines.colors, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(colorLoc); glEnableVertexAttribArray(defaultColorLoc);
glVertexAttribPointer(colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); glVertexAttribPointer(defaultColorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0);
if (vaoSupported) TraceLog(INFO, "[VAO ID %i] Lines VAO initialized successfully", vaoLines); 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]); 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 // Enable vertex attributes
glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[0]); glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*3*MAX_TRIANGLES_BATCH, triangles.vertices, GL_DYNAMIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*3*MAX_TRIANGLES_BATCH, triangles.vertices, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(vertexLoc); glEnableVertexAttribArray(defaultVertexLoc);
glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, 0, 0, 0); glVertexAttribPointer(defaultVertexLoc, 3, GL_FLOAT, 0, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[1]); glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(unsigned char)*4*3*MAX_TRIANGLES_BATCH, triangles.colors, GL_DYNAMIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(unsigned char)*4*3*MAX_TRIANGLES_BATCH, triangles.colors, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(colorLoc); glEnableVertexAttribArray(defaultColorLoc);
glVertexAttribPointer(colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); glVertexAttribPointer(defaultColorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0);
if (vaoSupported) TraceLog(INFO, "[VAO ID %i] Triangles VAO initialized successfully", vaoTriangles); 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]); 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 // Enable vertex attributes
glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[0]); glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*4*MAX_QUADS_BATCH, quads.vertices, GL_DYNAMIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*4*MAX_QUADS_BATCH, quads.vertices, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(vertexLoc); glEnableVertexAttribArray(defaultVertexLoc);
glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, 0, 0, 0); glVertexAttribPointer(defaultVertexLoc, 3, GL_FLOAT, 0, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[1]); glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*4*MAX_QUADS_BATCH, quads.texcoords, GL_DYNAMIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*4*MAX_QUADS_BATCH, quads.texcoords, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(texcoordLoc); glEnableVertexAttribArray(defaultTexcoordLoc);
glVertexAttribPointer(texcoordLoc, 2, GL_FLOAT, 0, 0, 0); glVertexAttribPointer(defaultTexcoordLoc, 2, GL_FLOAT, 0, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[2]); glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(unsigned char)*4*4*MAX_QUADS_BATCH, quads.colors, GL_DYNAMIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(unsigned char)*4*4*MAX_QUADS_BATCH, quads.colors, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(colorLoc); glEnableVertexAttribArray(defaultColorLoc);
glVertexAttribPointer(colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); glVertexAttribPointer(defaultColorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0);
// Fill index buffer // Fill index buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quadsBuffer[3]); 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! // TODO: If no data changed on the CPU arrays --> No need to update GPU arrays every frame!
static void UpdateBuffers(void) static void UpdateBuffers(void)
{ {
// Activate Lines VAO if (lines.vCounter > 0)
if (vaoSupported) glBindVertexArray(vaoLines); {
// Activate Lines VAO
// Lines - vertex positions buffer if (vaoSupported) glBindVertexArray(vaoLines);
glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[0]);
//glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*2*MAX_LINES_BATCH, lines.vertices, GL_DYNAMIC_DRAW); // Lines - vertex positions buffer
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*3*lines.vCounter, lines.vertices); // target - offset (in bytes) - size (in bytes) - data pointer glBindBuffer(GL_ARRAY_BUFFER, linesBuffer[0]);
//glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*2*MAX_LINES_BATCH, lines.vertices, GL_DYNAMIC_DRAW);
// Lines - colors buffer glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*3*lines.vCounter, lines.vertices); // target - offset (in bytes) - size (in bytes) - data pointer
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);
// 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 (triangles.vCounter > 0)
if (vaoSupported) glBindVertexArray(vaoTriangles); {
// Activate Triangles VAO
// Triangles - vertex positions buffer if (vaoSupported) glBindVertexArray(vaoTriangles);
glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[0]);
//glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*3*MAX_TRIANGLES_BATCH, triangles.vertices, GL_DYNAMIC_DRAW); // Triangles - vertex positions buffer
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*3*triangles.vCounter, triangles.vertices); glBindBuffer(GL_ARRAY_BUFFER, trianglesBuffer[0]);
//glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*3*MAX_TRIANGLES_BATCH, triangles.vertices, GL_DYNAMIC_DRAW);
// Triangles - colors buffer glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*3*triangles.vCounter, triangles.vertices);
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);
// 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 (quads.vCounter > 0)
if (vaoSupported) glBindVertexArray(vaoQuads); {
// Activate Quads VAO
if (vaoSupported) glBindVertexArray(vaoQuads);
// Quads - vertex positions buffer // Quads - vertex positions buffer
glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[0]); glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[0]);
//glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*4*MAX_QUADS_BATCH, quads.vertices, GL_DYNAMIC_DRAW); //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); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*3*quads.vCounter, quads.vertices);
// Quads - texture coordinates buffer // Quads - texture coordinates buffer
glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[1]); glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[1]);
//glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*4*MAX_QUADS_BATCH, quads.texcoords, GL_DYNAMIC_DRAW); //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); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*2*quads.vCounter, quads.texcoords);
// Quads - colors buffer // Quads - colors buffer
glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[2]); glBindBuffer(GL_ARRAY_BUFFER, quadsBuffer[2]);
//glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*4*MAX_QUADS_BATCH, quads.colors, GL_DYNAMIC_DRAW); //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); 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);
// 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 // Unbind the current VAO

View file

@ -5,7 +5,7 @@
* raylib now uses OpenGL 1.1 style functions (rlVertex) that are mapped to selected OpenGL version: * raylib now uses OpenGL 1.1 style functions (rlVertex) that are mapped to selected OpenGL version:
* OpenGL 1.1 - Direct map rl* -> gl* * OpenGL 1.1 - Direct map rl* -> gl*
* OpenGL 3.3+ - Vertex data is stored in VAOs, call rlglDraw() to render * 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) * Copyright (c) 2014 Ramon Santamaria (Ray San - raysan@raysanweb.com)
* *

View file

@ -40,7 +40,6 @@ LOCAL_SRC_FILES :=\
../../src/core.c \ ../../src/core.c \
../../src/rlgl.c \ ../../src/rlgl.c \
../../src/raymath.c \ ../../src/raymath.c \
../../src/stb_image.c \
../../src/textures.c \ ../../src/textures.c \
../../src/text.c \ ../../src/text.c \
../../src/shapes.c \ ../../src/shapes.c \