Skip to content
This repository was archived by the owner on Dec 24, 2019. It is now read-only.

Commit 82d87ce

Browse files
committed
some troubleshooting
1 parent 9f00827 commit 82d87ce

4 files changed

Lines changed: 71 additions & 52 deletions

File tree

Serie 2/Computergrafik-Basecode/jrtr/src/main/java/jrtr/Camera.java

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -112,38 +112,60 @@ private void calculateCameraMatrix() {
112112

113113
public void moveCamera(char direction) {
114114
Matrix4f tmp = new Matrix4f();
115-
Vector3f look2D = new Vector3f();
116-
Vector3f orthogonal2D = new Vector3f();
117-
orthogonal2D.cross(lookDirection, up);
118-
orthogonal2D.normalize();
119-
120-
if (direction == 'w')
121-
look2D = new Vector3f(lookDirection.x, 0, lookDirection.z);
122-
else if (direction == 's')
123-
look2D = new Vector3f(-lookDirection.x, 0, -lookDirection.z);
124-
else if (direction == 'a')
125-
look2D = new Vector3f(-orthogonal2D.x, 0, -orthogonal2D.z);
126-
else if (direction == 'd')
127-
look2D = new Vector3f(orthogonal2D.x, 0, orthogonal2D.z);
128-
129-
look2D.normalize();
130-
tmp.setTranslation(look2D);
115+
Vector3f lookDir = new Vector3f();
116+
Vector3f orthogonal = new Vector3f();
117+
orthogonal.cross(lookDirection, up);
118+
orthogonal.normalize();
119+
120+
if (direction == 'w' || direction == 'W')
121+
lookDir = new Vector3f(lookDirection);
122+
else if (direction == 's' || direction == 'S') {
123+
lookDir = new Vector3f(lookDirection);
124+
lookDir.negate();
125+
} else if (direction == 'a' || direction == 'A') {
126+
lookDir = new Vector3f(orthogonal);
127+
} else if (direction == 'd' || direction == 'D') {
128+
lookDir = new Vector3f(orthogonal);
129+
lookDir.negate();
130+
}
131+
132+
lookDir.normalize();
133+
tmp.setTranslation(lookDir);
131134
cameraMatrix.add(tmp);
132135
}
133-
134-
public void rotateCamera(Vector3f axis, float angle){
136+
137+
public void rotateCamera(Vector3f axis, float angle) {
138+
135139
Matrix4f rotation = new Matrix4f();
136140
rotation.setIdentity();
137-
rotation.setRotation(new AxisAngle4f(axis, -angle/10));
138-
139-
Vector3f translation = new Vector3f();
140-
cameraMatrix.get(translation);
141-
cameraMatrix.setTranslation(new Vector3f());
142-
143-
rotation.mul(cameraMatrix);
144-
145-
cameraMatrix.setTranslation(translation);
146-
147-
cameraMatrix = rotation;
141+
rotation.setRotation(new AxisAngle4f(axis, angle));
142+
cameraMatrix.invert();
143+
cameraMatrix.mul(rotation);
144+
145+
updateUpVector();
146+
updateCenter();
147+
148+
cameraMatrix.invert();
149+
}
150+
151+
private void updateUpVector() {
152+
Vector4f xVector = new Vector4f();
153+
cameraMatrix.getColumn(0, xVector);
154+
Vector3f x = new Vector3f(xVector.x, xVector.y, xVector.z);
155+
156+
Vector4f zVector = new Vector4f();
157+
cameraMatrix.getColumn(2, zVector);
158+
Vector3f z = new Vector3f(zVector.x, zVector.y, zVector.z);
159+
160+
up.cross(x, z);
161+
up.normalize();
162+
}
163+
164+
private void updateCenter() {
165+
Vector4f tmp = new Vector4f();
166+
cameraMatrix.getColumn(3, tmp);
167+
168+
center = new Vector3f(tmp.x, tmp.y, tmp.z);
169+
center.normalize();
148170
}
149171
}

Serie 2/Computergrafik-Basecode/simple/src/main/java/simple/Landscape.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -311,34 +311,36 @@ private float[] calculateLandscapeNormals() {
311311

312312
/**
313313
*
314-
* @param snow
314+
* @param d
315315
* whith which the average heihgt is multiplied to get the snow line
316316
* factor * average height = snow line;
317317
* @return the color array to the landscape
318318
*/
319-
public float[] setSnowLine(float snow) {
319+
public void setSnowAndWaterLine(double snow, double water) {
320320
colors = new float[(res + 1) * (res + 1) * 3];
321321
int average = 0;
322322

323323
for (int i = 0; i < 3 * (res + 1) * (res + 1); i += 3)
324-
average += positions[i + 2];
324+
average += positions[i + 1];
325325

326326
average /= (res + 1) * (res + 1);
327327

328328
for (int i = 0; i < 3 * (res + 1) * (res + 1); i += 3) {
329329

330-
if (positions[i + 1] < snow * average) {
331-
colors[i + 0] = 0;
332-
colors[i + 1] = (float) 0.75;
333-
colors[i + 2] = 0;
334-
} else {
330+
if (positions[i + 1] > snow * average) {
335331
colors[i + 0] = 1;
336332
colors[i + 1] = 1;
337333
colors[i + 2] = 1;
334+
} else if (positions[i + 1] < water * average) {
335+
colors[i + 0] = 0;
336+
colors[i + 1] = 0;
337+
colors[i + 2] = 1;
338+
} else {
339+
colors[i + 0] = 0;
340+
colors[i + 1] = (float) 0.75;
341+
colors[i + 2] = 0;
338342
}
339343
}
340-
341-
return colors;
342344
}
343345

344346
public Shape getShape(jrtr.RenderContext renderContext) {

Serie 2/Computergrafik-Basecode/simple/src/main/java/simple/SimpleTask2.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ else if (geomObj == 3) {
8787
}
8888

8989
else if (geomObj == 4) {
90-
int resolution = 16;
90+
int resolution = 32;
9191

9292
Landscape landscape = new Landscape(resolution, 15, 0, 0, 0, 5);
93-
landscape.setSnowLine(1);
93+
landscape.setSnowAndWaterLine(1.5, 0.5);
9494
shape = landscape.getShape(renderContext);
95-
shape.getTransformation().setScale((float) (1 / Math.sqrt(resolution)));
95+
shape.getTransformation().setScale((float) (10 / Math.sqrt(resolution)));
9696
sceneManager.addShape(shape);
9797
}
9898

Serie 2/Computergrafik-Basecode/simple/src/main/java/simple/SimpleTask3.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ else if (geomObj == 3) {
8383
else if (geomObj == 4) {
8484
int resolution = 64;
8585

86-
Landscape landscape = new Landscape(resolution, 10, 0, 0, 0, 0);
87-
landscape.setSnowLine(1);
86+
Landscape landscape = new Landscape(resolution, 15, 0, 0, 0, 0);
87+
landscape.setSnowAndWaterLine(1.5, 0.5);
88+
8889
shape = landscape.getShape(renderContext);
8990
shape.getTransformation().setScale((float) (5 / Math.sqrt(resolution)));
9091
sceneManager.addShape(shape);
@@ -230,15 +231,9 @@ public void mouseDragged(MouseEvent e) {
230231

231232
sceneManager.getCamera().rotateCamera(axis, angle);
232233

233-
// -----------------------------------
234-
Matrix4f rotation = new Matrix4f();
235-
rotation.setIdentity();
236-
rotation.setRotation(new AxisAngle4f(axis, -angle));
237-
238-
rotation.mul(transformation);
239-
// -----------------------------------
240-
241234
renderPanel.getCanvas().repaint();
235+
236+
p1 = new Vector3f(p2);
242237
}
243238

244239
public void mouseReleased(MouseEvent e) {

0 commit comments

Comments
 (0)