-
-
Save jose-villegas/e7757170a9e01e574f48 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 430 core | |
uniform ivec2 viewportDimensions; | |
uniform mat4 gl_ProjectionMatrix; | |
uniform ivec4 viewport; | |
uniform float imageAspectRatio; | |
uniform float angle; | |
out vec3 color; | |
struct Ray { | |
vec3 origin; | |
vec3 direction; | |
}; | |
struct Sphere { | |
vec3 origin; | |
float radius; | |
}; | |
bool intersect(Ray r, Sphere s); | |
void main() { | |
focal = 60; | |
angle = tan(focal * 0.5 * 3.1415926535897932384626433832795 / 180); // convert from degree to radian | |
float xx = (2 * (gl_FragCoord.x + 0.5) / viewportDimensions.x - 1) * angle * imageAspectRatio; | |
float yy = (1 - 2 * (gl_FragCoord.y + 0.5) / viewportDimensions.y) * angle; | |
vec3 rayOrigin = vec3(0,0,0); | |
vec3 rayDirection = vec3(xx, yy, -1) - rayOrigin; | |
normalize(rayDirection); | |
Ray r; | |
r.origin = rayOrigin; | |
r.direction = rayDirection; | |
Sphere s; | |
s.origin = vec3(0.0f,0.0f,-1.1f); | |
s.radius =0.55f; | |
if (intersect(r,s)) | |
color = vec3(1,0,1); | |
else | |
color = vec3(0,0,0); | |
} | |
bool intersect(Ray r, Sphere s) { | |
float a = dot(r.direction,r.direction); | |
float b = dot(r.direction, 2.0 * (r.origin-s.origin)); | |
float c = dot(s.origin, s.origin) + dot(r.origin,r.origin) +-2.0*dot(r.origin,s.origin) - (s.radius*s.radius); | |
float disc = b*b + (-4.0)*a*c; | |
if (disc < 0) | |
return false; | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment