Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GLSL将重复数据存储在常量数组中_Java_Opengl_Glsl_Shader_Lwjgl - Fatal编程技术网

Java GLSL将重复数据存储在常量数组中

Java GLSL将重复数据存储在常量数组中,java,opengl,glsl,shader,lwjgl,Java,Opengl,Glsl,Shader,Lwjgl,我对着色器非常陌生,我很难理解什么可以做什么不能做。例如,我有一个三维体素地形。每个顶点都有3个信息:位置、颜色和法线。每个顶点的位置几乎都是唯一的,但在我的游戏中,它们只有6条法线,左-右-上-下-前-后,256种不同的颜色。因此,我尝试在vertexShader中创建一个常量数组,并将6条法线放在其中,而不是为每个顶点使用3个字节来存储法线,只使用1来存储要查看的索引。但是,这不起作用,因为数组索引只能是常量。我还试着测试normal=0,那么值就是normals[0],等等。。但也不管用

我对着色器非常陌生,我很难理解什么可以做什么不能做。例如,我有一个三维体素地形。每个顶点都有3个信息:位置、颜色和法线。每个顶点的位置几乎都是唯一的,但在我的游戏中,它们只有6条法线,左-右-上-下-前-后,256种不同的颜色。因此,我尝试在vertexShader中创建一个常量数组,并将6条法线放在其中,而不是为每个顶点使用3个字节来存储法线,只使用1来存储要查看的索引。但是,这不起作用,因为数组索引只能是常量。我还试着测试normal=0,那么值就是normals[0],等等。。但也不管用

我的问题是:如何存储循环数据,然后通过在缓冲区中存储索引而不是上述数据来检索它

编辑: 我忘了提及我的行为: 当传递一个“in”变量作为数组的索引时,它会自动转换为0索引,当测试“in”的值并分配正确的索引时,我发现到处都有奇怪的工件

#版本400
常量向量4法线[6](向量4(1,0,0),…)
在int-normal中;
真空总管(真空){
法线[normal]-->始终返回数组的第一个元素
}
编辑2:

因此,在将glVertexAttribIPointer更改为glVertexAttribIPointer后,我仍然有大量工件,因此我将发布代码和结果:

方法以创建vbo的法线:


private void storeDataInAttributeList(int attributeNumber, int coordsSize,byte[] data) {
        int vboID = GL15.glGenBuffers();
        vbos.add(vboID);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER,vboID);
        ByteBuffer buffer = storeDataInByteBuffer(data);
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
        GL30.glVertexAttribIPointer(attributeNumber, coordsSize, GL11.GL_BYTE, 0,0);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
    }

顶点着色器:


#version 400 core

const vec4 normals[] = vec4[6](vec4(1,0,0,0),vec4(0,1,0,0),vec4(0,0,1,0),vec4(-1,0,0,0),vec4(0,-1,0,0),vec4(0,0,-1,0));

in vec3 position;
in vec3 color;
in int normal;

out vec4 color_out;
out vec3 unitNormal;
out vec3 unitLightVector;
out vec3 unitToCameraVector;
out float visibility;

uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform vec3 lightPosition;

uniform float fogDensity;
uniform float fogGradient;

void main(void){

    vec4 worldPosition = transformationMatrix * vec4(position,1.0);
    vec4 positionRelativeToCam = viewMatrix * worldPosition;
    
    gl_Position = projectionMatrix * positionRelativeToCam;
    color_out = vec4(color,0.0);
    unitNormal = normalize((transformationMatrix * normals[normal]).xyz);
    
    unitLightVector = normalize(lightPosition - worldPosition.xyz);
    unitToCameraVector = normalize((inverse(viewMatrix) * vec4(0.0,0.0,0.0,1.0)).xyz - worldPosition.xyz);
    
    visibility = clamp(exp(-pow(length(positionRelativeToCam.xyz)*fogDensity,fogGradient)),0.0,1.0);
}

结果:


最终编辑:我忘了将vertexarray的大小从3更改为1,因此现在一切正常

具有整数数据类型的顶点属性让您指定
glVertexAttributeInter
(重点是
I
),而不是
glVertexAttributePointer
。请参阅。

类型参数未指定属性的类型。type参数只指定源数据数组的类型。
glvertexattributepointer
指定的属性数据被转换为浮点。

数组的类型是什么?。“使用类型为int或uint的表达式访问数组元素。”-采样器除外:“聚集到着色器内数组中的采样器(使用方括号[])只能使用动态统一的整数表达式编制索引”它是vec4的数组,我将法线索引存储在字节缓冲区中,然后在着色器中转换为int。如果您在着色器中指定了版本。如何指定整数索引属性?您必须显示相关代码。问题可能是您使用的是glVertexAttribIPointer而不是glVertexAttribIPointer。请看,我在更改glvertexattributepointer到glvertexattributepointerokay后编辑了我的帖子谢谢我在我的代码中更改了这一点,现在工件不同了,我编辑了我的帖子我太愚蠢了,我忘了将值的大小从3更改为1,因为我使用向量作为法线,但使用索引,谢谢你给出了正确的答案