我在Google Cardboard SDK中处理了一些项目,并且在我的第一个涉及纹理的示例中遇到了一些问题。我已使用Cardboard SDK example和部分online tutorials中的代码将我的示例拼凑在一起。您可以浏览并签出项目来源here。
如果您克隆并构建项目,我的问题应该是不言而喻的。我也做了一个" lighting"表示我尝试将光照添加到纹理着色器的分支。唯一的变化是设置法线顶点属性指针,并将漫反射值乘以着色器中的颜色。这个改变使我的显示变为:
显然,第一张图像在地球上没有光照,第二张图像没有光线但没有纹理。是什么给了什么?
我确定我做错了。我不能为我的生活弄清楚我 做错了什么。我也一直试图从头开始重新创建一个最小的例子,但是如果不将代码复制到一个新项目中,我就会遇到无关的问题。我试图让我的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;
}
答案 0 :(得分:2)
我找到了答案!感谢@jimbo00000指出我需要为我的所有属性调用glEnableVertexAttribArray!对于那种特殊的材料,我根本就没有这么说。我知道这会很简单!
要清楚,我只需要添加
GLES20.glEnableVertexAttribArray(positionParam);
GLES20.glEnableVertexAttribArray(normalParam);
GLES20.glEnableVertexAttribArray(texCoordParam);
在我的着色器设置步骤中。