我正在Google Cardboard SDK中使用某些項目,並且在涉及紋理的第一個示例中遇到了一些問題。我用Cardboard SDK example和一些online tutorials的代碼拼湊了我的示例。您可以瀏覽和結算項目來源here。Android上的照明+紋理着色器OpenGL ES(和Cardboard SDK)不顯示紋理
如果你克隆和構建項目,我的問題應該是不言而喻的。我還製作了一個「lighting」分支,代表我試圖爲紋理着色器添加光照。唯一的變化是設置法線頂點屬性指針,並將漫反射值乘以着色器中的顏色。這種變化從原來這是我顯示:
顯然,第一個圖像的地球上沒有照明,而第二個有燈光,但沒有質感。是什麼賦予了?
我確定我做錯了。我只是不能爲我的生活找出我am做錯了什麼。我也一直在嘗試從頭開始重新創建一個簡單的例子,但是沒有將代碼複製到一個新項目中,我一直在遇到無關的問題。我被困在最近的嘗試中,試圖讓我的solid_color_lighting材料在普通的多維數據集上工作。
如果你懶得去看看我的藏匿回購,這裏是一些重要的代碼;)
繪圖功能:
public void draw(float[] view, float[] perspective, float[] model) {
GLES20.glUseProgram(program);
// Set the active texture unit to texture unit 0.
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
// Bind the texture to this unit.
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
// Tell the texture uniform sampler to use this texture in the shader by binding to texture unit 0.
GLES20.glUniform1i(textureParam, 0);
Matrix.multiplyMM(modelView, 0, view, 0, model, 0);
Matrix.multiplyMM(modelViewProjection, 0, perspective, 0, modelView, 0);
GLES20.glUniform3fv(lightPosParam, 1, RenderBox.instance.mainLight.lightPosInEyeSpace, 0);
GLES20.glUniform4fv(lightColParam, 1, RenderBox.instance.mainLight.color, 0);
// Set the ModelView in the shader, used to calculate lighting
GLES20.glUniformMatrix4fv(MVParam, 1, false, modelView, 0);
// Set the position of the cube
GLES20.glVertexAttribPointer(positionParam, 3, GLES20.GL_FLOAT, false, 0, vertexBuffer);
// Set the ModelViewProjection matrix in the shader.
GLES20.glUniformMatrix4fv(MVPParam, 1, false, modelViewProjection, 0);
// Set the normal positions of the cube, again for shading
if(normalParam > -1)
GLES20.glVertexAttribPointer(normalParam, 3, GLES20.GL_FLOAT, false, 0, normalBuffer);
GLES20.glVertexAttribPointer(texCoordParam, 2, GLES20.GL_FLOAT, false, 0, texCoordBuffer);
GLES20.glDrawElements(GLES20.GL_TRIANGLES, numIndices, GLES20.GL_UNSIGNED_SHORT, indexBuffer);
}
頂點着色器:
uniform mat4 u_MVPMatrix;
uniform mat4 u_MVMatrix;
attribute vec4 a_Position;
attribute vec3 a_Normal;
attribute vec2 a_TexCoordinate;
varying vec3 v_Position;
varying vec3 v_Normal;
varying vec2 v_TexCoordinate;
void main() {
// Transform the vertex into eye space.
v_Position = vec3(u_MVMatrix * a_Position);
// Pass through the color.
//v_Color = a_Color;
// Pass through the texture coordinate.
v_TexCoordinate = a_TexCoordinate;
// Transform the normal's orientation into eye space.
v_Normal = vec3(u_MVMatrix * vec4(a_Normal, 0.0));
// Multiply the vertex by the matrix to get the final point in normalized screen coordinates.
gl_Position = u_MVPMatrix * a_Position;
}
片段着色器:
precision mediump float; // Set the default precision to medium. We don't need as high of a
// precision in the fragment shader.
uniform vec3 u_LightPos; // The position of the light in eye space.
uniform vec4 u_LightCol;
uniform sampler2D u_Texture; // The input texture.
varying vec3 v_Position; // Interpolated position for this fragment.
// triangle per fragment.
varying vec3 v_Normal; // Interpolated normal for this fragment.
varying vec2 v_TexCoordinate; // Interpolated texture coordinate per fragment.
// The entry point for our fragment shader.
void main() {
// Will be used for attenuation.
float distance = length(u_LightPos - v_Position);
// Get a lighting direction vector from the light to the vertex.
vec3 lightVector = normalize(u_LightPos - v_Position);
// Calculate the dot product of the light vector and vertex normal. If the normal and light vector are
// pointing in the same direction then it will get max illumination.
float diffuse = max(dot(v_Normal, lightVector), 0.0);
// Add attenuation.
//diffuse = diffuse * (1.0/(1.0 + (0.1 * distance)));
// Add ambient lighting
//diffuse = diffuse + 0.3; //No ambient lighting.... this is space
// Multiply the color by the diffuse illumination level and texture value to get final output color.
//gl_FragColor = (v_Color * diffuse * texture2D(u_Texture, v_TexCoordinate));
//gl_FragColor = u_LightCol * diffuse * texture2D(u_Texture, v_TexCoordinate);
//gl_FragColor = texture2D(u_Texture, v_TexCoordinate);
gl_FragColor = texture2D(u_Texture, v_TexCoordinate) * diffuse;
//gl_FragColor = u_LightCol * diffuse;
}