绘制纹理四边形OpenGL

时间:2011-12-17 14:39:23

标签: java opengl

尝试使用纹理渲染四边形,但它无法正常工作。 这是我的代码:

//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();

1 个答案:

答案 0 :(得分:2)

我在这看到几个问题。首先,当您调用glTexImage2D时,似乎没有纹理绑定。在调用它之前,您需要调用glBindTexture(GL_TEXTURE_2D, tex[0]);,以便OpenGL正确设置纹理。当然,在此之前,您需要通过致电tex[0]来填充glGenTextures

其次,您无法在致电glBindTextureglBegin之间致电glEnd。因此,您在绘图部分调用的第一个绑定GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);将生效,但不会生成第二个绑定,因为第二个绑定是在您调用glBegin之后生效的。因此,渲染时不会使用纹理。