[英]OpenGL ES 2.0: glGetAttribLocation returns -1. Shader optimization?
我正在為Android編寫3D應用程序,但是每當我調用glGetAttribLocation()時,我總是得到-1。 我很清楚GLSL編譯器會刪除我的着色器中未使用的變量,但據我所知,一切都在使用中,但仍然出現空白屏幕。 GLSL為什么找不到我的屬性? 任何幫助表示贊賞。
相關代碼:
頂點着色器:
attribute vec3 vertex;
attribute vec3 normal;
uniform mat4 modelViewMatrix;
uniform mat4 MVPMatrix;
/*varying vec3 lightPosEye;*/
varying vec3 normalEye;
/*varying vec3 vertEye;*/
void main() {
/*Calculate normal matrix*/
mat4 normalMat = modelViewMatrix;
normalMat = inverse(normalMat);
normalMat = transpose(normalMat);
normalEye = normalize(vec3(normalMat * vec4(vNormal, 0.0)));
/*lightPosEye = modelViewMatrix * vec3(0.0, 0.8, 0.0);*/
/*vertEye = modelViewMatrix * vPosition;*/
gl_Position = MVPMatrix * vec4(vPosition, 1.0);
}
片段着色器:
precision mediump float;
/*uniform vec4 vColor; */
/*varying vec3 lightPosEye;*/
varying vec3 normalEye;
/*varying vec3 vertEye;*/
void main() {
gl_FragColor = vec4(normalEye, 1.0);
};
繪制方法:
public void draw(float[] mMVPMatrix, float[] mModelViewMatrix){
GLES20.glUseProgram(mProgram);
//get handle to vertex shader's vPosition
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
//enable vertex attrib array
GLES20.glEnableVertexAttribArray(mPositionHandle);
//load up coordinate data
GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false, 0, meshVertBuffer);
mNormalHandle = GLES20.glGetAttribLocation(mProgram, "vNormal");
//enable vertex attrib array
GLES20.glEnableVertexAttribArray(mNormalHandle);
//load up coordinate data
GLES20.glVertexAttribPointer(mNormalHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false, 0, meshNormBuffer);
// //get handle to fragment shader's vColor
// mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
//
// //set color uniform
// GLES20.glUniform4fv(mColorHandle, 1, colors, 0);
//get handle for and load MVP matrix
mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "MVPMatrix");
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
//load MV matrix
mModelViewMatrixHandle = GLES20.glGetUniformLocation(mProgram, "modelViewMatrix");
GLES20.glUniformMatrix4fv(mModelViewMatrixHandle, 1, false, mModelViewMatrix, 0);
//draw!
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, meshVerts.length/COORDS_PER_VERTEX);
//disable vertex attrib array
GLES20.glDisableVertexAttribArray(mPositionHandle);
}
您顯然沒有檢查編譯器錯誤。 因為如果有的話,您會看到以下內容:
gl_Position = MVPMatrix * vec4(vPosition, 1.0);
在着色器中的任何位置都未定義vPosition
。 您可能打算重命名attribute vec3 vertex;
對此。
始終檢查您的編譯器錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.