Diesal11
Diesal11

Reputation: 3427

Draw Textured Quad OpenGL

I'm trying to render a quad with a texture, but it's not working. Here is my code:

//Init Code
    GL11.glViewport(0, 0, WIDTH, HEIGHT);
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GLU.gluPerspective(45.0f, ((float) WIDTH / (float) HEIGHT), 0.1f, 100.0f);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glLoadIdentity();
    GL11.glShadeModel(GL11.GL_SMOOTH);
    GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    GL11.glClearDepth(1.0f);
    GL11.glEnable(GL11.GL_DEPTH_TEST);
    GL11.glDepthFunc(GL11.GL_LEQUAL);
    GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
    GL11.glEnable(GL11.GL_TEXTURE_2D);

//Render Code
    BufferedImage textures = null;
    try {
        textures = ImageIO.read(BlockWorldComponent.class.getResource("/textures.png"));

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    BufferedImage img = textures;
    int w = img.getWidth();
    int h = img.getHeight();
    int imgInt[] = new int[w * h];
    byte imgByte[] = new byte[w * h * 4];
    img.getRGB(0, 0, w, h, imgInt, 0, w);

    for (int i = 0; i < imgInt.length; i++) {
        int a = imgInt[i] >> 24 & 0xFF;
        int r = imgInt[i] >> 16 & 0xFF;
        int g = imgInt[i] >> 8 & 0xFF;
        int b = imgInt[i] & 0xFF;

        imgByte[i * 4 + 0] = (byte) r;
        imgByte[i * 4 + 1] = (byte) g;
        imgByte[i * 4 + 2] = (byte) b;
        imgByte[i * 4 + 3] = (byte) a;
    }

    ByteBuffer imageData = ByteBuffer.allocateDirect(imgByte.length);
    imageData.order(ByteOrder.nativeOrder());
    imageData.clear();
    imageData.put(imgByte, 0, imgByte.length);
    imageData.flip();
    int[] tex = new int[1];
    tex[0] = GL11.glGenTextures();
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, tex[0]);
    System.out.println(tex[0]);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, w, h, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, imageData);

    GL11.glScalef(0.25f, 0.25f, 0.25f);


    //GL11.glColor3f(0.0f,1.0f,0.0f); 
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, tex[0]);
    GL11.glBegin(GL11.GL_QUADS);
    //Top Left
    GL11.glTexCoord2f(0.0f, 1.0f);
    GL11.glVertex3f(1.0f, 1.0f, -1.0f);
    //Top Right
    GL11.glTexCoord2f(1.0f, 1.0f);
    GL11.glVertex3f(1.0f, 1.0f, 1.0f);
    //Bottom Left
    GL11.glTexCoord2f(0.0f, 0.0f);
    GL11.glVertex3f(-1.0f, 1.0f, 1.0f); 
    //Bottom Right
    GL11.glTexCoord2f(1.0f, 0.0f);
    GL11.glVertex3f(-1.0f, 1.0f, -1.0f);
    GL11.glEnd();

Upvotes: 0

Views: 2819

Answers (2)

nonVirtualThunk
nonVirtualThunk

Reputation: 2852

I see a couple problems here. First, you don't seem to have a texture bound when you call glTexImage2D. You will need to call glBindTexture(GL_TEXTURE_2D, tex[0]); prior to calling it in order for the texture to be properly set up by OpenGL. And, of course, you will need to have populated tex[0] by means of a call to glGenTextures prior to that.

Secondly, you cannot call glBindTexture between calls to glBegin and glEnd. As a result, the first bind you call in your drawing section, GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0); will take effect, but not the second, since the second one is after your call to glBegin. As such, no texture will be used in rendering.

Upvotes: 2

Georgi Khomeriki
Georgi Khomeriki

Reputation: 684

Have you enabled texturing in your project?

glEnable(GL_TEXTURE_2D);

Or perhaps in your case:

GL11.glEnable(GL_11.GL_TEXTURE_2D);

Upvotes: 0

Related Questions