实现球的光照模型代码Java
时间: 2024-05-17 14:17:06 浏览: 14
以下是一个简单的 Java 程序,用于实现球的光照模型:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SphereLighting extends JPanel {
private static final long serialVersionUID = 1L;
// 设置球的半径和位置
private static final double r = 100;
private static final double[] P = { 0, 0, 0 };
// 设置光源位置和强度
private static final double[] P0 = { 100, 100, 100 };
private static final double I0 = 1;
// 设置球的材质属性
private static final double Ka = 0.1; // 环境光反射系数
private static final double Kd = 0.7; // 漫反射系数
private static final double Ks = 0.2; // 镜面反射系数
private static final int n = 50; // 镜面反射指数
// 设置球的颜色
private static final Color Ca = Color.WHITE; // 环境光颜色
private static final Color Cd = Color.RED; // 漫反射颜色
private static final Color Cs = Color.WHITE; // 镜面反射颜色
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
// 抗锯齿绘制
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 创建一个 BufferedImage,用于绘制球体
BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
// 绘制球体
for (double theta = 0; theta < Math.PI; theta += Math.PI / 20) {
for (double phi = 0; phi < 2 * Math.PI; phi += Math.PI / 20) {
double x = r * Math.sin(theta) * Math.cos(phi);
double y = r * Math.sin(theta) * Math.sin(phi);
double z = r * Math.cos(theta);
// 计算光照强度
double[] P = { x, y, z };
double[] N = { x, y, z };
double[] L = { P0[0] - P[0], P0[1] - P[1], P0[2] - P[2] };
double lenL = Math.sqrt(L[0] * L[0] + L[1] * L[1] + L[2] * L[2]);
L[0] /= lenL;
L[1] /= lenL;
L[2] /= lenL;
double[] R = { 2 * dot(N, L) * N[0] - L[0], 2 * dot(N, L) * N[1] - L[1], 2 * dot(N, L) * N[2] - L[2] };
double[] V = { -P[0], -P[1], -P[2] };
double lenV = Math.sqrt(V[0] * V[0] + V[1] * V[1] + V[2] * V[2]);
V[0] /= lenV;
V[1] /= lenV;
V[2] /= lenV;
double Ia = I0 * Ka;
double Id = I0 * Kd * Math.max(dot(N, L), 0);
double Is = I0 * Ks * Math.pow(Math.max(dot(R, V), 0), n);
Color I = new Color((int)(Ia * Ca.getRed() + Id * Cd.getRed() + Is * Cs.getRed()),
(int)(Ia * Ca.getGreen() + Id * Cd.getGreen() + Is * Cs.getGreen()),
(int)(Ia * Ca.getBlue() + Id * Cd.getBlue() + Is * Cs.getBlue()));
g2.setColor(I);
g2.fillArc((int) (P[0] + getWidth() / 2), (int) (P[1] + getHeight() / 2), 2, 2, 0, 360);
}
}
// 将 BufferedImage 绘制到 JPanel 上
g2d.drawImage(img, 0, 0, null);
}
private double dot(double[] a, double[] b) {
double sum = 0;
for (int i = 0; i < a.length; i++) {
sum += a[i] * b[i];
}
return sum;
}
public static void main(String[] args) {
JFrame frame = new JFrame("Sphere Lighting");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
SphereLighting panel = new SphereLighting();
frame.setContentPane(panel);
frame.setVisible(true);
}
}
```
运行上述代码后,将会在新窗口中显示一个带有光照效果的球体。你可以自己尝试修改代码中的参数,调整光照模型的效果。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)