2013-10-09 200 views
0

我一直在嘗試在lwjgl中編寫遊戲引擎,並且遇到渲染問題,我想要一些幫助。基本上,我已經在lwjgl-basics(GitHub上的mattdesl)中遵循了一些步驟,並在我的渲染器中生成了一個Texture類和一個部分,但它們不會渲染紋理。這裏的代碼...LWJGL紋理不渲染

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

這就是它呈現的地方。這裏的結構類:

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); 
    } 
} 

的TexturedRectangle類:

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; 
    } 

} 

Rectangle類:

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); 
    } 
} 

而且質感的界面:

package com.nightfall.morningside; 

public interface Textured { 
    public Texture getTexture(); 
} 

和點類:

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; 
    } 
} 

我希望這是一切。我相信這是我渲染的一個問題,但在我加載紋理的位置可能存在問題。 如果您需要其他任何東西,我會爲您安排。 謝謝。

+0

哦,只是爲了澄清,它與l wjgl 2.90 – cstream24

+0

如果你正在製作引擎,那麼不要使用所有不贊成使用的OpenGL方法。 – Vallentin

+0

我只是在學習 - 我會盡快替換所有的東西,因爲我確信我可以做到。 – cstream24

回答

0

默認情況下,紋理處於禁用狀態。當你想呈現一個原始的紋理(一個紋理例如四),您必須激活紋理與

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

第一行告訴OpenGL使用紋理,第二個指定紋理要使用。

要停止使用紋理,你可以:

  • 綁定零到GL_TEXTURE_2D:glBindTexture(GL_TEXTURE_2D, 0);
  • 禁用GL_TEXTURE_2D:glDisable(GL_TEXTURE_2D);

編輯,我忘記了所有的GLxx.前綴(靜態導入我的代碼)

+0

你不能只給glBindTexture 0作爲句柄,你需要給它一個實際的句柄。 – Vallentin

+0

儘管您可以,但現在已棄用,但它意味着取消綁定紋理。 OP使用即時模式,glBindTexture(X,0)是可以接受的,工作正常,甚至爲我解決問題。請參閱http://www.talisman.org/opengl-1.1/Reference/glBindTexture.html。 –

+0

哦,我雖然你告訴他這是綁定紋理的方式,但我沒有看到/認爲你在哪裏暗示如何解開它,對於誤會感到抱歉。 – Vallentin