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!
gl_FragColorwants vec4, so if you want to draw your texture only, assigntexture2D(TexSampler, TexCoord)to thegl_FragColor.