Opengl es glsl将计算片段着色器绑定到立方体面

Opengl es glsl将计算片段着色器绑定到立方体面,opengl-es,glsl,Opengl Es,Glsl,我有一个立方体,我想将计算片段着色器绑定到立方体面。有什么帮助吗 我的顶点着色器是: precision highp float; attribute vec3 position; attribute vec3 normal; uniform mat3 normalMatrix; uniform mat4 modelViewMatrix; uniform mat4 projectionMatrix; varying vec3 fNormal; varying vec3 fPosition; v

我有一个立方体,我想将计算片段着色器绑定到立方体面。有什么帮助吗

我的顶点着色器是:

precision highp float;
attribute vec3 position;
attribute vec3 normal;
uniform mat3 normalMatrix;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
varying vec3 fNormal;
varying vec3 fPosition;

void main()
{
  fNormal = normalize(normalMatrix * normal);
  vec4 pos = modelViewMatrix * vec4(position, 1.0);
  fPosition = pos.xyz;
  gl_Position = projectionMatrix * pos;
}
和我的片段着色器:

precision highp float;
uniform float time;
uniform vec2 resolution;
varying vec3 fPosition;
varying vec3 fNormal;

void main()
{
  float minDimension = min(resolution.x, resolution.y);
    vec2 bounds = vec2(resolution.x / minDimension, resolution.y / minDimension);
    vec2 uv = gl_FragCoord.xy / minDimension;
    vec2 midUV = vec2(bounds.x * 0.5, bounds.y * 0.5);
    uv.xy-=midUV;

    float dist = sqrt(dot(uv, uv));
    float t = smoothstep(0.2+0.1*cos(time), 0.1-0.01, dist);
    vec4 disc_color=vec4(1.0,0.,0.,1.);
    vec4 bkg_color=vec4(0.0, 0.0, 0.0, 1.0);

    vec4 resultColor;
    resultColor = mix(bkg_color, disc_color, t);   
     gl_FragColor = resultColor;
}
我得到了一个在孔图像上有点的立方体

但是我想要的是在每一张脸上都有一个点


您可以直接在

下测试代码在OpenGL中执行此操作的标准方法是首先将内容渲染到FBO(帧缓冲区对象)。这允许您渲染到纹理。然后,可以在渲染立方体时将此纹理用于其中一侧

以下不是完整的代码,只是一个提纲,为您提供了一个起点:

// create a texture, and bind it
GLuint texId = 0;
glGenTextures(1, &texId);
glBindTexture(GL_TEXTURE_2D, texId);

// set texture parameters (filters, wrap mode, etc)
glTexParameteri(GL_TEXTURE_2D, ...);

// allocate texture data (no data provided)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_FLOAT, 0);
glBindTexture(GL_TEXTURE_2D, texId);

// create and set up FBO with texture as attachment
GLuint fboId = 0;
glGenFramebuffers(1, &fboId);
glBindFramebuffer(GL_FRAMEBUFFER, fboId);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texId, 0);

// create a depth renderbuffer and attach it if depth testing is needed

// start rendering
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
glDraw...(...);

// back to default framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, windowWidth, windowHeight);
glClear(GL_COLOR_BUFFER_BIT);

// render cube using texture we rendered to
glBindTexture(GL_TEXTURE_2D, texId);
// rest of state setup and rendering

thx的答案是,这是正确的,但我找到了一个更好的方法,然后渲染到纹理。我传递了纹理坐标,并使用它们代替片段着色器中的glfragcoord。

您当前的代码没有灯光方向。如果希望灯光照射到每个面上,则可能需要多个光源,因为立方体旋转时需要多个面看起来被照亮。如果计划一次显示一个面,则一个光源需要相对于立方体的任何旋转进行移动。无论如何,只要试着以
一致的方式通过灯光方向即可。在FS中,将其乘以
fNormal
。考虑在C++代码中规范光DIR,在着色器中进行FuMaq。顺便说一句,根据您当前的代码,为什么要使用
fNormal
?thx作为您的快速答案,但我想我无法很好地解释我的观点。让我再试一次。如果我有一个片段着色器,我在上面画了一些东西,比如说一个圆或一个圆盘,我怎么能把它映射到一个立方体的面上呢。我看到gl_FragCoord在窗口空间,这就是为什么我的画会出现在屏幕上而不是立方体表面上,对吗?