我正在开始使用LibGDX中的着色器。我正在尝试手动设置颜色,但我得到的只是白色。
顶点着色器:
attribute vec4 a_position;
uniform mat4 u_projTrans;
void main() {
gl_Position = u_projTrans * a_position;
gl_PointSize = 10.0;
}
片段着色器:
void main()
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
如你所见,非常简单。通过这种设置,我能够绘制形状,但它们总是呈现出白色。使用我的片段着色器,我希望一切都是红色的。
我是否遗漏了libgdx堆栈中的内容?是否某些libgdx着色器默认值或其他东西被链条进一步覆盖?
答案 0 :(得分:1)
不知何故,您需要将顶点传递给OpenGL,以便LibGDX具有Mesh
类来满足此要求。我正在使用SpriteBatch
拥有自己用于绘制的网格物体。
顶点着色器:
attribute vec4 a_position;
uniform mat4 u_projTrans;
void main()
{
gl_Position = u_projTrans * a_position;
}
Fragment Shader:
#ifdef GL_ES
#define LOWP lowp
precision mediump float;
#else
#define LOWP
#endif
void main()
{
gl_FragColor = vec4(1,0,0,1);
}
ShaderTest
public class ShaderTest extends ApplicationAdapter {
SpriteBatch spriteBatch;
Texture texture;
ShaderProgram shaderProgram;
@Override
public void create() {
spriteBatch=new SpriteBatch();
shaderProgram=new ShaderProgram(Gdx.files.internal("default.vertex.glsl"),Gdx.files.internal("default.fragment.glsl"));
shaderProgram.pedantic = false;
if(shaderProgram.isCompiled()) {
System.out.println("Compiled Successfully");
spriteBatch.setShader(shaderProgram);
}else {
System.out.println("Some Problem in Shader");
}
texture=new Texture("badlogic.jpg");
}
@Override
public void render() {
Gdx.gl.glClearColor(1,1,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
spriteBatch.begin();
spriteBatch.draw(texture,100,100);
spriteBatch.end();
}
@Override
public void dispose() {
shaderProgram.dispose();
texture.dispose();
spriteBatch.dispose();
}
}
输出是:
修改强>
网格不算什么,它只是一个很大的顶点数组,包含OpenGL所需的值,单个顶点可以保存有关Position
,Color
,Texture Coordinates
或其他任何内容的信息否则我们想传递到着色器。
根据问题你在片段着色器中修复了RED的颜色,所以你只需要将quard的位置传递给OpenGL。
public class QuardTest extends ApplicationAdapter {
Mesh quard;
OrthographicCamera cam;
ShaderProgram shaderProgram;
private int Idx = 0;
private float[] verts= new float[4 * 2];
@Override
public void create() {
cam=new OrthographicCamera();
shaderProgram=new ShaderProgram(Gdx.files.internal("default.vertex.glsl"),Gdx.files.internal("default.fragment.glsl"));
if(shaderProgram.isCompiled()) {
System.out.println("Compiled Successfully");
}else {
System.out.println("Some Problem in Shader");
}
quard =new Mesh(true,4,0,new VertexAttribute(VertexAttributes.Usage.Position, 2, "a_position"));
}
@Override
public void render() {
Gdx.gl.glClearColor(1,1,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
drawRect(100,100,100,100);
drawRect(250,250,50,50);
}
public void drawRect(float x, float y, float width, float height){
if (Idx ==verts.length) {
flush();
}
verts[Idx++] = x;
verts[Idx++] = y;
verts[Idx++] = x + width;
verts[Idx++] = y;
verts[Idx++] = x + width;
verts[Idx++] = y + height;
verts[Idx++] = x;
verts[Idx++] = y + height;
}
public void flush(){
if (Idx ==0)
return;
quard.setVertices(verts);
Gdx.gl.glDepthMask(false);
int vertexCount = (Idx /2);
cam.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
shaderProgram.begin();
shaderProgram.setUniformMatrix("u_projTrans", cam.combined);
quard.render(shaderProgram, GL20.GL_TRIANGLE_FAN, 0, vertexCount);
shaderProgram.end();
Gdx.gl.glDepthMask(true);
Idx =0;
}
}