11#include <glm/gtc/matrix_transform.hpp>
41 float len = glm::length(normal);
43 return Vec3(0.0f, 0.0f, 1.0f);
106 if (std::abs(
clip.w) < 1e-12f)
128 screen_y = (1.0f -
ndc.y) * 0.5f *
static_cast<float>(height);
162 float &
u,
float &
v,
float &
w) {
164 if (std::abs(
det) < 1e-12f) {
193 const Color &specular_color =
Color(0, 0, 0, 0),
194 float shininess = 0.0f) {
195 Vec3 n = glm::length(normal) > 1e-12f ? glm::normalize(normal) :
Vec3(0.0f, 0.0f, 1.0f);
197 float ndotl = std::max(0.0f, glm::dot(
n,
l));
198 float ambient = 0.3f;
203 if (shininess > 0.0f &&
ndotl > 0.0f) {
232 const std::vector<Light> &lights,
234 const Color &specular_color =
Color(0, 0, 0, 0),
235 float shininess = 0.0f) {
236 Vec3 n = glm::length(normal) > 1e-12f ? glm::normalize(normal) :
Vec3(0.0f, 0.0f, 1.0f);
242 int nlights =
static_cast<int>(lights.size());
255 if (shininess > 0.0f &&
ndotl > 0.0f) {
void compute_barycentric(float px, float py, float x0, float y0, float x1, float y1, float x2, float y2, float &u, float &v, float &w)
Compute barycentric coordinates (u, v, w) of a point in a triangle.
void ndc_to_screen(const Vec3 &ndc, int width, int height, float &screen_x, float &screen_y, float &depth)
Convert from normalized device coordinates (NDC) to screen (pixel) coordinates.
glm::mat4 Mat4
4×4 floating-point matrix.
glm::vec3 Vec3
3-component floating-point vector (xyz).
Vec3 transform_direction(const Mat4 &m, const Vec3 &d)
Transform a direction vector by a 4×4 matrix (with implicit w=0).
Vec3 compute_face_normal(const Vec3 &v0, const Vec3 &v1, const Vec3 &v2)
Compute the unit-length normal vector of a triangle face.
Color shade_pixel(const Color &base_color, const Vec3 &normal, const Vec3 &light_direction, const Color &specular_color=Color(0, 0, 0, 0), float shininess=0.0f)
Compute the shaded color of a pixel with a single directional light.
Color shade_pixel_multi(const Color &base_color, const Vec3 &normal, const std::vector< Light > &lights, float ambient, const Color &specular_color=Color(0, 0, 0, 0), float shininess=0.0f)
Compute the shaded color with multiple light sources.
Vec3 transform_point(const Mat4 &m, const Vec3 &p)
Transform a point by a 4×4 matrix (with implicit w=1).
Vec3 perspective_divide(const Vec4 &clip)
Perform perspective division: divide xyz by w.
Vec4 transform_point_homogeneous(const Mat4 &m, const Vec3 &p)
Transform a point by a 4×4 matrix, returning the full Vec4 result.
glm::vec4 Vec4
4-component floating-point vector (xyzw).
An RGBA color with floating-point components.
A light source for the Blinn-Phong shading model.
int x
Left edge of the bitmap, in image pixels.
Fundamental types used throughout the scimesh rendering engine.