diff options
Diffstat (limited to 'examples')
30 files changed, 745 insertions, 58 deletions
diff --git a/examples/audio_music_stream.c b/examples/audio_music_stream.c index 3add91da..e61d4839 100644 --- a/examples/audio_music_stream.c +++ b/examples/audio_music_stream.c @@ -4,10 +4,10 @@ * * NOTE: This example requires OpenAL Soft library installed * -* This example has been created using raylib 1.1 (www.raylib.com) +* This example has been created using raylib 1.3 (www.raylib.com) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) * -* Copyright (c) 2014 Ramon Santamaria (@raysan5) +* Copyright (c) 2015 Ramon Santamaria (@raysan5) * ********************************************************************************************/ @@ -61,9 +61,9 @@ int main() if (IsWindowMinimized()) PauseMusicStream(); else ResumeMusicStream(); - timePlayed = GetMusicTimePlayed() / GetMusicTimeLength() * 100 * 4; // We scale by 4 to fit 400 pixels + timePlayed = GetMusicTimePlayed()/GetMusicTimeLength()*100*4; // We scale by 4 to fit 400 pixels - UpdateMusicStream(); + UpdateMusicStream(); // Update music buffer with new stream data //---------------------------------------------------------------------------------- // Draw diff --git a/examples/core_3d_camera_first_person.c b/examples/core_3d_camera_first_person.c index cd37f873..aa947c3c 100644 --- a/examples/core_3d_camera_first_person.c +++ b/examples/core_3d_camera_first_person.c @@ -27,7 +27,7 @@ int main() // Generates some random columns float heights[MAX_COLUMNS]; - Vector3 positions[MAX_COLUMNS] = { 0.0, 2.5, 0.0 }; + Vector3 positions[MAX_COLUMNS]; Color colors[MAX_COLUMNS]; for (int i = 0; i < MAX_COLUMNS; i++) @@ -76,7 +76,7 @@ int main() DrawText("First person camera default controls:", 20, 20, 10, GRAY); DrawText("- Move with keys: W, A, S, D", 40, 50, 10, DARKGRAY); - DrawText("- Mouse move to lokk around", 40, 70, 10, DARKGRAY); + DrawText("- Mouse move to look around", 40, 70, 10, DARKGRAY); EndDrawing(); //---------------------------------------------------------------------------------- diff --git a/examples/core_3d_picking.c b/examples/core_3d_picking.c index 13839070..2fc05e81 100644 --- a/examples/core_3d_picking.c +++ b/examples/core_3d_picking.c @@ -5,7 +5,7 @@ * This example has been created using raylib 1.3 (www.raylib.com) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) * -* Copyright (c) 2015 Ramon Santamaria (Ray San - raysan@raysanweb.com) +* Copyright (c) 2015 Ramon Santamaria (@raysan5) * ********************************************************************************************/ diff --git a/examples/core_basic_window_web.c b/examples/core_basic_window_web.c index 65650b1a..1ecb22f3 100644 --- a/examples/core_basic_window_web.c +++ b/examples/core_basic_window_web.c @@ -1,26 +1,22 @@ /******************************************************************************************* * -* raylib [core] example - Basic window +* raylib [core] example - Basic window (adapted for HTML5 platform) * -* Welcome to raylib! +* This example is prepared to compile for PLATFORM_WEB, PLATFORM_DESKTOP and PLATFORM_RPI +* As you will notice, code structure is slightly diferent to the other examples... +* To compile it for PLATFORM_WEB just uncomment #define PLATFORM_WEB at beginning * -* 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.3 (www.raylib.com) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) * -* Copyright (c) 2014 Ramon Santamaria (@raysan5) +* Copyright (c) 2015 Ramon Santamaria (@raysan5) * ********************************************************************************************/ #include "raylib.h" +//#define PLATFORM_WEB + #if defined(PLATFORM_WEB) #include <emscripten/emscripten.h> #endif diff --git a/examples/core_drop_files.c b/examples/core_drop_files.c index 5802e48f..5eea35f3 100644 --- a/examples/core_drop_files.c +++ b/examples/core_drop_files.c @@ -2,10 +2,12 @@ * * raylib [core] example - Windows drop files * +* This example only works on platforms that support drag & drop (Windows, Linux, OSX, Html5?) +* * This example has been created using raylib 1.3 (www.raylib.com) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) * -* Copyright (c) 2014 Ramon Santamaria (Ray San - raysan@raysanweb.com) +* Copyright (c) 2015 Ramon Santamaria (@raysan5) * ********************************************************************************************/ diff --git a/examples/makefile b/examples/makefile index f351fbad..dac378ce 100644 --- a/examples/makefile +++ b/examples/makefile @@ -2,7 +2,7 @@ # # raylib makefile for desktop platforms, Raspberry Pi and HTML5 (emscripten) # -# Copyright (c) 2014 Ramon Santamaria (Ray San - raysan@raysanweb.com) +# Copyright (c) 2015 Ramon Santamaria (@raysan5) # # This software is provided "as-is", without any express or implied warranty. In no event # will the authors be held liable for any damages arising from the use of this software. @@ -154,9 +154,14 @@ EXAMPLES = \ core_input_keys \ core_input_mouse \ core_mouse_wheel \ + core_input_gamepad \ core_random_values \ core_color_select \ + core_drop_files \ core_3d_mode \ + core_3d_picking \ + core_3d_camera_free \ + core_3d_camera_first_person \ shapes_logo_raylib \ shapes_basic_shapes \ shapes_colors_palette \ @@ -165,16 +170,24 @@ EXAMPLES = \ textures_image_loading \ textures_rectangle \ textures_srcrec_dstrec \ + textures_to_image \ + textures_raw_data \ + textures_formats_loading \ + textures_particles_trail_blending \ text_sprite_fonts \ text_rbmf_fonts \ text_format_text \ text_font_select \ models_geometric_shapes \ - models_planes \ + models_box_collisions \ models_billboard \ models_obj_loading \ models_heightmap \ models_cubicmap \ + shaders_model_shader \ + shaders_shapes_textures \ + shaders_custom_uniform \ + shaders_postprocessing \ audio_sound_loading \ audio_music_stream \ fix_dylib \ @@ -201,16 +214,26 @@ core_input_keys: core_input_keys.c core_input_mouse: core_input_mouse.c $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) -ifeq ($(PLATFORM),PLATFORM_DESKTOP) # compile [core] example - gamepad input core_input_gamepad: core_input_gamepad.c +ifeq ($(PLATFORM),PLATFORM_DESKTOP) $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) +else + @echo core_input_gamepad: Only supported on desktop platform endif # compile [core] example - mouse wheel core_mouse_wheel: core_mouse_wheel.c $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) +# compile [core] example - drop files +core_drop_files: core_drop_files.c +ifeq ($(PLATFORM),PLATFORM_DESKTOP) + $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) +else + @echo core_drop_files: Only supported on desktop platform +endif + # compile [core] example - generate random values core_random_values: core_random_values.c $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) @@ -223,6 +246,18 @@ core_color_select: core_color_select.c core_3d_mode: core_3d_mode.c $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) +# compile [core] example - 3d picking +core_3d_picking: core_3d_picking.c + $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) + +# compile [core] example - 3d camera free +core_3d_camera_free: core_3d_camera_free.c + $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) + +# compile [core] example - 3d camera first person +core_3d_camera_first_person: core_3d_camera_first_person.c + $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) + # compile [shapes] example - raylib logo (with basic shapes) shapes_logo_raylib: shapes_logo_raylib.c $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) @@ -255,6 +290,22 @@ textures_rectangle: textures_rectangle.c textures_srcrec_dstrec: textures_srcrec_dstrec.c $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) +# compile [textures] example - texture to image +textures_to_image: textures_to_image.c + $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) + +# compile [textures] example - texture raw data +textures_raw_data: textures_raw_data.c + $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) + +# compile [textures] example - texture formats loading +textures_formats_loading: textures_formats_loading.c + $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) + +# compile [textures] example - texture particles trail blending +textures_particles_trail_blending: textures_particles_trail_blending.c + $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) + # compile [text] example - sprite fonts loading text_sprite_fonts: text_sprite_fonts.c $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) @@ -275,6 +326,10 @@ text_font_select: text_font_select.c models_geometric_shapes: models_geometric_shapes.c $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) +# compile [models] example - box collisions +models_box_collisions: models_box_collisions.c + $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) + # compile [models] example - basic window models_planes: models_planes.c $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) @@ -295,6 +350,22 @@ models_heightmap: models_heightmap.c models_cubicmap: models_cubicmap.c $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) +# compile [shaders] example - model shader +shaders_model_shader: shaders_model_shader.c + $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) + +# compile [shaders] example - shapes texture shader +shaders_shapes_textures: shaders_shapes_textures.c + $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) + +# compile [shaders] example - custom uniform in shader +shaders_custom_uniform: shaders_custom_uniform.c + $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) + +# compile [shaders] example - postprocessing shader +shaders_postprocessing: shaders_postprocessing.c + $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) + # compile [audio] example - sound loading and playing (WAV and OGG) audio_sound_loading: audio_sound_loading.c $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) diff --git a/examples/models_box_collisions.c b/examples/models_box_collisions.c new file mode 100644 index 00000000..18fca091 --- /dev/null +++ b/examples/models_box_collisions.c @@ -0,0 +1,119 @@ +/******************************************************************************************* +* +* raylib [models] example - Detect basic 3d collisions (box vs sphere vs box) +* +* This example has been created using raylib 1.3 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Copyright (c) 2015 Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + int screenWidth = 800; + int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [models] example - box collisions"); + + // Define the camera to look into our 3d world + Camera camera = {{ 0.0, 10.0, 10.0 }, { 0.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }}; + + Vector3 playerPosition = { 0, 1, 2 }; + Vector3 playerSize = { 1, 2, 1 }; + Color playerColor = GREEN; + + Vector3 enemyBoxPos = { -4, 1, 0 }; + Vector3 enemyBoxSize = { 2, 2, 2 }; + + Vector3 enemySpherePos = { 4, 0, 0 }; + float enemySphereSize = 1.5f; + + bool collision = false; + + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + + // Move player + if (IsKeyDown(KEY_RIGHT)) playerPosition.x += 0.2f; + else if (IsKeyDown(KEY_LEFT)) playerPosition.x -= 0.2f; + else if (IsKeyDown(KEY_DOWN)) playerPosition.z += 0.2f; + else if (IsKeyDown(KEY_UP)) playerPosition.z -= 0.2f; + + collision = false; + + // Check collisions player vs enemy-box + if (CheckCollisionBoxes((Vector3){ playerPosition.x - playerSize.x/2, + playerPosition.y - playerSize.y/2, + playerPosition.z - playerSize.z/2 }, + (Vector3){ playerPosition.x + playerSize.x/2, + playerPosition.y + playerSize.y/2, + playerPosition.z + playerSize.z/2 }, + (Vector3){ enemyBoxPos.x - enemyBoxSize.x/2, + enemyBoxPos.y - enemyBoxSize.y/2, + enemyBoxPos.z - enemyBoxSize.z/2 }, + (Vector3){ enemyBoxPos.x + enemyBoxSize.x/2, + enemyBoxPos.y + enemyBoxSize.y/2, + enemyBoxPos.z + enemyBoxSize.z/2 })) collision = true; + + // Check collisions player vs enemy-sphere + if (CheckCollisionBoxSphere((Vector3){ playerPosition.x - playerSize.x/2, + playerPosition.y - playerSize.y/2, + playerPosition.z - playerSize.z/2 }, + (Vector3){ playerPosition.x + playerSize.x/2, + playerPosition.y + playerSize.y/2, + playerPosition.z + playerSize.z/2 }, + enemySpherePos, enemySphereSize)) collision = true; + + if (collision) playerColor = RED; + else playerColor = GREEN; + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + Begin3dMode(camera); + + // Draw enemy-box + DrawCube(enemyBoxPos, enemyBoxSize.x, enemyBoxSize.y, enemyBoxSize.z, GRAY); + DrawCubeWires(enemyBoxPos, enemyBoxSize.x, enemyBoxSize.y, enemyBoxSize.z, DARKGRAY); + + // Draw enemy-sphere + DrawSphere(enemySpherePos, enemySphereSize, GRAY); + DrawSphereWires(enemySpherePos, enemySphereSize, 16, 16, DARKGRAY); + + // Draw player + DrawCubeV(playerPosition, playerSize, playerColor); + + DrawGrid(10.0, 1.0); // Draw a grid + + End3dMode(); + + DrawText("Move player with cursors to collide", 220, 40, 20, GRAY); + + DrawFPS(10, 10); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +}
\ No newline at end of file diff --git a/examples/models_box_collisions.png b/examples/models_box_collisions.png Binary files differnew file mode 100644 index 00000000..d01fd9dd --- /dev/null +++ b/examples/models_box_collisions.png diff --git a/examples/models_cubicmap.c b/examples/models_cubicmap.c index d7fe896c..98fc54af 100644 --- a/examples/models_cubicmap.c +++ b/examples/models_cubicmap.c @@ -5,7 +5,7 @@ * This example has been created using raylib 1.3 (www.raylib.com) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) * -* Copyright (c) 2015 Ramon Santamaria (Ray San - raysan@raysanweb.com) +* Copyright (c) 2015 Ramon Santamaria (@raysan5) * ********************************************************************************************/ diff --git a/examples/models_obj_loading.c b/examples/models_obj_loading.c index ef024356..dc302f99 100644 --- a/examples/models_obj_loading.c +++ b/examples/models_obj_loading.c @@ -23,8 +23,8 @@ int main() // Define the camera to look into our 3d world Camera camera = {{ 3.0, 3.0, 3.0 }, { 0.0, 1.5, 0.0 }, { 0.0, 1.0, 0.0 }}; - Texture2D texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model texture Model dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model + Texture2D texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model texture SetModelTexture(&dwarf, texture); // Bind texture to model Vector3 position = { 0.0, 0.0, 0.0 }; // Set model position @@ -36,10 +36,7 @@ int main() { // Update //---------------------------------------------------------------------------------- - if (IsKeyDown(KEY_LEFT)) position.x -= 0.2; - if (IsKeyDown(KEY_RIGHT)) position.x += 0.2; - if (IsKeyDown(KEY_UP)) position.z -= 0.2; - if (IsKeyDown(KEY_DOWN)) position.z += 0.2; + //... //---------------------------------------------------------------------------------- // Draw @@ -54,7 +51,7 @@ int main() DrawGrid(10.0, 1.0); // Draw a grid - DrawGizmo(position); // Draw gizmo + DrawGizmo(position); // Draw gizmo End3dMode(); diff --git a/examples/resources/shaders/base.vs b/examples/resources/shaders/base.vs index 78e543b7..59eae0a0 100644 --- a/examples/resources/shaders/base.vs +++ b/examples/resources/shaders/base.vs @@ -1,19 +1,19 @@ -#version 110 +#version 330 -attribute vec3 vertexPosition; -attribute vec2 vertexTexCoord; -attribute vec4 vertexColor; +in vec3 vertexPosition; +in vec2 vertexTexCoord; +in vec3 vertexNormal; + +out vec2 fragTexCoord; uniform mat4 projectionMatrix; uniform mat4 modelviewMatrix; -varying vec2 fragTexCoord; -varying vec4 fragColor; +// NOTE: Add here your custom variables void main() { fragTexCoord = vertexTexCoord; - fragColor = vertexColor; gl_Position = projectionMatrix*modelviewMatrix*vec4(vertexPosition, 1.0); }
\ No newline at end of file diff --git a/examples/resources/shaders/bloom.fs b/examples/resources/shaders/bloom.fs new file mode 100644 index 00000000..f9cebe18 --- /dev/null +++ b/examples/resources/shaders/bloom.fs @@ -0,0 +1,42 @@ +#version 330 + +in vec2 fragTexCoord; + +out vec4 fragColor; + +uniform sampler2D texture0; +uniform vec4 tintColor; + +// NOTE: Add here your custom variables + +void main() +{ + vec4 sum = vec4(0); + vec4 tc = vec4(0); + + for (int i = -4; i < 4; i++) + { + for (int j = -3; j < 3; j++) + { + sum += texture2D(texture0, fragTexCoord + vec2(j, i)*0.004) * 0.25; + } + } + + if (texture2D(texture0, fragTexCoord).r < 0.3) + { + tc = sum*sum*0.012 + texture2D(texture0, fragTexCoord); + } + else + { + if (texture2D(texture0, fragTexCoord).r < 0.5) + { + tc = sum*sum*0.009 + texture2D(texture0, fragTexCoord); + } + else + { + tc = sum*sum*0.0075 + texture2D(texture0, fragTexCoord); + } + } + + fragColor = tc; +}
\ No newline at end of file diff --git a/examples/resources/shaders/grayscale.fs b/examples/resources/shaders/grayscale.fs index 1b778871..38337e00 100644 --- a/examples/resources/shaders/grayscale.fs +++ b/examples/resources/shaders/grayscale.fs @@ -1,15 +1,20 @@ -#version 110 +#version 330 + +in vec2 fragTexCoord; + +out vec4 fragColor; uniform sampler2D texture0; -varying vec2 fragTexCoord; -varying vec4 fragColor; +uniform vec4 tintColor; + +// NOTE: Add here your custom variables void main() { - vec4 base = texture2D(texture0, fragTexCoord)*fragColor; - + vec4 base = texture2D(texture0, fragTexCoord)*tintColor; + // Convert to grayscale using NTSC conversion weights float gray = dot(base.rgb, vec3(0.299, 0.587, 0.114)); - gl_FragColor = vec4(gray, gray, gray, base.a); + fragColor = vec4(gray, gray, gray, tintColor.a); }
\ No newline at end of file diff --git a/examples/resources/shaders/custom.vs b/examples/resources/shaders/shapes_base.vs index 629c954d..78e543b7 100644 --- a/examples/resources/shaders/custom.vs +++ b/examples/resources/shaders/shapes_base.vs @@ -1,16 +1,19 @@ -#version 330 +#version 110 attribute vec3 vertexPosition; attribute vec2 vertexTexCoord; -attribute vec3 vertexNormal; +attribute vec4 vertexColor; uniform mat4 projectionMatrix; uniform mat4 modelviewMatrix; varying vec2 fragTexCoord; +varying vec4 fragColor; void main() { fragTexCoord = vertexTexCoord; + fragColor = vertexColor; + gl_Position = projectionMatrix*modelviewMatrix*vec4(vertexPosition, 1.0); }
\ No newline at end of file diff --git a/examples/resources/shaders/custom.fs b/examples/resources/shaders/shapes_grayscale.fs index 1e53933b..1b778871 100644 --- a/examples/resources/shaders/custom.fs +++ b/examples/resources/shaders/shapes_grayscale.fs @@ -1,16 +1,15 @@ -#version 330 +#version 110 uniform sampler2D texture0; varying vec2 fragTexCoord; - -uniform vec4 tintColor; +varying vec4 fragColor; void main() { - vec4 base = texture2D(texture0, fragTexCoord)*tintColor; - + vec4 base = texture2D(texture0, fragTexCoord)*fragColor; + // Convert to grayscale using NTSC conversion weights float gray = dot(base.rgb, vec3(0.299, 0.587, 0.114)); - gl_FragColor = vec4(gray, gray, gray, tintColor.a); + gl_FragColor = vec4(gray, gray, gray, base.a); }
\ No newline at end of file diff --git a/examples/resources/shaders/swirl.fs b/examples/resources/shaders/swirl.fs new file mode 100644 index 00000000..ba26cc05 --- /dev/null +++ b/examples/resources/shaders/swirl.fs @@ -0,0 +1,41 @@ +#version 330 + +in vec2 fragTexCoord; + +out vec4 fragColor; + +uniform sampler2D texture0; +uniform vec4 tintColor; + +// NOTE: Add here your custom variables + +const float renderWidth = 800; // HARDCODED for example! +const float renderHeight = 480; // Use uniforms instead... + +float radius = 250.0; +float angle = 0.8; + +uniform vec2 center = vec2(200, 200); + +void main (void) +{ + vec2 texSize = vec2(renderWidth, renderHeight); + vec2 tc = fragTexCoord*texSize; + tc -= center; + float dist = length(tc); + + if (dist < radius) + { + float percent = (radius - dist)/radius; + float theta = percent*percent*angle*8.0; + float s = sin(theta); + float c = cos(theta); + + tc = vec2(dot(tc, vec2(c, -s)), dot(tc, vec2(s, c))); + } + + tc += center; + vec3 color = texture2D(texture0, tc/texSize).rgb; + + fragColor = vec4(color, 1.0);; +}
\ No newline at end of file diff --git a/examples/shaders_custom_uniform.c b/examples/shaders_custom_uniform.c new file mode 100644 index 00000000..59350d35 --- /dev/null +++ b/examples/shaders_custom_uniform.c @@ -0,0 +1,108 @@ +/******************************************************************************************* +* +* raylib [shaders] example - Apply a postprocessing shader and connect a custom uniform variable +* +* NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support, +* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version. +* +* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3), to test this example +* on OpenGL ES 2.0 platforms (Android, Raspberry Pi, HTML5), use #version 100 shaders +* raylib comes with shaders ready for both versions, check raylib/shaders install folder +* +* This example has been created using raylib 1.3 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Copyright (c) 2015 Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + int screenWidth = 800; + int screenHeight = 450; + + SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available) + + InitWindow(screenWidth, screenHeight, "raylib [shaders] example - custom uniform variable"); + + // Define the camera to look into our 3d world + Camera camera = {{ 3.0, 3.0, 3.0 }, { 0.0, 1.5, 0.0 }, { 0.0, 1.0, 0.0 }}; + + Model dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model + Texture2D texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model texture + SetModelTexture(&dwarf, texture); // Bind texture to model + + Vector3 position = { 0.0, 0.0, 0.0 }; // Set model position + + Shader shader = LoadShader("resources/shaders/base.vs", + "resources/shaders/swirl.fs"); // Load postpro shader + + // Get variable (uniform) location on the shader to connect with the program + // NOTE: If uniform variable could not be found in the shader, function returns -1 + int swirlCenterLoc = GetShaderLocation(shader, "center"); + + float swirlCenter[2] = { screenWidth/2, screenHeight/2 }; + + SetPostproShader(shader); // Set fullscreen postprocessing shader + + // Setup orbital camera + SetCameraMode(CAMERA_ORBITAL); // Set an orbital camera mode + SetCameraPosition(camera.position); // Set internal camera position to match our camera position + SetCameraTarget(camera.target); // Set internal camera target to match our camera target + + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + Vector2 mousePosition = GetMousePosition(); + + swirlCenter[0] = mousePosition.x; + swirlCenter[1] = screenHeight - mousePosition.y; + + // Send new value to the shader to be used on drawing + SetShaderValue(shader, swirlCenterLoc, swirlCenter, 2); + + UpdateCamera(&camera); // Update internal camera and our camera + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + Begin3dMode(camera); + + DrawModel(dwarf, position, 2.0f, WHITE); // Draw 3d model with texture + + DrawGrid(10.0, 1.0); // Draw a grid + + End3dMode(); + + DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, GRAY); + + DrawFPS(10, 10); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadShader(shader); // Unload shader + UnloadTexture(texture); // Unload texture + UnloadModel(dwarf); // Unload model + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +}
\ No newline at end of file diff --git a/examples/shaders_custom_uniform.png b/examples/shaders_custom_uniform.png Binary files differnew file mode 100644 index 00000000..7d5400a1 --- /dev/null +++ b/examples/shaders_custom_uniform.png diff --git a/examples/shaders_model_shader.c b/examples/shaders_model_shader.c new file mode 100644 index 00000000..8ea390e5 --- /dev/null +++ b/examples/shaders_model_shader.c @@ -0,0 +1,93 @@ +/******************************************************************************************* +* +* raylib [shaders] example - Apply a shader to a 3d model +* +* NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support, +* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version. +* +* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3), to test this example +* on OpenGL ES 2.0 platforms (Android, Raspberry Pi, HTML5), use #version 100 shaders +* raylib comes with shaders ready for both versions, check raylib/shaders install folder +* +* This example has been created using raylib 1.3 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Copyright (c) 2014 Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + int screenWidth = 800; + int screenHeight = 450; + + SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available) + + InitWindow(screenWidth, screenHeight, "raylib [shaders] example - model shader"); + + // Define the camera to look into our 3d world + Camera camera = {{ 3.0, 3.0, 3.0 }, { 0.0, 1.5, 0.0 }, { 0.0, 1.0, 0.0 }}; + + Model dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model + Texture2D texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model texture + Shader shader = LoadShader("resources/shaders/base.vs", + "resources/shaders/grayscale.fs"); // Load model shader + + SetModelShader(&dwarf, shader); // Set shader effect to 3d model + SetModelTexture(&dwarf, texture); // Bind texture to model + + Vector3 position = { 0.0, 0.0, 0.0 }; // Set model position + + // Setup orbital camera + SetCameraMode(CAMERA_ORBITAL); // Set an orbital camera mode + SetCameraPosition(camera.position); // Set internal camera position to match our camera position + SetCameraTarget(camera.target); // Set internal camera target to match our camera target + + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + UpdateCamera(&camera); // Update internal camera and our camera + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + Begin3dMode(camera); + + DrawModel(dwarf, position, 2.0f, WHITE); // Draw 3d model with texture + + DrawGrid(10.0, 1.0); // Draw a grid + + End3dMode(); + + DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, GRAY); + + DrawFPS(10, 10); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadShader(shader); // Unload shader + UnloadTexture(texture); // Unload texture + UnloadModel(dwarf); // Unload model + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +}
\ No newline at end of file diff --git a/examples/shaders_model_shader.png b/examples/shaders_model_shader.png Binary files differnew file mode 100644 index 00000000..b829dea5 --- /dev/null +++ b/examples/shaders_model_shader.png diff --git a/examples/shaders_postprocessing.c b/examples/shaders_postprocessing.c new file mode 100644 index 00000000..5d7c1f61 --- /dev/null +++ b/examples/shaders_postprocessing.c @@ -0,0 +1,94 @@ +/******************************************************************************************* +* +* raylib [shaders] example - Apply a postprocessing shader to a scene +* +* NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support, +* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version. +* +* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3), to test this example +* on OpenGL ES 2.0 platforms (Android, Raspberry Pi, HTML5), use #version 100 shaders +* raylib comes with shaders ready for both versions, check raylib/shaders install folder +* +* This example has been created using raylib 1.3 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Copyright (c) 2015 Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + int screenWidth = 800; + int screenHeight = 450; + + SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available) + + InitWindow(screenWidth, screenHeight, "raylib [shaders] example - postprocessing shader"); + + // Define the camera to look into our 3d world + Camera camera = {{ 3.0, 3.0, 3.0 }, { 0.0, 1.5, 0.0 }, { 0.0, 1.0, 0.0 }}; + + Model dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model + Texture2D texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model texture + SetModelTexture(&dwarf, texture); // Bind texture to model + + Vector3 position = { 0.0, 0.0, 0.0 }; // Set model position + + Shader shader = LoadShader("resources/shaders/base.vs", + "resources/shaders/bloom.fs"); // Load postpro shader + + SetPostproShader(shader); // Set fullscreen postprocessing shader + + // Setup orbital camera + SetCameraMode(CAMERA_ORBITAL); // Set an orbital camera mode + SetCameraPosition(camera.position); // Set internal camera position to match our camera position + SetCameraTarget(camera.target); // Set internal camera target to match our camera target + + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + UpdateCamera(&camera); // Update internal camera and our camera + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + Begin3dMode(camera); + + DrawModel(dwarf, position, 2.0f, WHITE); // Draw 3d model with texture + + DrawGrid(10.0, 1.0); // Draw a grid + + End3dMode(); + + DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, BLACK); + + DrawFPS(10, 10); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadShader(shader); // Unload shader + UnloadTexture(texture); // Unload texture + UnloadModel(dwarf); // Unload model + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +}
\ No newline at end of file diff --git a/examples/shaders_postprocessing.png b/examples/shaders_postprocessing.png Binary files differnew file mode 100644 index 00000000..139080d1 --- /dev/null +++ b/examples/shaders_postprocessing.png diff --git a/examples/shaders_shapes_textures.c b/examples/shaders_shapes_textures.c new file mode 100644 index 00000000..37180cec --- /dev/null +++ b/examples/shaders_shapes_textures.c @@ -0,0 +1,113 @@ +/******************************************************************************************* +* +* raylib [shaders] example - Apply a shader to some shape or texture +* +* NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support, +* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version. +* +* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3), to test this example +* on OpenGL ES 2.0 platforms (Android, Raspberry Pi, HTML5), use #version 100 shaders +* raylib comes with shaders ready for both versions, check raylib/shaders install folder +* +* This example has been created using raylib 1.3 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Copyright (c) 2015 Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +#include <stdio.h> +#include <stdlib.h> + +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + int screenWidth = 800; + int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [shaders] example - shapes and texture shaders"); + + Texture2D sonic = LoadTexture("resources/texture_formats/sonic.png"); + + // NOTE: This shader is a bit different than model/postprocessing shaders, + // it requires the color data for every vertice to use it in every shape or texture independently + Shader shader = LoadShader("resources/shaders/shapes_base.vs", + "resources/shaders/shapes_grayscale.fs"); + + // Shader usage is also different than models/postprocessing, shader is just activated when required + + SetTargetFPS(60); + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + // TODO: Update your variables here + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + // Start drawing with default shader + + DrawText("USING DEFAULT SHADER", 20, 40, 10, RED); + + DrawCircle(80, 120, 35, DARKBLUE); + DrawCircleGradient(80, 220, 60, GREEN, SKYBLUE); + DrawCircleLines(80, 340, 80, DARKBLUE); + + + // Activate our custom shader to be applied on next shapes/textures drawings + SetCustomShader(shader); + + DrawText("USING CUSTOM SHADER", 190, 40, 10, RED); + + DrawRectangle(250 - 60, 90, 120, 60, RED); + DrawRectangleGradient(250 - 90, 170, 180, 130, MAROON, GOLD); + DrawRectangleLines(250 - 40, 320, 80, 60, ORANGE); + + // Activate our default shader for next drawings + SetDefaultShader(); + + DrawText("USING DEFAULT SHADER", 370, 40, 10, RED); + + DrawTriangle((Vector2){430, 80}, + (Vector2){430 - 60, 150}, + (Vector2){430 + 60, 150}, VIOLET); + + DrawTriangleLines((Vector2){430, 160}, + (Vector2){430 - 20, 230}, + (Vector2){430 + 20, 230}, DARKBLUE); + + DrawPoly((Vector2){430, 320}, 6, 80, 0, BROWN); + + // Activate our custom shader to be applied on next shapes/textures drawings + SetCustomShader(shader); + + DrawTexture(sonic, 380, -10, WHITE); // Using custom shader + + // Activate our default shader for next drawings + SetDefaultShader(); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadShader(shader); // Unload shader + UnloadTexture(sonic); // Unload texture + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +}
\ No newline at end of file diff --git a/examples/shaders_shapes_textures.png b/examples/shaders_shapes_textures.png Binary files differnew file mode 100644 index 00000000..ee5fed42 --- /dev/null +++ b/examples/shaders_shapes_textures.png diff --git a/examples/textures_image_loading.c b/examples/textures_image_loading.c index 47eb58af..54c73586 100644 --- a/examples/textures_image_loading.c +++ b/examples/textures_image_loading.c @@ -4,10 +4,10 @@ * * NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM) * -* This example has been created using raylib 1.1 (www.raylib.com) +* This example has been created using raylib 1.3 (www.raylib.com) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) * -* Copyright (c) 2014 Ramon Santamaria (@raysan5) +* Copyright (c) 2015 Ramon Santamaria (@raysan5) * ********************************************************************************************/ diff --git a/examples/textures_particles_trail_blending.c b/examples/textures_particles_trail_blending.c index 1e7abf7e..da01f863 100644 --- a/examples/textures_particles_trail_blending.c +++ b/examples/textures_particles_trail_blending.c @@ -13,6 +13,7 @@ #define MAX_PARTICLES 200 +// Particle structure with basic data typedef struct { Vector2 position; Color color; diff --git a/examples/textures_raw_data.c b/examples/textures_raw_data.c index a4ff71b3..d1922180 100644 --- a/examples/textures_raw_data.c +++ b/examples/textures_raw_data.c @@ -35,6 +35,7 @@ int main() int width = 1024; int height = 1024; + // Dynamic memory allocation to store pixels data (Color type) Color *pixels = (Color *)malloc(width*height*sizeof(Color)); for (int y = 0; y < height; y++) @@ -50,6 +51,8 @@ int main() Image checkedIm = LoadImageEx(pixels, width, height); Texture2D checked = LoadTextureFromImage(checkedIm); UnloadImage(checkedIm); // Unload CPU (RAM) image data + + // Dynamic memory must be freed after using it free(pixels); // Unload CPU (RAM) pixels data //--------------------------------------------------------------------------------------- diff --git a/examples/textures_rectangle.c b/examples/textures_rectangle.c index 61cce9fb..bf52bb18 100644 --- a/examples/textures_rectangle.c +++ b/examples/textures_rectangle.c @@ -2,7 +2,7 @@ * * raylib [textures] example - Texture loading and drawing a part defined by a rectangle * -* This example has been created using raylib 1.0 (www.raylib.com) +* This example has been created using raylib 1.3 (www.raylib.com) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) * * Copyright (c) 2014 Ramon Santamaria (@raysan5) diff --git a/examples/textures_srcrec_dstrec.c b/examples/textures_srcrec_dstrec.c index 72a209fb..58917421 100644 --- a/examples/textures_srcrec_dstrec.c +++ b/examples/textures_srcrec_dstrec.c @@ -2,10 +2,10 @@ * * raylib [textures] example - Texture source and destination rectangles * -* This example has been created using raylib 1.1 (www.raylib.com) +* This example has been created using raylib 1.3 (www.raylib.com) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) * -* Copyright (c) 2014 Ramon Santamaria (@raysan5) +* Copyright (c) 2015 Ramon Santamaria (@raysan5) * ********************************************************************************************/ diff --git a/examples/textures_to_image.c b/examples/textures_to_image.c index 3ea8e017..37c3b5a0 100644 --- a/examples/textures_to_image.c +++ b/examples/textures_to_image.c @@ -4,10 +4,10 @@ * * NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM) * -* This example has been created using raylib 1.1 (www.raylib.com) +* This example has been created using raylib 1.3 (www.raylib.com) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) * -* Copyright (c) 2014 Ramon Santamaria (Ray San - raysan@raysanweb.com) +* Copyright (c) 2015 Ramon Santamaria (@raysan5) * ********************************************************************************************/ |
