c++球的光照模型代码
时间: 2023-08-24 13:18:13 浏览: 97
以下是一个简单的 C++ 程序,演示了球的 Phong 光照模型:
```cpp
#include <iostream>
#include <cmath>
struct Vector3 {
float x, y, z;
Vector3(float x = 0, float y = 0, float z = 0) : x(x), y(y), z(z) {}
Vector3 operator+(const Vector3& other) const {
return Vector3(x + other.x, y + other.y, z + other.z);
}
Vector3 operator-(const Vector3& other) const {
return Vector3(x - other.x, y - other.y, z - other.z);
}
Vector3 operator*(float scalar) const {
return Vector3(x * scalar, y * scalar, z * scalar);
}
float length() const {
return std::sqrt(x * x + y * y + z * z);
}
Vector3 normalized() const {
float len = length();
return Vector3(x / len, y / len, z / len);
}
float dot(const Vector3& other) const {
return x * other.x + y * other.y + z * other.z;
}
Vector3 cross(const Vector3& other) const {
return Vector3(y * other.z - z * other.y,
z * other.x - x * other.z,
x * other.y - y * other.x);
}
};
struct Material {
Vector3 ambient, diffuse, specular;
float shininess;
Material(Vector3 ambient = Vector3(0.1f, 0.1f, 0.1f),
Vector3 diffuse = Vector3(1.0f, 1.0f, 1.0f),
Vector3 specular = Vector3(1.0f, 1.0f, 1.0f),
float shininess = 50.0f)
: ambient(ambient), diffuse(diffuse), specular(specular), shininess(shininess) {}
};
struct Light {
Vector3 position;
Vector3 ambient, diffuse, specular;
Light(Vector3 position = Vector3(0, 0, 0),
Vector3 ambient = Vector3(0.1f, 0.1f, 0.1f),
Vector3 diffuse = Vector3(1.0f, 1.0f, 1.0f),
Vector3 specular = Vector3(1.0f, 1.0f, 1.0f))
: position(position), ambient(ambient), diffuse(diffuse), specular(specular) {}
};
class Sphere {
public:
Sphere(Vector3 center = Vector3(0, 0, 0), float radius = 1.0f, Material material = Material())
: center_(center), radius_(radius), material_(material) {}
bool intersect(const Vector3& ray_origin, const Vector3& ray_direction, float& t) const {
Vector3 oc = ray_origin - center_;
float a = ray_direction.dot(ray_direction);
float b = 2.0f * ray_direction.dot(oc);
float c = oc.dot(oc) - radius_ * radius_;
float discriminant = b * b - 4.0f * a * c;
if (discriminant < 0.0f) {
return false;
}
float sqrt_discriminant = std::sqrt(discriminant);
float t0 = (-b - sqrt_discriminant) / (2.0f * a);
float t1 = (-b + sqrt_discriminant) / (2.0f * a);
if (t0 > t1) {
std::swap(t0, t1);
}
if (t1 < 0.0f) {
return false;
}
if (t0 >= 0.0f) {
t = t0;
} else {
t = t1;
}
return true;
}
Vector3 normal(const Vector3& point) const {
return (point - center_).normalized();
}
Material material() const {
return material_;
}
private:
Vector3 center_;
float radius_;
Material material_;
};
Vector3 phong(const Sphere& sphere, const Vector3& point, const Vector3& camera_position, const Light& light) {
Vector3 N = sphere.normal(point);
Vector3 L = (light.position - point).normalized();
Vector3 V = (camera_position - point).normalized();
Vector3 R = (N * 2.0f * L.dot(N) - L).normalized();
Vector3 ambient = sphere.material().ambient * light.ambient;
Vector3 diffuse = sphere.material().diffuse * light.diffuse * std::max(L.dot(N), 0.0f);
Vector3 specular = sphere.material().specular * light.specular * std::pow(std::max(R.dot(V), 0.0f), sphere.material().shininess);
return ambient + diffuse + specular;
}
int main() {
Sphere sphere(Vector3(0, 0, -5), 1.0f, Material(Vector3(0.1f, 0.1f, 0.1f), Vector3(1, 1, 1), Vector3(1, 1, 1), 50.0f));
Light light(Vector3(0, 0, -2), Vector3(0.1f, 0.1f, 0.1f), Vector3(1, 1, 1), Vector3(1, 1, 1));
const int width = 640;
const int height = 480;
const float aspect_ratio = static_cast<float>(width) / height;
for (int j = 0; j < height; ++j) {
for (int i = 0; i < width; ++i) {
float u = (2.0f * i - width) / width;
float v = (height - 2.0f * j) / height;
Vector3 ray_direction(u * aspect_ratio, v, -1);
Vector3 ray_origin(0, 0, 0);
float t;
if (sphere.intersect(ray_origin, ray_direction, t)) {
Vector3 point = ray_origin + ray_direction * t;
Vector3 color = phong(sphere, point, ray_origin, light);
std::cout << static_cast<int>(255 * color.x) << ' '
<< static_cast<int>(255 * color.y) << ' '
<< static_cast<int>(255 * color.z) << '\n';
} else {
std::cout << "0 0 0\n";
}
}
}
return 0;
}
```
该程序使用了结构体 `Vector3` 表示三维向量,结构体 `Material` 表示物体材质,结构体 `Light` 表示光源,类 `Sphere` 表示球体。`phong` 函数是 Phong 光照模型的实现,用于计算某个球体表面某一点的颜色。
在 `main` 函数中,首先定义了一个球体和一个光源。然后,程序循环遍历每个像素,计算出对应的射线,检测该射线与球体的交点,如果存在,则计算出该交点处的颜色,否则输出黑色。最后将颜色转换为 RGB 值输出。