Reputation: 7190
I am trying to write a porting of glm in java.
At the moment I created a package jglm and inside this package there are all the different classes Mat4, Vec4, Vec3, etc..
Such as:
public class Mat {
protected float[] matrix;
protected int order;
}
I can call them by typing
jgml.Mat4 modelviewMatrix = new Jgml.Mat4(1.0f);
And this is fine..
Now I am writing also some methods, like
mix(x, y, lerp)
And I also would like to use this as stated, that is
float value = jglm.mix(x, y, lerp)
But of course in this case Jglm must be a class...
Is there a way to conjugate the two things together?
Edit: If I create a class jglm.Jglm
package jglm;
/**
*
* @author gbarbieri
*/
public class Jglm {
public class Mat {
protected float[] matrix;
protected int order;
}
public class Mat4 extends Mat {
public Vec4 c0;
public Vec4 c1;
public Vec4 c2;
public Vec4 c3;
public Mat4(float value) {
order = 4;
matrix = new float[16];
for (int i = 0; i < 4; i++) {
matrix[i * 5] = value;
}
c0 = new Vec4(matrix, 0);
c1 = new Vec4(matrix, 4);
c2 = new Vec4(matrix, 8);
c3 = new Vec4(matrix, 12);
}
public float[] toFloatArray() {
return new float[]{
c0.x, c0.y, c0.z, c0.w,
c1.x, c1.y, c1.z, c1.w,
c2.x, c2.y, c2.z, c2.w,
c3.x, c3.y, c3.z, c3.w,};
}
}
public static float mix(float start, float end, float lerp) {
return (start + lerp * (end - start));
}
}
When I try to instantiate
cameraToClipMatrix_mat4 = new Jglm.Mat4(1.0f);
I get "an enclosing instance that contains Jglm.Mat4 is required"
Upvotes: 0
Views: 1567
Reputation: 200306
At the moment I created a package Jglm and inside this package there are all the different classes Mat4, Vec4, Vec3, etc..
In Java, packages must be named in all lowercase. Naming it the way you did will surprise anyone who looks at your code and may also cause serious name-resolution issues for the compiler.
It is also customary to import all classes we are using so we refer to them just by their simple name in code. Perhaps you could consider declaring
package org.example.jglm;
public class Jglm {
public static void mix(double x, double y, Lerp lerp) {
...
}
}
on the client side, you'd write
import org.example.Jglm;
void someMethod() {
Jglm.mix(x,y,lerp);
}
In general, when you need some pure functions in your code, then declare them as static
methods. Look at java.lang.Math
source code for guidance.
Upvotes: 2
Reputation: 68725
Having all the methods in your Jglm class as static will enable the method calls, both by instance and by classname.
Upvotes: 1
Reputation: 122026
Declare all your methods in Mat
class as static
which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class.
Ex: ClassName.methodName(args)
See Class Methods in http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
Upvotes: 2