diff --git a/examples/core/core_quat_conversion.c b/examples/core/core_quat_conversion.c index 5f2fc36e..0b2bb01e 100644 --- a/examples/core/core_quat_conversion.c +++ b/examples/core/core_quat_conversion.c @@ -2,29 +2,21 @@ * * raylib [core] example - quat conversions * -* Welcome to raylib! +* Generally you should really stick to eulers OR quats... +* This tests that various conversions are equivalent. * -* generally you should really stick to eulers OR quats... -* This tests that various conversions are equivilant. -* -* You can find all basic examples on [C:\raylib\raylib\examples] directory and -* 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 3.5 (www.raylib.com) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) * -* Copyright (c) 2013-2020 Ramon Santamaria (@raysan5) +* Example contributed by @chriscamacho and @codifies, reviewed by Ramon Santamaria (@raysan5) +* +* Copyright (c) 2020 @chriscamacho and @codifies * ********************************************************************************************/ #include "raylib.h" -#include "raymath.h" -#ifndef PI2 - #define PI2 PI*2 -#endif +#include "raymath.h" int main(void) { @@ -48,23 +40,25 @@ int main(void) SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- - Quaternion q1; - Matrix m1,m2,m3,m4; - Vector3 v1,v2; - + Quaternion q1; + Matrix m1,m2,m3,m4; + Vector3 v1,v2; + // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update - if (!IsKeyDown(KEY_SPACE)) { + //-------------------------------------------------------------------------------------- + if (!IsKeyDown(KEY_SPACE)) + { v1.x += 0.01; v1.y += 0.03; v1.z += 0.05; } - if (v1.x > PI2) v1.x-=PI2; - if (v1.y > PI2) v1.y-=PI2; - if (v1.z > PI2) v1.z-=PI2; + if (v1.x > PI*2) v1.x -= PI*2; + if (v1.y > PI*2) v1.y -= PI*2; + if (v1.z > PI*2) v1.z -= PI*2; q1 = QuaternionFromEuler(v1.x, v1.y, v1.z); m1 = MatrixRotateZYX(v1); @@ -74,10 +68,13 @@ int main(void) m3 = QuaternionToMatrix(q1); v2 = QuaternionToEuler(q1); - v2.x*=DEG2RAD; v2.y*=DEG2RAD; v2.z*=DEG2RAD; + v2.x *= DEG2RAD; + v2.y *= DEG2RAD; + v2.z *=DEG2RAD; m4 = MatrixRotateZYX(v2); - + //-------------------------------------------------------------------------------------- + // Draw //---------------------------------------------------------------------------------- BeginDrawing(); @@ -94,17 +91,16 @@ int main(void) mod.transform = m4; DrawModel(mod, (Vector3){0,0,-1},1.0,RED); - DrawGrid(10, 1.0f); EndMode3D(); - if (v2.x<0) v2.x+=PI2; - if (v2.y<0) v2.y+=PI2; - if (v2.z<0) v2.z+=PI2; + if (v2.x < 0) v2.x += PI*2; + if (v2.y < 0) v2.y += PI*2; + if (v2.z < 0) v2.z += PI*2; Color cx,cy,cz; - cx=cy=cz=BLACK; + cx = cy = cz = BLACK; if (v1.x == v2.x) cx = GREEN; if (v1.y == v2.y) cy = GREEN; if (v1.z == v2.z) cz = GREEN; @@ -113,7 +109,6 @@ int main(void) DrawText(TextFormat("%2.3f",v1.y),20,40,20,cy); DrawText(TextFormat("%2.3f",v1.z),20,60,20,cz); - DrawText(TextFormat("%2.3f",v2.x),200,20,20,cx); DrawText(TextFormat("%2.3f",v2.y),200,40,20,cy); DrawText(TextFormat("%2.3f",v2.z),200,60,20,cz); diff --git a/examples/core/core_quat_conversion.png b/examples/core/core_quat_conversion.png new file mode 100644 index 00000000..d4a9d14f Binary files /dev/null and b/examples/core/core_quat_conversion.png differ diff --git a/examples/shaders/resources/shaders/glsl100/color_mix.fs b/examples/shaders/resources/shaders/glsl100/color_mix.fs index f6ab7dcd..0074e6af 100644 --- a/examples/shaders/resources/shaders/glsl100/color_mix.fs +++ b/examples/shaders/resources/shaders/glsl100/color_mix.fs @@ -16,6 +16,9 @@ void main() // Texel color fetching from texture sampler vec4 texelColor0 = texture2D(texture0, fragTexCoord); vec4 texelColor1 = texture2D(texture1, fragTexCoord); - - gl_FragColor = (texelColor0 + texelColor1)*0.5f; + + float x = fract(fragTexCoord.s); + float out = smoothstep(0.4, 0.6, x); + + gl_FragColor = mix(texelColor0, texelColor1, out); } \ No newline at end of file diff --git a/examples/shaders/resources/shaders/glsl330/color_mix.fs b/examples/shaders/resources/shaders/glsl330/color_mix.fs index bac8d576..11515b0c 100644 --- a/examples/shaders/resources/shaders/glsl330/color_mix.fs +++ b/examples/shaders/resources/shaders/glsl330/color_mix.fs @@ -18,5 +18,8 @@ void main() vec4 texelColor0 = texture(texture0, fragTexCoord); vec4 texelColor1 = texture(texture1, fragTexCoord); - finalColor = (texelColor0 + texelColor1)*0.5f; + float x = fract(fragTexCoord.s); + float out = smoothstep(0.4, 0.6, x); + + finalColor = mix(texelColor0, texelColor1, out); } \ No newline at end of file diff --git a/examples/shaders/shaders_multi_sample2d.png b/examples/shaders/shaders_multi_sample2d.png new file mode 100644 index 00000000..435b8f48 Binary files /dev/null and b/examples/shaders/shaders_multi_sample2d.png differ diff --git a/examples/shaders/shaders_rlgl_mesh_instanced.c b/examples/shaders/shaders_rlgl_mesh_instanced.c index 6ef421f2..4cf780aa 100644 --- a/examples/shaders/shaders_rlgl_mesh_instanced.c +++ b/examples/shaders/shaders_rlgl_mesh_instanced.c @@ -4,17 +4,16 @@ * * This example uses [rlgl] module funtionality (pseudo-OpenGL 1.1 style coding) * -* This example has been created using raylib 3.0 (www.raylib.com) +* This example has been created using raylib 3.5 (www.raylib.com) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) * -* Copyright (c) 2018 Ramon Santamaria (@raysan5) +* Example contributed by @seanpringle and reviewed by Ramon Santamaria (@raysan5) +* +* Copyright (c) 2020 @seanpringle * ********************************************************************************************/ -#define GRAPHICS_API_OPENGL_33 -#define GLSL_VERSION 330 -#include #include "raylib.h" #include "raymath.h" #include "rlgl.h" @@ -22,6 +21,10 @@ #define RLIGHTS_IMPLEMENTATION #include "rlights.h" +#include + +#define GLSL_VERSION 330 + //------------------------------------------------------------------------------------ // Program main entry point //------------------------------------------------------------------------------------ @@ -29,11 +32,11 @@ int main(void) { // Initialization //-------------------------------------------------------------------------------------- - const int screenWidth = 1024; - const int screenHeight = 768; + const int screenWidth = 800; + const int screenHeight = 450; SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available) - InitWindow(screenWidth, screenHeight, "raylib [shaders] example - rlgl module usage for instanced meshes"); + InitWindow(screenWidth, screenHeight, "raylib [shaders] example - rlgl mesh instanced"); // Define the camera to look into our 3d world Camera camera = { 0 }; @@ -43,14 +46,12 @@ int main(void) camera.fovy = 45.0f; camera.type = CAMERA_PERSPECTIVE; - SetCameraMode(camera, CAMERA_FREE); - const int count = 10000; // Number of instances to display Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f); - Matrix* rotations = RL_MALLOC(count * sizeof(Matrix)); // Rotation state of instances - Matrix* rotationsInc = RL_MALLOC(count * sizeof(Matrix)); // Per-frame rotation animation of instances - Matrix* translations = RL_MALLOC(count * sizeof(Matrix)); // Locations of instances + Matrix *rotations = RL_MALLOC(count*sizeof(Matrix)); // Rotation state of instances + Matrix *rotationsInc = RL_MALLOC(count*sizeof(Matrix)); // Per-frame rotation animation of instances + Matrix *translations = RL_MALLOC(count*sizeof(Matrix)); // Locations of instances // Scatter random cubes around for (int i = 0; i < count; i++) @@ -67,11 +68,10 @@ int main(void) float angle = (float)GetRandomValue(0, 10) * DEG2RAD; rotationsInc[i] = MatrixRotate(axis, angle); - rotations[i] = MatrixIdentity(); } - Matrix* transforms = RL_MALLOC(count * sizeof(Matrix)); // Pre-multiplied transformations passed to rlgl + Matrix *transforms = RL_MALLOC(count*sizeof(Matrix)); // Pre-multiplied transformations passed to rlgl Shader shader = LoadShader(FormatText("resources/shaders/glsl%i/base_lighting_instanced.vs", GLSL_VERSION), FormatText("resources/shaders/glsl%i/lighting.fs", GLSL_VERSION)); @@ -81,7 +81,7 @@ int main(void) shader.locs[LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos"); shader.locs[LOC_MATRIX_MODEL] = GetShaderLocationAttrib(shader, "instance"); - // ambient light level + // Ambient light level int ambientLoc = GetShaderLocation(shader, "ambient"); SetShaderValue(shader, ambientLoc, (float[4]){ 0.2f, 0.2f, 0.2f, 1.0f }, UNIFORM_VEC4); @@ -90,6 +90,8 @@ int main(void) Material material = LoadMaterialDefault(); material.shader = shader; material.maps[MAP_DIFFUSE].color = RED; + + SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- @@ -111,7 +113,6 @@ int main(void) rotations[i] = MatrixMultiply(rotations[i], rotationsInc[i]); transforms[i] = MatrixMultiply(rotations[i], translations[i]); } - //---------------------------------------------------------------------------------- // Draw @@ -124,7 +125,8 @@ int main(void) rlDrawMeshInstanced(cube, material, transforms, count); EndMode3D(); - DrawText("A CUBE OF DANCING CUBES!", 400, 10, 20, MAROON); + DrawText("A CUBE OF DANCING CUBES!", 490, 10, 20, MAROON); + DrawFPS(10, 10); EndDrawing(); diff --git a/examples/shaders/shaders_rlgl_mesh_instanced.png b/examples/shaders/shaders_rlgl_mesh_instanced.png new file mode 100644 index 00000000..24d38b87 Binary files /dev/null and b/examples/shaders/shaders_rlgl_mesh_instanced.png differ diff --git a/examples/textures/textures_blend_modes.c b/examples/textures/textures_blend_modes.c index bb19f56c..2d9b0af9 100644 --- a/examples/textures/textures_blend_modes.c +++ b/examples/textures/textures_blend_modes.c @@ -4,9 +4,11 @@ * * NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM) * -* This example has been created using raylib 3.0 (www.raylib.com) +* This example has been created using raylib 3.5 (www.raylib.com) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) * +* Example contributed by Karlo Licudine (@accidentalrebel) and reviewed by Ramon Santamaria (@raysan5) +* * Copyright (c) 2020 Karlo Licudine (@accidentalrebel) * ********************************************************************************************/ @@ -15,86 +17,77 @@ int main(void) { - // Initialization - //-------------------------------------------------------------------------------------- - const int screenWidth = 800; - const int screenHeight = 450; - - InitWindow(screenWidth, screenHeight, "raylib [textures] example - blend modes"); + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; - // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required) - Image bgImage = LoadImage("resources/cyberpunk_street_background.png"); // Loaded in CPU memory (RAM) - Texture2D bgTexture = LoadTextureFromImage(bgImage); // Image converted to texture, GPU memory (VRAM) + InitWindow(screenWidth, screenHeight, "raylib [textures] example - blend modes"); - Image fgImage = LoadImage("resources/cyberpunk_street_foreground.png"); // Loaded in CPU memory (RAM) - Texture2D fgTexture = LoadTextureFromImage(fgImage); // Image converted to texture, GPU memory (VRAM) + // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required) + Image bgImage = LoadImage("resources/cyberpunk_street_background.png"); // Loaded in CPU memory (RAM) + Texture2D bgTexture = LoadTextureFromImage(bgImage); // Image converted to texture, GPU memory (VRAM) - // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM - UnloadImage(bgImage); - UnloadImage(fgImage); + Image fgImage = LoadImage("resources/cyberpunk_street_foreground.png"); // Loaded in CPU memory (RAM) + Texture2D fgTexture = LoadTextureFromImage(fgImage); // Image converted to texture, GPU memory (VRAM) - const int blendCountMax = 4; - BlendMode blendMode = 0; - - // Main game loop - while (!WindowShouldClose()) // Detect window close button or ESC key - { - // Update - //---------------------------------------------------------------------------------- - int key = GetKeyPressed(); - if ((key >= 32) && (key <= 126)) - { - if ( blendMode >= blendCountMax - 1 ) - blendMode = 0; - else - blendMode++; - } - - // Draw - //---------------------------------------------------------------------------------- - BeginDrawing(); + // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM + UnloadImage(bgImage); + UnloadImage(fgImage); - ClearBackground(RAYWHITE); + const int blendCountMax = 4; + BlendMode blendMode = 0; + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + if (IsKeyPressed(KEY_SPACE)) + { + if (blendMode >= (blendCountMax - 1)) blendMode = 0; + else blendMode++; + } + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); - BeginBlendMode(BLEND_ALPHA); - DrawTexture(bgTexture, screenWidth/2 - bgTexture.width/2, screenHeight/2 - bgTexture.height/2, WHITE); + ClearBackground(RAYWHITE); - // Apply the blend mode and then draw the foreground texture - BeginBlendMode(blendMode); - DrawTexture(fgTexture, screenWidth/2 - fgTexture.width/2, screenHeight/2 - fgTexture.height/2, WHITE); + DrawTexture(bgTexture, screenWidth/2 - bgTexture.width/2, screenHeight/2 - bgTexture.height/2, WHITE); - EndBlendMode(); + // Apply the blend mode and then draw the foreground texture + BeginBlendMode(blendMode); + DrawTexture(fgTexture, screenWidth/2 - fgTexture.width/2, screenHeight/2 - fgTexture.height/2, WHITE); + EndBlendMode(); - // Draw the texts - DrawText("Press any key to change blend modes.", 310, 350, 10, GRAY); - switch ( blendMode ) - { - case BLEND_ALPHA: - DrawText("Current: BLEND_ALPHA", (screenWidth / 2) - 60, 370, 10, GRAY); - break; - case BLEND_ADDITIVE: - DrawText("Current: BLEND_ADDITIVE", (screenWidth / 2) - 60, 370, 10, GRAY); - break; - case BLEND_MULTIPLIED: - DrawText("Current: BLEND_MULTIPLIED", (screenWidth / 2) - 60, 370, 10, GRAY); - break; - case BLEND_ADD_COLORS: - DrawText("Current: BLEND_ADD_COLORS", (screenWidth / 2) - 60, 370, 10, GRAY); - break; - } - DrawText("(c) Cyberpunk Street Environment by Luis Zuno (@ansimuz)", screenWidth - 330, screenHeight - 20, 10, GRAY); + // Draw the texts + DrawText("Press SPACE to change blend modes.", 310, 350, 10, GRAY); + + switch (blendMode) + { + case BLEND_ALPHA: DrawText("Current: BLEND_ALPHA", (screenWidth / 2) - 60, 370, 10, GRAY); break; + case BLEND_ADDITIVE: DrawText("Current: BLEND_ADDITIVE", (screenWidth / 2) - 60, 370, 10, GRAY); break; + case BLEND_MULTIPLIED: DrawText("Current: BLEND_MULTIPLIED", (screenWidth / 2) - 60, 370, 10, GRAY); break; + case BLEND_ADD_COLORS: DrawText("Current: BLEND_ADD_COLORS", (screenWidth / 2) - 60, 370, 10, GRAY); break; + default: break; + } + + DrawText("(c) Cyberpunk Street Environment by Luis Zuno (@ansimuz)", screenWidth - 330, screenHeight - 20, 10, GRAY); - EndDrawing(); - //---------------------------------------------------------------------------------- - } + EndDrawing(); + //---------------------------------------------------------------------------------- + } - // De-Initialization - //-------------------------------------------------------------------------------------- - UnloadTexture(fgTexture); // Unload foreground texture - UnloadTexture(bgTexture); // Unload background texture + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadTexture(fgTexture); // Unload foreground texture + UnloadTexture(bgTexture); // Unload background texture - CloseWindow(); // Close window and OpenGL context - //-------------------------------------------------------------------------------------- - - return 0; + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; } diff --git a/src/rlgl.h b/src/rlgl.h index ce2291ca..b3492718 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -2870,12 +2870,6 @@ void rlDrawMesh(Mesh mesh, Material material, Matrix transform) void rlDrawMeshInstanced(Mesh mesh, Material material, Matrix *transforms, int count) { #if defined(GRAPHICS_API_OPENGL_33) - - if (!RLGL.ExtSupported.vao) { - TRACELOG(LOG_ERROR, "VAO: Instanced rendering requires VAO support"); - return; - } - // Bind shader program glUseProgram(material.shader.id); @@ -2912,23 +2906,21 @@ void rlDrawMeshInstanced(Mesh mesh, Material material, Matrix *transforms, int c // At this point the modelview matrix just contains the view matrix (camera) // For instanced shaders "mvp" is not premultiplied by any instance transform, only RLGL.State.transform - glUniformMatrix4fv(material.shader.locs[LOC_MATRIX_MVP], 1, false, MatrixToFloat( - MatrixMultiply(MatrixMultiply(RLGL.State.transform, RLGL.State.modelview), RLGL.State.projection) - )); + glUniformMatrix4fv(material.shader.locs[LOC_MATRIX_MVP], 1, false, + MatrixToFloat(MatrixMultiply(MatrixMultiply(RLGL.State.transform, RLGL.State.modelview), RLGL.State.projection))); - float16* instances = RL_MALLOC(count * sizeof(float16)); + float16* instances = RL_MALLOC(count*sizeof(float16)); - for (int i = 0; i < count; i++) - instances[i] = MatrixToFloatV(transforms[i]); + for (int i = 0; i < count; i++) instances[i] = MatrixToFloatV(transforms[i]); // This could alternatively use a static VBO and either glMapBuffer or glBufferSubData. // It isn't clear which would be reliably faster in all cases and on all platforms, and // anecdotally glMapBuffer seems very slow (syncs) while glBufferSubData seems no faster // since we're transferring all the transform matrices anyway. - unsigned int instancesB; + unsigned int instancesB = 0; glGenBuffers(1, &instancesB); glBindBuffer(GL_ARRAY_BUFFER, instancesB); - glBufferData(GL_ARRAY_BUFFER, count * sizeof(float16), instances, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, count*sizeof(float16), instances, GL_STATIC_DRAW); // Instances are put in LOC_MATRIX_MODEL attribute location with space for 4x Vector4, eg: // layout (location = 12) in mat4 instance; @@ -2937,19 +2929,15 @@ void rlDrawMeshInstanced(Mesh mesh, Material material, Matrix *transforms, int c for (unsigned int i = 0; i < 4; i++) { glEnableVertexAttribArray(instanceA+i); - glVertexAttribPointer(instanceA+i, 4, GL_FLOAT, GL_FALSE, sizeof(Matrix), (void*)(i * sizeof(Vector4))); - glVertexAttribDivisor(instanceA+i, 1); + glVertexAttribPointer(instanceA + i, 4, GL_FLOAT, GL_FALSE, sizeof(Matrix), (void *)(i*sizeof(Vector4))); + glVertexAttribDivisor(instanceA + i, 1); } glBindBuffer(GL_ARRAY_BUFFER, 0); // Draw call! - if (mesh.indices != NULL) { - // Indexed vertices draw - glDrawElementsInstanced(GL_TRIANGLES, mesh.triangleCount*3, GL_UNSIGNED_SHORT, 0, count); - } else { - glDrawArraysInstanced(GL_TRIANGLES, 0, mesh.vertexCount, count); - } + if (mesh.indices != NULL) glDrawElementsInstanced(GL_TRIANGLES, mesh.triangleCount*3, GL_UNSIGNED_SHORT, 0, count); + else glDrawArraysInstanced(GL_TRIANGLES, 0, mesh.vertexCount, count); glDeleteBuffers(1, &instancesB); RL_FREE(instances);