2011-12-17 104 views
0

我试图渲染具有纹理的四元组,但它不起作用。 这里是我的代码:绘制纹理四元组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

请稍微描述一下,尽量减少代码大小。 – pmr 2011-12-17 15:54:16

+0

对于加载图片,请尝试newdawn.slick – 2011-12-17 15:55:31

+0

@pmr我不知道问题出在哪儿.....图片加载正常。我认为这是在OpenGL的东西 – Diesal11 2011-12-18 13:28:20

回答

0

是否启用纹理在您的项目?

glEnable(GL_TEXTURE_2D); 

或者,也许你的情况:

GL11.glEnable(GL_11.GL_TEXTURE_2D); 
+0

是的,虐待添加我的初始代码的问题。 – Diesal11 2011-12-19 12:16:50

2

我在这里看到一对夫妇的问题。首先,当您致电glTexImage2D时,您似乎没有纹理限制。在调用之前,您需要先拨打glBindTexture(GL_TEXTURE_2D, tex[0]);,以便OpenGL正确设置纹理。当然,在此之前,您需要通过致电glGenTextures填写tex[0]。其次,在glBeginglEnd的呼叫之间不能拨打glBindTexture。因此,您在绘图部分GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);中调用的第一个约束将生效,但不是第二个,因为第二个约束在您致电glBegin之后。因此,渲染中不会使用纹理。

+0

忘记在此代码中更改该tex [0]。作为一个整数,tex [0]仅为0。 glGenTextures又如何工作?我无法在Java/LWJGL中找到用于OpenGL的Javadoc。 感谢您的帮助! – Diesal11 2011-12-21 03:02:03

+0

我想出了glGenTextures部分,但它仍然不起作用。你想让我更新我的代码吗? – Diesal11 2011-12-21 03:17:15

+0

@ Diesal11如果你不介意的话,那可能会有所帮助,是的 – nonVirtualThunk 2011-12-21 17:05:54