Compare commits

..

14 Commits

7 changed files with 138 additions and 39 deletions

View File

@@ -4,8 +4,13 @@ project(64kDemo VERSION 0.1
DESCRIPTION "CPU SDF Renderer"
LANGUAGES CXX)
set (CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
SET(CMAKE_CXX_FLAGS "-Os -s")
add_executable(one main.cpp)
target_link_libraries(one m glfw GLEW GL)
find_package(glm REQUIRED)
target_link_libraries(one m glfw GLEW GL glm::glm)

40
PerformanceAnalyser.hpp Normal file
View File

@@ -0,0 +1,40 @@
#include <GL/glew.h>
#include <cmath>
#include <iostream>
class PerformanceAnalyser {
private:
double lastFrameTime = 0;
int frameCount = 0;
double timeAcc = 0;
double lastFps = 0;
public:
PerformanceAnalyser(int framesToAcc = 10, bool autoPrint = true)
: framesToAcc(framesToAcc), autoPrint(autoPrint) {};
int framesToAcc;
bool autoPrint;
int getFPS() { return lastFps; }
void update(float time) {
timeAcc += (time - lastFrameTime);
lastFrameTime = time;
frameCount++;
if (frameCount == framesToAcc) {
lastFps = double(framesToAcc) / timeAcc;
if (autoPrint) {
std::cout << "Fps: " << std::round(lastFps * 10.0) * 0.1 << "\n"
<< std::flush;
}
frameCount = 0;
timeAcc = 0;
}
}
};

14
build.sh Executable file
View File

@@ -0,0 +1,14 @@
#!/bin/bash
cd build
cmake --build .
CMD="#!/bin/bash
dd bs=1 skip=%s<\$0|unlzma>/tmp/C;chmod +x /tmp/C;trap '' HUP;/tmp/C&"
COUNT=$(printf "$CMD" | wc -c)
EXTRA=$(printf "$COUNT" | wc -c)
COUNT=$((COUNT + EXTRA + 1))
echo "$CMD" | sed 's/%s/'"$COUNT"'/' > two
chmod +x two
sstrip one
lzma -9 one
cat one.lzma >> two
rm one.lzma

View File

View File

@@ -1,14 +1,16 @@
#include "FullScreenQuad.hpp"
#include "PerformanceAnalyser.hpp"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <cstdio>
#include <iostream>
const char *vertexShaderSource =
#include "fsq.vs"
#include "fsq.glsl"
;
const char *fragmentShaderSource =
#include "sdf.fs"
#include "sdf.glsl"
;
/*void framebuffer_size_callback(GLFWwindow* window, int width, int height)
@@ -104,7 +106,15 @@ int main(int argc, char **argv) {
FullScreenQuad fsq{};
PerformanceAnalyser perf{};
double time = glfwGetTime();
while (!glfwWindowShouldClose(window)) {
time = glfwGetTime();
perf.update(time);
processInput(window);
/* Render here */
@@ -112,8 +122,12 @@ int main(int argc, char **argv) {
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
int aspectLocation = glGetUniformLocation(shaderProgram, "aspect");
glUniform1f(aspectLocation, aspect);
int timeLocation = glGetUniformLocation(shaderProgram, "t");
glUniform1f(timeLocation, 2. * time);
glBindVertexArray(fsq.getVAO());
glDrawArrays(GL_TRIANGLES, 0, 6);

View File

@@ -8,6 +8,9 @@
- [ ] Shader Management
- [ ] Configure Preprocessor
- [ ] Shader Storage
- [x] Binary compression
- [x] C++ Math Lib
- [ ] SDF
- [ ] Basic Shape Rendering
@@ -25,7 +28,7 @@
- [ ] Rounding
- [ ] Boolean
- [ ] Infinite
- [ ] Displacement
- [x] Displacement
- [ ] Twist
- [ ] Bend

View File

@@ -5,30 +5,54 @@ out vec4 FragColor;
in vec2 texCoords;
uniform float aspect;
uniform float t;
vec3 center = vec3(0.0,0.0,20.0);
vec3 center = vec3(0.0,-4.0,20.0);
float radius = 5.0;
float planeHeight = -7.0;
struct SmallHit {
float min;
float len;
bool firstHP;
};
struct Ray {
vec3 dir;
vec3 pos;
float min;
float len;
SmallHit small;
vec3 color;
};
Ray applyMin(Ray ray) {
vec3 off = ray.dir * ray.min;
Ray createRay(vec3 dir, vec3 pos){
return Ray(dir, pos, .0, .0, SmallHit(80.,0.,false), vec3(1.0));
}
Ray applyMin(Ray ray, float min) {
vec3 off = ray.dir * min;
ray.pos = ray.pos + off;
ray.len = ray.len + ray.min;
ray.len = ray.len + min;
if( (min < ray.min) && !ray.small.firstHP ){
ray.small.firstHP = true;
}
ray.min = min;
if((ray.min < ray.small.min) && ray.small.firstHP){
ray.small.min = ray.min;
ray.small.len = ray.len;
}
return ray;
}
float planeMin(vec3 pos){
return pos.y - planeHeight;
return pos.y - planeHeight+ 0.2*sin(5*pos.x+t)*sin(5*pos.y+t)*sin(5*pos.z+t);
}
float sphereMin(vec3 pos){
@@ -38,18 +62,15 @@ float sphereMin(vec3 pos){
Ray minFn(Ray ray){
float sp = sphereMin(ray.pos);
float pl = planeMin(ray.pos);
float min;
if (sp < pl){
ray.color = vec3(1.0, 0.5, 0.2);
ray.min = sp;
min = sp;
} else {
ray.color = vec3(0.2, 0.3, 0.3);
ray.min = pl;
min = pl;
}
return applyMin(ray);
return applyMin(ray,min);
}
float minFn(vec3 pos){
@@ -67,11 +88,13 @@ Ray march(Ray ray){
//ray.min = minFn(ray.pos);
ray = minFn(ray);
int c = 0;
while(( c < 200) && (ray.min < 100.0f) && (ray.min > 0.1f) ) {
while(( c < 1000) && (ray.min < 100.0f) && (ray.min > 0.01f) ) {
ray = minFn(ray);
c++;
}
if(ray.min > 0.01){
ray.color = vec3(1.0);
}
return ray;
}
@@ -93,8 +116,7 @@ void main()
vec3 dir = vec3((texCoords - 0.5)*2,1.0);
dir.x *= aspect;
vec3 pos = dir;
Ray ray = Ray(normalize(dir),pos,.0,.0,vec3(.0));
Ray ray = createRay(normalize(dir),pos);
ray = march(ray);
vec3 normal = calcNormal(ray);
@@ -104,31 +126,32 @@ void main()
float shLeve = 1.0;
vec2 offsets[8] = vec2[](
vec2( -0.94201624, -0.39906216 ),
vec2( 0.94558609, -0.76890725 ),
vec2( -0.094184101, -0.92938870 ),
vec2( 0.34495938, 0.29387760 ),
vec2( 0.94201624, 0.39906216 ),
vec2( -0.94558609, 0.76890725 ),
vec2( 0.094184101, 0.92938870 ),
vec2( -0.34495938, -0.29387760 )
);
for(int i = 0; i < 8; i++){
Ray shRay = Ray(lighDir * (-1.0) + vec3(offsets[i].x,.0,offsets[i].y)*0.05, ray.pos + 0.5*normal, .0,.0, vec3(.0));
Ray shRay = createRay(lighDir * (-1.0), ray.pos + (-0.5)*lighDir);
shRay = march(shRay);
if( (dot(lighDir,normal) < 0.0) && (shRay.len <20)){
shLeve -= 0.125*max(1.0-shRay.len*0.07 , 0.0);
}
}
light *= shLeve * .8;
if( dot(lighDir,normal) < 0. ){
if( shRay.len <80){
shLeve = .0;//shRay.len*0.02 , 0.0;
}
else {
shLeve = shRay.small.min / shRay.small.len;
shLeve *= 9.0;
//shLeve = pow(shLeve*10.0,0.5);
}
}
shLeve = min(max(shLeve, 0.0),1.0);
light *= shLeve;
Ray reflectR = createRay(reflect(ray.dir,normal),ray.pos + 0.1* normal);
reflectR = march(reflectR);
if(ray.min <= 0.2){
FragColor = vec4(ray.color * (light+0.3), 1.0);
//FragColor = vec4(0,1,0,1.0);
FragColor = vec4(mix(ray.color,reflectR.color,0.3) * (light+0.3), 1.0);
} else {
FragColor = vec4(.0,0.0,0.0,1.0);
FragColor = vec4(ray.color,1.0);
}
}