Skip to content

Commit

Permalink
frame buffer creation, shader refractor
Browse files Browse the repository at this point in the history
the frame buffer creation is in progresse, but we still need to think a proper way to draw the scene into the frame buffer. #8

also, the main shader as be cleaned a bit, now every object are independent, no more gobal setting.
  • Loading branch information
Cewein committed Mar 11, 2020
1 parent 7292c9c commit d45b943
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 11 deletions.
1 change: 0 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ int main()


cam->sendInfo();

obj.show();

nerv::window::get().update();
Expand Down
27 changes: 17 additions & 10 deletions shader/raytraced.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ layout (location = 28) uniform float fov;

uniform vec2 iResolution;
uniform float iTime;
uniform sampler3D hdrCubeMap;

in vec2 iTexCoord;

Expand All @@ -30,6 +31,8 @@ struct hitRecord {
vec3 normal;
int mat;
vec3 color;
float fuzz;
float refaction;
};

struct sphere
Expand All @@ -38,6 +41,8 @@ struct sphere
float radius;
int mat;
vec3 color;
float fuzz;
float refaction;
};

struct hitableList {
Expand Down Expand Up @@ -66,6 +71,8 @@ bool hitSphere(in ray r, float tmin, float tmax, inout hitRecord rec, sphere s)
rec.normal = (rec.p - s.center) / s.radius;
rec.mat = s.mat;
rec.color = s.color;
rec.fuzz = s.fuzz;
rec.refaction = s.refaction;
return true;
}
temp = (-b + sqrt(b*b-a*c))/a;
Expand All @@ -76,6 +83,8 @@ bool hitSphere(in ray r, float tmin, float tmax, inout hitRecord rec, sphere s)
rec.normal = (rec.p - s.center) / s.radius;
rec.mat = s.mat;
rec.color = s.color;
rec.fuzz = s.fuzz;
rec.refaction = s.refaction;
return true;
}
}
Expand Down Expand Up @@ -170,14 +179,13 @@ vec3 color(ray r, hitableList list, vec2 st)
}
if (rec.mat == 1)
{
float globalFuzz = 0.;// change to 0 for full reflection
vec3 reflected = reflect(unitDirection, rec.normal);
r = ray(rec.p, reflected + globalFuzz * randInUnitSphere(st));
r = ray(rec.p, reflected + rec.fuzz * randInUnitSphere(st + loopCount));
att *= rec.color; // if att *= rec.color * 50; then the shpere become a light source o_O
}
if (rec.mat == 2)
{
float refractiveIndex = 1.9;
float refractiveIndex = rec.refaction;
vec3 outwardNormal;
vec3 reflected = reflect(unitDirection, rec.normal);
float niOverNt;
Expand Down Expand Up @@ -218,7 +226,6 @@ vec3 color(ray r, hitableList list, vec2 st)
r = ray(rec.p, refract(unitDirection, outwardNormal, niOverNt));
}

//vec3 refracted = refract(unitDirection, rec.normal, 1.0 / refractiveIndex);
}
bounce++;
}
Expand All @@ -236,17 +243,17 @@ void main()
hitableList list;
list.size = 5;
list.sList = sphere[5](
sphere(vec3(0., 0., -1.5), 0.5, 0, vec3(0.8, 0.3, 0.3)),
sphere(vec3(-1., .0, -1.5), 0.5, 1, vec3(0.8, 0.8, 0.8)),
sphere(vec3(1.0, .0, -1.5), 0.5, 2, vec3(0.8, 0.6, 0.2)),
sphere(vec3(0., -100.5, -1.), 100., 0, vec3(0.8, 0.8, 0.0)),
sphere(vec3(0., 0.5, -10.), 5., 1, vec3(0.8, 0.6, 0.2))
sphere(vec3(0., 0., -1.5), 0.5, 0, vec3(0.8, 0.3, 0.3),0.0,0.0),
sphere(vec3(-1., .0, -1.5), 0.5, 1, vec3(0.8, 0.8, 0.8),0.5,0.),
sphere(vec3(1.0, .0, -1.5), 0.5, 2, vec3(0.8, 0.6, 0.2),0.0,2.5),
sphere(vec3(0., -100.5, -1.), 100., 0, vec3(0.8, 0.8, 0.0),0.,0.),
sphere(vec3(0., 0.5, -10.), 5., 1, vec3(0.8, 0.6, 0.2),0.,0.)
);;

ray r = getRay(st);
vec3 col = vec3(0.);

float sizeBlending = 50.;
float sizeBlending = 20.;

for( float x = 0.; x < sizeBlending; x++)
{
Expand Down
1 change: 1 addition & 0 deletions src/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "env/init/init.h"
#include "engine/material/shader.h"
#include "engine/material/texture.h"
#include "engine/material/framebuffer.h"
#include "engine/object/object.h"
#include "engine/scene.h"
#include "engine/object/camera.h"
31 changes: 31 additions & 0 deletions src/engine/material/framebuffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "framebuffer.h"


nerv::framebuffer::framebuffer()
{
logger.warning("FRAMEBUFFER", "Creating frame buffer");

glGenBuffers(1, &(this->id));
glBindFramebuffer(GL_FRAMEBUFFER, this->id); //this make a offscreen render of the buffer set to 0 for onscreen rendering
this->frameTexture = new nerv::texture(nerv::window::get().width, nerv::window::get().height);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->frameTexture->getId(), 0);
logger.info("FRAMEBUFFER", "Created texture for framebuffer");

glGenRenderbuffers(1, &(this->rbo));
glBindRenderbuffer(GL_RENDERBUFFER, this->rbo);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, this->frameTexture->getWidth(), this->frameTexture->getHeight());
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, this->rbo);
logger.info("FRAMEBUFFER", "alloced space for framebuffer");

if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
logger.error("FRAMEBUFFER", "Framebuffer creation did not succed");

glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

nerv::framebuffer::~framebuffer()
{
glDeleteBuffers(1, &(this->id));
delete(this->frameTexture);
}
22 changes: 22 additions & 0 deletions src/engine/material/framebuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once
#include "../../../dependencies.h"
#include "texture.h"
#include "../../in_out/windows/windows.h"

namespace nerv
{
class framebuffer
{
public:
size_t id;

//renderbuffer objet id
size_t rbo;

//texture where the frame buffer is drawn
nerv::texture * frameTexture;

framebuffer();
~framebuffer();
};
}
12 changes: 12 additions & 0 deletions src/engine/material/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,15 @@ nerv::texture::texture(std::string path)


}

nerv::texture::texture(int width, int height)
{
glGenTextures(1, &(this->id));
glBindTexture(GL_TEXTURE_2D, this->id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
this->height = height;
this->width = width;

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
4 changes: 4 additions & 0 deletions src/engine/material/texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ namespace nerv
//create a texture for a file, for more info look into std_image.h
//located in the dependencies folder.
texture(std::string path);

//Create a empty texture the size of widht and height
//usefull for framebuffer
texture(int width, int height);

//getter
inline int getWidth() { return this->width; }
Expand Down

0 comments on commit d45b943

Please sign in to comment.