PDA

View Full Version : LWJGL problem



addemup
02-21-2015, 06:07 PM
Hello, and for the past few couple of days, I've been working on a Minecraft Clone (called "Build") in LWJGL,
and I've been trying to draw ONE dirt block, but all it will draw only a square!

Here is the main class:



package game;

import game.block.DirtBlock;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

public class Boot {

public void start() {
try {
Display.setDisplayMode(new DisplayMode(800,600));
Display.setTitle("Build");
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}

// init OpenGL here
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 640, 480, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glEnable(GL11.GL_TEXTURE_COMPONENTS);

DirtBlock dirt = new DirtBlock(0, 0, 0);

while (!Display.isCloseRequested()) {

// render OpenGL here

dirt.draw();

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

Display.destroy();
}

public static void main(String[] args) {
Boot boot = new Boot();
boot.start();
}

}


And here is the main block class (the one which all Block Types interit from):



package game;

import static org.lwjgl.opengl.GL11.*;

import java.io.IOException;
import java.nio.FloatBuffer;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

public class Block {
private static Texture side1;
private static Texture side2;
private static Texture side3;
private static Texture side4;
private static Texture side5;
private static Texture side6;

public static Texture getSide1() {
return side1;
}
public static void setSide1(Texture side1) {
Block.side1 = side1;
}
public static Texture getSide2() {
return side2;
}
public static void setSide2(Texture side2) {
Block.side2 = side2;
}
public static Texture getSide3() {
return side3;
}
public static void setSide3(Texture side3) {
Block.side3 = side3;
}
public static Texture getSide4() {
return side4;
}
public static void setSide4(Texture side4) {
Block.side4 = side4;
}
public static Texture getSide5() {
return side5;
}
public static void setSide5(Texture side5) {
Block.side5 = side5;
}
public static Texture getSide6() {
return side6;
}
public static void setSide6(Texture side6) {
Block.side6 = side6;
}
private static float x;
private static float y;
private static float z;
public Block(Texture side1, Texture side2, Texture side3, Texture side4, Texture side5, Texture side6, float x, float y, float z) {
super();
this.x = x;
this.y = y;
this.z = z;
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
this.side4 = side4;
this.side5 = side5;
this.side6 = side6;
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public float getZ() {
return z;
}
public void setZ(float z) {
this.z = z;
}
public void draw() {
GL11.glBegin(GL_QUADS);
side1.bind();
GL11.glTexCoord2f(0, 0);
GL11.glVertex3f(1, 1, -1);
GL11.glTexCoord2f(1, 0);
GL11.glVertex3f(World.BLOCK_SIZE, 1, -1); //x = -1
GL11.glTexCoord2f(1, 1);
GL11.glVertex3f(World.BLOCK_SIZE, 1, World.BLOCK_SIZE); //x = -1, z = 1
GL11.glTexCoord2f(0, 1);
GL11.glVertex3f(1, 1, World.BLOCK_SIZE); //x = 1, z = 1
GL11.glLoadMatrix((FloatBuffer) side2);
side2.bind();
GL11.glTexCoord2f(0, 0);
GL11.glVertex3f(1, -1, 1);
GL11.glTexCoord2f(1, 0);
GL11.glVertex3f(World.BLOCK_SIZE, -1, 1);
GL11.glTexCoord2f(1, 1);
GL11.glVertex3f(World.BLOCK_SIZE, -1, World.BLOCK_SIZE);
GL11.glTexCoord2f(0, 1);
GL11.glVertex3f(1, -1, World.BLOCK_SIZE);
GL11.glLoadMatrix((FloatBuffer) side3);
side3.bind();
GL11.glTexCoord2f(0, 0);
GL11.glVertex3f( 1, 1, 1);
GL11.glTexCoord2f(1, 0);
GL11.glVertex3f(World.BLOCK_SIZE, 1, 1);
GL11.glTexCoord2f(1, 1);
GL11.glVertex3f(World.BLOCK_SIZE, World.BLOCK_SIZE, 1);
GL11.glTexCoord2f(0, 1);
GL11.glVertex3f(1, World.BLOCK_SIZE, 1);
GL11.glLoadMatrix((FloatBuffer) side4);
side4.bind();
GL11.glTexCoord2f(0, 0);
GL11.glVertex3f(1, -1, -1);
GL11.glTexCoord2f(1, 0);
GL11.glVertex3f(World.BLOCK_SIZE, -1, -1);
GL11.glTexCoord2f(1, 1);
GL11.glVertex3f(World.BLOCK_SIZE, World.BLOCK_SIZE, -1);
GL11.glTexCoord2f(0, 1);
GL11.glVertex3f(1, World.BLOCK_SIZE, -1);
GL11.glLoadMatrix((FloatBuffer) side5);
side5.bind();
GL11.glTexCoord2f(0, 0);
GL11.glVertex3f(-1, 1, 1);
GL11.glTexCoord2f(1, 0);
GL11.glVertex3f(-1, 1, World.BLOCK_SIZE);
GL11.glTexCoord2f(1, 1);
GL11.glVertex3f(-1, World.BLOCK_SIZE, World.BLOCK_SIZE);
GL11.glTexCoord2f(0, 1);
GL11.glVertex3f(-1, World.BLOCK_SIZE, -1);
GL11.glLoadMatrix((FloatBuffer) side6);
side6.bind();
GL11.glTexCoord2f(0, 0);
GL11.glVertex3f(1, 1,-1);
GL11.glTexCoord2f(1, 0);
GL11.glVertex3f(1, 1, World.BLOCK_SIZE);
GL11.glTexCoord2f(1, 1);
GL11.glVertex3f(1, World.BLOCK_SIZE, World.BLOCK_SIZE);
GL11.glTexCoord2f(0, 1);
GL11.glVertex3f(1, World.BLOCK_SIZE, -1);
GL11.glEnd();
}
}


and this is the Dirt Block class:



package game.block;

import static org.lwjgl.opengl.GL11.GL_QUADS;

import java.io.IOException;
import java.nio.FloatBuffer;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

import game.Block;
import game.World;

public class DirtBlock extends Block {

private static Texture side1;
private static Texture side2;
private static Texture side3;
private static Texture side4;
private static Texture side5;
private static Texture side6;
private static float x;
private static float y;
private static float z;
public DirtBlock(float x, float y,
float z) {
super(side1, side2, side3, side4, side5, side6, x, y, z);
try {
this.side1 = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/dirt.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
this.side2 = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/dirt.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
this.side3 = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/dirt.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
this.side4 = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/dirt.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
this.side5 = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/dirt.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
this.side6 = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/dirt.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Texture getSide1() {
return side1;
}
public static void setSide1(Texture side1) {
DirtBlock.side1 = side1;
}
public static Texture getSide2() {
return side2;
}
public static void setSide2(Texture side2) {
DirtBlock.side2 = side2;
}
public static Texture getSide3() {
return side3;
}
public static void setSide3(Texture side3) {
DirtBlock.side3 = side3;
}
public static Texture getSide4() {
return side4;
}
public static void setSide4(Texture side4) {
DirtBlock.side4 = side4;
}
public static Texture getSide5() {
return side5;
}
public static void setSide5(Texture side5) {
DirtBlock.side5 = side5;
}
public static Texture getSide6() {
return side6;
}
public static void setSide6(Texture side6) {
DirtBlock.side6 = side6;
}
public float getX() {
return x;
}
public void setX(float x) {
DirtBlock.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
DirtBlock.y = y;
}
public float getZ() {
return z;
}
public void setZ(float z) {
DirtBlock.z = z;
}
public void draw() {
GL11.glTranslatef(x, y, z);
GL11.glBegin(GL11.GL_QUADS);
side1.bind();
GL11.glTexCoord2f(0, 0);
GL11.glVertex3f(1, 1, -1);
GL11.glTexCoord2f(1, 0);
GL11.glVertex3f(World.BLOCK_SIZE, 1, -1); //x = -1
GL11.glTexCoord2f(1, 1);
GL11.glVertex3f(World.BLOCK_SIZE, 1, World.BLOCK_SIZE); //x = -1, z = 1
GL11.glTexCoord2f(0, 1);
GL11.glVertex3f(1, 1, World.BLOCK_SIZE); //x = 1, z = 1
side1.release();
side2.bind();
GL11.glTexCoord2f(0, 0);
GL11.glVertex3f(1, -1, 1);
GL11.glTexCoord2f(1, 0);
GL11.glVertex3f(World.BLOCK_SIZE, -1, 1);
GL11.glTexCoord2f(1, 1);
GL11.glVertex3f(World.BLOCK_SIZE, 1, World.BLOCK_SIZE);
GL11.glTexCoord2f(0, 1);
GL11.glVertex3f(1, -1, World.BLOCK_SIZE);
side2.release();
side3.bind();
GL11.glTexCoord2f(0, 0);
GL11.glVertex3f( 1, 1, 1);
GL11.glTexCoord2f(1, 0);
GL11.glVertex3f(World.BLOCK_SIZE, 1, 1);
GL11.glTexCoord2f(1, 1);
GL11.glVertex3f(World.BLOCK_SIZE, World.BLOCK_SIZE, 1);
GL11.glTexCoord2f(0, 1);
GL11.glVertex3f(1, World.BLOCK_SIZE, 1);
side3.release();
side4.bind();
GL11.glTexCoord2d(0, 0);
GL11.glVertex3f(1, -1, -1);
GL11.glTexCoord2f(1, 0);
GL11.glVertex3f(World.BLOCK_SIZE, -1, -1);
GL11.glTexCoord2f(1, 1);
GL11.glVertex3f(World.BLOCK_SIZE, World.BLOCK_SIZE, -1);
GL11.glTexCoord2f(0, 1);
GL11.glVertex3f(1, World.BLOCK_SIZE, -1);
side4.release();
side5.bind();
GL11.glTexCoord2f(0, 0);
GL11.glVertex3f(-1, 1, 1);
GL11.glTexCoord2f(1, 0);
GL11.glVertex3f(-1, 1, World.BLOCK_SIZE);
GL11.glTexCoord2f(1, 1);
GL11.glVertex3f(-1, -1, World.BLOCK_SIZE);
GL11.glTexCoord2f(0, 1);
GL11.glVertex3f(-1, World.BLOCK_SIZE, -1);
side5.release();
side6.bind();
GL11.glTexCoord2f(0, 0);
GL11.glVertex3f(1, 1, -1);
GL11.glTexCoord2f(1, 0);
GL11.glVertex3f(1, 1, World.BLOCK_SIZE);
GL11.glTexCoord2f(1, 1);
GL11.glVertex3f(1, World.BLOCK_SIZE, World.BLOCK_SIZE);
GL11.glTexCoord2f(0, 1);
GL11.glVertex3f(1, World.BLOCK_SIZE, -1);
side6.release();
GL11.glEnd();
}
}


and that is it for the code, of course that is not all of it.
I will edit if nessesary.

Help me please :)

carsten neumann
02-23-2015, 02:05 PM
I've been trying to draw ONE dirt block, but all it will draw only a square!


That's what a cube looks like under orthographic projection when looking straight at a single face.