Titel lesen.
Screenshot ist angefügt.
Hier einmal der Quellcode:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Random;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.*;
import org.lwjgl.util.glu.GLU;
public class LWJGLTest {
public LWJGLTest(){
try {
Display.setDisplayMode(new DisplayMode(500,500));
Display.create();
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluPerspective(45, Display.getWidth()/Display.getHeight(), (float) 0.1, 100);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glClearColor(1, 1, 1, 1);
GL11.glClearDepth(1.0);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthFunc(GL11.GL_LEQUAL);
GL11.glEnable(GL11.GL_TEXTURE_2D);
int rotation = 0;
int textur = 0;
try {
textur = loadTextureFromImage(javax.imageio.ImageIO.read(new File("textur.bmp")));
} catch (IOException e) {e.printStackTrace();}
while(!Display.isCloseRequested()){
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glLoadIdentity();
GL11.glTranslatef(0.0f, 0.0f, -5f);
GL11.glRotatef(rotation++, 1, 1, 1);
GL11.glColor4f(0,0,0,255);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textur);
drawBoxTextured();
Display.update();
Display.sync(60);
}
Display.destroy();
} catch (LWJGLException e) {
e.printStackTrace();
}
}
public void drawBoxTextured() {
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0f, 0f);
GL11.glVertex3f(-1.0f, -1.0f, 1.0f);
GL11.glTexCoord2f(0f, 1f);
GL11.glVertex3f( 1.0f, -1.0f, 1.0f);
GL11.glTexCoord2f(1f, 1f);
GL11.glVertex3f( 1.0f, 1.0f, 1.0f);
GL11.glTexCoord2f(1f, 0f);
GL11.glVertex3f(-1.0f, 1.0f, 1.0f);
//..Code zum zeichnen der anderen Seiten und GL11.glEnd();
}
public static int loadTextureFromImage(BufferedImage image){
int texture = GL11.glGenTextures();
GL11.glBindTexture(GL11.GL_TEXTURE_2D,texture);
int[] data = new int[image.getWidth()*image.getHeight()];
image.getRGB(0,0,image.getWidth(),image.getHeight(),data,image.getWidth(),0);
byte[] dataInBytes = new byte[image.getWidth()*image.getHeight()*4];
for(int i = 0; i < data.length; i++){
dataInBytes[i*4] = (byte) ((data[i] >> 16) & 0xFF);
dataInBytes[i*4+1] = (byte) ((data[i] >> 8) & 0xFF);
dataInBytes[i*4+2] = (byte) (data[i] & 0xFF);
dataInBytes[i*4+3] = (byte) ((data[i] >> 24) & 0xFF);
}
ByteBuffer imageData = ByteBuffer.allocateDirect(dataInBytes.length*4);
imageData.clear();
imageData.put(dataInBytes);
imageData.position(0).limit(dataInBytes.length);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0,GL11.GL_RGBA, image.getWidth(), image.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, imageData);
GL11.glBindTexture(GL11.GL_TEXTURE_2D,0);
return texture;
}
}
Es werden keine Fehler ausgegeben.