It's my first time here so be gently.
I've had this old graphics project laying around (written in oberon) and since i wrote it as one of my first projects it looks kinda chaotic.
So I descided that, since i'm bored anyway, i would rewrite it in java.
Everything so far seems to work... Until i try to rotate and do my eye-point transformation.
If i simply draw the image without rotating or changing the "eye's" position it works fine.
However the moment i try to do any of the operations that require me to multiply a point with a transformation matrix it all goes bad.
1. the eye point transformation generates stupidly small numbers with end coördinates like [-0.002027571306540029, 0.05938634628270456, -123.30022583847628]
this causes the resulting image to look empty but upon multiplying each coordinate with 1000 it turns out it's just moved around the screen rather than being rotated
if i then ignore the eye point and simply focus on my rotations the results are also pretty strange:
2. setting xRotation to 90° only makes the image very narrow and way too high (resolution should be 1000x1000 and is then 138x1000
3. setting yRotation to 90° makes it very wide (1000x138)
4. setting zRotation to 90° simply seems to translate the image all the way to the right side of the screen.
What i have checked so far:
1. i have checked and re-checked my rotation matrices at least 15 times now so they are correct
2. doing a test multiplication with a vector and the identity matrix does return the original vector
3. my matrices are initialized as identity matrices prior to being used as rotation matrices
4. the angles in the files are in degrees but are converted to radian when read.
Having said that i have 2 more notes:
1. a vector in this case is a simple 3 value array of doubles (obviously representing the x, y and z values)
2. a matrix is a 4x4 array of doubles initialized as the identity matrix
The reason i gave them their own classes is for code readability and to make it easier when doing operations on them.
So without further ado here are a couple of code snippets...
here's the code responsible for initializing the rotation matrices based on an angle or the eye-point vector if it's the eye-transformation matrix
[spoiler]
[/spoilerpublic static Matrix eye_transformation(Vector3D eye)throws ParseException { double r = eye.length(); double teta = Math.atan2(eye.y(), eye.x()); double zr = eye.z()/r; double fi = Math.acos(zr); Matrix v = new Matrix(); v.set(0, 0, -Math.sin(teta)); v.set(0, 1, -Math.cos(teta) * Math.cos(fi)); v.set(0, 2, Math.cos(teta) * Math.sin(fi)); v.set(1, 0, Math.cos(teta)); v.set(1, 1, -Math.sin(teta) * Math.cos(fi)); v.set(1, 2, Math.sin(teta) * Math.sin(fi)); v.set(2, 1, Math.sin(fi)); v.set(2, 2, Math.cos(fi)); v.set(3, 2, -r); return v; } public static Matrix z_rotation(double angle) throws ParseException { Matrix v = new Matrix();; v.set(0, 0, Math.cos(angle)); v.set(0, 1, Math.sin(angle)); v.set(1, 0, -Math.sin(angle)); v.set(1, 1, Math.cos(angle)); return v; } public static Matrix x_rotation(double angle) throws ParseException { Matrix v = new Matrix();; v.set(1, 1, Math.cos(angle)); v.set(1, 2, Math.sin(angle)); v.set(2, 1, -Math.sin(angle)); v.set(2, 2, Math.cos(angle)); return v; } public static Matrix y_rotation(double angle) throws ParseException { Matrix v = new Matrix(); v.set(0, 0, Math.cos(angle)); v.set(0, 2, -Math.sin(angle)); v.set(2, 0, Math.sin(angle)); v.set(2, 2, Math.cos(angle)); return v; } public static Matrix translation(double a, double b, double c) throws ParseException { Matrix v = new Matrix();; v.set(3, 0, a); v.set(3, 1, b); v.set(3, 2, c); return v; }
here's the code that actually multiplies a point with a matrix:
[spoiler]
[/spoiler]public static Vector3D mult(Vector3D lhs, Matrix rhs) throws ParseException { //notes: NR_DIMS = 3 // since the matrix is 4x4 we need to add an extra value to our point (depending on whether it's a point or a vector) if(rhs.get(0, 3)!=0 || rhs.get(1, 3)!=0 || rhs.get(2, 3)!=0 || rhs.get(3, 3)!=1) throw new ParseException("the matrix multiplificiation thingy just borked"); Vector3D ret = new Vector3D(); double[] vec = new double[NR_DIMS]; double[] temp = new double[NR_DIMS+1]; temp[0] = lhs.x; temp[1] = lhs.y; temp[2] = lhs.z; temp[3] = lhs.infty? 0:1; //is it a point or a vector? for (int i = 0; i < NR_DIMS; i++) { vec[i] = 0; // Multiply the original vector with the i-th column of the matrix. for (int j = 0; j <= NR_DIMS; j++) { vec[i] += temp[j] * rhs.get(j,i); } } ret.x = vec[0]; ret.y = vec[1]; ret.z = vec[2]; ret.infty = lhs.infty; return ret; }
and here's the code that should rotate the figure
[spoiler]
[/spoiler]protected void rotate() throws ParseException { Matrix rotate_x = Transformations.x_rotation(rotateX); Matrix rotate_y = Transformations.y_rotation(rotateY); Matrix rotate_z = Transformations.z_rotation(rotateZ); Matrix translate = Transformations.translation(center.x(), center.y(), center.z()); for(Vector3D point : points) { System.out.println(point.toString()); point = Vector3D.mult(point, scale); System.out.println(point.toString()); point = Vector3D.mult(point, rotate_x); System.out.println(point.toString()); point = Vector3D.mult(point, rotate_y); System.out.println(point.toString()); point = Vector3D.mult(point, rotate_z); System.out.println(point.toString()); point = Vector3D.mult(point, translate); System.out.println(point.toString()); point = Vector3D.mult(point, eye); System.out.println(point.toString()); if(point.z() != 0) { point.setX(point.x()/(-point.z())); point.setY(point.y()/(-point.z())); } System.out.println(point.toString() + "\n"); checkMinMax(point); } }
Any ideas as to what i might be doing wrong?
edit: this forum doesn't support spoilers?
Also i have posted this question on one other forum: http://www.teamavolition.com/topic/1...n-not-working/
but it seems to have died there :\