奇怪的是,我传递给 GLSL 的每个整数都是 0,我发送了一个充满 2 和 1 的数组,但读取的全是 0。这些整数被发送到 OpenGL 来选择纹理,所以我只得到对应于 0 的纹理核心。这是我的顶点着色器:
String vertexshader = "#version 330 core"
+ System.getProperty("line.separator")
+ "in vec4 vertexin;"
+ "in vec2 textcoords;"
+ " in int yup;"
+ "uniform mat4 orthogonal;"
+ "out vec2 supertextcoords;"
+ "out int yup1;"
+ "void main(){"
+ "gl_Position = orthogonal * vertexin ;"
+ "supertextcoords = textcoords;"
+ "yup1=yup;"
+ "}";
这是片段着色器:
String fragmentshader="#version 330 core"
+ System.getProperty("line.separator")
+"out vec4 outcolor;"
+"flat in int yup;"
+"in vec2 supertextcoords;"
+"uniform sampler2D texture1;"
+"uniform sampler2D texture2;"
+"void main(){"
+"vec4 texturev1=texture2D(texture1, supertextcoords);"
+"vec4 texturev2=texture2D(texture2, supertextcoords);"
+"vec4 final=texture2D(texture2, supertextcoords);"
+ "if(yup==1){final=texturev1; outcolor=final;}else if(yup==0){"
+"final=texturev2; outcolor=final;}else{"
+ "final=vec4(0,1,0,0); outcolor=final;}"
我发送整数的方式是
glEnableVertexAttribArray(2);
glVertexAttribIPointer(2,1,GL_INT,0,(vertices.length+textcoord.length)*4);
glDrawArrays(GL_TRIANGLES, 0, vertices.length/4);
glDisableVertexAttribArray(2);
对于一个立方体,应该将 10 个数字发送到 OpenGL。每个三角形有 1 个数字(背面缺失)来决定每种尺寸的纹理。第二个数字与第一个数字相同,以使纹理相等。但只使用过texturev2。我想要用于此实验的纹理是vec4(0,1,0,0);因为该数字是 2 并且超出了映射范围。为什么?如果您需要更多来源,请询问。
最佳答案
在顶点着色器中,您有 yup1
作为输出,但在片段着色器中没有相应的 yup1
作为输入。
除了精确的符号匹配之外,OpenGL 没有其他方法来链接程序部分之间的非 block in
/out
声明。未链接的输入在运行时不会有任何特别有用的内容。
在片段着色器中进行更改
flat in int yup;
至
flat in int yup1;
使其与顶点着色器中的out int yup1
声明匹配。
关于java - 在 GLSL 中整数都是 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24499391/