0

I'm new to Shaders, and to OpenGL in general. After reading some tutorials, I decided to create my first shader (using LibGDX).

The problem is that the screen is entirely black ...

My Java code :

public class Application extends
                        ApplicationAdapter {

    private ShaderProgram   mShaderProgram;
    private Mesh            mMesh;

    @Override
    public void create() {
        this.createMesh();

        if (Gdx.graphics.isGL20Available()) {
            this.createShaderProgram();
        }

        this.log("" + this.mShaderProgram.isCompiled()); // outputs "true"
    }

    private void createMesh() {
        this.mMesh = new Mesh(true, 3, 4, VertexAttribute.Position());

        this.mMesh.setIndices(new short[] { 0, 1, 2 });
        this.mMesh.setVertices(new float[] {
                -0.5f, -0.5f, 0,
                0f, 0.5f, 0,
                0.5f, -0.5f, 0
        });
    }

    private void createShaderProgram() {
        final FileHandle fragmentShader = Gdx.files.internal("fragment.txt");
        final FileHandle vertexShader = Gdx.files.internal("vertex.txt");

        this.mShaderProgram = new ShaderProgram(vertexShader, fragmentShader);
    }

    @Override
    public void dispose() {
        this.mMesh.dispose();
        if (this.mShaderProgram != null) {
            this.mShaderProgram.dispose();
        }
    }

    private void log(final String message) {
        Gdx.app.log("", message);
    }

    @Override
    public void render() {
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        if (Gdx.graphics.isGL20Available()) {
            this.mShaderProgram.begin();
            this.mMesh.render(this.mShaderProgram, GL10.GL_TRIANGLES);
            this.mShaderProgram.end();
        }
        else {
            this.mMesh.render(GL10.GL_TRIANGLES);
        }
    }
}

My fragment shader :

void main() {
    gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}

My vertex shader :

void main() {
    gl_Position = ftransform();
}

Using gl_Vertex instead of ftransform() didn't change anything.

2 Answers 2

1

The reason you aren't seeing anything is because your vertex shader doesn't do anything. To make the code work correctly you would need to have it do the following at a minimum:

Vertex Shader:

#ifdef GL_ES
precision mediump float;
#endif

attribute vec4 a_position;

void main()
{
    gl_Position = a_position;
}

I would suggest you take a look at some shader tutorials to better understand how shaders work in OpenGL ES.

To quickly explain the above shader code, in your mesh you are setting vertex attributes for the "a_position" attribute. The vertex shader is simply passing that position information to the fragment shader. The fragment shader is then assigning white to all the fragments to be rendered.

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah I found this basic code in a tutorial 5 mins ago and it works. But I read in some tutorials that GLSL has built-in variables including gl_Vertex which holds the position data like a_position in the vertex shader, and the ftransform() function which simulate the fixed pipeline of previous OpenGL versions (modelViewMatrix * position). Is it possible to use these variables instead of creating new ones ? That would simplify the code of the shader. Same question for input gl_Color in the fragment shader.
You got to remember that OpenGL ES and OpenGL are different, as well as the versions for each have different parts of the language. The fixed pipeline functions I believe are only valid for OpenGL ES 1.x and not OpenGL ES 2.0/3.0. In 2.0 you have to pass in the values as attributes or as uniform variables. If you want a more complicated example in libGDX you can refer to this answer: stackoverflow.com/questions/15398184/…
After searching the web, I found that some GLSL built-in attributes & functions like gl_Vertex and ftransform() are deprecated in new versions of GLSL. I didn't know that now you have to register position & color attributes by yourself.
If you really want to get into the details of the OpenGL ES shaders the most invaluable reference is the quick reference guide found here: khronos.org/opengles/sdk/docs/reference_cards/…
0

After searching the web, I found that some GLSL built-in attributes & functions like gl_Vertex and ftransform() are deprecated in new versions of GLSL.

I didn't know that now you have to register even position & color attributes by yourself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.