Shaders unification: Phong shading, bump mapping and CommonSurfaceShader on mobile, 100% modern rendering on desktop

Posted on

Steep parallax bump mapping with self-shadowing on Android
Monkey with bump mapping on Android
lizardman animation with bump mapping on Android
FPS game example on Android

I have just finished a large rework of our rendering code 🙂 This unifies desktop (OpenGL) and mobile (OpenGLES) shader rendering better, and brings many new rendering features:

  • You can now choose between Gouraud or Phong shading, on both OpenGLES (mobile) or OpenGL (desktop). Our shaders support all combinations. By default we do Gouraud shading, but you can switch to Phong for the whole scene by Scene.Attributes.PhongShading := true or only for a particular shape using Shape.Shading := shPhong. See also the X3D Shape.shading field.

  • Bump mapping (even steep parallax bump mapping with self-shadowing), specular maps and other CommonSurfaceShader features are now fully available on OpenGLES (mobile platforms).

  • You can set global EnableFixedFunction variable to false on desktops, to force absolutely all rendering to go through shaders, without any fixed-function calls. This makes our desktop rendering 100% based on shaders, instead of the previous mix of shaders and fixed-function operations. This will be the default (on modern GPUs) soon, and you’re welcome to test it now. You can also test it using view3dscene, just pass command-line option --debug-disable-fixed-function.

    Update: TODO: We still use fixed-function commands for shadow volume rendering and occlusion query features. This will be fixed (along with some OpenGLES upgrades), but until then: using these features will make your application call fixed-function commands, regardless of the EnableFixedFunction flag. Contributions to fix it rather sooner than later and most welcome! These are easy, local problems, they just need a dedicated person to handle them!

  • P.S. See also a simple example how to build a scene with custom shaders (X3D ComposedShader node), on display_box_custom_shaders.lpr.

Comments on the forum ➤

Lots of improvements for developers: iOS, Android, camera NavigationType, T3D.Visible, more!

Posted on

sdr

Lots of new things for developers! 🙂

  1. Improvements for iOS application building:

    • Additional attributes in CastleEngineManifest.xml for iOS: team identifier, overriding qualified name and version for iOS, specifying uses_non_exempt_encryption.

    • Loading music from OggVorbis (using Tremolo customized for iOS).

    • Compilation fixes (workarounds for some FPC problems) to work in release mode.

    • Fixed touch up (mouse up) event. Test multi-touch with our drawing_toy example.

  2. Fixed Android building from Windows.

  3. Large camera API simplification:

    In short: TUniversalCamera class is now gone. Change the navigation type using the new SceneManager.NavigationType property.

    Details: I came to a realization that the TUniversalCamera class is a needless complication. We now expose NavigationType at TCastleAbstractViewport (ancestor of TCastleSceneManager and TCastleViewport).

    We also expose methods ExamineCamera and WalkCamera at TCastleAbstractViewport. They create the camera instance, and can switch the navigation as requested.

    So now you can do

    SceneManager.NavigationType := ntWalk;
    

    instead of the previous (ugly):

    (SceneManager.RequiredCamera as TUniversalCamera).NavigationType := ntWalk;
    

    And instead of

    SceneManager.Camera := SceneManager.CreateDefaultCamera;
    (SceneManager.Camera as TUniversalCamera).NavigationType := ntWalk;
    (SceneManager.Camera as TUniversalCamera).Walk.MoveSpeed := 10;
    

    now you can do

    SceneManager.WalkCamera.MoveSpeed := 10;
    

    This is much simpler, right? 🙂 It’s also safer, without these ugly typecasts.

  4. More new stuff!

  5. And, in case you missed our announcements from 2 weeks ago, our engine is now integrated with an amazing Kraft Physics Engine by Benjamin ‘BeRo’ Rosseaux.

    The manual page about physics in Castle Game Engine should be helpful.

    The main API point to start reading is the T3DTransform.RigidBody property, and the things it links to: TRigidBody, TCollider and it’s descendants.

Comments on the forum (3) ➤

Castle Game Engine 2D physics using Kraft

Posted on

