General Purpose library for Freestanding C++ and POSIX systems
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 

128 рядки
3.1 KiB

#include "test_scaffold.h"
#include "gp/array.hpp"
#include "gp/math.hpp"
#include "gp/rendering/renderer.hpp"
#include "gp/rendering/bmp_viewport.hpp"
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <chrono>
struct sin_test : public test_scaffold {
sin_test() {
name = __FILE__ ":1";
}
virtual int run() {
int res = 0;
for(float i = 0; i < 100; i += 0.1) {
float v = gp::sin(i);
float ref = sin(i);
res += 0.3 < gp::abs<float>(ref - v)*100.0/(gp::abs(ref+0.00000001));
}
for(float i = 0; i < 100; i += 0.1) {
float v = gp::cos(i);
float ref = cos(i);
res += 0.3 < gp::abs<float>(ref - v)*100.0/(gp::abs(ref+0.00000001));
}
return res;
}
};
append_test dummy_mldffh6f(new sin_test{});
struct render_test : public test_scaffold {
render_test() {
name = __FILE__ ":2";
}
virtual int run() {
int res = 0;
renderer a;
a._resolution = vec2{1000,500};
a.sky_box = [](vec3) -> color_t {return {0,0,0,0};};
auto v = a.materials.push(
[&](vec3 p) -> color_t {
//return color_t{0,0,1,1};
const float EPSILON = 0.001;
/*vec3(
a.sdf(vec3(p.x + EPSILON, p.y, p.z)).distance - a.sdf(vec3(p.x - EPSILON, p.y, p.z)).distance,
a.sdf(vec3(p.x, p.y + EPSILON, p.z)).distance - a.sdf(vec3(p.x, p.y - EPSILON, p.z)).distance,
a.sdf(vec3(p.x, p.y, p.z + EPSILON)).distance - a.sdf(vec3(p.x, p.y, p.z - EPSILON)).distance
)*/
auto normals = p.normalize();
auto light = vec3(1,1,1).normalize();
auto tmp = light*p;
auto color = tmp.x+tmp.y+tmp.z;
return vec4(vec3(color, color, color), 1.0);
//return {v.normalize(), 1.0};
}
);
auto sphere = a.scene_elements.push(
[=](vec3 pos) -> render_point {
render_point ret;
ret.distance = gp::fixed_sqrt<float, 6>(pos.x*pos.x + pos.y*pos.y + pos.z*pos.z) - 1.0;
ret.material = v;
return ret;
}
);
a._camera.position = vec3{0, 0, -2};
a._camera.normal = vec3{0, 0, 1};
using pic_color = gp::vec4_g<uint8_t>;
gp::bmp_viewport<true, pic_color> vp{
{1000,500},
[&](gp::vec2_g<int32_t> p) -> pic_color {
auto orig = a.render({(float)p.x,(float)p.y});
pic_color ret{};
ret.x = (uint8_t)(orig.x*255);
ret.y = (uint8_t)(orig.y*255);
ret.z = (uint8_t)(orig.z*255);
ret.w = (uint8_t)(orig.w*255);
return ret;
}
};
gp::array<char, 400000000>* buff = new gp::array<char, 400000000>();
auto begin = std::chrono::steady_clock::now();
auto r_end = vp.write(buff->as_buffer());
auto end = std::chrono::steady_clock::now();
std::cout << "render time: " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << std::endl;
auto myfile = std::fstream("render.bmp", std::ios::out | std::ios::binary);
myfile.write(buff->begin().data, r_end - buff->begin());
myfile.close();
delete buff;
//gp_config::assertion(a.render(vec2{64,32}).x == color_t{1.0,0,0,1.0}.x, "red sphere not perceived");
//gp_config::assertion(a.render(vec2{0,0}).x == color_t{0.0,0,1.0,1.0}.x, "blue sky not perceived");
return res;
}
};
append_test dummy_ml8576f(new render_test{});