Skip to content

Commit

Permalink
Merge pull request #13 from desdic/glsl
Browse files Browse the repository at this point in the history
Add glsl
  • Loading branch information
desdic committed Mar 21, 2023
2 parents 4de0073 + c5d65a7 commit 8b824d4
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -52,7 +52,7 @@ jobs:
run: |
export PATH="${PWD}/_neovim/bin:${PATH}"
export VIM="${PWD}/_neovim/share/nvim/runtime"
nvim --headless -c "lua require'nvim-treesitter.install'.prefer_git=false" -c "TSInstallSync lua c cpp rust python go make yaml markdown perl" -c "q"
nvim --headless -c "lua require'nvim-treesitter.install'.prefer_git=false" -c "TSInstallSync lua c cpp rust python go make yaml markdown perl glsl" -c "q"
- name: Run tests
run: |
Expand Down
1 change: 1 addition & 0 deletions queries/glsl/agrolens.functions.scm
@@ -0,0 +1 @@
(function_definition declarator:(function_declarator declarator:(identifier) @agrolens.name)) @agrolens.scope
108 changes: 108 additions & 0 deletions tests/glsl/default.frag
@@ -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();
}
28 changes: 28 additions & 0 deletions tests/glsl_spec.lua
@@ -0,0 +1,28 @@
describe("glsl", function()

local lens = nil
local buffers = nil

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

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

lens = require("telescope._extensions.agrolenslib")
lens._get_captures({queries={"functions"}, bufids=buffers})
end)

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

-- functions
assert.equals(#entries, 4)
assert.equals("tests/glsl/default.frag:26:0:vec4 pointLight()", entries[1])
assert.equals("tests/glsl/default.frag:55:0:vec4 direcLight()", entries[2])
assert.equals("tests/glsl/default.frag:75:0:vec4 spotLight()", entries[3])
assert.equals("tests/glsl/default.frag:104:0:void main()", entries[4])

end)
end)

0 comments on commit 8b824d4

Please sign in to comment.