我正在嘗試使用OpenGL ES在一個模型上實作3D照明。我按照這個指南https://arm-software.github.io/opengl-es-sdk-for-android/lighting.html,但我搞不清楚為什么我一直得到這個錯誤:資訊:無效的頂點屬性索引。想要的索引。4294967295. 最大索引。16.
坐標和規范都是大小為9的Float陣列,后來被投到floatBuffers。當我只想繪制形狀時,一切都能正常作業(因此頂點緩沖區可能是好的),但當我試圖使用規范時,一切都會崩潰。
我將非常感謝對這個問題的任何幫助。
Buffers creation:
private var vertexBuffer: FloatBuffer =
ByteBuffer.allocateDirect(triangleCoordinates.size * 4).run {
order(ByteOrder.nativeOrder()
asFloatBuffer().apply {
put(triangleCoordinates)
position(0)
}
}
private var normalsBuffer: FloatBuffer =
ByteBuffer.allocateDirect(normals.size * 4).run {
order(ByteOrder.nativeOrder()
asFloatBuffer().apply {
put(normals)
position(0)
}
}
頂點著色器代碼
private val vertexShaderCode =
"屬性vec4 vertexPosition;
"
"
"
"
"uniform mat4 modelView;
"
"void main(){
"
/* [設定場景向量。] */
" vec3 transformedVertexNormal = normalize((modelView * vec4(vertexNormal, 0.0)) .xyz);"
" vec3 inverseLightDirection = normalize(vec3(0.0, 1.0, 1.0));
"
" fragColour = vec3(0.0);
"
/* [設定場景矢量。] */
"
"
/* [Calculate the diffuse component. ] */
" vec3 diffuseLightIntensity = vec3(1.0, 1.0, 1.0);
"
" vec3 vertexDiffuseReflectionConstant = vertexColour;
"
" float normalDotLight = max(0.0, dot(transformedVertexNormal, inverseLightDirection));
"
" fragColour = normalDotLight * vertexDiffuseReflectionConstant * diffuseLightIntensity;
"
" /* 確保片段的顏色在0和1之間。*/"
" clamp(fragColour, 0.0, 1.0);
"
"
" gl_Position = modelView * vertexPosition;
"
"}"";````。
片段著色器:
private val fragmentShaderCode =
" precision mediump float;"
"uniform vec4 vertexColour;"
"void main() {"
"gl_FragColor = vertexColour;"
"}"/span>
繪制代碼:
fun draw(mvpMatrix: FloatArray) {
positionHandle = glGetAttribLocation(program, "vertexPosition"/span>)
vertexNormalLocation = glGetAttribLocation(program, "vertexNormal")
mColorHandle = glGetUniformLocation(program, "vertexColour").也{ colorHandle ->
glUniform4fv(colorHandle, 1, color, 0)
}
vPMatrixHandle = glGetUniformLocation(program, "modelView"/span>)
glUniformMatrix4fv(vPMatrixHandle, 1, false, mvpMatrix, 0)
glUseProgram(program)
glVertexAttribPointer(
positionHandle,
3,
gles20.gl_float,
false,
vertexStride。
vertexBuffer
)
glVertexAttribPointer(
vertexNormalLocation,
3,
GL_FLOAT,
false。
0,
規范緩沖區(normalsBuffer
)
glEnableVertexAttribArray(positionHandle)
glEnableVertexAttribArray(vertexNormalLocation)
glDrawArrays(GL_TRIANGLES, 0, vertexCount)
glDisableVertexAttribArray(positionHandle)
}
錯誤 mgs:
2021-09-19 13: 41: 59. 783 19419-19472/com.example.druggame E/emuglGLESv2_enc: device/generic/goldfish-opengl/system/GLESv2_enc/GL2Encoder. cpp:s_glVertexAttribPointer:637 GL錯誤 0x501
資訊:無效的頂點屬性index。想要index。4294967295. 最多 index: 16。
2021-09-19 13:41:59。 783 19419-19472/com. example.druggame E/emuglGLESv2_enc: device/generic/goldfish-opengl/system/GLESv2_enc/GL2Encoder.cpp:s_glEnableVertexAttribArray:1028 GL error 0 x501
資訊:無效的頂點屬性index。想要index。4294967295. 最多 index: 16。
uj5u.com熱心網友回復:
4294967295是十六進制的fffffff,分別是-1。glGetAttribLocation如果一個屬性資源在被喜歡的程式中不存在,則回傳這個值。你得到這個值,是因為其中一個屬性不是一個活躍的程式資源。
如果一個屬性沒有被使用(直接或間接),它將被著色器編譯器或聯結器優化,并且不會被賦予一個屬性索引。
在你的案例中,屬性沒有屬性索引,因為頂點著色器的輸出fragColour不是片段著色器的輸入。在片段著色器中使用fragColour:
precision mediump float;
變化的vec3 fragColour。
void main()
{
gl_FragColor = vec4(fragColour, 1.0) 。
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/328868.html
標籤:
