0
我目前正在使用着色器構建測試場景。 這裏是我的Vertext shader代碼:C#OpenGL - 試圖使用着色器使用相機移動光線
public const string VertexShaderText = @"
#version 130
in vec3 vertexPosition;
in vec3 vertexNormal;
in vec3 vertexTangent;
in vec2 vertexUV;
uniform vec3 light_direction;
out vec3 normal;
out vec2 uv;
out vec3 light;
uniform mat4 projection_matrix;
uniform mat4 view_matrix;
uniform mat4 model_matrix;
uniform bool enable_mapping;
void main(void)
{
normal = normalize((model_matrix * vec4(floor(vertexNormal), 0)).xyz);
uv = vertexUV;
mat3 tbnMatrix = mat3(vertexTangent, cross(vertexTangent, normal), normal);
light = (enable_mapping ? light_direction * tbnMatrix : light_direction);
gl_Position = projection_matrix * view_matrix * model_matrix * vec4(vertexPosition, 1);
}
";
這裏是我的片段着色器:
public const string FragmentShaderText = @"
#version 130
uniform sampler2D colorTexture;
uniform sampler2D normalTexture;
uniform bool enableToggleLighting;
uniform mat4 model_matrix;
uniform bool enable_mapping;
uniform float alpha;
uniform float ambi;
in vec3 normal;
in vec2 uv;
in vec3 light;
out vec4 fragment;
void main(void)
{
vec3 fragmentNormal = texture2D(normalTexture, uv).xyz * 2 - 1;
vec3 selectedNormal = (enable_mapping ? fragmentNormal : normal);
float diffuse = max(dot(selectedNormal, light), 0);
float ambient = ambi;
float lighting = (enableToggleLighting ? max(diffuse, ambient) : 1);
fragment = vec4(lighting * texture2D(colorTexture, uv).xyz, alpha);
}
";
我的項目被初始化這樣的:
CanvasControlSettings.ShaderProgram.Use();
CanvasControlSettings.ShaderProgram["projection_matrix"].SetValue(mainSceneProj);
CanvasControlSettings.ShaderProgram["light_direction"].SetValue(new Vector3(0f, 0f, 1f));
CanvasControlSettings.ShaderProgram["enableToggleLighting"].SetValue(CanvasControlSettings.ToggleLighting);
CanvasControlSettings.ShaderProgram["normalTexture"].SetValue(1);
CanvasControlSettings.ShaderProgram["enable_mapping"].SetValue(CanvasControlSettings.ToggleNormalMapping);
我的旋轉移動相機周圍的物體。我想要將照明位置與相機一起移動,以便陰影始終可見。
如何將Camera Pos發送給着色器並執行此操作?
旋轉的:
編輯:
我更新攝像機位置旋轉後是這樣的:
GL.Light(LightName.Light0, LightParameter.Position, new float[] { CanvasControlSettings.Camera.Position.X, CanvasControlSettings.Camera.Position.Y, CanvasControlSettings.Camera.Position.Z, 0.0f });
,改變了光行此
light = gl_LightSource[0].position.xyz;
這種近乎完美的作品接受材料的光色是現在出現了燦爛的!我會顯示圖片,但似乎我需要更多的代表。
編輯:
好了,下面提供的鏈接後,我發現我的方式。我將頂點着色器光代碼更改爲:
vec4 lightPos4 = vec4(gl_LightSource[0].position.xyz, 1.0);
vec4 pos4 = model_matrix * vec4(vertexPosition, 1.0);
light = normalize((lightPos4 - pos4).xyz);
現在完美工作。謝謝
您可以通過腳本簡單地將光源移動到與相機相匹配的世界位置和旋轉角度。 – SimpleVar
你能舉個例子嗎? –
[this](https://www.opengl.org/discussion_boards/showthread.php/138021-Light-moving-with-camera)和[that](http://gamedev.stackexchange.com/questions/72352/爲什麼光是跟隨我的相機)可能會指出你在正確的方向*,嘿。 – SimpleVar