При выполнении команды glGetAttribLocation произошла ошибка. И я не знаю, почему это так. Я пытался проверить программные вещи, шейдеры и логику. Но я не смог решить эту проблему ... Я думаю, что проблема возникла при создании программы или пропущенной программы. Но я не знаю, где проблема .. Ниже приведен журнал выполнения и коды.
Журнал
Shader has loaded : 1, 2
D/Debug: Program has linked
E/Error: vPosition attribute: GL_INVALID_OPERATION
E/AndroidRuntime: FATAL EXCEPTION: GLThread 401
Process: com.sholgames.projd, PID: 11352
java.lang.RuntimeException: Cannot find value 'vPosition'.
at com.sholgames.projd.CModel.<init>(CModel.java:114)
at com.sholgames.projd.CGLRenderer.onSurfaceCreated(CGLRenderer.java:18)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1539)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1270)
D/EGL_emulation: eglMakeCurrent: 0x9f76b320: ver 3 1 (tinfo 0xefaf4a40)
Process 11352 terminated.
CModel. java
public CModel(float[] verticeCoords_, short[] drawOrder_, float[] color_, int vertexShaderId_, int fragmentShaderId_) {
LogManager.LOG_DEBUG_MESSAGE("Start creating model class");
//Input factor data to member variable
mVertexCoords = verticeCoords_;
mDrawOrder = drawOrder_;
mColor = color_;
//Shader Load
mVertexShader = IOManager.LoadShaderCode(GL_VERTEX_SHADER, vertexShaderId_);
mFragmentShader = IOManager.LoadShaderCode(GL_FRAGMENT_SHADER, fragmentShaderId_);
LogManager.LOG_DEBUG_MESSAGE("Shader has loaded : " + mVertexShader + ", " + mFragmentShader);
//Create program
mProgramObject = glCreateProgram();
//Attach shader
glAttachShader(mProgramObject, mVertexShader);
LogManager.CHECK_ERROR("Attach vertex shader");
glAttachShader(mProgramObject, mFragmentShader);
LogManager.CHECK_ERROR("Attach fragment shader");
//Bind shader
glBindAttribLocation(mProgramObject, 0, "vPosition");
LogManager.CHECK_ERROR("Bind position attrib");
glBindAttribLocation(mProgramObject, 1, "vColor");
LogManager.CHECK_ERROR("Bind color attrib");
//Link program
glLinkProgram(mProgramObject);
LogManager.CHECK_ERROR("Linking program");
LogManager.LOG_DEBUG_MESSAGE("Program has linked");
int[] result = new int[1];
glGetProgramiv(mProgramObject, GL_LINK_STATUS, result, 0);
if(result[0] == -1) throw new RuntimeException("ERROR : shader program link failed.");
//active data
glUseProgram(mProgramObject);
LogManager.CHECK_ERROR("Check using program");
LogManager.LOG_DEBUG_MESSAGE("Program has actived");
//Generate buffer id
mBuffersID = new int[10];
//Vertex buffer
mVertexBuffer = ByteBuffer.allocateDirect(mVertexCoords.length * 4)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
mVertexBuffer.put(mVertexCoords);
mVertexBuffer.position(0);
//Draw order buffer
mDrawOrderBuffer = ByteBuffer.allocateDirect(mDrawOrder.length * 2)
.order(ByteOrder.nativeOrder()).asShortBuffer();
mDrawOrderBuffer.put(mDrawOrder);
mDrawOrderBuffer.position(0);
//Color Buffer
mColorBuffer = ByteBuffer.allocateDirect(mColor.length * 4)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
mColorBuffer.put(mColor);
mColorBuffer.position(0);
//VBO
glGenBuffers(3, mBuffersID, 0);
glBindBuffer(GL_ARRAY_BUFFER, mBuffersID[0]);
glBufferData(GL_ARRAY_BUFFER, mVertexCoords.length, mVertexBuffer, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, mBuffersID[1]);
glBufferData(GL_ARRAY_BUFFER, mColor.length, mColorBuffer, GL_STATIC_DRAW);
//EBO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBuffersID[2]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, mDrawOrder.length, mDrawOrderBuffer, GL_STATIC_DRAW);
//VAO
int[] vaoIDs = new int[1];
glGenVertexArrays(1, vaoIDs, 0);
glBindVertexArray(vaoIDs[0]);
//Pointing vertex attribute
int vertexCoordAttrib = glGetAttribLocation(mProgramObject, "vPosition");
LogManager.CHECK_ERROR("vPosition attribute");
if(vertexCoordAttrib == -1) throw new RuntimeException("Cannot find value 'vPosition'.");
int colorAttrib = glGetAttribLocation(mProgramObject, "vColor");
LogManager.CHECK_ERROR("vColor attribute");
if(colorAttrib == -1) throw new RuntimeException("Cannot find value 'vColor'.");
glBindBuffer(GL_ARRAY_BUFFER, mBuffersID[0]);
glVertexAttribPointer(vertexCoordAttrib, 3, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(vertexCoordAttrib);
glBindBuffer(GL_ARRAY_BUFFER, mBuffersID[1]);
glVertexAttribPointer(colorAttrib, 4, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(colorAttrib);
LogManager.LOG_DEBUG_MESSAGE("Create model class done");
}
vertexshader.glsl
#version 300 es
attribute vec3 vPosition;
attribute vec4 vColor;
varying vec4 vPassColor;
void main() {
gl_Position = vec4(vPosition, 1.0);
vPassColor = vColor;
}
фрагментовhader.glsl
#version 300 es
precision mediump float;
varying vec4 vPassColor;
void main() {
gl_FragColor = vPassColor;
}