【发布时间】:2016-03-04 18:13:14
【问题描述】:
我正在使用 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;
}
【问题讨论】:
标签: android opengl-es texture-mapping google-cardboard glsles