|
I'm trying to draw a textured quad with LWJGL, but I don't know why my quad renders with only one pixel of color.
This is the first time that i use LWJGL to create a 2D Fullscreen game.
Here's the code used to draw the quad:
public void drawRect(int x, int y, int width, int height){ glBegin(GL_QUADS); { GL11.glVertex2f(x, y); GL11.glVertex2f(width + x, y); GL11.glVertex2f(width + x, height + y); GL11.glVertex2f(x, height + y); } glEnd(); } Here's the code for load texture image:
public int getTexture(File f) { Integer integer = textureMap.get(f);
if (integer != null) { return integer.intValue(); }
try { singleIntBuffer.clear(); GLAllocation.generateTextureNames(singleIntBuffer); int i = singleIntBuffer.get(0); InputStream inputstream = new FileInputStream(f); setupTexture(readTextureImage(inputstream), i); textureMap.put(f, Integer.valueOf(i)); return i; } catch (Exception exception) { exception.printStackTrace(); }
GLAllocation.generateTextureNames(singleIntBuffer); int j = singleIntBuffer.get(0); setupTexture(missingTexture, j); textureMap.put(f, Integer.valueOf(j)); return j; } public void setupTexture(BufferedImage par1BufferedImage, int par2) { GL11.glBindTexture(GL11.GL_TEXTURE_2D, par2); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR); int i = par1BufferedImage.getWidth(); int j = par1BufferedImage.getHeight(); int ai[] = new int[i * j]; byte abyte0[] = new byte[i * j * 4]; par1BufferedImage.getRGB(0, 0, i, j, ai, 0, i);
for (int k = 0; k < ai.length; k++) { int i1 = ai[k] >> 24 & 0xff; int k1 = ai[k] >> 16 & 0xff; int i2 = ai[k] >> 8 & 0xff; int k2 = ai[k] & 0xff;
/** if (options != null && options.anaglyph) { int i3 = (k1 * 30 + i2 * 59 + k2 * 11) / 100; int k3 = (k1 * 30 + i2 * 70) / 100; int i4 = (k1 * 30 + k2 * 70) / 100; k1 = i3; i2 = k3; k2 = i4; } */
abyte0[k * 4 + 0] = (byte)k1; abyte0[k * 4 + 1] = (byte)i2; abyte0[k * 4 + 2] = (byte)k2; abyte0[k * 4 + 3] = (byte)i1; }
imageData.clear(); imageData.put(abyte0); imageData.position(0).limit(abyte0.length); GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, i, j, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, imageData); } private BufferedImage readTextureImage(InputStream par1InputStream) throws IOException { BufferedImage bufferedimage = ImageIO.read(par1InputStream); par1InputStream.close(); return bufferedimage; } Here's the code for binding textures:
public void bindTexture(int par1) { if (par1 < 0) { return; } else { GL11.glBindTexture(GL11.GL_TEXTURE_2D, par1); return; } } Here's init game code:
public BrickBroken() { renderEngine = new RenderEngine(); try { Display.setDisplayMode(Display.getDesktopDisplayMode()); Display.setTitle("Brick Broken"); Display.sync(300); Display.setFullscreen(true); Display.create(); initOpenGL(); loop(); }catch (LWJGLException e){ e.printStackTrace(); System.exit(0); }
//startGame(); }
private void loop(){ while(!Display.isCloseRequested()){ GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); runTick(); Display.update(); } Display.destroy(); closeGame(); }
private void runTick(){ File background = new File(BrickBroken.getGameDir() + File.separator + "ressources" + File.separator + "imgs" + File.separator + "brickStone_state0.png"); int i = renderEngine.getTexture(background); renderEngine.bindTexture(i); renderEngine.drawRect(20, 20, 100, 50); }
private void initOpenGL(){ GL11.glPushMatrix(); GL11.glEnable(GL11.GL_TEXTURE_2D); GL11.glEnable(GL11.GL_BLEND); GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); GL11.glOrtho(0, getScreenWidth(), getScreenHeight(), 0, 1, -1); GL11.glMatrixMode(GL11.GL_MODELVIEW); } I have tried many things for rendering textured quads for this game. This is a "break the brick" game who can play with DataInputStream used for multiplayer.
In the paste I have created this game using JAVA2D and javax.swing, but due to lags and compatibility problems on Windows Non Areo Systems (XP, ...) and with some Linux and Mac I've decided to reconstruct the rendering code with LWJGL.
Can you help me?
|