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

Commit 4f21476

Browse files
committed
finished task 1 in assginment 6
implemented texture coordinates
1 parent 7ec533f commit 4f21476

2 files changed

Lines changed: 59 additions & 34 deletions

File tree

Serie 6/Computergrafik-Basecode-master/simple/src/main/java/simple/BezierRotation.java

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
import java.util.ArrayList;
66

77
import javax.vecmath.Matrix4f;
8+
import javax.vecmath.Point2f;
89
import javax.vecmath.Vector4f;
910

1011
public class BezierRotation {
1112

1213
private BezierCurve bCurve;
13-
private float[] points;
14-
private float[] colors;
15-
private float[] normals;
14+
private float[] points, colors, normals, texCoord;
1615
private int[] indices;
16+
private ArrayList<Point2f> coords;
1717

1818
private Shape shape;
1919

@@ -31,19 +31,11 @@ public class BezierRotation {
3131
*/
3232
public BezierRotation(int nPoints, ArrayList<Vector4f> controlPoints, int nRotations, RenderContext renderContext) {
3333
bCurve = new BezierCurve(nPoints, controlPoints);
34-
int nSegments = controlPoints.size()/4 %4;
35-
36-
// bCurves = new ArrayList<BezierCurve>();
34+
int nSegments = controlPoints.size() / 4 % 4;
3735

3836
// TODO: set test variable
3937
boolean test = false;
4038

41-
/*
42-
* for (int i = 0; i < nRotations; i++) { bCurves.add(new
43-
* BezierCurve(nPoints, rotateArray(controlPoints, ((float) i) /
44-
* ((float) nRotations)))); }
45-
*/
46-
4739
if (test) {
4840
test(bCurve, nPoints, renderContext);
4941
}
@@ -52,23 +44,27 @@ public BezierRotation(int nPoints, ArrayList<Vector4f> controlPoints, int nRotat
5244
concatinateCurves(nRotations);
5345
calculateColors(nPoints, nRotations);
5446
calculateIndices(nPoints, nRotations, nSegments);
55-
createShape(points.length/3, renderContext);
47+
createShape(points.length / 3, renderContext);
5648
}
5749
}
5850

51+
public Shape getShape() {
52+
return shape;
53+
}
54+
5955
private void test(BezierCurve bCurve, int nPoints, RenderContext renderContext) {
6056
bCurve.getPoints().add(new Vector4f(0, 1, 0, 1));
61-
bCurve.getNormals().add(new Vector4f(-1,0,0,0));
57+
bCurve.getNormals().add(new Vector4f(-1, 0, 0, 0));
6258

63-
points = toBArray(bCurve.getPoints());
59+
points = toFArray(bCurve.getPoints());
6460
colors = new float[points.length];
65-
normals = toBArray(bCurve.getNormals());
61+
normals = toFArray(bCurve.getNormals());
6662
indices = toIArray(testIndices(nPoints));
6763

6864
for (int i = 0; i < colors.length; i++)
6965
colors[i] = 1;
7066

71-
VertexData vData = renderContext.makeVertexData(points.length/3);
67+
VertexData vData = renderContext.makeVertexData(points.length / 3);
7268

7369
vData.addElement(points, VertexData.Semantic.POSITION, 3);
7470
vData.addElement(colors, VertexData.Semantic.COLOR, 3);
@@ -90,32 +86,33 @@ private ArrayList<Integer> testIndices(int nPoints) {
9086
return indices;
9187
}
9288

93-
public Shape getShape() {
94-
return shape;
95-
}
96-
9789
/**
9890
* collects all points and normals from the existing curves and stores them
9991
* in the points and normals member arrays.
10092
*/
10193
private void concatinateCurves(int nRotations) {
10294
ArrayList<Vector4f> points = new ArrayList<Vector4f>();
10395
ArrayList<Vector4f> normals = new ArrayList<Vector4f>();
104-
96+
ArrayList<Float> texCoords = new ArrayList<Float>();
97+
10598
for (int i = 0; i < nRotations; i++) {
106-
points.addAll(rotateArray(bCurve.getPoints(), ((float) i) / ((float) nRotations)));
107-
normals.addAll(rotateArray(bCurve.getNormals(), ((float) i) / ((float) nRotations)));
99+
float angle = ((float) i) / ((float) nRotations);
100+
101+
ArrayList<Vector4f> segmentPoints = rotateArray(bCurve.getPoints(), angle);
102+
points.addAll(segmentPoints);
103+
104+
normals.addAll(rotateArray(bCurve.getNormals(), angle));
105+
106+
texCoords.addAll(calculateTextureCoord(segmentPoints, angle));
108107
}
109108

110-
/*for (BezierCurve c : bCurves) {
111-
points.addAll(c.getPoints());
112-
normals.addAll(c.getNormals());
113-
}*/
114-
115-
this.points = toBArray(points);
116-
this.normals = toBArray(normals);
109+
this.points = toFArray(points);
110+
this.normals = toFArray(normals);
111+
this.texCoord = toFarray(texCoords);
117112
}
118113

114+
115+
119116
/**
120117
* set all colors to white
121118
*/
@@ -132,7 +129,7 @@ private void calculateIndices(int nPoints, int nRotations, int nSegments) {
132129
ArrayList<Integer> indices = new ArrayList<Integer>();
133130

134131
for (int j = 0; j < nRotations; j++) {
135-
for (int i = 0; i < 6 * (nPoints *nSegments - 1); i += 6) {
132+
for (int i = 0; i < 6 * (nPoints * nSegments - 1); i += 6) {
136133

137134
indices.add((i / 6 + (j * nPoints * nSegments)));
138135
indices.add(((i / 6 + (j * nPoints * nSegments)) + 1 + (nPoints * nSegments)) % ((nPoints * nSegments) * nRotations));
@@ -146,12 +143,29 @@ private void calculateIndices(int nPoints, int nRotations, int nSegments) {
146143
this.indices = toIArray(indices);
147144
}
148145

146+
/**
147+
*
148+
* @param segmentPoints the points of a segment to calculate texcoord for
149+
* @param angle the angle the original curve is rotated. to be used as a v coordinate for texcoord
150+
*/
151+
private ArrayList<Float> calculateTextureCoord(ArrayList<Vector4f> segmentPoints, float angle) {
152+
ArrayList<Float> texCoord = new ArrayList<Float>();
153+
154+
for (int i=0; i< segmentPoints.size(); i++){
155+
texCoord.add(segmentPoints.get(i).y);
156+
texCoord.add(angle);
157+
}
158+
159+
return texCoord;
160+
}
161+
149162
private void createShape(int size, RenderContext renderContext) {
150163
VertexData vData = renderContext.makeVertexData(size);
151164

152165
vData.addElement(points, VertexData.Semantic.POSITION, 3);
153166
vData.addElement(colors, VertexData.Semantic.COLOR, 3);
154167
vData.addElement(normals, VertexData.Semantic.NORMAL, 3);
168+
vData.addElement(texCoord, VertexData.Semantic.TEXCOORD, 2);
155169
vData.addIndices(indices);
156170

157171
shape = new Shape(vData);
@@ -183,7 +197,7 @@ private Vector4f rotate(Vector4f v, float angle) {
183197
return result;
184198
}
185199

186-
private float[] toBArray(ArrayList<Vector4f> vectors) {
200+
private float[] toFArray(ArrayList<Vector4f> vectors) {
187201
float[] result = new float[vectors.size() * 3];
188202
int i = 0;
189203

@@ -196,6 +210,15 @@ private float[] toBArray(ArrayList<Vector4f> vectors) {
196210

197211
return result;
198212
}
213+
214+
private float[] toFarray(ArrayList<Float> floats) {
215+
float[] array = new float[floats.size()];
216+
217+
for (int i=0; i<floats.size(); i++)
218+
array[i] = floats.get(i);
219+
220+
return array;
221+
}
199222

200223
private int[] toIArray(ArrayList<Integer> integers) {
201224
int[] ints = new int[integers.size()];

Serie 6/Computergrafik-Basecode-master/simple/src/main/java/simple/simple.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ public void init(RenderContext r)
111111

112112
BezierRotation bRotation = new BezierRotation(30, controlPoints, 30, renderContext);
113113
shape = bRotation.getShape();
114+
//shape = new Shape(vertexData);
115+
114116
sceneManager.addShape(shape);
115117

116118
// Add the scene to the renderer
@@ -138,7 +140,7 @@ public void init(RenderContext r)
138140
material.shader = diffuseShader;
139141
material.texture = renderContext.makeTexture();
140142
try {
141-
material.texture.load("../textures/plant.jpg");
143+
material.texture.load("../textures/wood.jpg");
142144
} catch(Exception e) {
143145
System.out.print("Could not load texture.\n");
144146
System.out.print(e.getMessage());

0 commit comments

Comments
 (0)