Skip to content

Commit

Permalink
Added glsl test
Browse files Browse the repository at this point in the history
  • Loading branch information
desdic committed Feb 25, 2024
1 parent 30894d8 commit ea7f9ac
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 0 deletions.
108 changes: 108 additions & 0 deletions tests/glsl/default.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#version 330 core

// Outputs colors in RGBA
out vec4 FragColor;

// Imports the current position from the Vertex Shader
in vec3 crntPos;
// Imports the normal from the Vertex Shader
in vec3 Normal;
// Imports the color from the Vertex Shader
in vec3 color;
// Imports the texture coordinates from the Vertex Shader
in vec2 texCoord;

// Gets the Texture Units from the main function
uniform sampler2D diffuse0;
uniform sampler2D specular0;
// Gets the color of the light from the main function
uniform vec4 lightColor;
// Gets the position of the light from the main function
uniform vec3 lightPos;
// Gets the position of the camera from the main function
uniform vec3 camPos;


vec4 pointLight()
{
// used in two variables so I calculate it here to not have to do it twice
vec3 lightVec = lightPos - crntPos;

// intensity of light with respect to distance
float dist = length(lightVec);
float a = 3.0;
float b = 0.7;
float inten = 1.0f / (a * dist * dist + b * dist + 1.0f);

// ambient lighting
float ambient = 0.20f;

// diffuse lighting
vec3 normal = normalize(Normal);
vec3 lightDirection = normalize(lightVec);
float diffuse = max(dot(normal, lightDirection), 0.0f);

// specular lighting
float specularLight = 0.50f;
vec3 viewDirection = normalize(camPos - crntPos);
vec3 reflectionDirection = reflect(-lightDirection, normal);
float specAmount = pow(max(dot(viewDirection, reflectionDirection), 0.0f), 16);
float specular = specAmount * specularLight;

return (texture(diffuse0, texCoord) * (diffuse * inten + ambient) + texture(specular0, texCoord).r * specular * inten) * lightColor;
}

vec4 direcLight()
{
// ambient lighting
float ambient = 0.20f;

// diffuse lighting
vec3 normal = normalize(Normal);
vec3 lightDirection = normalize(vec3(1.0f, 1.0f, 0.0f));
float diffuse = max(dot(normal, lightDirection), 0.0f);

// specular lighting
float specularLight = 0.50f;
vec3 viewDirection = normalize(camPos - crntPos);
vec3 reflectionDirection = reflect(-lightDirection, normal);
float specAmount = pow(max(dot(viewDirection, reflectionDirection), 0.0f), 16);
float specular = specAmount * specularLight;

return (texture(diffuse0, texCoord) * (diffuse + ambient) + texture(specular0, texCoord).r * specular) * lightColor;
}

vec4 spotLight()
{
// controls how big the area that is lit up is
float outerCone = 0.90f;
float innerCone = 0.95f;

// ambient lighting
float ambient = 0.20f;

// diffuse lighting
vec3 normal = normalize(Normal);
vec3 lightDirection = normalize(lightPos - crntPos);
float diffuse = max(dot(normal, lightDirection), 0.0f);

// specular lighting
float specularLight = 0.50f;
vec3 viewDirection = normalize(camPos - crntPos);
vec3 reflectionDirection = reflect(-lightDirection, normal);
float specAmount = pow(max(dot(viewDirection, reflectionDirection), 0.0f), 16);
float specular = specAmount * specularLight;

// calculates the intensity of the crntPos based on its angle to the center of the light cone
float angle = dot(vec3(0.0f, -1.0f, 0.0f), -lightDirection);
float inten = clamp((angle - outerCone) / (innerCone - outerCone), 0.0f, 1.0f);

return (texture(diffuse0, texCoord) * (diffuse * inten + ambient) + texture(specular0, texCoord).r * specular * inten) * lightColor;
}


void main()
{
/* outputs final color */
FragColor = spotLight();
}
52 changes: 52 additions & 0 deletions tests/glsl_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
describe("glsl", function()
local core = require("agrolens.core")
local buffers = nil
local eq = assert.equals

vim.filetype.add({extension = {frag = "glsl", vert = "glsl"}})

it("load", function()
vim.cmd.edit("tests/glsl/default.frag")
buffers = vim.api.nvim_list_bufs()
eq(#buffers, 1)

local content = vim.api.nvim_buf_get_lines(buffers[1], 0, -1, false)

-- make sure buffer has content
eq(string.match(content[1], "version"), "version")

core.get_captures({ queries = { "functions" }, bufids = buffers })
end)

it("functions", function()
local entries = core.get_captures({ queries = { "functions" }, bufids = buffers })

-- functions
eq(#entries, 4)
eq(entries[1].filename, "tests/glsl/default.frag")
eq(entries[1].lnum, 26)
eq(entries[1].col, 0)
eq(entries[1].line, "vec4 pointLight()")
eq(entries[2].line, "vec4 direcLight()")
eq(entries[3].line, "vec4 spotLight()")
eq(entries[4].line, "void main()")
end)
it("callings", function()
local entries = core.get_captures({ queries = { "callings" }, bufids = buffers })

eq(#entries, 1)
eq(entries[1].filename, "tests/glsl/default.frag")
eq(entries[1].lnum, 107)
eq(entries[1].col, 2)
eq(entries[1].line, " FragColor = spotLight();")
end)
it("comments", function()
local entries = core.get_captures({ queries = { "comments" }, bufids = buffers })
eq(#entries, 23)
eq(entries[1].filename, "tests/glsl/default.frag")
eq(entries[1].lnum, 3)
eq(entries[1].col, 0)
eq(entries[1].line, "// Outputs colors in RGBA")
eq(entries[23].line, " /* outputs final color */")
end)
end)

0 comments on commit ea7f9ac

Please sign in to comment.