budnhead/org.budnhead/core/GlobalDefaults.cs

104 lines
2.3 KiB
C#
Raw Normal View History

2017-04-25 22:01:18 +02:00
using System;
using OpenTK.Graphics.OpenGL;
using org.budnhead.graphics;
using org.budnhead.tools;
namespace org.budnhead.core
2017-04-25 22:01:18 +02:00
{
public static class GlobalDefaults
2017-04-25 22:01:18 +02:00
{
internal static bool initialize(){
ResourceLibrary.initializeResourceType(typeof(Shader), "shader/", "");
ResourceLibrary.initializeResourceType(typeof(ShaderProgram), "shader/", ".program");
ResourceLibrary.initializeResourceType(typeof(LoadedModel), "models/", ".obj");
2017-04-25 22:01:18 +02:00
ResourceLibrary.cacheInstance("default.vshader",new Shader(OpenTK.Graphics.OpenGL.ShaderType.VertexShader, defVertexShaderSource));
ResourceLibrary.cacheInstance("default.fshader",new Shader(OpenTK.Graphics.OpenGL.ShaderType.FragmentShader, defFragmentShaderSource));
2017-04-25 22:01:18 +02:00
ResourceLibrary.cacheInstance("default",new ShaderProgram("default.vshader","default.fshader"));
2017-04-25 22:01:18 +02:00
return true;
2017-04-25 22:01:18 +02:00
}
private static string defVertexShaderSource = @"#version 330
2017-04-25 22:01:18 +02:00
2017-05-08 14:30:41 +02:00
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;
2017-04-25 22:01:18 +02:00
uniform mat4 mObject;
2017-04-25 22:01:18 +02:00
uniform mat4 mCamera;
uniform mat4 mObjectCamera;
uniform mat4 mObjectCameraIT;
2017-04-25 22:01:18 +02:00
uniform mat4 mProjection;
uniform mat4 mScene;
2017-04-25 22:01:18 +02:00
out vec4 color;
out vec3 norm;
out vec2 uv;
2017-05-01 11:26:05 +02:00
out vec3 coord;
2017-04-25 22:01:18 +02:00
2017-05-01 01:33:33 +02:00
out float shading;
2017-04-25 22:01:18 +02:00
void main()
{
gl_Position = mScene * vec4( iv_position, 1 );
2017-04-25 22:01:18 +02:00
2017-05-01 01:33:33 +02:00
float fading = 1.0;
2017-04-25 22:01:18 +02:00
2017-05-01 01:33:33 +02:00
norm = normalize( ( mObjectCameraIT * vec4( iv_normal , 1 ) ).xyz );
vec3 camray = (mObjectCamera * vec4( iv_position , 1 )).xyz;
vec3 ncamray = normalize( camray );
2017-06-07 19:27:20 +02:00
float distance = length(camray);
float idistance = clamp( 10000 / (distance), 0, 1);
2017-05-01 01:33:33 +02:00
2017-06-07 19:27:20 +02:00
/*
2017-05-01 01:33:33 +02:00
float cosTheta = abs( clamp(dot( norm , ncamray ), -1, 1) );
2017-06-07 19:27:20 +02:00
shading = clamp(0.3 + (cosTheta * 0.7), 0, 1) * sight;
2017-05-08 14:30:41 +02:00
color = clamp( vec4( iv_color.xyz, fading ) * iv_col2, 0, 1);
2017-06-07 19:27:20 +02:00
*/
shading = 1.0;
color = iv_color;
coord = ncamray;
2017-04-25 22:01:18 +02:00
}
";
private static string defFragmentShaderSource = @"#version 330
2017-04-25 22:01:18 +02:00
in vec4 color;
in vec3 norm;
in vec2 uv;
2017-05-01 11:26:05 +02:00
in vec3 coord;
2017-04-25 22:01:18 +02:00
2017-05-01 01:33:33 +02:00
in float shading;
2017-04-25 22:01:18 +02:00
out vec4 _color;
void main()
{
2017-06-07 19:27:20 +02:00
float cosTheta = dot( coord, norm );
2017-05-01 11:26:05 +02:00
2017-06-07 19:27:20 +02:00
if (cosTheta < 0){
2017-05-01 11:26:05 +02:00
discard;
};
2017-06-07 19:27:20 +02:00
float shade = 0.2 + (0.8 * cosTheta);
_color = clamp( vec4( color.xyz * shade, color.w ), 0, 1);
//_color = vec4( (vec3( 1,1,1 ) + norm)/2 , 1 );
2017-04-25 22:01:18 +02:00
}
";
}
}