Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #version 430 core
- #define FAR_CLIP 10000.0f
- #define width 1080
- #define height 720
- struct Triangle
- {
- vec3 vA, vB, vC;
- };
- struct Light{
- vec4 dir;
- vec4 color;
- vec4 attenuation;
- };
- struct Primitive{
- vec4 A, B, C;
- };
- struct Camera{
- vec3 pos, dir, yAxis, xAxis ;
- float tanFovY, tanFovX;
- };
- struct Ray{
- vec3 origin;
- vec3 dir;
- };
- layout(binding = 0, rgba32f) uniform image2D framebuffer;
- layout(std430, binding = 1) buffer meshtriangles
- {
- Triangle tris[ ];
- };
- uniform int mNumTriangles;
- uniform sampler2D diffuse;
- uniform uint reflectionDepth;
- uniform Camera camera;
- uniform mat4 mvpMatrix;
- Light light;
- float hitTriangle(Ray r, Primitive t)
- {
- vec3 v0 = t.A.xyz, v1 = t.B.xyz, v2 = t.C.xyz;
- vec3 v0v1 = v1 - v0;
- vec3 v0v2 = v2 - v0;
- vec3 pvec = cross(r.dir, v0v2);
- float det = dot(v0v1, pvec);
- if (det < 0.000001)
- return FAR_CLIP;
- float invDet = 1.0 / det;
- vec3 tvec = r.origin - v0;
- float u = dot(tvec, pvec) * invDet;
- if (u < 0 || u > 1)
- return FAR_CLIP;
- vec3 qvec = cross(tvec, v0v1);
- float v = dot(r.dir, qvec) * invDet;
- if (v < 0 || u + v > 1)
- return FAR_CLIP;
- return dot(v0v2, qvec) * invDet;
- }
- Ray initRay(uint x, uint y, Camera cam){
- Ray r;
- vec3 dir;
- float a, b, halfWidth, halfHeight;
- halfWidth = float(width)/2.0f;
- halfHeight = float(height)/2.0f;
- a = cam.tanFovX*( (float(x)-halfWidth+0.5f) / halfWidth);
- b = cam.tanFovY*( (halfHeight - float(y)-0.5f) / halfHeight);
- dir = normalize( a*cam.xAxis.xyz + b*cam.yAxis.xyz + cam.dir.xyz);
- r.dir = dir;
- r.origin = cam.pos.xyz;
- return r;
- }
- layout (local_size_x = 25, local_size_y = 8) in;
- void main(void) {
- uint x = gl_GlobalInvocationID.x;
- uint y = gl_GlobalInvocationID.y;
- light.dir = vec4(0, -1, 0, 0);
- light.color = vec4(1, 1, 1, 1);
- light.attenuation = vec4(1.0f);
- if(x < width && y < height)
- {
- vec4 color = vec4(0.0f, 0.0f, 0.0f, 1.0f);
- vec4 tempColor = vec4(0);
- //Initialize the ray
- Ray r = initRay(x, y, camera);
- float t = FAR_CLIP, temp = FAR_CLIP;
- int currentObject = -1;
- for(uint n = 0; n < reflectionDepth; ++n)
- {
- for(int i = 0; i < mNumTriangles; i++)
- {
- Primitive t0;
- t0.A = vec4(tris[i].vA, 1);
- t0.B = vec4(tris[i].vB, 1);
- t0.C = vec4(tris[i].vC, 1);
- temp = hitTriangle(r, t0);
- if(temp < t && temp >= -0.001f)
- {
- color = vec4(0, 1, 0,1);
- t = temp;
- currentObject = i;
- }
- }
- }
- imageStore(framebuffer, ivec2(x, y), color);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement