简体   繁体   English

将一个浮点从顶点着色器传递到片段着色器

[英]Passing one float from the vertex shader to the fragment shader

Currently I'm passing one float from the vertex shader : 目前,我正在从顶点着色器传递一个浮点数:

varying float fog_factor;
...
fog_factor = clamp(gl_Position.z, 0.0, 1.0);
...

To the fragment shader : 到片段着色器:

varying float fog_factor;
...
gl_FragColor = texture2D(sampler_texture_4, ...) * fog_factor;
...

My question, is there something I need to add in the java code ? 我的问题是,我需要在Java代码中添加一些内容吗? When passing an array of float I need to add something like this : 当传递一个float数组时,我需要添加以下内容:

vertex_position_handle = GLES20.glGetAttribLocation(program, "vertex_position");
GLES20.glEnableVertexAttribArray(vertex_position_handle);
GLES20.glVertexAttribPointer(vertex_position_handle, 3, GLES20.GL_FLOAT, false, 3 * 4, vertex_buffer);

For now I'm only doing this in my java code for my float : 现在,我只在我的float的Java代码中执行此操作:

fog_handle = GLES20.glGetAttribLocation(program, "fog_factor");

I'm asking this because this code is working on my device, but crash on others... 我问这个问题是因为此代码可在我的设备上运行,但在其他设备上崩溃...

Well, the issue is that fog_factor is a varying. 好吧,问题在于fog_factor是变化的。 While this will pass information from the vertex to the fragment shader, you don't have access to it from client (java) code. 尽管这会将信息从顶点传递到片段着色器,但是您无法从客户端(java)代码访问它。 If you want to send information into the shader you need either an attribute or a uniform variable. 如果要将信息发送到着色器,则需要一个属性或一个统一变量。

attributes can change per vertex, while uniforms stay the same for each set of vertices (glDrawElements call) 每个顶点的属性可以更改,而每组顶点的制服保持相同(glDrawElements调用)

What I frequently do in the vertex shader is this: 我在顶点着色器中经常做的是这样的:

  attribute vec2 clientTexCoord;
  varying vec2 texCoord;  

  main(){
        ... // other code
        texCoord = clientTexCoord;
        ... // other code
  }

and in the client/java code get the attribute location for clientTexCoord as you are doing correctly. 并在客户端/ java代码中按正确方式获取clientTexCoord的属性位置。 It's incredible this works correctly right now but i've seen some GLSL compilers be a little less picky than others. 令人难以置信的是,它现在可以正常工作,但是我已经看到某些GLSL编译器的挑剔程度比其他编译器差。

Try modifying your vertex shader to allow for an attribute to be passed in to set the value for the varying fog_factor you have. 尝试修改您的顶点着色器,以允许传递属性以设置具有变化的fog_factor的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM