Clean up basics a bit; add vector_negate, ray_parameterize
This commit is contained in:
parent
0fa10c3220
commit
21ccba45e4
2 changed files with 34 additions and 13 deletions
39
src/basics.c
39
src/basics.c
|
@ -9,7 +9,7 @@
|
|||
#include "basics.h"
|
||||
|
||||
|
||||
const Vector3 ZeroVector3 = {0.0, 0.0, 0.0};
|
||||
const Vector3 Vector3Zero = {0.0, 0.0, 0.0};
|
||||
|
||||
const Color ColorBlack = {0, 0, 0, 0};
|
||||
|
||||
|
@ -31,26 +31,26 @@ vector_init(float x, float y, float z)
|
|||
|
||||
|
||||
/*
|
||||
* vector_mult_scalar --
|
||||
* vector_add_vector
|
||||
*
|
||||
* Multiply a vector by a scalar. Return a new vector.
|
||||
* Add a vector to another vector. Return a new vector.
|
||||
*/
|
||||
Vector3
|
||||
vector_mult_scalar(Vector3 v, float f)
|
||||
vector_add_vector(Vector3 v, Vector3 a)
|
||||
{
|
||||
return vector_init(f * v.x, f * v.y, f * v.z);
|
||||
return vector_init(v.x + a.x, v.y + a.y, v.z + a.z);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* vector_mult_vector --
|
||||
* vector_mul_scalar --
|
||||
*
|
||||
* Multiply a vector by another vector. Return a new vector.
|
||||
* Multiply a vector by a scalar. Return a new vector.
|
||||
*/
|
||||
Vector3
|
||||
vector_mult_vector(Vector3 v, Vector3 f)
|
||||
vector_mul_scalar(Vector3 v, float f)
|
||||
{
|
||||
return vector_init(v.x * f.x, v.y * f.y, v.z * f.z);
|
||||
return vector_init(f * v.x, f * v.y, f * v.z);
|
||||
}
|
||||
|
||||
|
||||
|
@ -66,6 +66,18 @@ vector_sub_vector(Vector3 m, Vector3 s)
|
|||
}
|
||||
|
||||
|
||||
/*
|
||||
* vector_negate --
|
||||
*
|
||||
* Negate a vector. Return a new vector.
|
||||
*/
|
||||
Vector3
|
||||
vector_negate(Vector3 v)
|
||||
{
|
||||
return vector_init(-v.x, -v.y, -v.z);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* vector_length2 --
|
||||
*
|
||||
|
@ -111,7 +123,7 @@ Vector3
|
|||
vector_normalize(Vector3 v)
|
||||
{
|
||||
float length2 = vector_length2(v);
|
||||
if (length2 <= 0) {
|
||||
if (length2 <= 0.0) {
|
||||
return v;
|
||||
}
|
||||
|
||||
|
@ -157,3 +169,10 @@ ray_init(Vector3 location, Vector3 direction)
|
|||
r.direction = direction;
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
Vector3
|
||||
ray_parameterize(Ray ray, float t)
|
||||
{
|
||||
return vector_add_vector(ray.location, vector_mul_scalar(ray.direction, t));
|
||||
}
|
||||
|
|
|
@ -17,13 +17,14 @@ typedef struct {
|
|||
} Vector3;
|
||||
|
||||
|
||||
extern const Vector3 ZeroVector3;
|
||||
extern const Vector3 Vector3Zero;
|
||||
|
||||
|
||||
Vector3 vector_init(float x, float y, float z);
|
||||
Vector3 vector_mult_scalar(Vector3 v, float f);
|
||||
Vector3 vector_mult_vector(Vector3 v, Vector3 f);
|
||||
Vector3 vector_add_vector(Vector3 v, Vector3 a);
|
||||
Vector3 vector_mul_scalar(Vector3 v, float f);
|
||||
Vector3 vector_sub_vector(Vector3 m, Vector3 s);
|
||||
Vector3 vector_negate(Vector3 v);
|
||||
float vector_length2(Vector3 v);
|
||||
float vector_length(Vector3 v);
|
||||
float vector_dot(Vector3 v, Vector3 f);
|
||||
|
@ -45,6 +46,7 @@ typedef struct {
|
|||
} Ray;
|
||||
|
||||
Ray ray_init(Vector3 location, Vector3 direction);
|
||||
Vector3 ray_parameterize(Ray ray, float t);
|
||||
|
||||
|
||||
typedef struct {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue