【问题标题】:Can't get OpenGL code to work properly无法让 OpenGL 代码正常工作
【发布时间】:2015-07-16 11:44:26
【问题描述】:

我对 OpenGL、GLSL 和 WebGL 非常陌生。我正在尝试让这个示例代码在 http://shdr.bkcore.com/ 这样的工具中工作,但我无法让它工作。

顶点着色器:

void main()
{ 
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    gl_TexCoord[0] = gl_MultiTexCoord0;
}

片段着色器:

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

uniform sampler2D tex0;

void main()
{
  float border = 0.01;
  float circle_radius = 0.5;
  vec4 circle_color = vec4(1.0, 1.0, 1.0, 1.0);
  vec2 circle_center = vec2(0.5, 0.5);

  vec2 uv = gl_TexCoord[0].xy;
  vec4 bkg_color = texture2D(tex0,uv * vec2(1.0, -1.0));

  // Offset uv with the center of the circle.
  uv -= circle_center;
  float dist = sqrt(dot(uv, uv));

  if ( (dist > (circle_radius+border)) || (dist < (circle_radius-border)) )
gl_FragColor = bkg_color;
else
gl_FragColor = circle_color;
}

我认为这段代码一定来自过时的语言版本,所以我将顶点着色器更改为:

precision highp float;
attribute vec2 position;
attribute vec3 normal;

varying vec2 TextCoord;
attribute vec2 textCoord;

uniform mat3 normalMatrix;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
varying vec3 fNormal;
varying vec3 fPosition;

void main()
{

  gl_Position = vec4(position, 0.0, 1.0);
  TextCoord = vec2(textCoord);
}

这似乎解决了有关未声明标识符的错误消息,并且无法“从'float'转换为highp 4-component something-or-other”,但我不知道从功能上讲,这是否会做同样的事情原意。

另外,当我转换到这个版本的顶点着色器时,我不知道我应该如何处理片段着色器中的这一行:

vec2 uv = gl_TexCoord[0].xy;

如何转换这条线以适应转换后的顶点着色器,如何确保顶点着色器甚至转换正确?

【问题讨论】:

  • 我认为网络着色器不支持 gl_TexCoord,我也试过 Glsl SandboxShader Toy 并且 gl_TexCoord 行出错了。
  • 在该列表中包括 gl_ModelViewProjectionMatrixgl_Vertexgl_MultiTexCoord0。所有这些都是 OpenGL ES 或 WebGL 中不存在的旧数据 OpenGL 事物。基本上有没有着色器的原始 OpenGL。添加着色器时,他们试图将着色器与旧的东西混合在一起,其中许多gl_XXX 变量正在访问旧的东西。当 OpenGL ES 出现时,他们摆脱了所有旧的东西。现在一切都 100% 取决于用户。您决定数据,名称,一切。见 (webglfundamentals.org)?
  • 即使您修复了代码,也不清楚它是否可以在任何这些网站上运行。它正在处理纹理。 AFAICT Shrd Editor 和 glsl 沙箱不支持纹理。着色器玩具允许一些,但上面的着色器似乎不是为着色器玩具使用着色器而设计的。为纹理使用不同的颜色it looks like this

标签: opengl-es glsl webgl fragment-shader vertex-shader


【解决方案1】:

gl_TexCoord 来自桌面 OpenGL,而不是 OpenGL ES 的一部分。您需要创建一个新的用户定义的 vec2 变量来保存坐标值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多