gpt4 book ai didi

android - Opengl es 2.0 试图将浮点值传递给 fragment 着色器 android

转载 作者:行者123 更新时间:2023-11-30 03:16:20 25 4
gpt4 key购买 nike

如何将浮点值传递给 fragment 着色器?

这是我在安卓上的代码:

int aUseTexture = GLES20.glGetAttribLocation(program, "uUseTexture");

GLES20.glUniform1f(aUseTexture, 1.0f);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);

这是我的着色器:

String verticesShader =
"uniform mat4 uScreen;\n" +
"attribute vec2 aPosition;\n" +
"attribute vec3 aColor;\n" +
"attribute vec2 aTexPos; \n" +
"varying vec2 vTexPos; \n" +
"varying vec3 vColor;\n" +
"void main() {\n" +
" vTexPos = aTexPos; \n" +
" gl_Position = uScreen * vec4(aPosition.xy, 0.0, 1.0);\n" +
" vColor = aColor;\n" +
"}";

// Our fragment shader. Just return vColor.
// If you look at this source and just said 'WTF?', remember
// that all the attributes are defined in the VERTEX shader and
// all the 'varying' vars are considered OUTPUT of vertex shader
// and INPUT of the fragment shader. Here we just use the color
// we received and add a alpha value of 1.
String fragmentShader =
"uniform float uUseTexture; \n" +
"uniform sampler2D uTexture;\n" +
"precision mediump float;\n"+
"varying vec2 vTexPos; \n" +
"varying vec3 vColor;\n" +
"void main(void)\n" +
"{\n" +
" if ( uUseTexture != 1.0 ) \n" +
" gl_FragColor = vec4(vColor.xyz, 1); \n" +
" else \n" +
" gl_FragColor = texture2D(uTexture, vTexPos); \n" +
//" gl_FragColor = vec4(vColor.xyz, 1);\n" +
"}";

您可以在 fragment 着色器中看到 if 语句,这是我试图检查我是否传入 1.0 的语句,它应该做纹理,否则使用颜色。

最佳答案

您可能对“统一变量”使用了错误的函数调用。尝试 glGetUniformLocation() 如下:

int aUseTexture = GLES20.glGetUniformLocation(program, "uUseTexture"); 

此外,浮点测试 (uUseTexture != 1.0) 可能并不总是可靠的。您可能想要使用整数类型。

关于android - Opengl es 2.0 试图将浮点值传递给 fragment 着色器 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20043606/

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