简体   繁体   中英

LWJGL texture does not render

I've been trying to write a game engine in lwjgl and I've come across an issue with rendering and I'd like a bit of help. Basically, I've followed some steps in the lwjgl-basics (mattdesl on GitHub) and produced a Texture class and a section in my renderer that work except that they don't render a texture. Here's the code...

public void drawTexturedRectangle(float x1, float y1, float w, float h, Texture t){
        t.bind();
        GL11.glColor4f(1.0f, 0, 1.0f, 1.0f);
        GL11.glBegin(GL11.GL_QUADS);
        GL11.glTexCoord2f(0, 0);
        GL11.glVertex2f(x1, y1);
        GL11.glTexCoord2f(1.0f, 0);
        GL11.glVertex2f(x1 + w, y1);
        GL11.glTexCoord2f(1.0f, 1.0f);
        GL11.glVertex2f(x1 + w, y1 + h);
        GL11.glTexCoord2f(0, 1.0f);
        GL11.glVertex2f(x1, y1 + h);
        GL11.glEnd();
    }

That's where it renders. Here's the Texture class:

package com.nightfall.morningside;

import java.io.*;
import java.net.URL;
import java.nio.ByteBuffer;

import de.matthiasmann.twl.utils.*;

import org.lwjgl.opengl.*;
import org.lwjgl.opengl.GL12.*;
import org.lwjgl.BufferUtils;

public class Texture {
    public int width;
    public int height;
    public int id;

    public Texture(URL pathToTexture){
        try {
            InputStream pngstream = pathToTexture.openStream();
            PNGDecoder pngdecoder = new PNGDecoder(pngstream);

            width = pngdecoder.getWidth();
            height = pngdecoder.getHeight();

            int bpp = 4;

            ByteBuffer buffer = BufferUtils.createByteBuffer(bpp * width * height);

            pngdecoder.decode(buffer, width * bpp, PNGDecoder.Format.RGBA);

            buffer.flip();

            id = GL11.glGenTextures();

            GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);

            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);

            GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void bind(){
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);
    }
}

The TexturedRectangle class:

package com.nightfall.morningside.geometry;

import com.nightfall.morningside.Texture;
import com.nightfall.morningside.Textured;

public class TexturedRectangle extends Rectangle implements Textured{
    public Texture texture;

    public TexturedRectangle(float xone, float yone, float w, float h, Texture t) {
        super(xone, yone, w, h);
        texture = t;
    }

    public Texture getTexture() {
        return texture;
    }

}

The Rectangle class:

package com.nightfall.morningside.geometry;

public class Rectangle {
    private float x1;
    private float y1;
    private float width;
    private float height;

    public Rectangle(float xone, float yone, float w, float h){
        x1 = xone;
        y1 = yone;
        width = w;
        height = h;
    }

    public float getX1(){
        return x1;
    }

    public float getY1(){
        return y1;
    }

    public float getHeight(){
        return height;
    }

    public float getWidth(){
        return width;
    }

    public Point getLocation(){
        return new Point(x1, y1);
    }
}

And the Textured interface:

package com.nightfall.morningside;

public interface Textured {
    public Texture getTexture();
}

And the Point class:

package com.nightfall.morningside.geometry;

public class Point {
    private float x1;
    private float y1;

    public Point(float x, float y){
        x1 = x;
        y1 = y;
    }

    public float getX(){
        return x1;
    }

    public float getY(){
        return y1;
    }
}

I hope this is everything. I believe it's an issue in the way I'm rendering but maybe there's an issue in where I load textures. If you need anything else, I'll put it up for you. Thanks.

Texture are disable by default. When you want to render a textured primitive (a textured quad for instance), you must activate texturing with

glEnable(GL_TEXTURE2D);
glBindTexture(GL_TEXTURE_2D, textureId);

The first line tells openGL to use texturing, the second specify which texture you want to use.

To stop using the texture, you can:

  • Bind zero to GL_TEXTURE_2D : glBindTexture(GL_TEXTURE_2D, 0); or
  • Disable GL_TEXTURE_2D : glDisable(GL_TEXTURE_2D);

edit, I forgot all the GLxx. prefixes (static import in my codes)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM