mirror of
https://gitea.windcorp.ru/Wind-Corporation/Progressia.git
synced 2025-07-05 13:03:33 +03:00
Initial commit
This commit is contained in:
123
main/rendering/graphics_interface.h
Normal file
123
main/rendering/graphics_interface.h
Normal file
@ -0,0 +1,123 @@
|
||||
#pragma once
|
||||
|
||||
#include "boost/core/noncopyable.hpp"
|
||||
#include <vector>
|
||||
|
||||
#define GLM_FORCE_RADIANS
|
||||
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
|
||||
#include <glm/mat4x4.hpp>
|
||||
#include <glm/vec2.hpp>
|
||||
#include <glm/vec3.hpp>
|
||||
#include <glm/vec4.hpp>
|
||||
|
||||
#include "image.h"
|
||||
|
||||
namespace progressia {
|
||||
namespace main {
|
||||
|
||||
struct Vertex {
|
||||
|
||||
using Index = uint16_t;
|
||||
|
||||
glm::vec3 position;
|
||||
glm::vec4 color;
|
||||
glm::vec3 normal;
|
||||
glm::vec2 texCoord;
|
||||
};
|
||||
|
||||
class Texture : private boost::noncopyable {
|
||||
public:
|
||||
using Backend = void *;
|
||||
|
||||
private:
|
||||
Backend backend;
|
||||
|
||||
friend class Primitive;
|
||||
|
||||
public:
|
||||
Texture(Backend);
|
||||
~Texture();
|
||||
};
|
||||
|
||||
class Primitive : private boost::noncopyable {
|
||||
public:
|
||||
using Backend = void *;
|
||||
|
||||
private:
|
||||
Backend backend;
|
||||
|
||||
friend class GraphicsInterface;
|
||||
|
||||
public:
|
||||
Primitive(Backend);
|
||||
~Primitive();
|
||||
|
||||
void draw();
|
||||
|
||||
const Texture *getTexture() const;
|
||||
};
|
||||
|
||||
class View : private boost::noncopyable {
|
||||
public:
|
||||
using Backend = void *;
|
||||
|
||||
private:
|
||||
Backend backend;
|
||||
|
||||
public:
|
||||
View(Backend);
|
||||
~View();
|
||||
|
||||
void configure(const glm::mat4 &proj, const glm::mat4 &view);
|
||||
void use();
|
||||
};
|
||||
|
||||
class Light : private boost::noncopyable {
|
||||
public:
|
||||
using Backend = void *;
|
||||
|
||||
private:
|
||||
Backend backend;
|
||||
|
||||
public:
|
||||
Light(Backend);
|
||||
~Light();
|
||||
|
||||
void configure(const glm::vec3 &color, const glm::vec3 &from,
|
||||
float contrast, float softness);
|
||||
void use();
|
||||
};
|
||||
|
||||
class GraphicsInterface : private boost::noncopyable {
|
||||
public:
|
||||
using Backend = void *;
|
||||
|
||||
private:
|
||||
Backend backend;
|
||||
|
||||
public:
|
||||
GraphicsInterface(Backend);
|
||||
~GraphicsInterface();
|
||||
|
||||
Texture *newTexture(const Image &);
|
||||
|
||||
Primitive *newPrimitive(const std::vector<Vertex> &,
|
||||
const std::vector<Vertex::Index> &,
|
||||
Texture *texture);
|
||||
|
||||
glm::vec2 getViewport() const;
|
||||
|
||||
void setModelTransform(const glm::mat4 &);
|
||||
|
||||
View *newView();
|
||||
Light *newLight();
|
||||
|
||||
void flush();
|
||||
void startNextLayer();
|
||||
|
||||
float tmp_getTime();
|
||||
uint64_t getLastStartedFrame();
|
||||
};
|
||||
|
||||
} // namespace main
|
||||
} // namespace progressia
|
64
main/rendering/image.cpp
Normal file
64
main/rendering/image.cpp
Normal file
@ -0,0 +1,64 @@
|
||||
#include "image.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#include "stb/stb_image.h"
|
||||
|
||||
namespace progressia {
|
||||
namespace main {
|
||||
|
||||
std::size_t Image::getSize() const { return data.size(); }
|
||||
|
||||
const Image::Byte *Image::getData() const { return data.data(); }
|
||||
|
||||
Image::Byte *Image::getData() { return data.data(); }
|
||||
|
||||
Image loadImage(const std::filesystem::path &path) {
|
||||
|
||||
std::ifstream file(path, std::ios::ate | std::ios::binary);
|
||||
|
||||
if (!file.is_open()) {
|
||||
std::cout << "Could not read a PNG image from file " << path
|
||||
<< std::endl;
|
||||
// REPORT_ERROR
|
||||
exit(1);
|
||||
}
|
||||
|
||||
std::size_t fileSize = static_cast<std::size_t>(file.tellg());
|
||||
std::vector<Image::Byte> png(fileSize);
|
||||
|
||||
file.seekg(0);
|
||||
file.read(reinterpret_cast<char *>(png.data()), fileSize);
|
||||
|
||||
file.close();
|
||||
|
||||
int width;
|
||||
int height;
|
||||
int channelsInFile;
|
||||
|
||||
Image::Byte *stbAllocatedData =
|
||||
stbi_load_from_memory(png.data(), png.size(), &width, &height,
|
||||
&channelsInFile, STBI_rgb_alpha);
|
||||
|
||||
if (stbAllocatedData == NULL) {
|
||||
std::cout << "Could not load a PNG image from file " << path
|
||||
<< std::endl;
|
||||
// REPORT_ERROR
|
||||
exit(1);
|
||||
}
|
||||
|
||||
std::vector<Image::Byte> data(width * height * STBI_rgb_alpha);
|
||||
memcpy(data.data(), stbAllocatedData, data.size());
|
||||
|
||||
stbi_image_free(stbAllocatedData);
|
||||
|
||||
return {static_cast<std::size_t>(width), static_cast<std::size_t>(height),
|
||||
data};
|
||||
}
|
||||
|
||||
} // namespace main
|
||||
} // namespace progressia
|
25
main/rendering/image.h
Normal file
25
main/rendering/image.h
Normal file
@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <vector>
|
||||
|
||||
namespace progressia {
|
||||
namespace main {
|
||||
|
||||
class Image {
|
||||
public:
|
||||
using Byte = unsigned char;
|
||||
|
||||
std::size_t width;
|
||||
std::size_t height;
|
||||
std::vector<Byte> data;
|
||||
|
||||
std::size_t getSize() const;
|
||||
const Byte *getData() const;
|
||||
Byte *getData();
|
||||
};
|
||||
|
||||
Image loadImage(const std::filesystem::path &);
|
||||
|
||||
} // namespace main
|
||||
} // namespace progressia
|
Reference in New Issue
Block a user