Skip to content

Commit

Permalink
Merge pull request #11 from fszewczyk/export-render
Browse files Browse the repository at this point in the history
Export render
  • Loading branch information
fszewczyk authored Mar 23, 2023
2 parents 2f6617f + e83569b commit 915bfbe
Show file tree
Hide file tree
Showing 23 changed files with 823 additions and 345 deletions.
10 changes: 8 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ project(shkyeraEngine)

set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")

if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -fPIE -no-pie")
endif()

set(FETCHCONTENT_BASE_DIR ${CMAKE_BINARY_DIR}/_deps)

Expand Down Expand Up @@ -47,6 +52,7 @@ add_library(
src/ui/settings/cameraSettingsWindow.cpp
src/ui/settings/worldSettingsWindow.cpp
src/ui/settings/planetSettingsWindow.cpp
src/ui/settings/exportSettingsWindow.cpp
)


Expand Down
60 changes: 57 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
![Sample](resources/instructions/image.png)

<div align="center">

<h1>SHKYERA Engine</h1>
Expand All @@ -12,16 +14,68 @@

</div>

![Sample](resources/instructions/image.png)

## Building
## Quick start
Download appropriate executives for your system from [here]().
### Linux
Everything should work from the beginning. Just extract the contents and launch the app.

### Setup
### MacOS
Open Terminal in the extracted directory and run
```
./shkyera
```

## Building from source
Everything should be taken care of if you just do this.

```sh
chmod +x ./build.sh

./build.sh -r
```

## Using the Engine
![UI](resources/instructions/ui.png)
### Camera
Once you click on the top-left **Render** window. You can move the camera freely using:
1. W - forward
2. A - left
3. S - backwards
4. D - right
5. E - up
6. Q - down

If you hold left mouse button, you can freely rotate the camera.

In the top-right **Camera** window, you can manually select camera's position as well as its:
1. Depth of Field - "blur effect"
2. Focus Distance - focus of the camera
3. Field of View - camera "zoom"

Camera's position can also be manipulated on the plots in the bottom-left window.

### Planets
Click "Add New Object" in the **World** menu in the bottom-right window to create a new planet.

By clicking on one of the planet's names, you can manipulate its:
1. Position
2. Size
3. Texture
4. Remove the object

In the _Texture_ Menu, you can choose planet's color and press _Set Color_ to apply changes or choose one of the preloaded textures.

You have to select _Emit Light_ checkbox in order to emit light from that object. You can also change light's color and intensity. If you want to apply these changes, you have to select the texture **again** or _Set Color_.

### Ambient Light
Change Ambient Light in the **World** window to alter the color that the "unvierse" shines onto the objects.

## Plots
In the bottom-left window, you can see your world from all three different sides. You can drag and drop the planets as well as the camera. You can also see the focus distance of the camera. This is especially useful if you have high Depth of Field.

## Resources
1. [_Ray Tracing in One Weekend_](https://raytracing.github.io/books/RayTracingInOneWeekend.html)
2. [_Atta_](https://github.com/brenocq/atta)
3. [_The Cherno's raytracing series_](https://www.youtube.com/watch?v=gfW1Fhd9u9Q&list=PLlrATfBNZ98edc5GshdBtREv5asFW3yXl)
4. [_Planets' textures_](https://www.solarsystemscope.com/textures/)
Binary file modified resources/instructions/image.png
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 resources/instructions/ui.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions src/core/image.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "core/image.hpp"

#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image.h"
#include "stb_image_write.h"

#define GL_SILENCE_DEPRECATION
#if defined(IMGUI_IMPL_OPENGL_ES2)
Expand Down Expand Up @@ -29,6 +31,9 @@ image::image(const char *filename) {

if (!raw_data) {
std::cerr << "\033[0;31mCould not load texture from file '" << filename << "'\033[0m" << std::endl;
#ifdef __APPLE__
std::cerr << "\tMake sure you are running the executive from its directory." << std::endl;
#endif
m_width = 0;
m_height = 0;
return;
Expand Down Expand Up @@ -176,6 +181,36 @@ void image::writeColor(std::ostream &out, color pixelColor) const {
color &image::operator()(int x, int y) { return m_data[y][x]; }
color &image::at(int x, int y) { return m_data[y][x]; }

void image::saveToPng(std::string path) {
char *data = new char[3 * width() * height()];

for (int y = 0; y < height(); ++y) {
for (int x = 0; x < width(); ++x) {
color c = at(x, y);
data[(y * width() + x) * 3 + 0] = static_cast<int>(c[0] * 255);
data[(y * width() + x) * 3 + 1] = static_cast<int>(c[1] * 255);
data[(y * width() + x) * 3 + 2] = static_cast<int>(c[2] * 255);
}
}

stbi_write_png(path.c_str(), width(), height(), 3, data, 3 * width());
}

void image::saveToJpg(std::string path) {
char *data = new char[3 * width() * height()];

for (int y = 0; y < height(); ++y) {
for (int x = 0; x < width(); ++x) {
color c = at(x, y);
data[(y * width() + x) * 3 + 0] = static_cast<int>(c[0] * 255);
data[(y * width() + x) * 3 + 1] = static_cast<int>(c[1] * 255);
data[(y * width() + x) * 3 + 2] = static_cast<int>(c[2] * 255);
}
}

stbi_write_jpg(path.c_str(), width(), height(), 3, data, 100);
}

std::shared_ptr<image> image::EARTH_DAY_TEXTURE = std::make_shared<image>("resources/textures/earthday.jpg");
std::shared_ptr<image> image::EARTH_NIGHT_TEXTURE = std::make_shared<image>("resources/textures/earthnight.jpg");
std::shared_ptr<image> image::MARS_TEXTURE = std::make_shared<image>("resources/textures/mars.jpg");
Expand Down
3 changes: 3 additions & 0 deletions src/core/image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ class image {
color &operator()(int x, int y);
color &at(int x, int y);

void saveToPng(std::string path);
void saveToJpg(std::string path);

static constexpr int TEXTURE_BYTES_PER_PIXEL = 3;
static constexpr int ICON_SIZE = 64;

Expand Down
2 changes: 1 addition & 1 deletion src/core/vec3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void vec3::rotateAroundY(double angle) {
}

void vec3::rotateUpAndDown(double angle) {
float baseAngle = atan2(m_cords[0], m_cords[2]);
float baseAngle = atan2(m_cords[2], m_cords[0]);
rotateAroundY(baseAngle);

double new_x = m_cords[0] * cos(angle) - m_cords[1] * sin(angle);
Expand Down
10 changes: 7 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

using namespace shkyera;

int main(int argc, char *argv[]) {

ui prepareUserInterface() {
const auto aspectRatio = 16.0 / 9.0;
const int imageWidth = 600;
const int imageHeight = static_cast<int>(imageWidth / aspectRatio);
Expand Down Expand Up @@ -46,8 +45,13 @@ int main(int argc, char *argv[]) {

ui interface(im, r, world, cam);

interface.init();
return interface;
}

int main(int argc, char *argv[]) {
ui interface = prepareUserInterface();

interface.init();
while (interface.isOpen()) {
interface.run();
}
Expand Down
98 changes: 54 additions & 44 deletions src/shapes/material.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,70 +14,80 @@ typedef struct hitData hitData;
enum MATERIAL_TYPE { LAMBERTIAN, METAL, REFRACTOR, DIFFUSE_LIGHT };

class material {
public:
virtual bool scatter(const ray &rayIn, const hitData &data, color &attenuation, ray &rayOut) const = 0;
virtual color emit(double u, double v, const point3 &p, bool firstHit) const;
virtual std::shared_ptr<solidColor> getLightMaterial() const;
virtual color getVisibleColor() const;
public:
virtual bool scatter(const ray &rayIn, const hitData &data,
color &attenuation, ray &rayOut) const = 0;
virtual color emit(double u, double v, const point3 &p, bool firstHit) const;
virtual std::shared_ptr<solidColor> getLightMaterial() const;
virtual color getVisibleColor() const;
};

class lambertian : public material {
public:
lambertian(const color &c);
lambertian(shared_ptr<texture> c);
public:
lambertian(const color &c);
lambertian(shared_ptr<texture> c);

static std::shared_ptr<lambertian> generateFromImage(std::shared_ptr<image> im);
static std::shared_ptr<lambertian> generateFromImageTextureType(int imageTextureType);
static std::shared_ptr<lambertian>
generateFromImage(std::shared_ptr<image> im);
static std::shared_ptr<lambertian>
generateFromImageTextureType(int imageTextureType);

virtual bool scatter(const ray &rayIn, const hitData &data, color &attenuation, ray &rayOut) const override;
virtual color getVisibleColor() const;
virtual bool scatter(const ray &rayIn, const hitData &data,
color &attenuation, ray &rayOut) const override;
virtual color getVisibleColor() const override;

private:
shared_ptr<texture> m_albedo;
private:
shared_ptr<texture> m_albedo;
};

class metal : public material {
public:
metal(const color &c, double f);
public:
metal(const color &c, double f);

virtual bool scatter(const ray &rayIn, const hitData &data, color &attenuation, ray &rayOut) const override;
virtual color getVisibleColor() const;
virtual bool scatter(const ray &rayIn, const hitData &data,
color &attenuation, ray &rayOut) const override;
virtual color getVisibleColor() const override;

private:
color m_albedo;
double m_fuzz;
private:
color m_albedo;
double m_fuzz;
};

class refractor : public material {
public:
refractor(double eta);
public:
refractor(double eta);

virtual bool scatter(const ray &rayIn, const hitData &data, color &attenuation, ray &rayOut) const override;
virtual bool scatter(const ray &rayIn, const hitData &data,
color &attenuation, ray &rayOut) const override;

private:
static double reflectance(double cosine, double eta);
private:
static double reflectance(double cosine, double eta);

double m_eta;
double m_eta;
};

class diffuseLight : public material {
public:
diffuseLight(shared_ptr<texture> text, color lightColor);
diffuseLight(color even, color odd, color lightColor);
diffuseLight(color displayColor, color lightColor);

static std::shared_ptr<diffuseLight> generateFromImage(std::shared_ptr<image> im, color c);
static std::shared_ptr<diffuseLight> generateFromImageTextureType(IMAGE_TEXTURE_TYPE imageTextureType, color c);

virtual bool scatter(const ray &rayIn, const hitData &data, color &attenuation, ray &rayOut) const override;
virtual color emit(double u, double v, const point3 &p, bool firstHit) const override;

virtual std::shared_ptr<solidColor> getLightMaterial() const override;
virtual color getVisibleColor() const;

private:
shared_ptr<texture> m_textureToDisplay;
shared_ptr<solidColor> m_lightColor;
public:
diffuseLight(shared_ptr<texture> text, color lightColor);
diffuseLight(color even, color odd, color lightColor);
diffuseLight(color displayColor, color lightColor);

static std::shared_ptr<diffuseLight>
generateFromImage(std::shared_ptr<image> im, color c);
static std::shared_ptr<diffuseLight>
generateFromImageTextureType(IMAGE_TEXTURE_TYPE imageTextureType, color c);

virtual bool scatter(const ray &rayIn, const hitData &data,
color &attenuation, ray &rayOut) const override;
virtual color emit(double u, double v, const point3 &p,
bool firstHit) const override;

virtual std::shared_ptr<solidColor> getLightMaterial() const override;
virtual color getVisibleColor() const override;

private:
shared_ptr<texture> m_textureToDisplay;
shared_ptr<solidColor> m_lightColor;
};

} // namespace shkyera
Expand Down
8 changes: 7 additions & 1 deletion src/ui/render/renderWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void renderWindow::updateImageTexture() {
m_loadWidth = m_image->width();
m_loadHeight = m_image->height();

if (m_renderTexture.size() == 0)
if (m_renderTexture.size() != 4 * m_loadWidth * m_loadHeight)
m_renderTexture.resize(4 * m_loadWidth * m_loadHeight);

for (size_t y = 0; y < m_loadHeight; ++y) {
Expand All @@ -103,6 +103,7 @@ void renderWindow::updateImageTexture() {
m_renderTexture[(y * m_loadWidth + x) * 4 + 3] = 255;
}
}

unsigned textureId = m_loadTexId;

if (m_loadedImage)
Expand All @@ -122,6 +123,11 @@ void renderWindow::updateImageTexture() {
m_loadTexId = textureId;
}

void renderWindow::setImage(std::shared_ptr<image> image) {
m_image = image;
updateImageTexture();
}

std::shared_ptr<image> renderWindow::getImage() const { return m_image; }

} // namespace shkyera
1 change: 1 addition & 0 deletions src/ui/render/renderWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class renderWindow {

point3 render(bool sampleTexture, bool &updated, std::pair<int, int> &mouseMovement);

void setImage(std::shared_ptr<image> image);
std::shared_ptr<image> getImage() const;

private:
Expand Down
Loading

0 comments on commit 915bfbe

Please sign in to comment.