Hello! I got it to work for 5 mins, thenit stopped… argh I am tearing my hair right now… Why won´t this code work (look at the render method). Basically I am drawing all quads on zAxis = 0.0f and then I want to look at point 0,0,0 with the camera… Why does this display just black? Anyone?
import org.lwjgl.Display;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.*;
public class Schlug implements Runnable {
private boolean fullscreen, finished;
private org.lwjgl.DisplayMode displayMode = null;
private String displayName;
private float camX, camY, camZ;
private double frameTime, temp;
public static GL gl;
private static GLU glu;
private Player you;
private Structure repair;
private Vehicle Tank;
private java.util.Vector textures;
private java.util.Vector textureNames;
// Starting it all
public static void main(String args[]) { new Schlug(); }
// Creates a new Game Object
public Schlug() {
camZ = -15;
gl = new GL();
glu = new org.lwjgl.opengl.GLU(gl);
textures = new java.util.Vector();
textureNames = new java.util.Vector();
displayName = "Schlug Attuck";
Thread mainThread = new Thread(this, "Main");
mainThread.start();
}
// Thread start
public void run() {
try {
initGL();
you = new Player(getTexID("soldier"));
repair = new Structure(gl, getTexID("repairBay"));
Tank = new Vehicle(getTexID("tank"));
// TEMP enter the tank
you.setVehicle(Tank);
System.gc();
while(!finished) {
// Calculate frame Time
frameTime = (Sys.getTime() - temp)/Sys.getTimerResolution();
temp = Sys.getTime();
Keyboard.poll();
org.lwjgl.input.Mouse.poll();
processControls();
render();
gl.swapBuffers();
}
}
catch (Exception e) { }
finally { cleanup(); }
}
// Render method aka paint
private void render() {
gl.clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT);
gl.loadIdentity();
glu.lookAt(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, camZ, 0.0f, 0.0f, 1.0f);
gl.enable(GL.BLEND);
gl.blendFunc(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA);
gl.texEnvf(GL.TEXTURE_ENV, GL.TEXTURE_ENV_MODE, GL.MODULATE);
gl.enable(GL.TEXTURE_2D);
you.setPos(you.getX(), you.getY(), 0.0f);
// you.draw(frameTime);
gl.loadIdentity();
Tank.draw(frameTime);
// drawBackground();
// repair.setPos(0.0f, 0.0f, camZ);
// repair.draw();
gl.disable(GL.TEXTURE_2D);
gl.disable(GL.BLEND);
}
private void drawBackground() {
gl.loadIdentity();
gl.color3ub((byte) 39, (byte) 47, (byte) 41);
gl.begin(GL.QUADS);
gl.vertex3f(0.0f, 0.0f, 0.0f);
gl.vertex3f(200.0f, 0.0f, 0.0f);
gl.vertex3f(200.0f, 200.0f, 0.0f);
gl.vertex3f(0.0f, 200.0f, 0.0f);
gl.end();
gl.color3ub((byte) 255, (byte) 255, (byte) 255);
}
private void setViewCenter(float x, float y) {
camX = -x;
camY = -y;
}
// Initialize Opengl
public void initGL() throws Exception {
org.lwjgl.DisplayMode[] modes = Display.getAvailableDisplayModes();
for (int i = 0; i < modes.length; i++) {
if (modes[i].width == 1024 && modes[i].height == 768 && modes[i].bpp == 32) {
displayMode = modes[i];
break;
}
}
Display.create(displayMode, fullscreen, displayName);
gl.create();
org.lwjgl.input.Mouse.create();
Keyboard.create();
gl.shadeModel(GL.SMOOTH); // Enable Smooth Shading
gl.clearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
gl.clearDepth(1.0); // Depth Buffer Setup
gl.enable(GL.DEPTH_TEST); // Enables Depth Testing
gl.depthFunc(GL.LEQUAL); // The Type Of Depth Testing To Do
gl.matrixMode(GL.PROJECTION); // Select The Projection Matrix
// Calculate The Aspect Ratio Of The Window
glu.perspective(45.0f, (float) Display.getWidth() / (float) Display.getHeight(), 0.1f, 100.0f);
gl.matrixMode(GL.MODELVIEW); // Select The Modelview Matrix
gl.hint(GL.PERSPECTIVE_CORRECTION_HINT, GL.NICEST); // Really Nice Perspective Calculations
gl.wglSwapIntervalEXT(1); // Minimum number of frames before swapping
loadTexture("data/soldier.png", "soldier", true);
loadTexture("data/repair.png", "repairBay", true);
loadTexture("data/tank.png", "tank", true);
}
// Takes care of ALL keyboard & mouse events
public void processControls() {
if (Keyboard.isKeyDown(Keyboard.KEY_R)) camZ -= 10 * (float)frameTime;
if (Keyboard.isKeyDown(Keyboard.KEY_F)) camZ += 10 * (float)frameTime;
if (Keyboard.isKeyDown(Keyboard.KEY_W)) you.accelerate(true);
if (Keyboard.isKeyDown(Keyboard.KEY_S)) you.accelerate(false);
if (Keyboard.isKeyDown(Keyboard.KEY_A)) you.setAngle(you.getAngle() + you.getAngleSpeed());
if (Keyboard.isKeyDown(Keyboard.KEY_D)) you.setAngle(you.getAngle() - you.getAngleSpeed());
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) finished = true;
if (Keyboard.isKeyDown(Keyboard.KEY_F1)) {
fullscreen = !fullscreen;
cleanup();
try { initGL(); }
catch (Exception ex) {
System.out.println("Error initializing Opengl: switching fullscreen mode.");
System.exit(0);
}
}
}
public void loadTexture (String path, String name, boolean alpha) {
java.awt.image.BufferedImage tex = null;
try {
tex = javax.imageio.ImageIO.read(new java.io.File(path));
}
catch (Exception e) {
System.out.println("Error: " + e);
System.exit(0);
}
// Put Image In Memory
java.nio.ByteBuffer scratch = java.nio.ByteBuffer.allocateDirect(4 * tex.getWidth() * tex.getHeight());
int scratchAddress = org.lwjgl.Sys.getDirectBufferAddress(scratch);
byte data[] = (byte[])tex.getRaster().getDataElements(0, 0, tex.getWidth(), tex.getHeight(), null);
scratch.clear();
scratch.put(data);
// Create A IntBuffer For Image Address In Memory
java.nio.IntBuffer buf = java.nio.ByteBuffer.allocateDirect(4).order(java.nio.ByteOrder.nativeOrder()).asIntBuffer();
int bufPtr = org.lwjgl.Sys.getDirectBufferAddress(buf);
gl.genTextures(1, bufPtr);
gl.bindTexture(GL.TEXTURE_2D, buf.get(0));
gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.NEAREST);
gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.NEAREST);
if(alpha) gl.texImage2D(GL.TEXTURE_2D, 0, GL.RGBA, tex.getWidth(), tex.getHeight(), 0, GL.RGBA, GL.UNSIGNED_BYTE, scratchAddress);
else gl.texImage2D(GL.TEXTURE_2D, 0, GL.RGB, tex.getWidth(), tex.getHeight(), 0, GL.RGB, GL.UNSIGNED_BYTE, scratchAddress);
addTexID(name, buf.get(0));
}
private int getTexID(String name) {
Integer temp = new Integer(((textures.get(textureNames.indexOf(name)))).toString());
return temp.intValue();
}
private void addTexID(String name, int id) {
textures.addElement(new Integer(id));
textureNames.addElement(name);
}
public void cleanup() {
Keyboard.destroy();
org.lwjgl.input.Mouse.destroy();
gl.destroy();
Display.destroy();
}
}