Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aaburamadan patch 1 #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions include/types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should not be submitted


#include <optional>
#include <vector>
#include <memory>
#include <thread>
#include <math.h>
#include "opencv2/opencv.hpp"

using namespace cv;

#ifdef _WIN32
using byte = unsigned __int8;
using word = unsigned __int16;
using dword = unsigned __int32;
using qword = unsigned __int64;
#else
using byte = uint8_t;
using word = uint16_t;
using dword = uint32_t;
using qword = uint64_t;
#endif

static const double Pi = 3.1415926; ///< Pi number
static const float Pif = 3.1415926f; ///< Pi number

static const float Epsilon = 1E-4;

#define RGB(r, g, b) Vec3f((b), (g), (r))

template <class T> T& lvalue_cast(T&& t) { return t; }
Binary file added renders/flat.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added renders/flat2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added renders/flat3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added renders/flat4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
132 changes: 66 additions & 66 deletions src/CameraPerspective.h
Original file line number Diff line number Diff line change
@@ -1,66 +1,66 @@
// Perspective Camera class
// Written by Sergey Kosov in 2005 for Rendering Competition
#pragma once

#include "ICamera.h"

// ================================ Perspective Camera Class ================================
/**
* @brief Perspective Camera class
*/
class CCameraPerspective : public ICamera
{
public:
/**
* @brief Constructor
* @param resolution The image resolution in pixels
* @param pos Camera origin (center of projection)
* @param dir Normalized camera viewing direction
* @param up Normalized camera up-vector
* @param angle (Vertical) full opening angle of the viewing frustum in degrees
*/
CCameraPerspective(Size resolution, const Vec3f& pos, const Vec3f& dir, const Vec3f& up, float angle)
: ICamera(resolution)
, m_pos(pos)
, m_dir(dir)
, m_up(up)
, m_focus(1.0f / tanf(angle * Pif / 360)) // f = 1 / tg(angle / 2)
{
m_zAxis = dir;
m_xAxis = m_zAxis.cross(m_up);
m_yAxis = m_zAxis.cross(m_xAxis);

m_xAxis = normalize(m_xAxis);
m_yAxis = normalize(m_yAxis);
m_zAxis = normalize(m_zAxis);
}
virtual ~CCameraPerspective(void) = default;

virtual void InitRay(Ray& ray, int x, int y) override
{
float dx = 0.5f; // x-shift to the center of the pixel
float dy = 0.5f; // y-shift to the center of the pixel

// Screen space coordinates [-1, 1]
float sscx = 2 * (x + dx) / getResolution().width - 1;
float sscy = 2 * (y + dy) / getResolution().height - 1;

ray.org = m_pos;
ray.dir = normalize(getAspectRatio() * sscx * m_xAxis + sscy * m_yAxis + m_focus * m_zAxis);
ray.t = std::numeric_limits<float>::infinity();
}


private:
// input values
Vec3f m_pos; ///< Camera origin (center of projection)
Vec3f m_dir; ///< Camera viewing direction
Vec3f m_up; ///< Camera up-vector
float m_focus; ///< The focal length

// preprocessed values
Vec3f m_xAxis; ///< Camera x-axis in WCS
Vec3f m_yAxis; ///< Camera y-axis in WCS
Vec3f m_zAxis; ///< Camera z-axis in WCS
};

// Perspective Camera class
// Written by Sergey Kosov in 2005 for Rendering Competition
#pragma once
#include "ICamera.h"
// ================================ Perspective Camera Class ================================
/**
* @brief Perspective Camera class
*/
class CCameraPerspective : public ICamera
{
public:
/**
* @brief Constructor
* @param resolution The image resolution in pixels
* @param pos Camera origin (center of projection)
* @param dir Normalized camera viewing direction
* @param up Normalized camera up-vector
* @param angle (Vertical) full opening angle of the viewing frustum in degrees
*/
CCameraPerspective(Size resolution, const Vec3f& pos, const Vec3f& dir, const Vec3f& up, float angle)
: ICamera(resolution)
, m_pos(pos)
, m_dir(dir)
, m_up(up)
, m_focus(1.0f / tanf(angle * Pif / 360)) // f = 1 / tg(angle / 2)
{
m_zAxis = dir;
m_xAxis = m_zAxis.cross(m_up);
m_yAxis = m_zAxis.cross(m_xAxis);
m_xAxis = normalize(m_xAxis);
m_yAxis = normalize(m_yAxis);
m_zAxis = normalize(m_zAxis);
}
virtual ~CCameraPerspective(void) = default;
virtual void InitRay(Ray& ray, int x, int y) override
{
float dx = 0.5f; // x-shift to the center of the pixel
float dy = 0.5f; // y-shift to the center of the pixel
// Screen space coordinates [-1, 1]
float sscx = 2 * (x + dx) / getResolution().width - 1;
float sscy = 2 * (y + dy) / getResolution().height - 1;
ray.org = m_pos;
ray.dir = normalize(getAspectRatio() * sscx * m_xAxis + sscy * m_yAxis + m_focus * m_zAxis);
ray.t = std::numeric_limits<float>::infinity();
}
private:
// input values
Vec3f m_pos; ///< Camera origin (center of projection)
Vec3f m_dir; ///< Camera viewing direction
Vec3f m_up; ///< Camera up-vector
float m_focus; ///< The focal length
// preprocessed values
Vec3f m_xAxis; ///< Camera x-axis in WCS
Vec3f m_yAxis; ///< Camera y-axis in WCS
Vec3f m_zAxis; ///< Camera z-axis in WCS
};
108 changes: 54 additions & 54 deletions src/ICamera.h
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
// Camera Abstract Interface class
// Written by Sergey Kosov in 2005 for Rendering Competition
#pragma once

#include "ray.h"

// ================================ Camera Interface Class ================================
/**
* @brief Basic camera abstract interface class
*/
class ICamera
{
public:
/**
* @brief Constructor
* @param resolution The resolution of the camera in pixels
*/
ICamera(Size resolution)
: m_resolution(resolution)
, m_aspectRatio(static_cast<float>(resolution.width) / resolution.height)
{}
ICamera(const ICamera&) = delete;
virtual ~ICamera(void) = default;
const ICamera& operator=(const ICamera&) = delete;

/**
* @brief Initializes a ray \b ray passing trough a screen pixel with coordinates \b x anf \b y
* @details This function initializes the ray pointing from the camera origin to the pixel
* on the camera screen defyned by the coodrinates \b (x,y). The pixel coordinates need to lie
* in the ranges of camera resolution.
* @param[out] ray Reference to the @ref Ray structure to be filled
* @param[in] x The x-coordinate of the pixel lying on the camera screen
* @param[in] y The y-coordinate of the pixel lying on the camera screen
*/
virtual void InitRay(Ray& ray, int x, int y) = 0;

/**
* @brief Retuns the camera resolution in pixels
* @return The camera resolution in pixels
*/
Size getResolution(void) const { return m_resolution; }
/**
* @brief Returns the camera aspect ratio
* @return The camera aspect ratio
*/
float getAspectRatio(void) const { return m_aspectRatio; }


private:
const Size m_resolution; ///< Camera image resolution in pixels
const float m_aspectRatio; ///< Camera image aspect ratio
};

using ptr_camera_t = std::shared_ptr<ICamera>;
// Camera Abstract Interface class
// Written by Sergey Kosov in 2005 for Rendering Competition
#pragma once
#include "ray.h"
// ================================ Camera Interface Class ================================
/**
* @brief Basic camera abstract interface class
*/
class ICamera
{
public:
/**
* @brief Constructor
* @param resolution The resolution of the camera in pixels
*/
ICamera(Size resolution)
: m_resolution(resolution)
, m_aspectRatio(static_cast<float>(resolution.width) / resolution.height)
{}
ICamera(const ICamera&) = delete;
virtual ~ICamera(void) = default;
const ICamera& operator=(const ICamera&) = delete;
/**
* @brief Initializes a ray \b ray passing trough a screen pixel with coordinates \b x anf \b y
* @details This function initializes the ray pointing from the camera origin to the pixel
* on the camera screen defyned by the coodrinates \b (x,y). The pixel coordinates need to lie
* in the ranges of camera resolution.
* @param[out] ray Reference to the @ref Ray structure to be filled
* @param[in] x The x-coordinate of the pixel lying on the camera screen
* @param[in] y The y-coordinate of the pixel lying on the camera screen
*/
virtual void InitRay(Ray& ray, int x, int y) = 0;
/**
* @brief Retuns the camera resolution in pixels
* @return The camera resolution in pixels
*/
Size getResolution(void) const { return m_resolution; }
/**
* @brief Returns the camera aspect ratio
* @return The camera aspect ratio
*/
float getAspectRatio(void) const { return m_aspectRatio; }
private:
const Size m_resolution; ///< Camera image resolution in pixels
const float m_aspectRatio; ///< Camera image aspect ratio
};
using ptr_camera_t = std::shared_ptr<ICamera>;
84 changes: 42 additions & 42 deletions src/ILight.h
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
#pragma once

#include "types.h"

struct Ray;

// ================================ Light Interface Class ================================
/**
* @brief Base light source abstract interface class
*/
class ILight
{
public:
/**
* @brief Constructor
* @param castShadow Flag indicating whether the light source casts shadows
*/
ILight(bool castShadow) : m_shadow(castShadow) {}
ILight(const ILight&) = delete;
virtual ~ILight(void) = default;
const ILight& operator=(const ILight&) = delete;

/**
* @brief Calculates the light intensity, at the point \b ray.org which is to be illuminated.
* @details This function sets the \b ray.dir to be the the direction vector from the surface point \b ray.org to the light source.
* @param[in, out] ray The ray from object point to the light source. The direction ray.dir is modified within the function
* @return The intensity of light hitting the point \b ray.org
*/
virtual std::optional<Vec3f> illuminate(Ray& ray) = 0;
/**
* @brief Flag indicating if the light source casts shadow or not
* @retval true If the light source casts shadow
* @retval false Otherwise
*/
virtual bool shadow(void) const { return m_shadow; }


private:
bool m_shadow;
};

using ptr_light_t = std::shared_ptr<ILight>;
#pragma once
#include "types.h"
struct Ray;
// ================================ Light Interface Class ================================
/**
* @brief Base light source abstract interface class
*/
class ILight
{
public:
/**
* @brief Constructor
* @param castShadow Flag indicating whether the light source casts shadows
*/
ILight(bool castShadow) : m_shadow(castShadow) {}
ILight(const ILight&) = delete;
virtual ~ILight(void) = default;
const ILight& operator=(const ILight&) = delete;
/**
* @brief Calculates the light intensity, at the point \b ray.org which is to be illuminated.
* @details This function sets the \b ray.dir to be the the direction vector from the surface point \b ray.org to the light source.
* @param[in, out] ray The ray from object point to the light source. The direction ray.dir is modified within the function
* @return The intensity of light hitting the point \b ray.org
*/
virtual std::optional<Vec3f> illuminate(Ray& ray) = 0;
/**
* @brief Flag indicating if the light source casts shadow or not
* @retval true If the light source casts shadow
* @retval false Otherwise
*/
virtual bool shadow(void) const { return m_shadow; }
private:
bool m_shadow;
};
using ptr_light_t = std::shared_ptr<ILight>;
Loading