Castle Game Engine 2D physics demonstration. A quick 2D game where a plane is using spheres to shoot the boxes 🙂 The physics is done using the fantastic Kraft Physics Engine!

The code to do this is 100% open-source, you can view it on GitHub here: physics_2d_game_sopwith (the main code is in “game.pas” unit).

Also, we now have a manual page describing how to use physics in Castle Game Engine.

This work was sponsored by Castle Game Engine supporters on Patreon. If you want to help in the engine development, please support me on Patreon! Thank you!

Comments on the forum ➤

Castle Game Engine physics – Mesh Collider

Posted on

More progress integrating Castle Game Engine with Kraft Physics Engine!

  1. New physics classes (TRigidBody, TCollider and descendants) are now part of the Castle Game Engine core.

    To use this yourself, see the T3DTransform.RigidBody property, and the TRigidBody and TCollider descendants documentation.

    The example code on https://github.com/castle-engine/castle-engine/tree/master/examples/physics/physics_3d_demo should also help 🙂

  2. We have mesh collider, to perform collisions with a scene mesh.

    The mesh collider was beautifully trivial to implement. I honestly didn’t expect it to be so easy to code. Lots of kudos go to Benjamin ‘BeRo’ Rosseaux for creating the wonderful Kraft Physics Engine!

This was implemented thanks to the supporters of Castle Game Engine on Patreon. If you like what I do, please support Castle Game Engine on Patreon!

Comments on the forum ➤

Castle Game Engine integration with Kraft Physics Engine

Posted on

First results of Castle Game Engine integration with Kraft Physics Engine! 🙂

The physics demo source code is in GitHub in engine examples.

Kraft Physics Engine by amazing Benjamin ‘BeRo’ Rosseaux. Open-source, clean Object Pascal code, portable (Delphi, FPC, including FPC on Android and iOS).

I want to extend this a bit, and will post more details (on Castle Game Engine news/blog) soon. For now just enjoy the video 🙂

The physics fun was sponsored by Castle Game Engine supporters on Patreon. If you want to help in the engine development, please support me on Patreon!

Comments on the forum ➤

New features: KTX (Khronos texture format), nvcompress, X3D helpers…

Posted on

lets_take_a_walk_screen_3
lets_take_a_walk_screen_4
lets_take_a_walk_screen_5
fps_game_screen_0
fps_game_screen_2

As a little break from the work on Delphi compatibility, I added some new features to the engine 🙂

  1. KTX (Khronos Texture format) support throughout the engine. See the details about KTX features supported. KTX format is in many ways an alternative to DDS, with a clean specification, supporting the same features (GPU compression, 2D and 3D textures…). The KTX Khronos page has links to various tools that can create KTX files.

    You can test it now by downloading glViewImage 1.7.0! Sample KTX files are e.g. inside official KTX repository.

  2. Support nvcompress for automatic compressing of textures for GPU.

    nvcompress is part of the NVidia Texture Tools. It is cross-platform (Windows, Linux…), free and open source. On Debian and derivatives (like Ubuntu) you can install it simply by apt-get install libnvtt-bin. Thanks to Eugene Loza for advicing it!

    The nvcompress will be automatically used underneath when you call castle-engine auto-generate-textures in your project. This requires using our build tool to manage your game project.

  3. Our fps_demo shows how to easily get texture compression in your games. See the material_properties.xml in fps_game for how to trivially compress your textures to DXT1 and DXT5. See material_properties.xml and texture compression documentation for more information.

    There was also an important fix to using this approach for textures referenced from X3D.

  4. Useful helpers for X3D building, to easily create and modify X3D graph:

    • New methods AddChildren, RemoveChildren for grouping nodes (like TTransformNode, TGroupNode).
    • New methods CreateShape, CreateTransform for geometry nodes (like TBoxNode, TIndexedFaceSetNode).
    • Also on geometry nodes: Coord, FogCoord, TexCoord and some more are now simple properties.
    • SetXxx methods available to set various multiple-value (array) fields. Use them like this:
      Coord.SetPoint([Vector3(...), Vector3(...)]);
      IndexedFaceSet.SetCoordIndex([0, 1, 2]);
      Inline.SetUrl(['my_file.x3d']);
      

      These look much better than

      Coord.FdPoint.Items.Assign([Vector3(...), Vector3(...)]);
      Coord.FdPoint.Changed;
      

      and they always cause an appropriate update (they automatically will call Changed for you, and send the new value using X3D events).

    This is all part of my ongoing effort to enable you to operate on X3D graph more comfortably, without accessing “somewhat-internal” fields instances in FdXxx (like FdChildren, FdCoord…).

  5. Improved and simplified lets_take_a_walk example (examples/3d_sound_game/).
Comments on the forum ➤

Delphi base compatibility, Spine improvements, other stuff :)

Posted on

Delphi running CGE base test :)
  1. Intensive work on the engine Delphi compatibility is ongoing 🙂

    We can now actually compile and run in Delphi (10.2) a simple console demo using our “base” units. Vectors, matrices, colors, rectangles, time measuring, logging, a lot of string and class and other utilities… it’s all working in Delphi. This is the demo code.

    In parallel, I’m fixing the syntax of all the code to compile in “FPC Delphi test mode”, this is providing a nice baseline to later compile in the actual Delphi. On this front, I almost finished porting everything non-visual (I’m finishing the X3DNodes unit, which is a huge unit that depends on almost everything non-visual in the engine). So, we’re close 🙂

    The “FPC Delphi test mode” means that we use FPC, but in Delphi mode, and disable macros, and disable operators like += etc. And I can run on Linux too, which is comfortable for me 🙂 So it’s like “make FPC behave as much as Delphi”. Any changes required at this stage would be required by Delphi too. To use it, you can set environment variable CASTLE_ENGINE_TEST_DELPHI_MODE to “true” before running the build tool.

  2. Units cleanup was done along the way. I rearranged some units, to remove some uncomfortable dependencies (when too many units are inter-dependent on each other). In effect, we now have a “files” unit group that includes units from old group “net” + units dealing with files. More details in the units map in the wiki.

  3. Spine (2D animations format) improvements:

    1. When animating in Spine using Bezier curves (translations or rotations), the resulting movement in our engine is now exactly what you see in Spine.
    2. Animating the scale in Spine is now fixed (it was accidentally changing the order of layers).
  4. DDS (image file format) improvements: support reading DX10 + DXGI_FORMAT_R8G8B8A8_UNORM case (produced e.g. by PowerVR Texture Tools when converting RGBA image to DDS).

  5. In a spur of the moment, I wrote a short page about “What are range and overflow checks (and errors) in Pascal” 🙂

  6. TDebug3D class is a new simple approach to visualize debug things attached to 3D objects.

Comments on the forum (1) ➤

New modern API for vectors and matrices

Posted on

Chinchilla model in X3D, based on high-poly version in "Big Buck Bunny"

Our unit CastleVectors was completely reworked this week, and it now features a new, comfortable API for vector and matrix types, using “advanced records” (records with methods).

I hope that you like the changes 🙂 Of course you can already test it all by using the engine version from GitHub !

Type names

The main vector and matrix types (using Single-precision) are now called TVector2, TVector3, TVector4 (in short: TVector{2,3,4}) and TMartix{2,3,4}. So we have 2D, 3D and 4D vectors (4D vectors are useful for “homogeneous coordinates” in 3D, and to store colors with alpha). And we have 2×2, 3×3, 4×4 matrices.

These correspond to previous array-based types called TVector{2,3,4}Single and TMatrix{2,3,4}Single. As you can see, we have removed the Single suffix, as these types are used very often throughout the engine and, as such, they are our “default” vector and matrix types.

As before, we also feature vectors with Double precision (TVector{2,3,4}Double), and based on various integer types (Byte, Integer and Cardinal). They are all available in 2D, 3D, 4D variants, and are implented using the new “advanced records approach” and are consistent with Single-precision vectors.

Features

  • As these types are “advanced records”, they include various methods, class methods and constants nicely grouped within the record. This makes the API look nicer, e.g. now you can write MyVector.Length instead of VectorLen(MyVector). They have one field, Data, which is defined like array [0..2] of Single for TVector3. This allows to keep them fast (in terms of speed, and memory layout). See the documentation of the generic 3D vector (this API is used by both TVector3 and TVector3Double).

  • You can access the individual components of the vector as V.X, V[0] or V.Data[0]. And the last two notations are checked, even at compile-time, so this will make a clear warning (that index is 3, but should be between 0..2):

    uses CastleVectors;
    var
      V: TVector3;
    begin
      V[3] := 123; // incorrect example, index should be in 0..2 !
    end.
    
  • All the operators, like + and *, are overloaded on vectors and matrices. Just like with old types.

  • These types work in both FPC and Delphi. They work without the FPC macros (as Delphi doesn’t support them). I wish they could use generics, but they cannot (it’s not possible to put constraints on Pascal generics to enable efficient arithmetic operations on fields).

  • Along with it, triangles (TTriangle{2,3,4}) was also remodelled as a record, and the Double-precision equivalent (TTriangle3Double) was removed (not really useful, and implementing it without macros or generics is uneasy). See TTriangle3 documentation.

  • Along with it, the TBox3D was a bit improved, it now has Min and Max properties, that look friendlier than Data[0] and Data[1]. See TBox3D documentation.

  • Along with it, the color types have changed too, as TCastleColor and TCastleColorRGB are still just aliases for (respectively) TVector4 and TVector3. See CastleColors unit documentation.

  • The new vectors vaguely resemble the vectors from Delphi standard unit System.Math.Vectors, but are better in many ways. We offer many more types and with many more methods. And, frankly, our naming is more consistent, IMHO 🙂

Backward compatibility

While the change is for good, it is not 100% backward-compatible. I put a lot of effort to minimize the compatibility “disruption”, adding many “glue” functions and types to keep your existing code working (and merely warn about being “deprecated” during compilation). But there is still chance that you will need to change something in your code to make it compile with the engine >= 6.3 API. Read the hints below, and please ask on the forum if you’re unsure how to upgrade.

The changes that break compatibility (things that you will have to change in your code in order to compile):

  • When declaring constants, your old code may use this:

    const MyConstant: TVector2Single = (1.0, 2.0); // old code

    As the vectors, matrices, colors and triangles are now record types with a Data field, so it has to be changed to:

    const MyConstant: TVector2Single = (Data: (1.0, 2.0));
  • The TBox3D.Data, and your custom array of TVectorXxx types, are no longer “arrays of arrays”. They are now “arrays of records”. Your old code may use this:

    Box.Data[0, 0] := 0.0; // old code

    This has to be changed to:

    Box.Data[0][0] := 0.0;

    Or this, if you want to avoid any “getter” properties and just access the (addressable) variable directly:

    Box.Data[0].Data[0] := 0.0;
  • Although you can still access the vector component using V[0], this is now a “getter” property. It often doesn’t matter… unless you want to use it as var or out parameter for some function, or if you use C-like operators like +=. So if your old code has this:

    V[0] += 10; // old code

    This has to be changed to this:

    V.Data[0] += 10;

    Of course, in this case you can also just resign from using C-like operators, and have this:

    V[0] := V[0] + 10;

    Or this:

    V.X := V.X + 10;
Sidenote: I tried a different approach to backward-compatibility too (keeping both old and new APIs in parallel) but this was creating more compatibility problems than it was solving (gory details in the KEEP_OLD_VECTOR_API comments in this commit).

Have fun with the new API, and, as always, please ask on the forum if you have any questions!

Comments on the forum ➤

Generics.Collections in the book and CGE

Posted on

To help you familiarize with the Generics.Collections unit, I wrote a nice chapter about it in the “Modern Pascal Introduction”. It’s a longer chapter, with 4 nice examples using containers from Generics.Collections.

In the related news: complete migration to Generics.Collections in Castle Game Engine is done 🙂 We now use Generics.Collections for all the purposes where we previously used the FGL and CastleGenericLists units. This brings us closer to Delphi compatibility, and it makes code better (Generics.Collections works with records out-of-the-box, it has a TDictionary with a much cleaner API, etc.)

Also, Michalis is back from vacations, with lots of new strength 🙂

Comments on the forum ➤