98 lines
2.3 KiB
C#
98 lines
2.3 KiB
C#
using System;
|
|
|
|
using OpenTK.Graphics.OpenGL;
|
|
|
|
using org.budnhead.graphics;
|
|
using org.budnhead.tools;
|
|
|
|
namespace org.budnhead.core
|
|
{
|
|
public static class GlobalDefaults
|
|
{
|
|
internal static bool initialize(){
|
|
ResourceLibrary.initializeResourceType(typeof(Shader), "shader/", "");
|
|
ResourceLibrary.initializeResourceType(typeof(ShaderProgram), "shader/", ".program");
|
|
ResourceLibrary.initializeResourceType(typeof(LoadedModel), "models/", ".obj");
|
|
|
|
ResourceLibrary.cacheInstance("default.vshader",new Shader(OpenTK.Graphics.OpenGL.ShaderType.VertexShader, defVertexShaderSource));
|
|
ResourceLibrary.cacheInstance("default.fshader",new Shader(OpenTK.Graphics.OpenGL.ShaderType.FragmentShader, defFragmentShaderSource));
|
|
|
|
ResourceLibrary.cacheInstance("default",new ShaderProgram("default.vshader","default.fshader"));
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
private static string defVertexShaderSource = @"#version 330
|
|
|
|
layout(location = 0) in vec3 iv_position;
|
|
layout(location = 1) in vec4 iv_color;
|
|
layout(location = 2) in vec3 iv_normal;
|
|
layout(location = 3) in vec4 iv_col2;
|
|
layout(location = 4) in vec2 iv_uv;
|
|
|
|
uniform mat4 mObject;
|
|
uniform mat4 mCamera;
|
|
uniform mat4 mObjectCamera;
|
|
uniform mat4 mObjectCameraIT;
|
|
uniform mat4 mProjection;
|
|
uniform mat4 mScene;
|
|
|
|
out vec4 color;
|
|
out vec3 norm;
|
|
out vec2 uv;
|
|
out vec3 coord;
|
|
|
|
out float shading;
|
|
|
|
void main()
|
|
{
|
|
gl_Position = mScene * vec4( iv_position, 1 );
|
|
|
|
float fading = 1.0;
|
|
|
|
norm = normalize( ( mObjectCameraIT * vec4( iv_normal , 1 ) ).xyz );
|
|
vec3 camray = (mObjectCamera * vec4( iv_position , 1 )).xyz;
|
|
vec3 ncamray = normalize( camray );
|
|
|
|
float cosTheta = abs( clamp(dot( norm , ncamray ), -1, 1) );
|
|
float distance = length(camray);
|
|
float idist = 25000 / distance;
|
|
float sight = clamp( idist * idist * idist, 0.1, 1.0);
|
|
|
|
shading = clamp(0.3 + (cosTheta * 0.7), 0.1, 0.9) * sight;
|
|
color = clamp( vec4( iv_color.xyz, fading ) * iv_col2, 0, 1);
|
|
coord = camray;
|
|
}
|
|
|
|
";
|
|
|
|
private static string defFragmentShaderSource = @"#version 330
|
|
|
|
in vec4 color;
|
|
in vec3 norm;
|
|
in vec2 uv;
|
|
in vec3 coord;
|
|
|
|
in float shading;
|
|
|
|
out vec4 _color;
|
|
|
|
void main()
|
|
{
|
|
float cosTheta = dot( -coord, norm );
|
|
|
|
if (cosTheta >= 0){
|
|
discard;
|
|
};
|
|
|
|
_color = vec4( color.xyz * shading, color.w );
|
|
//_color = vec4( (vec3( 1,1,1 ) + norm)/2 , 1 );
|
|
}
|
|
|
|
";
|
|
|
|
}
|
|
}
|