Convert basics to C++

This commit is contained in:
Eryn Wells 2013-09-09 22:47:19 -07:00
parent 86a5055d63
commit b973003a6b
4 changed files with 172 additions and 222 deletions

View file

@ -5,12 +5,7 @@
Import('env')
files = Split("""
basics.c
camera.c
object.c
scene.c
texture.c
writer_png.c
basics.cpp
""")
lib = env.Library('charles', files)

View file

@ -1,178 +0,0 @@
/* basics.c
*
* Definition of basic types: Vector.
*
* Eryn Wells <eryn@erynwells.me>
*/
#include <math.h>
#include "basics.h"
const Vector3 Vector3Zero = {0.0, 0.0, 0.0};
const Color ColorBlack = {0, 0, 0, 0};
/*
* vector_init --
*
* Create a new vector given x, y, and z coordinates.
*/
Vector3
vector_init(float x, float y, float z)
{
Vector3 v;
v.x = x;
v.y = y;
v.z = z;
return v;
}
/*
* vector_add_vector
*
* Add a vector to another vector. Return a new vector.
*/
Vector3
vector_add_vector(Vector3 v, Vector3 a)
{
return vector_init(v.x + a.x, v.y + a.y, v.z + a.z);
}
/*
* vector_mul_scalar --
*
* Multiply a vector by a scalar. Return a new vector.
*/
Vector3
vector_mul_scalar(Vector3 v, float f)
{
return vector_init(f * v.x, f * v.y, f * v.z);
}
/*
* vector_sub_vector --
*
* m - s. Return a new vector.
*/
Vector3
vector_sub_vector(Vector3 m, Vector3 s)
{
return vector_init(m.x - s.x, m.y - s.y, m.z - s.z);
}
/*
* 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 --
*
* Return the length-squared of the given vector.
*/
float
vector_length2(Vector3 v)
{
return (v.x * v.x) + (v.y * v.y) + (v.z * v.z);
}
/*
* vector_length --
*
* Return the length of the given vector.
*/
float
vector_length(Vector3 v)
{
return sqrtf(vector_length2(v));
}
/*
* vector_dot --
*
* Compute the dot product of v and f.
*/
float
vector_dot(Vector3 v, Vector3 f)
{
return (v.x * f.x) + (v.y * f.y) + (v.z * f.z);
}
/*
* vector_normalize --
*
* Normalize the given vector. Return a new vector.
*/
Vector3
vector_normalize(Vector3 v)
{
float length2 = vector_length2(v);
if (length2 <= 0.0) {
return v;
}
// Multiplying by the inverse of the length is more efficient than dividing by the length.
float inverse_length = 1.0 / sqrtf(length2);
return vector_init(v.x * inverse_length, v.y * inverse_length, v.z * inverse_length);
}
/*
* Rects
*/
/*
* rect_init --
*
* Create a new Rect given x, y coordinates, height, and width.
*/
Rect
rect_init(float x, float y, float w, float h)
{
Rect r;
r.x = x;
r.y = y;
r.h = h;
r.w = w;
return r;
}
/*
* Rays
*/
/*
* ray_init --
*
* Create a new Ray.
*/
Ray
ray_init(Vector3 location, Vector3 direction)
{
Ray r;
r.location = location;
r.direction = direction;
return r;
}
Vector3
ray_parameterize(Ray ray, float t)
{
return vector_add_vector(ray.location, vector_mul_scalar(ray.direction, t));
}

133
src/basics.cpp Normal file
View file

@ -0,0 +1,133 @@
/* basics.c
*
* Definition of basic types: Vector.
*
* Eryn Wells <eryn@erynwells.me>
*/
#include <cmath>
#include "basics.h"
#pragma mark - Vectors
const Vector3 Vector3::Zero = Vector3();
Vector3::Vector3()
: Vector3(0.0, 0.0, 0.0)
{ }
Vector3::Vector3(float _x, float _y, float _z)
: x(_x), y(_y), z(_z)
{ }
inline Vector3
Vector3::operator+(Vector3 v)
{
return Vector3(x + v.x, y + v.y, z + v.z);
}
inline Vector3
Vector3::operator*(float a)
{
return Vector3(a*x, a*y, a*z);
}
inline Vector3
Vector3::operator-(Vector3 v)
{
return Vector3(x - v.x, y - v.y, z - v.z);
}
inline Vector3
Vector3::operator-()
{
return Vector3(-x, -y, -z);
}
inline float
Vector3::length2()
{
return x*x + y*y + z*z;
}
inline float
Vector3::length()
{
return sqrtf(length2());
}
inline float
Vector3::dot(Vector3 v)
{
return x*v.x + y*v.x + z*v.z;
}
void
Vector3::normalize()
{
float len2 = length2();
if (len2 <= 0.0) {
return;
}
// Multiplying by the inverse of the length is more efficient than dividing by the length.
float inverse_length = 1.0 / sqrtf(len2);
x *= inverse_length;
y *= inverse_length;
z *= inverse_length;
}
Vector3
operator*(float a, Vector3 v)
{
return v * a;
}
#pragma mark - Rays
Ray::Ray()
: Ray(Vector3::Zero, Vector3::Zero)
{ }
Ray::Ray(Vector3 o, Vector3 d)
: origin(o), direction(d)
{ }
Vector3
Ray::parameterize(float t)
{
return origin + t * direction;
}
#pragma mark - Colors
const Color Color::Black = Color();
const Color Color::White = Color(1.0, 1.0, 1.0, 1.0);
const Color Color::Red = Color(1.0, 0.0, 0.0, 1.0);
const Color Color::Green = Color(0.0, 1.0, 0.0, 1.0);
const Color Color::Blue = Color(0.0, 0.0, 1.0, 1.0);
Color::Color()
: Color(0.0, 0.0, 0.0, 0.0)
{ }
Color::Color(float r, float g, float b, float a)
: red(r), green(g), blue(b), alpha(a)
{ }

View file

@ -5,55 +5,55 @@
* Eryn Wells <eryn@erynwells.me>
*/
#ifndef __BASICS_H
#define __BASICS_H
#include <stdint.h>
struct Vector3
{
Vector3();
Vector3(float x, float y, float z);
Vector3 operator+(Vector3 v);
Vector3 operator*(float a);
Vector3 operator-(Vector3 v);
Vector3 operator-();
typedef struct {
float length2();
float length();
float dot(Vector3 v);
void normalize();
static const Vector3 Zero;
float x, y, z;
} Vector3;
};
Vector3 operator*(float a, Vector3 v);
extern const Vector3 Vector3Zero;
struct Ray
{
Ray();
Ray(Vector3 o, Vector3 d);
Vector3 parameterize(float t);
Vector3 origin, direction;
};
Vector3 vector_init(float x, float y, float z);
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);
struct Color
{
Color();
Color(float r, float g, float b, float a);
Vector3 vector_normalize(Vector3 v);
typedef struct {
float x, y;
float w, h;
} Rect;
Rect rect_init(float x, float y, float h, float w);
typedef struct {
Vector3 location;
Vector3 direction;
} Ray;
Ray ray_init(Vector3 location, Vector3 direction);
Vector3 ray_parameterize(Ray ray, float t);
typedef struct {
uint8_t red, green, blue, alpha;
} Color;
extern const Color ColorBlack;
static const Color Black;
static const Color White;
static const Color Red;
static const Color Green;
static const Color Blue;
float red, green, blue, alpha;
};
#endif