gl3n - linear algebra and more for D

Alex Rønne Petersen xtzgzorex at gmail.com
Sun Dec 4 05:16:34 PST 2011


On 03-12-2011 23:36, David wrote:
> Am 03.12.2011 22:32, schrieb Kiith-Sa:
>> David wrote:
>>
>>> Hello,
>>>
>>> I am currently working on gl3n - https://bitbucket.org/dav1d/gl3n - gl3n
>>> provides all the math you need to work with OpenGL, DirectX or just
>>> vectors and matrices (it's mainly targeted at graphics - gl3n will never
>>> be more then a pure math library). What it supports:
>>>
>>> * vectors
>>> * matrices
>>> * quaternions
>>> * interpolation (lerp, slerp, hermite, catmull rom, nearest)
>>> * nearly all glsl functions (according to spec 4.1)
>>> * some more cool features, like templated types (vectors, matrices,
>>> quats), cool ctors, dynamic swizzling
>>>
>>> And the best is, it's MIT licensed ;). Unfortunatly there's no
>>> documentation yet, but it shouldn't be hard to understand how to use it,
>>> if you run anytime into troubles just take a look into the source, I did
>>> add to every part of the lib unittests, so you can see how it works when
>>> looking at the unittests, furthermore I am very often at #D on freenode.
>>> But gl3n isn't finished! My current plans are to add more interpolation
>>> functions and the rest of the glsl defined functions, but I am new to
>>> graphics programming (about 4 months I am now into OpenGL), so tell me
>>> what you're missing, the chances are good that I'll implement and add
>>> it. So let me know what you think about it.
>>>
>>> Before I forget it, a bit of code to show you how to use gl3n:
>>>
>>> ------------------------------------------------------------------------
>>> vec4 v4 = vec4(1.0f, vec3(2.0f, 3.0f, 4.0f));
>>> vec4 v4 = vec4(1.0f, vec4(1.0f, 2.0f, 3.0f, 4.0f).xyz)); // "dynamic"
>>> swizzling with opDispatch
>>> vec3 v3 = my_3dvec.rgb;
>>> float[] foo = v4.xyzzzwzyyxw // not useful but possible!
>>> glUniformMatrix4fv(location, 1, GL_TRUE, mat4.translation(-0.5f, -0.54f,
>>> 0.42f).rotatex(PI).rotatez(PI/2).value_ptr); // yes they are row major!
>>> mat3 inv_view = view.rotation;
>>> mat3 inv_view = mat3(view);
>>> mat4 m4 = mat4(vec4(1.0f, 2.0f, 3.0f, 4.0f), 5.0f, 6.0f, 7.0f, 8.0f,
>>> vec4(...) ...);
>>>
>>> struct Camera {
>>> vec3 position = vec3(0.0f, 0.0f, 0.0f);
>>> quat orientation = quat.identity;
>>>
>>> Camera rotatex(real alpha) { orientation.rotatex(alpha); return this; }
>>> Camera rotatey(real alpha) { orientation.rotatey(alpha); return this; }
>>> Camera rotatez(real alpha) { orientation.rotatez(alpha); return this; }
>>>
>>> Camera move(float x, float y, float z) {
>>> position += vec3(x, y, z);
>>> return this;
>>> }
>>> Camera move(vec3 s) {
>>> position += s;
>>> return this;
>>> }
>>>
>>> @property camera() {
>>> //writefln("yaw: %s, pitch: %s, roll: %s",
>>> degrees(orientation.yaw), degrees(orientation.pitch),
>>> degrees(orientation.roll));
>>> return mat4.translation(position.x, position.y, position.z) *
>>> orientation.to_matrix!(4,4);
>>> }
>>> }
>>>
>>> glUniformMatrix4fv(programs.main.view, 1, GL_TRUE,
>>> cam.camera.value_ptr);
>>> glUniformMatrix3fv(programs.main.inv_rot, 1, GL_TRUE,
>>> cam.orientation.to_matrix!(3,3).inverse.value_ptr);
>>> ------------------------------------------------------------------------
>>>
>>> I hope this gave you a little introduction of gl3n.
>>>
>>> - dav1d
>>
>>
>> I looked at your project yesterday (found it on derelict forums)
>> and it looks really good. Currently I'm using my own code for
>> vectors/matrices but a dedicated library could be better.
>>
>>
>> My comments:
>>
>> Not sure if DMD will do a good job optimizing your code atm
>> (probably no way around this but to wait - uglifying the code would serve
>> no purpose)
>>
>> In the future, SSE support would be nice (maybe will be easier to do
>> if we
>> ever get SSE intrinsics)
>>
>> Seems like most of the code is in linalg.d - wouldn't it be more
>> maintainable
>> to have it separated for each struct, and then public import it
>> through one
>> module for easy usage?
>>
>> I'm doing a lot of 2D work, and could use at least a rectangle/aabbox
>> struct
>> (if I use your lib, I'll implement rectangles on top of it anyway).
>> Some other structs might also be useful (3D aabbox, circle, sphere?)
>> Although, if you want to be as close to GLSL as possible, this might
>> not be a good idea.
>>
>> Most D projects are under the Boost license.
>> If you want to get this to Phobos,
>> (I'd like something like this in Phobos :P)
>> I recommend using that license
>> (IANAL, but I don't see much difference between MIT and Boost)
>>
>> The GLSL style is good if you want it as close to GLSL as possible,
>> but it'd be good to have more D-style aliases (again hinting at Phobos).
>> (Personally I'd probably use the GLSL style, though)
>>
> Hi,
>
> Thanks for your feedback. SSE is planed, but it will be the last step,
> optimization at the end. Well gl3n shouldn't be the bottleneck anyways,
> because it's normally the GPU.
> I've already thought about splitting linalg into 3 different files (also
> it was suggested by some people), but I dont like how D(md) handles
> imports, something like this would be cool:
>
> import gl3n.linalg.matrix;
> import gl3n.linalg.vector;
> import gl3n.linalg.quaternion;
> import gl3n.linalg; // this would import
> gl3n.linalg.matrix/vector/quaternion publically
>
> Like __init__.py in Python, unfortunatly this isn't supported (yet?).
>
> It is also planed to add some useful stuff for graphics programming,
> like as you mentioned spheres or AABB (axis aligned bounding boxes).
> Well I dont want it to be GLSL conform (then it would be glm), because I
> dont like all of the GLSL design choices and D is much more poweful!
>
> I am glad you like it :)
>
> - dav1d

You can make a gl3n.linalg.all modules that goes like:

public import gl3n.linalg.matrix;
public import gl3n.linalg.vector;

etc...

- Alex


More information about the Digitalmars-d-announce mailing list