gpt4 book ai didi

Android OpenGL ES 2.0 光照?

转载 作者:行者123 更新时间:2023-11-30 04:06:21 27 4
gpt4 key购买 nike

我是 GLES 2.0 的新手。这真的让我很紧张,因为 OpenGL ES 1 II 只是设置了 GL_LIGHTNING1,然后设置了光照。在 GLES 2.0 中它不起作用。实际上整个屏幕都是黑色的。所以请告诉我应该如何处理下面的代码。这些是我正在设置的顶点:

        static float quadrateCoords[] = { // in counterclockwise order:
// front
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
// back
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
// left
-0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
// right
0.5f, -0.5f, 0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, -0.5f,
// up
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
// bottom
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f
};
static float normal[] = {
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,

0.0f, 0.0f, -1.0f,
0.0f, 0.0f, -1.0f,
0.0f, 0.0f, -1.0f,
0.0f, 0.0f, -1.0f,

-1.0f, 0.0f, 0.0f,
-1.0f, 0.0f, 0.0f,
-1.0f, 0.0f, 0.0f,
-1.0f, 0.0f, 0.0f,

1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,

0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,

0.0f, -1.0f, 0.0f,
0.0f, -1.0f, 0.0f,
0.0f, -1.0f, 0.0f,
0.0f, -1.0f, 0.0f
};

static float quadrateColors[] = { // in counterclockwise order:
// front
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,

1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,

1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,

1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,

1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,

1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f
};

着色器代码是:

        private final String vertexShaderCode =
"uniform mat4 uMVPMatrix;\n" +
"uniform mat4 uMVMatrix;\n" +
"uniform vec3 uLightPos;\n" +

"attribute vec4 aPosition;\n" +
"attribute vec4 aColor;\n" +
"attribute vec4 aNormal;\n" +

"varying vec4 vColor;\n" +

"void main() {\n" +
"vec3 modelViewVertex = vec3(uMVMatrix * a_Position);\n" +
"vec3 modelViewNormal = vec3(uMVMatrix * vec4(aNormal, 0.0));\n" +
"float distance = length(uLightPos - modelViewVertex);\n" +
"vec3 lightVector = normalize(uLightPos - modelViewVertex);\n" +
"float diffuse = max(dot(modelViewNormal,lightVector), 0.1):\n" +
"diffuse = diffuse * (1.0 / (1.0 + (0.25 * distance * distance)));\n" +

"vColor = aColor * diffuse;\n" +

"gl_Position = aPosition * uMVPMatrix;\n" +
"}\n";

private final String fragmentShaderCode =
"precision mediump float;\n" +

"varying vec4 vColor;\n" +

"void main() {\n" +
"gl_FragColor = vColor;\n" +
"}\n";

最后绘制函数:

        public void draw(float[] mvpMatrix, float[] mVMatrix) {
// Add program to OpenGL environment
GLES20.glUseProgram(mProgram);

// get handle to vertex shader's aPosition member
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
nNormalHandle = GLES20.glGetAttribLocation(mProgram, "aNormal");
nColorHandle = GLES20.glGetAttribLocation(mProgram, "aColor");

// Enable a handle to the triangle vertices
// Prepare the triangle coordinate data
GLES20.glEnableVertexAttribArray(mPositionHandle);
GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);

GLES20.glEnableVertexAttribArray(nNormalHandle);
GLES20.glVertexAttribPointer(nNormalHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer2);

GLES20.glEnableVertexAttribArray(nColorHandle);
GLES20.glVertexAttribPointer(nColorHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer3);

muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mvpMatrix, 0);

muMVMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVMatrix");
GLES20.glUniformMatrix4fv(muMVMatrixHandle, 1, false, mVMatrix, 0);

muLightPosHandle = GLES20.glGetUniformLocation(mProgram, "uLightPos");
GLES20.glUniform3f(muLightPosHandle, 10.0f, 10.0f, 15.0f);

// Front
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);

// Back
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 4, 4);

// Left
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 8, 4);

// Right
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 12, 4);

// Top
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 16, 4);

// Right
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 20, 4);

// Disable vertex array
GLES20.glDisableVertexAttribArray(mPositionHandle);
}

最佳答案

这样调试:gl_FragColor = vec4(0.5, 0, 0, 0.5) + vColor;

尽管您的着色器是正确的,但如果有错误,请找出错误。

如果你测试错误后仍然有错误(通过直接使用颜色代替自定义值)看这个ADS着色器

How to implement shadow in OpenglES 1.x (on iPhone)

还有一点要记住,如果您启用了剔除,请禁用剔除

关于Android OpenGL ES 2.0 光照?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11592699/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com