From e5fe2c216ec92d6ec2205fac10023b5a304f6f26 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Tue, 1 Sep 2015 22:59:16 +0200 Subject: Added some comments to examples --- examples/makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/makefile') diff --git a/examples/makefile b/examples/makefile index f351fbad..1f3ddb7f 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. -- cgit v1.2.3 From 9a578c59624f671418a1f3b046b6b09aa4233909 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 2 Sep 2015 01:06:55 +0200 Subject: Added shaders examples resources --- examples/makefile | 70 +++++++++++++++++++++++++- examples/resources/shaders/base.vs | 14 +++--- examples/resources/shaders/bloom.fs | 42 ++++++++++++++++ examples/resources/shaders/custom.fs | 16 ------ examples/resources/shaders/custom.vs | 16 ------ examples/resources/shaders/grayscale.fs | 17 ++++--- examples/resources/shaders/shapes_base.vs | 19 +++++++ examples/resources/shaders/shapes_grayscale.fs | 15 ++++++ examples/resources/shaders/swirl.fs | 41 +++++++++++++++ 9 files changed, 203 insertions(+), 47 deletions(-) create mode 100644 examples/resources/shaders/bloom.fs delete mode 100644 examples/resources/shaders/custom.fs delete mode 100644 examples/resources/shaders/custom.vs create mode 100644 examples/resources/shaders/shapes_base.vs create mode 100644 examples/resources/shaders/shapes_grayscale.fs create mode 100644 examples/resources/shaders/swirl.fs (limited to 'examples/makefile') diff --git a/examples/makefile b/examples/makefile index 1f3ddb7f..15a4bd0a 100644 --- a/examples/makefile +++ b/examples/makefile @@ -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,23 @@ 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_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 +213,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 +245,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 +289,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) @@ -295,6 +345,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/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/custom.fs b/examples/resources/shaders/custom.fs deleted file mode 100644 index 1e53933b..00000000 --- a/examples/resources/shaders/custom.fs +++ /dev/null @@ -1,16 +0,0 @@ -#version 330 - -uniform sampler2D texture0; -varying vec2 fragTexCoord; - -uniform vec4 tintColor; - -void main() -{ - 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, tintColor.a); -} \ No newline at end of file diff --git a/examples/resources/shaders/custom.vs b/examples/resources/shaders/custom.vs deleted file mode 100644 index 629c954d..00000000 --- a/examples/resources/shaders/custom.vs +++ /dev/null @@ -1,16 +0,0 @@ -#version 330 - -attribute vec3 vertexPosition; -attribute vec2 vertexTexCoord; -attribute vec3 vertexNormal; - -uniform mat4 projectionMatrix; -uniform mat4 modelviewMatrix; - -varying vec2 fragTexCoord; - -void main() -{ - fragTexCoord = vertexTexCoord; - gl_Position = projectionMatrix*modelviewMatrix*vec4(vertexPosition, 1.0); -} \ 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/shapes_base.vs b/examples/resources/shaders/shapes_base.vs new file mode 100644 index 00000000..78e543b7 --- /dev/null +++ b/examples/resources/shaders/shapes_base.vs @@ -0,0 +1,19 @@ +#version 110 + +attribute vec3 vertexPosition; +attribute vec2 vertexTexCoord; +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/shapes_grayscale.fs b/examples/resources/shaders/shapes_grayscale.fs new file mode 100644 index 00000000..1b778871 --- /dev/null +++ b/examples/resources/shaders/shapes_grayscale.fs @@ -0,0 +1,15 @@ +#version 110 + +uniform sampler2D texture0; +varying vec2 fragTexCoord; +varying vec4 fragColor; + +void main() +{ + 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, 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 -- cgit v1.2.3 From a7a81ca7492772637c43cb48a92405784872f857 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 2 Sep 2015 02:41:21 +0200 Subject: Added example: box collisions --- examples/makefile | 5 ++ examples/models_box_collisions.c | 119 +++++++++++++++++++++++++++++++++++++ examples/models_box_collisions.png | Bin 0 -> 22077 bytes 3 files changed, 124 insertions(+) create mode 100644 examples/models_box_collisions.c create mode 100644 examples/models_box_collisions.png (limited to 'examples/makefile') diff --git a/examples/makefile b/examples/makefile index 15a4bd0a..dac378ce 100644 --- a/examples/makefile +++ b/examples/makefile @@ -179,6 +179,7 @@ EXAMPLES = \ text_format_text \ text_font_select \ models_geometric_shapes \ + models_box_collisions \ models_billboard \ models_obj_loading \ models_heightmap \ @@ -325,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) 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 new file mode 100644 index 00000000..d01fd9dd Binary files /dev/null and b/examples/models_box_collisions.png differ -- cgit v1.2.3