Libgdx 中的简单 OpenGL ES 着色器输出为白色应该为红色
Simple OpenGL ES Shader in Libgdx Output is white should be red
我想在我的 2D-libgdx-project 中使用一个简单的着色器,使用 OpenGL ES。我以前从未使用过 OpenGL ES,所以也许我的 headers 不对?
Vertex-Shader:
in vec3 position;
in vec2 textureCoords;
out vec2 passTextureCoords;
void main(void)
{
gl_Position = vec4(position.x, position.y, position.z, 1.0);
passTextureCoords = textureCoords;
}
Fragment-Shader:
#ifdef GL_ES
precision mediump float;
#endif
in vec2 passTextureCoords;
out vec4 outColour;
uniform sampler2D textureSampler;
void main(void)
{
// Get color at coordinate by sampler and output it
// outColour = texture(textureSampler, passTextureCoords);
// gl_FragColor = vec4(1.0,0.0,0.0,1.0);
outColour = vec4(1.0, 0.0, 0.0, 1.0);
}
实际上,我过去经常使用 OpenGL 3+,但从未与 Libgdx 一起使用,也从未与 OpenGL ES 一起使用。
此着色器应执行的操作:将每个像素取而代之 return 红色。但是我得到的只是白色。
我是这样创建的:
private ShaderProgram shader;
this.shader = new ShaderProgram(Gdx.files.internal("shaders/test.vertex").readString(), Gdx.files.internal("shaders/test.fragment").readString());
this.shader.pedantic = false;
并像这样渲染:
public void render(SpriteBatch spriteBatch)
{
spriteBatch.setShader(this.shader);
model.render(spriteBatch);
spriteBatch.setShader(null);
outline.render(spriteBatch);
}
我得到的模型只有白色,不是红色。所以我想出了点问题。我不想指定更多制服,所以我将 pedantic 设置为 false。我做错了什么?
Libgdx 仍然使用 OpenGL ES 2.0,因此着色器需要不同的关键字。 (这在 OpenGL ES 3.0 中已更改,因此它更接近 OpenGL 3.x,但 Libgdx 尚未完全支持。)
- 将顶点着色器中的关键字
in
替换为 attribute
。
- 将顶点着色器中的关键字
out
替换为 varying
。
- 将片段着色器中的关键字
in
替换为 varying
。
- 删除
outColour
并使用内置的 gl_FragColor
此外,您必须使用 SpriteBatch 期望的属性名称,因此您的属性必须是 a_position
、a_texCoord0
,如果需要,则必须是 a_color
。同样,您的制服必须是 SpriteBatch 使用的制服,因此请将 textureSampler
更改为 u_texture
。
而且我看到您在注释掉的代码中调用了函数 texture
,但是在 OpenGL ES 2.0 中,您需要改用函数 texture2D
。
我想在我的 2D-libgdx-project 中使用一个简单的着色器,使用 OpenGL ES。我以前从未使用过 OpenGL ES,所以也许我的 headers 不对?
Vertex-Shader:
in vec3 position;
in vec2 textureCoords;
out vec2 passTextureCoords;
void main(void)
{
gl_Position = vec4(position.x, position.y, position.z, 1.0);
passTextureCoords = textureCoords;
}
Fragment-Shader:
#ifdef GL_ES
precision mediump float;
#endif
in vec2 passTextureCoords;
out vec4 outColour;
uniform sampler2D textureSampler;
void main(void)
{
// Get color at coordinate by sampler and output it
// outColour = texture(textureSampler, passTextureCoords);
// gl_FragColor = vec4(1.0,0.0,0.0,1.0);
outColour = vec4(1.0, 0.0, 0.0, 1.0);
}
实际上,我过去经常使用 OpenGL 3+,但从未与 Libgdx 一起使用,也从未与 OpenGL ES 一起使用。
此着色器应执行的操作:将每个像素取而代之 return 红色。但是我得到的只是白色。
我是这样创建的:
private ShaderProgram shader;
this.shader = new ShaderProgram(Gdx.files.internal("shaders/test.vertex").readString(), Gdx.files.internal("shaders/test.fragment").readString());
this.shader.pedantic = false;
并像这样渲染:
public void render(SpriteBatch spriteBatch)
{
spriteBatch.setShader(this.shader);
model.render(spriteBatch);
spriteBatch.setShader(null);
outline.render(spriteBatch);
}
我得到的模型只有白色,不是红色。所以我想出了点问题。我不想指定更多制服,所以我将 pedantic 设置为 false。我做错了什么?
Libgdx 仍然使用 OpenGL ES 2.0,因此着色器需要不同的关键字。 (这在 OpenGL ES 3.0 中已更改,因此它更接近 OpenGL 3.x,但 Libgdx 尚未完全支持。)
- 将顶点着色器中的关键字
in
替换为attribute
。 - 将顶点着色器中的关键字
out
替换为varying
。 - 将片段着色器中的关键字
in
替换为varying
。 - 删除
outColour
并使用内置的gl_FragColor
此外,您必须使用 SpriteBatch 期望的属性名称,因此您的属性必须是 a_position
、a_texCoord0
,如果需要,则必须是 a_color
。同样,您的制服必须是 SpriteBatch 使用的制服,因此请将 textureSampler
更改为 u_texture
。
而且我看到您在注释掉的代码中调用了函数 texture
,但是在 OpenGL ES 2.0 中,您需要改用函数 texture2D
。