Hello Guest

Cannot draw a simple Rectangle aufter calling TextureLoader.getTexture(...)

  • 2 Replies
  • 12145 Views
Hi,

me and some of my friends began to create a game based on lwjgl a few weeks ago. Drawing textures works perfectly, but then we decided to write a method to draw single-colored rectangles (for backgrounds and things).

The method works as long as I don't call TextureLoader.getTexture(...) before.

Here is my Code:

The main-class:
Code: [Select]
package net;
import static org.lwjgl.opengl.GL11.GL_BLEND;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_ONE_MINUS_SRC_ALPHA;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.GL_SRC_ALPHA;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.glBlendFunc;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glOrtho;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;

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

public class Main {

public static void main(String[] args) {
init();
while(!Display.isCloseRequested()) {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);

TextureUtil.drawRect(); //calling the method, which should draw a test-rect
    Display.update();
    Display.sync(60);
}

Display.destroy();
}

private static void init() {
  initDisplay();
  initOGL();
}

private static void initDisplay() {
try {
Display.setDisplayMode(new DisplayMode(800, 600));
Display.setTitle("Pathfinder");
Display.create();
} catch(LWJGLException e){
e.printStackTrace();
}

}

private static void initOGL() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 800, 0, 600, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
}


And the TextureUtil-Class:

Code: [Select]
package net;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

public class TextureUtil {
private static final Texture errTexture = getErrTexture(); //when i delete this row, the rectangle is visible

private static Texture getErrTexture() { //loads a default texture, which will be shown if some texture misses
try {
return TextureLoader.getTexture("PNG", new FileInputStream(new File("res/textures/err.png")));
} catch (Exception e) {
Display.destroy();
e.printStackTrace();
System.exit(0);
}
return null;
}

public static void drawTexture(Texture texture, int x, int y, int width, int height) { //draws a texture on the given position
texture.bind();
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0); GL11.glVertex2i(x, y);
GL11.glTexCoord2f(1, 0); GL11.glVertex2i(x + width, y);
GL11.glTexCoord2f(1, 1); GL11.glVertex2i(x + width, y + height);
GL11.glTexCoord2f(0, 1); GL11.glVertex2i(x, y + height);
GL11.glEnd();
}

public static void drawRect() { //draw some test-rect

// set the color of the quad (R,G,B,A)
    GL11.glColor3f(10.5f,2.5f,1.0f);
   
    // draw quad
    GL11.glBegin(GL11.GL_QUADS);
        GL11.glVertex2f(100,100);
GL11.glVertex2f(100+200,100);
GL11.glVertex2f(100+200,100+200);
GL11.glVertex2f(100,100+200);
    GL11.glEnd();

}
}

I absolutely do not understand why these completely different things influence each other.

Thank you for your help :)

Hoeman

*

Offline Daslee

  • ***
  • 126
I can't see your problem, but I noticed this line in your code:
Code: [Select]
glColor3f(10.5f,2.5f,1.0f);
I think that colors can be only from 0f to 1f.

And before some time when I wanted to draw line while enabled texture2d and not binding any texture before drawing line, I couldn't see it. Try to disable texture2d before rendering quad.
« Last Edit: February 20, 2013, 21:09:02 by Daslee »

Thank you very much :)

I added
Code: [Select]
GL11.glDisable(GL11.GL_TEXTURE_2D);
before drawing the rectangle and it works :D