2011-11-19 68 views
0

在我的項目開始時,我用簡單的String s爲代碼填充我的Shader s。看起來像這樣:片段着色器無效。鏈接無法繼續

public final static String chunkDefaultVertexInit = "" 
    +constantParameters 
    +"precision mediump float;" 

    +"uniform mat4 mPMatrix;" 
    +"uniform mat4 mVMatrix;" 
    +"uniform mat4 mMMatrix;" 
    +"uniform mat4 mMVMatrix;" 

    +"attribute vec4 Vertex;" 
    +"attribute vec3 Normal;" 
    +"attribute vec2 TexCoord;" 

    +"varying vec3 normal;" 
    +"varying vec2 uv;" 
    +"varying vec4 positionM;" 
    +"varying vec4 positionMV;"; 
    etc.... 

這對我很有效,但它不是很清楚。所以我想了解如何讓我的代碼更清晰,更清晰地爲每個人使用。我的想法是,將我的整個代碼放在一個真實的.cc - 文件中,並將其移動到res/raw文件夾中。說到做到。 我想通過InputStream讀出我的代碼並保存到一個String中。這也工作得很好,所以我給着色器String源。

所以......現在出現了一個問題,正如我所說,我還沒有得到它。我甚至讓我對自己有點生氣,因爲我想過一個簡單的方法來修復它,但我沒有看到它。

我甚至沒有顯示我放入的源代碼...但它看起來正確! OO

Log.i("Llama3D Shader",shaderCode); 

(不要擔心怪異的 「調試ID」,它的項目名稱)

下面是着色器的源代碼:

Vertexshader:

//vertexshader 
precision mediump float; 

uniform mat4 mPMatrix; 
uniform mat4 mVMatrix; 
uniform mat4 mMMatrix; 
uniform mat4 mMVMatrix; 

attribute vec4 aVertex; 
attribute vec3 aNormal; 
attribute vec2 aTexCoord; 

varying vec2 vecTexCoord; 
varying vec3 vecNormal; 
varying vec4 vecVertex[2]; 

void main() { 
    gl_Position = mPMatrix * mMVMatrix * aVertex; 
    vecVertex[0] = mMMatrix * aVertex; 
    vecVertex[1] = mMVMatrix * aVertex; 
    vecTexCoord = aTexCoord; 
    vecNormal = normalize(vec3(mMMatrix * -vec4(aNormal,0.0))); 
} 

Fragmentshader:

#define MAX_POINT_LIGHTS 4 
precision mediump float; 

varying vec2 vecTexCoord; 
varying vec3 vecNormal; 
varying vec4 vecVertex[2]; 

uniform vec3 uVecEye; 
uniform vec3 uPointLightPosition[MAX_POINT_LIGHTS]; 
uniform vec3 uPointLightColor[MAX_POINT_LIGHTS]; 
uniform sampler2D textureHandle; 

vec3 V = normalize(uVecEye.xyz-vecVertex[1].xyz); 
vec3 N = vNormal; 

vec3 vecLight[MAX_POINT_LIGHTS]; 
vec4 pointDiffuse = vec4(0.0); 
vec4 pointSpecular = vec4(0.0); 

vec4 ambient = vec4(0.2,0.2,0.2,1.0); 
vec4 color = vec4(1.0,1.0,1.0,1.0); 
vec4 matSpec = vec4(1.0,1.0,1.0,1.0); 
vec4 lightSpec = vec4(1.0,1.0,1.0,1.0); 
vec4 spec = matSpec * lightSpec; 

float shininess = 20.0; 

void main() { 
    for (int i=0;i<MAX_POINT_LIGHTS;i++) { 

     vecLight[i].xyz = vecVertex[0].xyz - uPointLightPosition[i].xyz; 
     float vecDistance = length(vecLight[i].xyz); 

     if (vecDistance<=25.0) { 

      vecDistance = 1.0 - max(0.0,vecDistance)/25.0; 
      vec3 L = normalize(vecLight[i]); 
      vec3 R = normalize(reflect(L,N)); 
      float LND = max(0.0,dot(N,L)) * vecDistance; 

      pointDiffuse += color * vec4(uPointLightColor[i].xyz,0.0) * LND; 

      if (shininess!=0.0 && spec!=0.0) { 
       pointSpecular += spec * pow(max(0.0,dot(R,V)),shininess) * LND; 
      } else { 
       pointSpecular += vec4(0.0,0.0,0.0,0.0); 
      } 
     } 
    } 
    vec4 colorTexture = texture2D(textureHandle,vec2(+vTexCoord.x,-vTexCoord.y)); 
    gl_FragColor = ambient + colorTexture * pointDiffuse + pointSpecular; 
} 

每次我嘗試運行該程序,ShaderlogInfo和ProgramlogInfo對我說:

無效的片段着色器。鏈接無法繼續。*

我瘋了還是隻是盲目?! 我希望你知道答案...我真的不知道...請幫助我! :(

+0

什麼編譯和鏈接日誌說? –

+0

如何在eclipse中獲得這些編譯和鏈接日誌?顯然我還沒有看到他們:/ – TheWhiteLlama

+0

這不是Eclipse的責任。 Shader編譯/鏈接錯誤是_you_必須在您的代碼中檢查。它們是着色器運行時編譯的一部分。如果存在編譯器/鏈接器錯誤,您必須使用代碼獲取它們並顯示它們。我不知道Android和Java如何處理它,但[本頁](http://www.opengl.org/wiki/GLSL#Error_Checking)解釋瞭如何在C/C++代碼中執行此操作。 –

回答

4

你得到的日誌是從程序連接階段,glGetProgramInfoLog

你需要調試什麼是片段着色器日誌,glGetShaderInfoLog

東西沿着這些路線:。

def _compile(self, source): 
    ptr = cast(c_char_p(source), POINTER(c_char)) 
    glShaderSource(self.id, 1, byref(ptr), None) 
    glCompileShader(self.id) 
    status = c_int(0) 
    glGetShaderiv(self.id, GL_COMPILE_STATUS, byref(status)) 
    log = self.check() 
    print(log), 
    if not status.value: 
     raise Exception(log) 

def check(self): 
    length = c_int(0) 
    glGetShaderiv(self.id, GL_INFO_LOG_LENGTH, byref(length)) 
    log = create_string_buffer(length.value) 
    glGetShaderInfoLog(self.id, length.value, None, log) 
    return log.value 

雖然這不是在Java中,但在Python中,它應該給你一個如何讓你的着色器編譯日誌的想法。

編譯你的s在我的環境中的哈德給我這個日誌,這可能會或可能不會對你有用:

Vertex shader was successfully compiled to run on hardware. 
WARNING: 0:2: warning(#260) Keyword 'precision' is supported in GLSL 1.3 
Fragment shader failed to compile with the following errors: 
WARNING: 0:2: warning(#260) Keyword 'precision' is supported in GLSL 1.3 
ERROR: 0:14: error(#143) Undeclared identifier vNormal 
WARNING: 0:14: warning(#402) Implicit truncation of vector from size 1 to size 3. 
ERROR: 0:50: error(#143) Undeclared identifier vTexCoord 
ERROR: 0:50: error(#216) Vector field selection out of range 'y' 
ERROR: error(#273) 4 compilation errors. No code generated 
+0

正是我所期待的,謝謝! – trippedout

+0

你會把它標記爲接受的答案嗎? – Berserker

+0

希望我可以@Berserker但我沒有問這個問題:) – trippedout