2015-01-04 71 views
0

sharpgl不能調用擴展功能glShaderSource

System.Exception: Cannot invoke extension function glShaderSource ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: Cannot marshal: Encountered unmappable character. 
    at System.StubHelpers.MngdNativeArrayMarshaler.ConvertContentsToNative(IntPtr pMarshalState, Object& pManagedHome, IntPtr pNativeHome) 
    --- End of inner exception stack trace --- 
    at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) 
    at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) 
    at System.Delegate.DynamicInvokeImpl(Object[] args) 
    at System.Delegate.DynamicInvoke(Object[] args) 
    at SharpGL.OpenGL.InvokeExtensionFunction[T](Object[] args) in c:\Repositories\GitHub\sharpgl\source\SharpGL\Core\SharpGL\OpenGLExtensions.cs:line 66 
    --- End of inner exception stack trace --- 
    at SharpGL.OpenGL.InvokeExtensionFunction[T](Object[] args) in c:\Repositories\GitHub\sharpgl\source\SharpGL\Core\SharpGL\OpenGLExtensions.cs:line 70 
    at SharpGL.OpenGL.ShaderSource(UInt32 shader, String source) in c:\Repositories\GitHub\sharpgl\source\SharpGL\Core\SharpGL\OpenGLExtensions.cs:line 1296 
    at SharpGL.Shaders.Shader.Create(OpenGL gl, UInt32 shaderType, String source) in c:\Repositories\GitHub\sharpgl\source\SharpGL\Core\SharpGL\Shaders\Shader.cs:line 20 
    at SharpGL.Shaders.ShaderProgram.Create(OpenGL gl, String vertexShaderSource, String fragmentShaderSource, Dictionary`2 attributeLocations) in c:\Repositories\GitHub\sharpgl\source\SharpGL\Core\SharpGL\Shaders\ShaderProgram.cs:line 26 

我目前使用的代碼是

try 
{ 
    // Create the shader program. 
    var vertexShaderSource = @"#version 150 core 
in vec3 in_Position; 
in vec3 in_Color; 
out vec3 pass_Color; 
uniform mat4 projectionMatrix; 
uniform mat4 viewMatrix; 
uniform mat4 modelMatrix; 
void main(){gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(in_Position, 1.0);pass_Color = in_Color;}"; 

    var fragmentShaderSource = @"#version 150 core 
in vec3 pass_Color; 
out vec4 out_Color; 
void main(void){ out_Color = vec4(pass_Color, 1.0); }"; 

    shaderProgram = new ShaderProgram(); 
    shaderProgram.Create(gl, vertexShaderSource, fragmentShaderSource, null); // <- exception 
    shaderProgram.BindAttributeLocation(gl, attributeIndexPosition, "in_Position"); 
    shaderProgram.BindAttributeLocation(gl, attributeIndexColour, "in_Color"); 
    shaderProgram.AssertValid(gl); 
} 
catch (Exception e) 
{ 
    System.Diagnostics.Debug.Write(e.ToString()); 
} 

唯一的例外是在shaderProgram.Create一行。當移除或縮小着色器源字符串時,錯誤不會發生,但是隨後着色器將無法編譯,並且出現Failed to compile shader with ID 2.錯誤。我還發現,在源代碼的地方,除了自帶from但對如何解決這一問題或有什麼實際的原因是不知道。

+0

不知道這是否可以解釋你的症狀,但着色器肯定不會成功編譯。你聲明'in_Position',然後使用'in_Potision'。 – 2015-01-04 20:54:52

+0

不錯,但它沒有解決問題,因爲它沒有得到着色器的編譯 – ColmanJ 2015-01-06 16:12:08

回答

0

我已經解決了這一問題。在着色器角色旁邊顯示它們是in_Color後的一個奇怪字符。使用notepad ++和ANSI編碼查看源代碼顯示了一些不應該是它們的字符。

供將來參考查看在不同的編碼源,並檢查是否有誰不應該有任何字符。