在我的项目开始时,我用简单的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对我说:
无效的片段着色器。链接无法继续。*
我疯了还是只是盲目?! 我希望你知道答案...我真的不知道...请帮助我! :(
什么编译和链接日志说? –
如何在eclipse中获得这些编译和链接日志?显然我还没有看到他们:/ – TheWhiteLlama
这不是Eclipse的责任。 Shader编译/链接错误是_you_必须在您的代码中检查。它们是着色器运行时编译的一部分。如果存在编译器/链接器错误,您必须使用代码获取它们并显示它们。我不知道Android和Java如何处理它,但[本页](http://www.opengl.org/wiki/GLSL#Error_Checking)解释了如何在C/C++代码中执行此操作。 –