1

I am currently testing something out with GLSL, but now whenever I use shaders, my texture doesn't show up on the screen. I have 2 classes. It works without the shaders though...

Main Class

public class Main {

    private static final int WIDTH = 1280;
    private static final int HEIGHT = 720;
    private static int shaderProgram;

    private static int vertexShader;
    private static int fragmentShader;

    private static Terrain terrain;

    public static void main(String[] args) {


        initDisplay();
        initGL();

        terrain = new Terrain();
        terrain.createTerrain();

        createShader();

        gameLoop();

        removeShader();

    }

    private static void gameLoop() {
        while (!Display.isCloseRequested()) {
            glClear(GL_COLOR_BUFFER_BIT);
            glUseProgram(shaderProgram);

            terrain.drawTerrain();
            glUseProgram(0);


            Display.update();
            Display.sync(60);

        }
    }

    private static void initGL() {
        glMatrixMode(GL_PROJECTION);

        glLoadIdentity();

        glOrtho(0, WIDTH, 0, HEIGHT, -1, 1);

        glMatrixMode(GL_MODELVIEW);

        glDisable(GL_DEPTH_TEST);
        glEnable(GL_TEXTURE_2D);

        glClearColor(0, 0, 0, 1);
    }

    private static void initDisplay() {
        try {
            Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
            Display.create();

        } catch (LWJGLException e) {
            e.printStackTrace();
        }
    }

    private static void createShader() {
        shaderProgram = glCreateProgram();
        vertexShader = glCreateShader(GL_VERTEX_SHADER);
        fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
        StringBuilder vertexShaderSource = new StringBuilder();
        StringBuilder fragmentShaderSource = new StringBuilder();
        try {
            BufferedReader reader = new BufferedReader(new FileReader("src/me/mateo226/shaders/vertexShader.txt"));
            String line;
            while ((line = reader.readLine()) != null) {
                vertexShaderSource.append(line).append("\n");
            }
            reader.close();
        } catch (IOException e) {
            System.err.println("Vertex shader wasn't loaded properly!");
            Display.destroy();
            System.exit(1);
        }
        try {
            BufferedReader reader = new BufferedReader(new FileReader("src/me/mateo226/shaders/fragmentShader.txt"));
            String line;
            while ((line = reader.readLine()) != null) {
                fragmentShaderSource.append(line).append("\n");
            }
            reader.close();
        } catch (IOException e) {
            System.err.println("Fragment shader wasn't loaded properly!");
            Display.destroy();
            System.exit(1);
        }

        glShaderSource(vertexShader, vertexShaderSource);
        glCompileShader(vertexShader);
        if (glGetShader(vertexShader, GL_COMPILE_STATUS) == GL_FALSE) {
            System.err.println("Vertex shader not compiled!");
        }
        glShaderSource(fragmentShader, fragmentShaderSource);
        glCompileShader(fragmentShader);
        if (glGetShader(fragmentShader, GL_COMPILE_STATUS) == GL_FALSE) {
            System.err.println("Fragment shader not compiled!");
        }

        glAttachShader(shaderProgram, vertexShader);
        glAttachShader(shaderProgram, fragmentShader);
        glLinkProgram(shaderProgram);
        glValidateProgram(shaderProgram);
    }

    private static void removeShader() {

        glDeleteProgram(shaderProgram);
        glDeleteShader(vertexShader);
        glDeleteShader(fragmentShader);
        Display.destroy();
    }

}

Terrain Class

public class Terrain {
    private static List<Tile> tiles = new ArrayList<Tile>();

    public Terrain() {

    }

    public void createTerrain() {
        Random rand = new Random();
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 15; j++) {
                int r = rand.nextInt(2);
                tiles.add(new Tile(i * 64, j * 64, r));
            }
        }
    }

    public void drawTerrain() {
        for (Tile t : tiles) {
            t.draw();
        }
    }

}

class Tile {

    int type = 0;

    private float x = 0;
    private float y = 0;
    private float width = 64;
    private float height = 64;

    private Texture tex;

    public Tile(float x, float y, int type) {
        this.type = type;
        this.x = x;
        this.y = y;
        try {
            if (this.type == 1) {
                tex = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/grass.png")));
            } else {
                tex = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/grass.png")));
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void draw() {
        tex.bind();
        glBegin(GL_QUADS);
        {
            glTexCoord2f(0, 0);
            glVertex2f(x, y);
            glTexCoord2f(0, 1);
            glVertex2f(x, y + height);
            glTexCoord2f(1, 1);
            glVertex2f(x + width, y + height);
            glTexCoord2f(1, 0);
            glVertex2f(x + width, y);
        }
        glEnd();
    }

}

While it may seem like a lot of code, it really isn't, just a simple program. However I am still unable to figure out what makes it not work. My vertex shader just has:

gl_Position = ftransform();

I tried adding gl_FragColor to the fragment shader but it doesn't help. Cheers!

2
  • You have to pass the texture to the fragment shader. gl_FragColor wants vec4, so if you want to draw your texture only, assign texture2D(TexSampler, TexCoord) to the gl_FragColor. Commented Oct 3, 2015 at 17:27
  • 1
    @ProXicT That worked thanks! Post an answer and I will accept it :) Commented Oct 3, 2015 at 21:56

1 Answer 1

2

If you don't want to do anything with the texture e.g. lighting, you just have to assign texture2D(TexSampler, TexCoord) to the gl_FragColor in your frag shader.
That's because fragment shader will take care of each pixel (fragment) on screen (in current shader program).
If you don't assign any value to the gl_FragColor variable, nothing will drawn. What you want is to assign your texture (a bunch of pixels) to the gl_FragColor and you're good to go.

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

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.