Hi there,
I’ve been lurking on this forum for a few weeks now, I’ve found lots of handy materials and information here that have helped in learning Java. I’ve signed up now to bother you with a question about OpenGL with lwjgl.
Today has marked my first experiments with OpenGL, and it has been pretty hair-pulling. It seems to be much more complicated than basic java-y stuff, and I’ve had a lot of trouble just getting stuff to show up on screen, but up till now I’ve managed to find solutions to all my problems. This one has me stumped, though:
Attached is an image of my game. Its going to be an isometric type thing, though it is still early days at the moment. I’ve been focusing on getting a nice framework established and printing some basic background sprites to the screen.
This is my code to draw the pretty background picture. OpenGL can only do textures of maximum size 512x512 apparently. so I wrote some code to split up large incoming images into 512x512 blocks, and the bit below fits them all together again. It basically just loops through all the 512x512 tiles stores and draws them, one after another.
` GL11.glPushMatrix();
for(int i=0; i<tilesy; i++) {
for(int j = 0; j<tilesx; j++) {
if(i*tileSize<window.getHeight() || j*tileSize<window.getWidth()) {
textureTiles[j][i].bind();
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0);
GL11.glVertex2i(j*tileSize,i*tileSize);
GL11.glTexCoord2f(0,1);
GL11.glVertex2i(j*tileSize,i*tileSize+tileSize);
GL11.glTexCoord2f(1,1);
GL11.glVertex2i(j*tileSize+tileSize,i*tileSize+tileSize);
GL11.glTexCoord2f(1,0);
GL11.glVertex2i(j*tileSize+tileSize,i*tileSize);
GL11.glEnd();
}
}
}
GL11.glPopMatrix();
`
That bit works all nicely. This bit below is trying to draw isometric squares onto the grid:
` GL11.glPushMatrix();
isoGrid.bind();
int sizex = isoGrid.getImageWidth();
int sizey = isoGrid.getImageHeight();
for(int i=0; i<numXTiles; i++) {
for(int j = 0; j<numYTiles; j++) {
if(i*sizey<window.getHeight() || j*sizex<window.getWidth()) {
GL11.glEnable (GL11.GL_BLEND);
GL11.glBlendFunc (GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0);
GL11.glVertex2i(j*sizex,i*sizey);
GL11.glTexCoord2f(0,1);
GL11.glVertex2i(j*sizex,i*sizey+sizey);
GL11.glTexCoord2f(1,1);
GL11.glVertex2i(j*sizex+sizex,i*sizey+sizey);
GL11.glTexCoord2f(1,0);
GL11.glVertex2i(j*sizex+sizex,i*sizey);
GL11.glEnd();
}
}
}
GL11.glPopMatrix(); `
Really similar to the first bit, right? isoGrid.bind() is just binding a .gif image of a square (attached) to the GL context, and the two extra GL-ish lines make it be able to do transparency. However, this code doesn’t work! As you can see, the isoGrid squares are not next to each other on the screen - they have a little bit of space seperating them.
I don’t understand why it is doing this! The way I see it, I should get the whole screen filled with an isometric grid sitting over the top of the background image. There shouldn’t be a boarder seperating them, as I am using more or less the same code that draws the background tiles, which are not seperated by a boarder. Is it some strange side effect of the transparency code that is doing it? The code that sets both these methods going looks like this:
public void draw(GameWindow window) { background.draw(window); isoGrid.draw(window); }
So its not like something elsewhere in the code is making it do this.
Any ideas?