Skip to content

Commit

Permalink
Merge pull request #10 from fszewczyk/objects-plot
Browse files Browse the repository at this point in the history
Plotting the world
  • Loading branch information
fszewczyk authored Mar 21, 2023
2 parents 501739d + 51eb6e5 commit 2f6617f
Show file tree
Hide file tree
Showing 24 changed files with 531 additions and 43 deletions.
9 changes: 7 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ add_library(
src/ui/ui.cpp
src/ui/renderer.cpp
src/ui/render/renderWindow.cpp
src/ui/plot/plotWindow.cpp
src/ui/plot/plotViewTab.cpp
src/ui/settings/cameraSettingsWindow.cpp
src/ui/settings/worldSettingsWindow.cpp
src/ui/settings/planetSettingsWindow.cpp
Expand All @@ -58,10 +60,13 @@ target_include_directories(shkyera PRIVATE src)
target_include_directories(core PRIVATE src ${CMAKE_BINARY_DIR}/_deps/glfw-src/include)
target_include_directories(shapes PRIVATE src)
target_include_directories(world PRIVATE src)
target_include_directories(ui PRIVATE src ${CMAKE_BINARY_DIR}/_deps/imgui-src ${CMAKE_BINARY_DIR}/_deps/glfw-src/include)
target_include_directories(ui PRIVATE src
${CMAKE_BINARY_DIR}/_deps/imgui-src
${CMAKE_BINARY_DIR}/_deps/implot-src
${CMAKE_BINARY_DIR}/_deps/glfw-src/include)

target_link_libraries(shkyera PRIVATE core shapes world ui imgui glfw glad)
target_link_libraries(ui PRIVATE imgui glfw glad core)
target_link_libraries(ui PRIVATE world imgui implot glfw glad core)
target_link_libraries(core PRIVATE stb_image glfw glad)


Expand Down
1 change: 1 addition & 0 deletions extern/extern.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH};${CMAKE_CURRENT_SOURCE_DIR}/extern)

include(solveGlfw)
include(solveImGui)
include(solveImPlot)
include(solveGlad)
include(solveStbImage)
20 changes: 20 additions & 0 deletions extern/solveImPlot.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FetchContent_Declare(
implot
GIT_REPOSITORY "https://github.com/epezent/implot"
GIT_PROGRESS TRUE
)

FetchContent_MakeAvailable(implot)

set(IMPLOT_SOURCE
${CMAKE_BINARY_DIR}/_deps/implot-src/implot.cpp
${CMAKE_BINARY_DIR}/_deps/implot-src/implot_demo.cpp
${CMAKE_BINARY_DIR}/_deps/implot-src/implot_items.cpp
)

add_library(implot STATIC
${IMPLOT_SOURCE}
)

target_include_directories(implot PUBLIC "${CMAKE_BINARY_DIR}/implot-src")
target_link_libraries(implot PRIVATE glfw imgui)
13 changes: 11 additions & 2 deletions src/core/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

namespace shkyera {

image::image() : m_width(0), m_height(0) {}
image::image() : m_width(0), m_height(0), m_dominantColor(randomDouble(0, 1), randomDouble(0, 1), randomDouble(0, 1)) {}

image::image(int width, int height)
: m_width(width), m_height(height), m_data(height, std::vector<color>(width)), m_verticalPixels(height),
m_horizontalPixels(width) {
m_horizontalPixels(width), m_dominantColor(0, 0, 0) {
std::iota(m_verticalPixels.begin(), m_verticalPixels.end(), 0);
std::iota(m_horizontalPixels.begin(), m_horizontalPixels.end(), 0);
}
Expand Down Expand Up @@ -48,6 +48,9 @@ image::image(const char *filename) {

m_data.resize(height());

color averageColor;
double imageArea = height() * width();

for (int y = 0; y < height(); ++y) {
std::vector<color> row(width());

Expand All @@ -60,11 +63,15 @@ image::image(const char *filename) {
}

row[x] = pixel;

averageColor += pixel / imageArea;
}

m_data[y] = row;
}

m_dominantColor = averageColor / 255;

delete raw_data;
}

Expand All @@ -78,6 +85,8 @@ void image::writeImage(std::ostream &out) const {
}
}

color image::getColor() const { return m_dominantColor; }

void image::scaleImage(std::shared_ptr<image> destinationImage, bool uniformScaling) {
float scaleHorizontal = static_cast<float>(width()) / destinationImage->width();
float scaleVertical = static_cast<float>(height()) / destinationImage->height();
Expand Down
4 changes: 4 additions & 0 deletions src/core/image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class image {
void clear();
void writeImage(std::ostream &out) const;

color getColor() const;

void scaleImage(std::shared_ptr<image> destinationImage, bool uniformScaling = false);

void updateTextureId();
Expand Down Expand Up @@ -109,6 +111,8 @@ class image {

uint64_t m_textureId;
bool m_assignedTextureId;

color m_dominantColor;
};

} // namespace shkyera
Expand Down
6 changes: 6 additions & 0 deletions src/core/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace shkyera {

color texture::getColor() const { return color(0.2, 0.8, 0.4); }

solidColor::solidColor(color c) : m_color(c) {}
solidColor::solidColor(double r, double g, double b) : m_color(color(r, g, b)) {}

Expand Down Expand Up @@ -29,6 +31,8 @@ color checkerTexture::value(double u, double v, const point3 &p) const {
return m_even->value(u, v, p);
}

color checkerTexture::getColor() const { return (m_even->getColor() + m_odd->getColor()) / 2; }

imageTexture::imageTexture(std::shared_ptr<image> image) : m_image(image) {}

color imageTexture::value(double u, double v, const point3 &p) const {
Expand All @@ -50,4 +54,6 @@ color imageTexture::value(double u, double v, const point3 &p) const {
return pixelColor;
}

color imageTexture::getColor() const { return m_image->getColor(); }

} // namespace shkyera
5 changes: 4 additions & 1 deletion src/core/texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ enum TEXTURE_TYPE { SOLID_COLOR, IMAGE };
class texture {
public:
virtual color value(double u, double v, const point3 &p) const = 0;
virtual color getColor() const;
};

class solidColor : public texture {
Expand All @@ -22,7 +23,7 @@ class solidColor : public texture {

virtual color value(double u, double v, const point3 &p) const override;

color getColor() const;
virtual color getColor() const override;
void setColor(color c);

private:
Expand All @@ -38,6 +39,7 @@ class checkerTexture : public texture {
checkerTexture(shared_ptr<texture> even, shared_ptr<texture> odd, double grain);

virtual color value(double u, double v, const point3 &p) const override;
virtual color getColor() const override;

private:
shared_ptr<texture> m_even;
Expand All @@ -54,6 +56,7 @@ class imageTexture : public texture {
~imageTexture() = default;

virtual color value(double u, double v, const point3 &p) const override;
virtual color getColor() const override;

private:
std::shared_ptr<image> m_image;
Expand Down
5 changes: 3 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using namespace shkyera;

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

const auto aspectRatio = 16.0 / 9.0;
const int imageWidth = 600;
const int imageHeight = static_cast<int>(imageWidth / aspectRatio);
Expand All @@ -31,8 +32,8 @@ int main(int argc, char *argv[]) {
world->add(make_shared<sphere>(point3(0, 0, -17.5), 10.0, sunMaterial, "Sun"));
world->add(make_shared<sphere>(point3(0, 0, 3), 1.0, marsMaterial, "Mars"));

point3 lookfrom(6, 2, 8);
point3 lookat(100, 20, 100);
point3 lookfrom(-26, 2, -18);
point3 lookat(-100, 20, -100);
auto dist_to_focus = 10.0;
auto aperture = 0.0;

Expand Down
8 changes: 8 additions & 0 deletions src/shapes/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@ color material::emit(double u, double v, const point3 &p, bool firstHit) const {

std::shared_ptr<solidColor> material::getLightMaterial() const { return nullptr; }

color material::getVisibleColor() const { return color(0.2, 0.8, 0.4); }

bool lambertian::scatter(const ray &rayIn, const hitData &data, color &attenuation, ray &rayOut) const {
auto scatterDirection = randomInUnitHemisphere(data.normal);
rayOut = ray(data.p, scatterDirection);
attenuation = m_albedo->value(data.u, data.v, data.p);
return true;
}

color lambertian::getVisibleColor() const { return m_albedo->getColor(); }

std::shared_ptr<lambertian> lambertian::generateFromImage(std::shared_ptr<image> im) {
auto texture = std::make_shared<imageTexture>(im);
auto material = std::make_shared<lambertian>(texture);
Expand Down Expand Up @@ -103,6 +107,8 @@ bool metal::scatter(const ray &rayIn, const hitData &data, color &attenuation, r
return dot(rayOut.direction(), data.normal) > 0;
}

color metal::getVisibleColor() const { return m_albedo; }

bool refractor::scatter(const ray &rayIn, const hitData &data, color &attenuation, ray &rayOut) const {
attenuation = color(1.0, 1.0, 1.0);
double refractionRatio = data.frontFace ? (1.0 / m_eta) : m_eta;
Expand Down Expand Up @@ -208,4 +214,6 @@ color diffuseLight::emit(double u, double v, const point3 &p, bool firstHit) con

std::shared_ptr<solidColor> diffuseLight::getLightMaterial() const { return m_lightColor; }

color diffuseLight::getVisibleColor() const { return m_textureToDisplay->getColor(); }

} // namespace shkyera
4 changes: 4 additions & 0 deletions src/shapes/material.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class material {
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 {
Expand All @@ -29,6 +30,7 @@ class lambertian : public material {
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;

private:
shared_ptr<texture> m_albedo;
Expand All @@ -39,6 +41,7 @@ class metal : public material {
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;

private:
color m_albedo;
Expand Down Expand Up @@ -70,6 +73,7 @@ class diffuseLight : public material {
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;
Expand Down
6 changes: 6 additions & 0 deletions src/shapes/sphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ void sphere::setMaterial(shared_ptr<material> material) { m_material = material;
std::string sphere::getName() const { return m_name; }
void sphere::setName(std::string name) { m_name = name; }

point3 sphere::getOrigin() const { return m_center; }
void sphere::setOrigin(point3 p) { m_center = p; }

double sphere::getRadius() const { return m_radius; }
void sphere::setRadius(double radius) { m_radius = radius; }

planetSettings sphere::getSettings() const {
planetSettings settings;

Expand Down
6 changes: 6 additions & 0 deletions src/shapes/sphere.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ class sphere {
std::string getName() const;
void setName(std::string name);

point3 getOrigin() const;
void setOrigin(point3 p);

double getRadius() const;
void setRadius(double radius);

shared_ptr<material> getMaterial() const;
void setMaterial(shared_ptr<material> material);

Expand Down
Loading

0 comments on commit 2f6617f

Please sign in to comment.