Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.nio.ByteBuffer;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import javax.imageio.ImageIO;
- import org.lwjgl.BufferUtils;
- import org.lwjgl.LWJGLException;
- import org.lwjgl.opengl.Display;
- import org.lwjgl.opengl.DisplayMode;
- import static org.lwjgl.opengl.GL11.*;
- public class Main
- {
- public static void main(String[] args)
- {
- try
- {
- BufferedImage image = null;
- try
- {
- Path currentRelativePath = Paths.get("");
- String currentPathString = currentRelativePath.toAbsolutePath().toString();
- image = ImageIO.read(new File(currentPathString + File.separator + "res" + File.separator + "texture.png"));
- }
- catch (IOException exception)
- {
- exception.printStackTrace();
- }
- int[] pixels = new int[image.getWidth() * image.getHeight() * 4];
- image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
- ByteBuffer buffer = BufferUtils.createByteBuffer(pixels.length);
- 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();
- Display.setDisplayMode(new DisplayMode(640, 480));
- Display.setTitle("Title");
- Display.create();
- glEnable(GL_TEXTURE_2D);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
- glClearColor(0, 0, 0, 1);
- glMatrixMode(GL_PROJECTION);
- glOrtho(0, Display.getWidth(), 0, Display.getHeight(), 0, 1);
- glMatrixMode(GL_MODELVIEW);
- glColor3d(1, 0, 0);
- int textureId = glGenTextures();
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
- System.out.println(glGetError());
- while (true)
- {
- if (Display.isCloseRequested())
- return;
- glClear(GL_COLOR_BUFFER_BIT);
- glBindTexture(GL_TEXTURE_2D, textureId);
- glBegin(GL_QUADS);
- glVertex2d(50, 50);
- glVertex2d(100, 100);
- glVertex2d(300, 10);
- glVertex2d(20, 100);
- glEnd();
- glBindTexture(GL_TEXTURE_2D, 0);
- Display.update();
- try
- {
- Thread.sleep(10);
- }
- catch (InterruptedException exception)
- {
- exception.printStackTrace();
- }
- }
- }
- catch (LWJGLException exception)
- {
- exception.printStackTrace();
- }
- Display.destroy();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment