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 Camera camera;
- Light light;
- float hitTriangle(Ray r, Primitive t)
- {
- vec3 AB = t.B.xyz - t.A.xyz;
- vec3 AC = t.C.xyz - t.A.xyz;
- float det = determinant( mat3(AB, AC, -1.0f*r.dir) );
- if(det == 0.0f){
- return FAR_CLIP;
- } else {
- vec3 oA = r.origin - t.A.xyz;
- mat3 Di = inverse(mat3(AB, AC, -1.0f*r.dir));
- vec3 solution = Di*oA;
- if(solution.x >= -0.0001 && solution.x <= 1.0001){
- if(solution.y >= -0.0001 && solution.y <= 1.0001){
- if(solution.x + solution.y <= 1.0001){
- return solution.z;
- }
- }
- }
- return FAR_CLIP;
- }
- }
- 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;
- //Hardcoded light values
- light.dir = vec4(0, -1, 0, 0);
- light.color = vec4(1, 1, 1, 1);
- light.attenuation = vec4(1.0f);
- if(x < width && y < height)
- {
- //Color used to find out the final color.
- vec4 color = vec4(0.0f, 0.0f, 0.0f, 1.0f);
- vec4 tempColor = vec4(0);
- //Initialize the pixel/camera ray.
- Ray r = initRay(x, y, camera);
- float t = FAR_CLIP, temp = FAR_CLIP;
- int currentObject = -1;
- 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);
- //Basically if it finds something, it'll return something thats not FAR_CLIP
- temp = hitTriangle(r, t0);
- //If the ray hit something, then we need to shade it. Just give it a color for now.
- if(temp != t)
- {
- 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