First commit

master
zovjsra 2 years ago
commit 4e7a085ed9

7
.gitignore vendored

@ -0,0 +1,7 @@
.vscode
build/
light.ppm
bonus.ppm
normal.ppm
red.ppm
.DS_Store

@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 2.8)
project(ray)
set(CMAKE_CXX_FLAGS "-g -Wall")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
add_executable(ray60 primaryRays60.cpp)
add_executable(test001 main.cpp)
add_executable(skybox skybox.cpp)
add_executable(diffuse primaryRaysDiffuse.cpp)
add_executable(light primaryRaysLightSource.cpp)
add_executable(bonus bonus.cpp)

@ -0,0 +1,121 @@
Creative Commons Legal Code
CC0 1.0 Universal
CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
HEREUNDER.
Statement of Purpose
The laws of most jurisdictions throughout the world automatically confer
exclusive Copyright and Related Rights (defined below) upon the creator
and subsequent owner(s) (each and all, an "owner") of an original work of
authorship and/or a database (each, a "Work").
Certain owners wish to permanently relinquish those rights to a Work for
the purpose of contributing to a commons of creative, cultural and
scientific works ("Commons") that the public can reliably and without fear
of later claims of infringement build upon, modify, incorporate in other
works, reuse and redistribute as freely as possible in any form whatsoever
and for any purposes, including without limitation commercial purposes.
These owners may contribute to the Commons to promote the ideal of a free
culture and the further production of creative, cultural and scientific
works, or to gain reputation or greater distribution for their Work in
part through the use and efforts of others.
For these and/or other purposes and motivations, and without any
expectation of additional consideration or compensation, the person
associating CC0 with a Work (the "Affirmer"), to the extent that he or she
is an owner of Copyright and Related Rights in the Work, voluntarily
elects to apply CC0 to the Work and publicly distribute the Work under its
terms, with knowledge of his or her Copyright and Related Rights in the
Work and the meaning and intended legal effect of CC0 on those rights.
1. Copyright and Related Rights. A Work made available under CC0 may be
protected by copyright and related or neighboring rights ("Copyright and
Related Rights"). Copyright and Related Rights include, but are not
limited to, the following:
i. the right to reproduce, adapt, distribute, perform, display,
communicate, and translate a Work;
ii. moral rights retained by the original author(s) and/or performer(s);
iii. publicity and privacy rights pertaining to a person's image or
likeness depicted in a Work;
iv. rights protecting against unfair competition in regards to a Work,
subject to the limitations in paragraph 4(a), below;
v. rights protecting the extraction, dissemination, use and reuse of data
in a Work;
vi. database rights (such as those arising under Directive 96/9/EC of the
European Parliament and of the Council of 11 March 1996 on the legal
protection of databases, and under any national implementation
thereof, including any amended or successor version of such
directive); and
vii. other similar, equivalent or corresponding rights throughout the
world based on applicable law or treaty, and any national
implementations thereof.
2. Waiver. To the greatest extent permitted by, but not in contravention
of, applicable law, Affirmer hereby overtly, fully, permanently,
irrevocably and unconditionally waives, abandons, and surrenders all of
Affirmer's Copyright and Related Rights and associated claims and causes
of action, whether now known or unknown (including existing as well as
future claims and causes of action), in the Work (i) in all territories
worldwide, (ii) for the maximum duration provided by applicable law or
treaty (including future time extensions), (iii) in any current or future
medium and for any number of copies, and (iv) for any purpose whatsoever,
including without limitation commercial, advertising or promotional
purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
member of the public at large and to the detriment of Affirmer's heirs and
successors, fully intending that such Waiver shall not be subject to
revocation, rescission, cancellation, termination, or any other legal or
equitable action to disrupt the quiet enjoyment of the Work by the public
as contemplated by Affirmer's express Statement of Purpose.
3. Public License Fallback. Should any part of the Waiver for any reason
be judged legally invalid or ineffective under applicable law, then the
Waiver shall be preserved to the maximum extent permitted taking into
account Affirmer's express Statement of Purpose. In addition, to the
extent the Waiver is so judged Affirmer hereby grants to each affected
person a royalty-free, non transferable, non sublicensable, non exclusive,
irrevocable and unconditional license to exercise Affirmer's Copyright and
Related Rights in the Work (i) in all territories worldwide, (ii) for the
maximum duration provided by applicable law or treaty (including future
time extensions), (iii) in any current or future medium and for any number
of copies, and (iv) for any purpose whatsoever, including without
limitation commercial, advertising or promotional purposes (the
"License"). The License shall be deemed effective as of the date CC0 was
applied by Affirmer to the Work. Should any part of the License for any
reason be judged legally invalid or ineffective under applicable law, such
partial invalidity or ineffectiveness shall not invalidate the remainder
of the License, and in such case Affirmer hereby affirms that he or she
will not (i) exercise any of his or her remaining Copyright and Related
Rights in the Work or (ii) assert any associated claims and causes of
action with respect to the Work, in either case contrary to Affirmer's
express Statement of Purpose.
4. Limitations and Disclaimers.
a. No trademark or patent rights held by Affirmer are waived, abandoned,
surrendered, licensed or otherwise affected by this document.
b. Affirmer offers the Work as-is and makes no representations or
warranties of any kind concerning the Work, express, implied,
statutory or otherwise, including without limitation warranties of
title, merchantability, fitness for a particular purpose, non
infringement, or the absence of latent or other defects, accuracy, or
the present or absence of errors, whether or not discoverable, all to
the greatest extent permissible under applicable law.
c. Affirmer disclaims responsibility for clearing rights of other persons
that may apply to the Work or any use thereof, including without
limitation any person's Copyright and Related Rights in the Work.
Further, Affirmer disclaims responsibility for obtaining any necessary
consents, permissions or other rights required for any use of the
Work.
d. Affirmer understands and acknowledges that Creative Commons is not a
party to this document and has no duty or obligation with respect to
this CC0 or use of the Work.

@ -0,0 +1,77 @@
#include <cmath>
#include <fstream>
#include <iostream>
#include "ray.h"
#include "vec3.h"
using namespace std;
//skybox
float hit_sphere(const vec3& center, float radius, const ray& r) {
float a, b, c, t;
a = dot(r.D, r.D);
b = 2 * (dot(r.D, (r.O - center)));
c = dot(r.O - center, r.O - center) - radius * radius;
t = (((-b) - sqrt((b * b) - (4 * a * c))) / (2 * a));
//cout << a << " " << b << " " << c;
return t >= 0 ? t : 0;
}
vec3 color(ray& r) {
vec3 center(0, 0, -1);
float t = hit_sphere(center, 0.5, r);
if (t > 0.0) {
vec3 N = unit_vector(r.point_at_parameter(t) - center);
vec3 L = unit_vector(vec3(1, 1, 0) - r.point_at_parameter(t));
vec3 I = vec3(1, 1, 1); //intensity of lightsource
return (dot(N, L) > 0 ? dot(N, L) : 0) * I;
}
vec3 unit_direction = unit_vector(r.direction());
t = 0.5 * (unit_direction.y() + 1.0);
return (1.0 - t) * vec3(1, 1, 1) + t * vec3(0.5, 0.7, 1.0);
}
int main() {
int width = 200;
int height = 100;
int anti_aliasing_scale = 5;
fstream file;
file.open("ray.ppm", ios::out);
vec3 lower_left_corner(-2, -1, -1);
vec3 origin(0, 0, 0);
vec3 horizontal(4, 0, 0);
vec3 vertical(0, 2, 0);
file << "P3\n"
<< width << " " << height << "\n255\n";
vec3** colors = new vec3*[height * anti_aliasing_scale];
for (int i = 0; i < height * anti_aliasing_scale; i++) {
colors[i] = new vec3[width * anti_aliasing_scale];
}
for (int j = (height * anti_aliasing_scale) - 1; j >= 0; j--) {
for (int i = 0; i < (width * anti_aliasing_scale); i++) {
float u = float(i) / float(width * anti_aliasing_scale);
float v = float(j) / float(height * anti_aliasing_scale);
ray r = ray(origin, lower_left_corner + u * horizontal + v * vertical);
vec3 color2 = color(r);
colors[j][i] = color2 * 255;
}
}
for (int j = height - 1; j >= 0; j--) {
for (int i = 0; i < width; i++) {
vec3 color3 = vec3(0, 0, 0);
for (int a = 0; a < anti_aliasing_scale; a++) {
for (int b = 0; b < anti_aliasing_scale; b++) {
color3 = vec3(color3.r() + colors[j * anti_aliasing_scale + a][i * anti_aliasing_scale + b].r(), color3.g() + colors[j * anti_aliasing_scale + a][i * anti_aliasing_scale + b].g(), color3.b() + colors[j * anti_aliasing_scale + a][i * anti_aliasing_scale + b].b());
}
}
color3 = vec3(color3.r() / (anti_aliasing_scale * anti_aliasing_scale), color3.g() / (anti_aliasing_scale * anti_aliasing_scale), color3.b() / (anti_aliasing_scale * anti_aliasing_scale));
file << int(color3.r()) << " " << int(color3.g()) << " " << int(color3.b()) << "\n";
}
}
file.close();
}

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="cghw1" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/cghw1" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/cghw1" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename="main.cpp" />
<Extensions>
<code_completion />
<envvars />
<debugger />
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>

@ -0,0 +1,27 @@
#include <fstream>
#include <iostream>
//#include "vec3.h"
using namespace std;
int main() {
int width = 200;
int height = 100;
fstream file;
file.open("ray.ppm", ios::out);
file << "P3\n"
<< width << " " << height << "\n255\n";
for (int j = height - 1; j >= 0; j--) {
for (int i = 0; i < width; i++) {
float r = float(i) / float(width);
float g = float(j) / float(height);
float b = 0.2;
file << int(r * 255) << " " << int(g * 255) << " " << int(b * 255) << "\n";
}
}
return 0;
}

@ -0,0 +1,52 @@
#include <fstream>
#include <iostream>
#include "ray.h"
#include "vec3.h"
using namespace std;
//skybox
bool hit_sphere(const vec3& center, float radius, const ray& r) {
float a, b, c;
a = dot(r.D, r.D);
b = 2 * (dot(r.D, (r.O - center)));
c = dot(r.O - center, r.O - center) - radius * radius;
return ((b * b) - (4 * a * c) >= 0);
}
vec3 color(ray& r) {
if (hit_sphere(vec3(0, 0, -1), 0.5, r)) {
return vec3(1, 0, 0);
}
vec3 unit_direction = unit_vector(r.direction());
float t = 0.5 * (unit_direction.y() + 1.0);
return vec3(1, 1, 1) * float(1.0 - t) + float(t) * vec3(0.5, 0.7, 1.0);
}
int main() {
int width = 200;
int height = 100;
fstream file;
file.open("ray.ppm", ios::out);
vec3 lower_left_corner(-2, -1, -1);
vec3 origin(0, 0, 0);
vec3 horizontal(4, 0, 0);
vec3 vertical(0, 2, 0);
file << "P3\n"
<< width << " " << height << "\n255\n";
for (int j = height - 1; j >= 0; j--) {
for (int i = 0; i < width; i++) {
float u = float(i) / float(width);
float v = float(j) / float(height);
ray r = ray(origin, lower_left_corner + u * horizontal + v * vertical);
vec3 color2 = color(r);
file << int(color2.r() * 255) << " " << int(color2.g() * 255) << " " << int(color2.b() * 255) << "\n";
}
}
file.close();
}

@ -0,0 +1,55 @@
#include <iostream>
#include <fstream>
#include <cmath>
#include "vec3.h"
#include "ray.h"
using namespace std;
//skybox
float hit_sphere(const vec3 &center, float radius, const ray& r) {
float a, b, c;
a = dot(r.D, r.D);
b = 2 * (dot(r.D, (r.O - center)));
c = dot(r.O - center, r.O - center) - radius * radius;
return (((-b) + sqrt((b * b) - (4 * a * c))) / (2 * a));
}
vec3 color(ray& r){
vec3 center = vec3(0, 0, -1);
float t = hit_sphere(center, 0.5, r);
if (t > 0.0) {
vec3 N = unit_vector(r.point_at_parameter(t) - center);
return 0.5 * vec3(N.x()+1, N.y()+1, N.z()+1);
}
vec3 unit_direction = unit_vector(r.direction());
t = 0.5 * (unit_direction.y() + 1.0);
return (1.0-t)* vec3(1, 1, 1) + t* vec3(0.5, 0.7, 1.0);
}
int main(){
int width = 200;
int height = 100;
fstream file;
file.open("ray.ppm", ios::out);
vec3 lower_left_corner(-2, -1, -1);
vec3 origin(0, 0, 0);
vec3 horizontal(4, 0, 0);
vec3 vertical(0, 2, 0);
file << "P3\n" << width << " " << height << "\n255\n";
for (int j = height - 1; j >= 0; j--) {
for (int i = 0; i < width; i++) {
float u = float(i) / float(width);
float v = float(j) / float(height);
ray r = ray(origin, lower_left_corner + u*horizontal + v*vertical);
vec3 color2 = color(r);
file << int(color2.r() * 255) << " " << int(color2.g() * 255) << " " << int(color2.b() * 255) << "\n";
}
}
file.close();
}

@ -0,0 +1,60 @@
#include <cmath>
#include <fstream>
#include <iostream>
#include "ray.h"
#include "vec3.h"
using namespace std;
//skybox
float hit_sphere(const vec3& center, float radius, const ray& r) {
float a, b, c, t;
a = dot(r.D, r.D);
b = 2 * (dot(r.D, (r.O - center)));
c = dot(r.O - center, r.O - center) - radius * radius;
t = (((-b) - sqrt((b * b) - (4 * a * c))) / (2 * a));
//cout << a << " " << b << " " << c;
return t >= 0 ? t : 0;
}
vec3 color(ray& r) {
vec3 center(0, 0, -1);
float t = hit_sphere(center, 0.5, r);
if (t > 0.0) {
vec3 N = unit_vector(r.point_at_parameter(t) - center);
vec3 L = unit_vector(vec3(1, 1, 0) - r.point_at_parameter(t));
vec3 I = vec3(1, 1, 1); //intensity of lightsource
cout << t << " " << I * dot(N, L) << endl;
return (dot(N, L) > 0 ? dot(N, L) : 0) * I;
}
vec3 unit_direction = unit_vector(r.direction());
t = 0.5 * (unit_direction.y() + 1.0);
return (1.0 - t) * vec3(1, 1, 1) + t * vec3(0.5, 0.7, 1.0);
}
int main() {
int width = 200;
int height = 100;
fstream file;
file.open("ray.ppm", ios::out);
vec3 lower_left_corner(-2, -1, -1);
vec3 origin(0, 0, 0);
vec3 horizontal(4, 0, 0);
vec3 vertical(0, 2, 0);
file << "P3\n"
<< width << " " << height << "\n255\n";
for (int j = height - 1; j >= 0; j--) {
for (int i = 0; i < width; i++) {
float u = float(i) / float(width);
float v = float(j) / float(height);
ray r = ray(origin, lower_left_corner + u * horizontal + v * vertical);
vec3 color2 = color(r);
file << int(color2.r() * 255) << " " << int(color2.g() * 255) << " " << int(color2.b() * 255) << "\n";
}
}
file.close();
}

20
ray.h

@ -0,0 +1,20 @@
#ifndef RAYH
#define RAYH
#include "vec3.h"
class ray
{
public:
ray() {}
ray(const vec3& a, const vec3& b) { O = a; D = b; }
vec3 origin() const { return O; }
vec3 direction() const { return D; }
vec3 point_at_parameter(float t) const {
return vec3(D * t + O);
}
vec3 O;
vec3 D;
};
#endif

@ -0,0 +1,54 @@
#include <fstream>
#include <iostream>
#include "ray.h"
#include "vec3.h"
using namespace std;
//skybox
bool hit_sphere(const vec3& center, float radius, const ray& r) {
float a, b, c;
a = dot(r.D, r.D);
b = 2 * (dot(r.D, (r.O - center)));
c = dot(r.O - center, r.O - center) - radius * radius;
return ((b * b) - (4 * a * c) >= 0);
}
vec3 color(ray& r) {
/*
if (hit_sphere(vec3(0, 0, -1), 0.5, r)) {
return vec3(1, 0, 0);
}
*/
vec3 unit_direction = unit_vector(r.direction());
float t = 0.5 * (unit_direction.y() + 1.0);
return vec3(1, 1, 1) * float(1.0 - t) + float(t) * vec3(0.5, 0.7, 1.0);
}
int main() {
int width = 200;
int height = 100;
fstream file;
file.open("ray.ppm", ios::out);
vec3 lower_left_corner(-2, -1, -1);
vec3 origin(0, 0, 0);
vec3 horizontal(4, 0, 0);
vec3 vertical(0, 2, 0);
file << "P3\n"
<< width << " " << height << "\n255\n";
for (int j = height - 1; j >= 0; j--) {
for (int i = 0; i < width; i++) {
float u = float(i) / float(width);
float v = float(j) / float(height);
ray r = ray(origin, lower_left_corner + u * horizontal + v * vertical);
vec3 color2 = color(r);
file << int(color2.r() * 255) << " " << int(color2.g() * 255) << " " << int(color2.b() * 255) << "\n";
}
}
file.close();
}

File diff suppressed because it is too large Load Diff

113
vec3.h

@ -0,0 +1,113 @@
//=================================================================================================
// Written in 2016 by Peter Shirley <ptrshrl@gmail.com>
//
// To the extent possible under law, the author(s) have dedicated all copyright and related and
// neighboring rights to this software to the public domain worldwide. This software is distributed
// without any warranty.
//
// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along
// with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//==================================================================================================
#ifndef VEC3H
#define VEC3H
#include <cmath>
#include <cstdlib>
#include <iostream>
class vec3 {
public:
vec3() {}
vec3(float e0, float e1, float e2) {
e[0] = e0;
e[1] = e1;
e[2] = e2;
}
float x() const { return e[0]; }
float y() const { return e[1]; }
float z() const { return e[2]; }
float r() const { return e[0]; }
float g() const { return e[1]; }
float b() const { return e[2]; }
inline vec3& operator+=(const vec3& v2) const {
vec3* temp = new vec3(this->x() + v2.x(), this->y() + v2.y(), this->z() + v2.z());
return *temp;
};
inline vec3& operator-=(const vec3& v2) const {
vec3* temp = new vec3(this->x() - v2.x(), this->y() - v2.y(), this->z() - v2.z());
return *temp;
};
inline vec3& operator*=(const vec3& v2) {
vec3* temp = new vec3(this->x() * v2.x(), this->y() * v2.y(), this->z() * v2.z());
return *temp;
};
inline vec3& operator/=(const vec3& v2) {
vec3* temp = new vec3(this->x() / v2.x(), this->y() / v2.y(), this->z() / v2.z());
return *temp;
};
inline vec3 operator*=(const float t) const {
return vec3(this->x() * t, this->y() * t, this->z() * t);
};
inline vec3& operator/=(const float t) {
vec3* temp = new vec3(this->x() / t, this->y() / t, this->z() / t);
return *temp;
};
inline float length() const { return sqrt(e[0] * e[0] + e[1] * e[1] + e[2] * e[2]); }
inline float squared_length() const { return e[0] * e[0] + e[1] * e[1] + e[2] * e[2]; }
inline void make_unit_vector();
float e[3];
};
inline vec3 operator+(vec3 v1, vec3 v2) {
return vec3(v1.x() + v2.x(), v1.y() + v2.y(), v1.z() + v2.z());
}
inline vec3 operator-(vec3 v1, vec3 v2) {
return vec3(v1.x() - v2.x(), v1.y() - v2.y(), v1.z() - v2.z());
}
inline vec3 operator*(vec3 v1, vec3 v2) {
return vec3(v1.x() * v2.x(), v1.y() * v2.y(), v1.z() * v2.z());
}
inline vec3 operator/(vec3 v1, vec3 v2) {
return vec3(v1.x() / v2.x(), v1.y() / v2.y(), v1.z() / v2.z());
}
inline vec3 operator*(vec3 v1, float t) {
return vec3(v1.x() * t, v1.y() * t, v1.z() * t);
}
inline vec3 operator*(float t, vec3 v1) {
return vec3(v1.x() * t, v1.y() * t, v1.z() * t);
}
inline vec3 operator/(vec3 v1, float t) {
return vec3(v1.x() / t, v1.y() / t, v1.z() / t);
}
inline std::ostream& operator<<(std::ostream& os, const vec3& t) {
os << "(" << t.x() << ", " << t.y() << ", " << t.z() << ")";
return os;
}
inline void vec3::make_unit_vector() {
float length = sqrt(this->x() * this->x() + this->y() * this->y() + this->z() * this->z());
this->e[0] = this->x() / length;
this->e[1] = this->y() / length;
this->e[2] = this->z() / length;
}
inline float dot(const vec3& v1, const vec3& v2) {
return (v1.x() * v2.x() + v1.y() * v2.y() + v1.z() * v2.z());
}
inline vec3 cross(const vec3& v1, const vec3& v2) {
return vec3((v1.x() * v2.y() - v1.y() * v2.x()), (v1.y() * v2.z() - v1.z() * v2.y()), (v1.z() * v2.x() - v1.x() * v2.z()));
}
inline vec3 unit_vector(vec3 v) {
float length = v.length();
return vec3(v.x() / length, v.y() / length, v.z() / length);
}
#endif
Loading…
Cancel
Save