Hello Guest

Blurred Image

  • 0 Replies
  • 3482 Views
Blurred Image
« on: March 24, 2017, 15:49:35 »
I'm new at LWJGL and I'm always rendering a blurred image. :(
I've tried everything and searched in the internet. I hope you can help me.

Render Class:
Code: [Select]
public class Render {

private Main instance;
private boolean isRunning;

public Render(Main instance) {
this.instance = instance;
this.isRunning = true;
}

public void renderLauncher() throws LWJGLException {
init();
loop();
clean();
}

private void init() throws LWJGLException {
LoadGL11Event loadGL11Event = new LoadGL11Event();
loadGL11Event.setState(EventState.PRE);
loadGL11Event.call();

Display.setDisplayMode(new DisplayMode(getInstance().getDisplayWidth(), getInstance().getDisplayHeight()));
Display.setTitle(getInstance().getName());
Display.setResizable(true);
Display.setVSyncEnabled(true);
Display.create();

glEnable(GL_TEXTURE_2D);
glClearColor(0.0F, 0.0F, 0.0F, 0.0F);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glViewport(0, 0, getInstance().getDisplayWidth(), getInstance().getDisplayHeight());
glMatrixMode(GL_MODELVIEW);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, getInstance().getDisplayWidth(), getInstance().getDisplayHeight(), 0, 1, -1);
glMatrixMode(GL_MODELVIEW);

loadGL11Event.setState(EventState.POST);
loadGL11Event.call();
}

private void loop() {
while (!Display.isCloseRequested() && isRunning) {
glClear(GL_COLOR_BUFFER_BIT);

if (Display.wasResized()) {
glViewport(0, 0, Display.getWidth(), Display.getHeight());
}

getInstance().setDisplayWidth(Display.getWidth());
getInstance().setDisplayHeight(Display.getHeight());

glPushMatrix();

Render2DEvent render2DEvent = new Render2DEvent(Display.getWidth(), Display.getHeight());
render2DEvent.call();

glPopMatrix();

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

private void clean() {
getInstance().shutdown();
isRunning = false;
Display.destroy();
System.exit(0);
}

private Main getInstance() {
return instance;
}
}


Background Class:

Code: [Select]
public class Background {

private int textureID;
private int width;
private int height;

@EventHandler
public void onLoadGL11(LoadGL11Event loadGL11Event) {
if (loadGL11Event.getState().equals(EventState.POST)) {
try {
InputStream input = Main.getInstance().getClass().getResourceAsStream("/res/background.jpg");
BufferedImage image = ImageIO.read(input);
this.textureID = loadTexture(image);
this.width = image.getWidth();
this.height = image.getHeight();
} catch (IOException e) {
e.printStackTrace();
}
}
}

@EventHandler
public void onRender2D(Render2DEvent render2DEvent) {
glBindTexture(GL_TEXTURE_2D, textureID);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(1, 0);
glVertex2f(width, 0);
glTexCoord2f(1, 1);
glVertex2f(width, height);
glTexCoord2f(0, 1);
glVertex2f(0, height);
        glEnd();
}

public static int loadTexture(BufferedImage image) {
int[] pixels = new int[image.getWidth() * image.getHeight()];
image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());

ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4);

for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
int pixel = pixels[y * image.getWidth() + x];
buffer.put((byte) ((pixel >> 16) & 0xFF));
buffer.put((byte) ((pixel >> 8) & 0xFF));
buffer.put((byte) (pixel & 0xFF));
buffer.put((byte) ((pixel >> 24) & 0xFF));
}
}

buffer.flip();

int textureID = glGenTextures();

glBindTexture(GL_TEXTURE_2D, textureID);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);


glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);


glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE,
buffer);

return textureID;
}
}