LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: TonytheD on December 11, 2012, 22:35:17

Title: Problem rendering tiles
Post by: TonytheD on December 11, 2012, 22:35:17
So I have a bit of an issue rendering tiles, I seem to have done everything right i just cant get my head around why the tiles aren't showing up.
*I should also note that i'm not that experienced with LWJGL or slick util*

Here's the code(very simplistic):


public class startclass {

static int width = 800;
static int height = 600;
static int TextureWidth = 52;
static int TextureHeight = 52;

private static Texture Grass;



public static void main(String[] args) throws IOException {

initDisplay();
//setup textures
LoadTexture();

initGL();

gameLoop();

Clearup();


}

public static void Clearup(){
Display.destroy();
}

public static void initDisplay() {

try {
Display.setDisplayMode(new DisplayMode(width,height));
Display.create();
Display.setVSyncEnabled(true);
Display.setTitle("Test");
} catch (LWJGLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public static void LoadTexture(){
try {
Grass = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/GrassTexture2.png")));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public static void initGL(){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0 ,width,height,0,1,-1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);


}

public static void gameLoop(){
while(!Display.isCloseRequested()){
render();
}


}

public static void render(){
glClear(GL_COLOR_BUFFER_BIT);

Grass.bind();

glBegin(GL_QUADS);
{

glTexCoord2f(0,0);
glVertex2f(0,0);
glTexCoord2f(TextureWidth, 0);
glVertex2f(1,0);
glTexCoord2f(TextureWidth, TextureHeight);
glVertex2f(1,1);
glTexCoord2f(0, TextureHeight);
glVertex2f(0, 1);


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

}

}
}

I've checked though this at least 20 times and just can't seem to see the problem, I just keep getting a black screen.
Title: Re: Problem rendering tiles
Post by: Fool Running on December 12, 2012, 14:21:32
My best guess is that you are trying to create a polygon that is only 1x1 pixels. I'm not sure what GrassTexture2.png looks like, but if the edges of the image are transparent, then you would probably end up with a 1-pixel, transparent polygon which wouldn't show up.

Try changing the 1s in your glVertex2f() calls to be 100s and see if that helps.

EDIT: Also noticed that you are hard-coding your texture coordinates to be 52. OpenGL works on a scale from 0.0 to 1.0 (where 1.0 is the edge of your texture). Changing those to 1.0 would probably work better.
Also, if your texture is really 52x52, keep in mind that Slick will create a 64x64 texture for it because OpenGL likes textures to be a power-of-two size. To get the coordinates of where your grass texture is inside the OpenGL texture, make sure you call Grass.getWidth() and Grass.getHeight() instead of using a hard-coded 1.0 value.

EDIT 2: Also noticed that you are trying to update the display inside of a glBegin()/glEnd() call (missing glEnd()). This also doesn't work.
Basically, try this code:
glBegin(GL_QUADS);
{

glTexCoord2f(0,0);
glVertex2f(0,0);
glTexCoord2f(Grass.getWidth(), 0);
glVertex2f(100,0);
glTexCoord2f(Grass.getWidth(), Grass.getHeight());
glVertex2f(100,100);
glTexCoord2f(0, Grass.getHeight());
glVertex2f(0, 100);
}
glEnd();

Display.sync(60);
Display.update();
Title: Re: Problem rendering tiles
Post by: TonytheD on December 12, 2012, 15:46:47
Ahh thank you, that worked, proves my checking skills suck  :(