我正在按照教程从OpenGL ES 2 for Android快速入门指南中学习用于Android的opengl es 2.0。我在3.5节,我的“编译”代码将无法编译。我已经检查过并仔细检查了但是我的代码与本书匹配。我搜索了SO和其他网站,但似乎只是我有这个代码的问题。我已经在AVD和一个软糖的物理设备上尝试了它,但我总是得到Compilation of shader failed
这是我正在尝试编译的代码。
顶点代码:
attribute vec4 a_Position;
void main(){
gl_Position = a_Position;
}
片段代码:
precision mediump float;
uniform vec4 u_Color;
void main(){
gl_FragColor = u_Color;
}
帮助程序类,它定位资源并编译opengl的代码:
public class ShaderHelper {
private static final String TAG = "ShaderHelper";
public static int compileVertexShader(String shaderCode){
return compileShader(GL_VERTEX_SHADER, shaderCode);
}
public static int compileFragmentShader(String shaderCode){
return compileShader(GL_FRAGMENT_SHADER, shaderCode);
}
public static int compileShader(int type, String shaderCode){
final int shaderObjectId = glCreateShader(type);
if(shaderObjectId == 0){
if(LoggerConfig.ON){
Log.w(TAG, "Could not create new shader.");
}
}
final int[] compileStatus = new int[1];
glGetShaderiv(shaderObjectId, GL_COMPILE_STATUS, compileStatus, 0);
if(LoggerConfig.ON){
Log.v(TAG, "Results of compiling source:" + "\n" + shaderCode + "\n:" + glGetShaderInfoLog(shaderObjectId));
}
if(compileStatus[0] == 0){
glDeleteShader(shaderObjectId);
if(LoggerConfig.ON){
Log.w(TAG, "Compilation of shader failed."); <--This is where I get notification that the code did not compile...
}
return 0;
}
return shaderObjectId;
}
public static int linkProgram(int vertexShaderId, int fragmentShaderId){
final int programObjectId = glCreateProgram();
if(programObjectId == 0){
if (LoggerConfig.ON){
Log.w(TAG, "Could not create new program.");
}
return 0;
}
glAttachShader(programObjectId, vertexShaderId);
glAttachShader(programObjectId, fragmentShaderId);
final int[] linkStatus = new int[1];
glGetProgramiv(programObjectId, GL_LINK_STATUS, linkStatus, 0);
if(LoggerConfig.ON){
Log.v(TAG, "Results of linking program:\n" + glGetProgramInfoLog(programObjectId));
}
if(linkStatus[0] == 0){
glDeleteProgram(programObjectId);
if(LoggerConfig.ON){
Log.w(TAG, "Linking of program failed.");
}
return 0;
}
return programObjectId;
}
public static boolean validateProgram(int programObjectId){
glValidateProgram(programObjectId);
final int[] validateStatus = new int[1];
glGetProgramiv(programObjectId, GL_VALIDATE_STATUS, validateStatus, 0);
Log.v(TAG, "Results of validating program: " + "\nLog:" + glGetProgramInfoLog(programObjectId));
return validateStatus[0] != 0;
}
}
答案 0 :(得分:0)
在本书中发现了一些错误,后面的章节中提供了额外的代码行,这使得以前的版本工作。