从配置加载灯
Load light from configuration
我创建了一个光源class,它包含位置、颜色和衰减三个Vector3fs
。我已经能够制作一种将 Light 保存到配置中的方法,如下所示:
lightName: (0.0, 1000.0, -7000.0), (1.0, 1.0, 1.0), (1.0, 0.0, 0.0)
现在我需要一种方法,可以加载 return 带有已保存信息的灯光。到目前为止我有:
public Light getLight(String name) {
String line;
try {
while((line = bufferedReader.readLine()) != null) {
if(line.startsWith(name)) {
line = line.replace(name + ": ", "");
return new Light(new Vector3f(x , y, z), new Vector3f(r, g, b), new Vector3f(x1, y1, z1));
}
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
如有任何建议,我们将不胜感激!谢谢!
----更新------
多亏了 Johnny 的回复,我才弄明白了。这是完整的工作代码:
public Light getLight(String name) {
String line;
float x = 0, y = 0, z = 0, r = 0, g = 0, b = 0, x1 = 1, y1 = 0, z1 = 0;
try {
while((line = bufferedReader.readLine()) != null) {
if(line.startsWith(name)) {
line = line.replace(name + ": ", "").replace("(", "").replace(")", "");
Scanner parser = new Scanner(line);
parser.useDelimiter(", ");
x = parser.nextFloat();
y = parser.nextFloat();
z = parser.nextFloat();
r = parser.nextFloat();
g = parser.nextFloat();
b = parser.nextFloat();
x1 = parser.nextFloat();
y1 = parser.nextFloat();
z1 = parser.nextFloat();
parser.close();
break;
}
}
return new Light(new Vector3f(x , y, z), new Vector3f(r, g, b), new Vector3f(x1, y1, z1));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
您不需要替换 line
中的任何内容,因为您需要的只是数字。但是,您确实需要解析 line
.
中的数字
您应该在循环中或一次解析所有数字,将每个数字分配给一个变量,然后创建 Light
。
类似下面的东西应该可以工作:
public Light getLight(String name) {
String line;
double x, y, z, r, g, b, x1, y1, z1;
try {
while((line = bufferedReader.readLine()) != null) {
if(line.startsWith(name)) {
Scanner parser = new Scanner(line);
x = parser.nextDouble();
y = parser.nextDouble();
// continue assigning variables
// break out of while loop, only interested in one line
break;
}
}
return new Light(new Vector3f(x , y, z), new Vector3f(r, g, b), new Vector3f(x1, y1, z1));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
我创建了一个光源class,它包含位置、颜色和衰减三个Vector3fs
。我已经能够制作一种将 Light 保存到配置中的方法,如下所示:
lightName: (0.0, 1000.0, -7000.0), (1.0, 1.0, 1.0), (1.0, 0.0, 0.0)
现在我需要一种方法,可以加载 return 带有已保存信息的灯光。到目前为止我有:
public Light getLight(String name) {
String line;
try {
while((line = bufferedReader.readLine()) != null) {
if(line.startsWith(name)) {
line = line.replace(name + ": ", "");
return new Light(new Vector3f(x , y, z), new Vector3f(r, g, b), new Vector3f(x1, y1, z1));
}
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
如有任何建议,我们将不胜感激!谢谢!
----更新------
多亏了 Johnny 的回复,我才弄明白了。这是完整的工作代码:
public Light getLight(String name) {
String line;
float x = 0, y = 0, z = 0, r = 0, g = 0, b = 0, x1 = 1, y1 = 0, z1 = 0;
try {
while((line = bufferedReader.readLine()) != null) {
if(line.startsWith(name)) {
line = line.replace(name + ": ", "").replace("(", "").replace(")", "");
Scanner parser = new Scanner(line);
parser.useDelimiter(", ");
x = parser.nextFloat();
y = parser.nextFloat();
z = parser.nextFloat();
r = parser.nextFloat();
g = parser.nextFloat();
b = parser.nextFloat();
x1 = parser.nextFloat();
y1 = parser.nextFloat();
z1 = parser.nextFloat();
parser.close();
break;
}
}
return new Light(new Vector3f(x , y, z), new Vector3f(r, g, b), new Vector3f(x1, y1, z1));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
您不需要替换 line
中的任何内容,因为您需要的只是数字。但是,您确实需要解析 line
.
您应该在循环中或一次解析所有数字,将每个数字分配给一个变量,然后创建 Light
。
类似下面的东西应该可以工作:
public Light getLight(String name) {
String line;
double x, y, z, r, g, b, x1, y1, z1;
try {
while((line = bufferedReader.readLine()) != null) {
if(line.startsWith(name)) {
Scanner parser = new Scanner(line);
x = parser.nextDouble();
y = parser.nextDouble();
// continue assigning variables
// break out of while loop, only interested in one line
break;
}
}
return new Light(new Vector3f(x , y, z), new Vector3f(r, g, b), new Vector3f(x1, y1, z1));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}