2013-01-02 67 views
1

我有一個問題,似乎將紋理綁定到一個形狀。當我這樣做時,我會得到正確尺寸的白色形狀,其顏色由Color.white.bind()給出。紋理不綁定在OpenGL(LWJGL/Slick-util)

GraphicsManager.java

package com.jackwilsdon.spectrun; 

import java.io.IOException; 

import org.lwjgl.LWJGLException; 
import org.lwjgl.opengl.Display; 
import org.lwjgl.opengl.DisplayMode; 
import org.lwjgl.opengl.GL11; 
import org.lwjgl.util.vector.Vector2f; 
import org.newdawn.slick.Color; 
import org.newdawn.slick.opengl.Texture; 
import org.newdawn.slick.opengl.TextureLoader; 
import org.newdawn.slick.util.ResourceLoader; 

public class GraphicsManager { 

    final static int WIDTH = 800; 
    final static int HEIGHT = 600; 

    private static Texture cloudTexture = null; 

    public static void initDisplay() throws LWJGLException 
    { 
     Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT)); 
     Display.create(); 
    } 

    public static void initOpenGL() 
    {  
     GL11.glMatrixMode(GL11.GL_PROJECTION); 
     GL11.glLoadIdentity(); 
     GL11.glOrtho(0, WIDTH, 0, HEIGHT, 1, -1); 
     GL11.glMatrixMode(GL11.GL_MODELVIEW); 
    } 

    public static void loadTextures() 
    { 
     Texture currentObject = null; 
     try { 
      currentObject = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("./resources/cloud.png")); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     System.out.println("Texture loaded: "+currentObject); 
     System.out.println(">> Image width: "+currentObject.getImageWidth()); 
     System.out.println(">> Image height: "+currentObject.getImageHeight()); 
     System.out.println(">> Texture width: "+currentObject.getTextureWidth()); 
     System.out.println(">> Texture height: "+currentObject.getTextureHeight()); 
     System.out.println(">> Texture ID: "+currentObject.getTextureID()); 
     cloudTexture = currentObject; 
    } 

    public static void DrawRectangle(int x, int y, int width, int height) 
    { 
     GL11.glBegin(GL11.GL_QUADS); 
      GL11.glVertex2f(x,y); 
      GL11.glVertex2f(x+width,y); 
      GL11.glVertex2f(x+width,y+height); 
      GL11.glVertex2f(x,y+height); 
     GL11.glEnd(); 
    } 

    public static void DrawRectangle(int x, int y, int width, int height, int red, int green, int blue) 
    { 
     GL11.glColor3ub((byte)red, (byte)green, (byte)blue); 
     DrawRectangle(x, y, width, height); 
    } 

    public static Vector2f DrawCloud(int x, int y) 
    { 
     cloudTexture.bind(); 
     int width = cloudTexture.getImageWidth(); 
     int height = cloudTexture.getImageHeight(); 
     GL11.glBegin(GL11.GL_QUADS); 
      GL11.glVertex2f(x,y); 
      GL11.glVertex2f(x+width,y); 
      GL11.glVertex2f(x+width,y+height); 
      GL11.glVertex2f(x,y+height); 
     GL11.glEnd(); 
     return new Vector2f(cloudTexture.getImageWidth(), cloudTexture.getImageHeight()); 
    } 
} 

我懷疑它是與我的initOpenGL()方法,如我已經看過(http://ninjacave.com/slickutil1)爲例,他們有很大的不同。只需將它們複製到我的屏幕就會產生意想不到的結果(黑屏)。從我的initOpenGL()方法是缺少的東西?

編輯:loadTextures()方法輸出正確的圖像尺寸,但紋理的尺寸不同,這是什麼意思?

編輯2:PNG不是問題,我試過2個人找到相同的結果。


修復我的代碼後,通過設置紋理座標,出現了一個新問題。 我現在得到一個奇怪的效果,像這樣; i.imgur.com/5lr79.png。圖像不再是全尺寸,並在其左下方有一個黑色框。

GraphicsManager.java

package com.jackwilsdon.spectrun; 

import java.io.IOException; 

import org.lwjgl.LWJGLException; 
import org.lwjgl.opengl.Display; 
import org.lwjgl.opengl.DisplayMode; 
import org.lwjgl.opengl.GL11; 
import org.lwjgl.util.vector.Vector2f; 
import org.newdawn.slick.Color; 
import org.newdawn.slick.opengl.Texture; 
import org.newdawn.slick.opengl.TextureLoader; 
import org.newdawn.slick.util.ResourceLoader; 

public class GraphicsManager { 

    final static int WIDTH = 800; 
    final static int HEIGHT = 600; 

    private static Texture cloudTexture = null; 

    public static void initDisplay() throws LWJGLException 
    { 
     Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT)); 
     Display.create(); 
    } 

    public static void initOpenGL() 
    {  
     GL11.glEnable(GL11.GL_TEXTURE); 
     GL11.glMatrixMode(GL11.GL_PROJECTION); 
     GL11.glLoadIdentity(); 
     GL11.glOrtho(0, WIDTH, 0, HEIGHT, 1, -1); 
     GL11.glMatrixMode(GL11.GL_MODELVIEW); 
    } 

    public static void loadTextures() 
    { 
     Texture currentObject = null; 
     try { 
      currentObject = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("./resources/cloud.png")); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     System.out.println("Texture loaded: "+currentObject); 
     System.out.println(">> Image width: "+currentObject.getImageWidth()); 
     System.out.println(">> Image height: "+currentObject.getImageHeight()); 
     System.out.println(">> Texture width: "+currentObject.getTextureWidth()); 
     System.out.println(">> Texture height: "+currentObject.getTextureHeight()); 
     System.out.println(">> Texture ID: "+currentObject.getTextureID()); 
     cloudTexture = currentObject; 
    } 

    public static void DrawRectangle(int x, int y, int width, int height) 
    { 
     GL11.glBegin(GL11.GL_QUADS); 
      GL11.glVertex2f(x,y); 
      GL11.glVertex2f(x+width,y); 
      GL11.glVertex2f(x+width,y+height); 
      GL11.glVertex2f(x,y+height); 
     GL11.glEnd(); 
    } 

    public static void DrawRectangle(int x, int y, int width, int height, int red, int green, int blue) 
    { 
     GL11.glColor3ub((byte)red, (byte)green, (byte)blue); 
     DrawRectangle(x, y, width, height); 
    } 

    public static Vector2f DrawCloud(int x, int y) 
    { 
     cloudTexture.bind(); 
     int width = cloudTexture.getImageWidth(); 
     int height = cloudTexture.getImageHeight(); 
     GL11.glBegin(GL11.GL_QUADS); 
      GL11.glTexCoord2f(1,1); 
      GL11.glVertex2f(x,y); 
      GL11.glTexCoord2f(0,1); 
      GL11.glVertex2f(x+width,y); 
      GL11.glTexCoord2f(0,0); 
      GL11.glVertex2f(x+width,y+height); 
      GL11.glTexCoord2f(1,0); 
      GL11.glVertex2f(x,y+height); 
     GL11.glEnd(); 
     return new Vector2f(cloudTexture.getImageWidth(), cloudTexture.getImageHeight()); 
    } 
} 

回答

2

它沒有幫助,你沒有調用GL11.glEnable(GL11.GL_TEXTURE),你也沒有設置任何紋理座標。

這兩個會,而弄亂你看到四紋理能力......

+0

設置紋理座標後,我現在得到一個奇怪的效果,像這樣; http://i.imgur.com/5lr79.png。圖像不再是全尺寸,並在其左下方有一個黑色框。 –

+0

@jackwilsdon:看起來像lwjgl將圖像重新調整爲2的冪。您最好使用軸上的2的冪的紋理,或者將max tex-coord設置爲(actualWidth/pow2Width),而不是1. – Goz

1

我相信電話綁定()需要你在glBegin()和glEnd之間()調用,使堅持下去。尺寸問題有點奇怪 - 也許你的紋理尺寸不是2的冪,它正在被驅動程序翻譯?

+0

對不起,但那不行。我也添加了'GL11.glEnable(GL11.GL_TEXTURE_2D);'試圖解決它,但這也不起作用。 –

+1

是的,我的OpenGL知識真的很生疏,或者我在第二秒就發現了缺失的texcoord調用...我被LibGDX寵壞了。在本地編程環境中學習OpenGL可能會有所幫助,而不是從Java角度學習。 LWJGL只是Java和(本地)OpenGL API之間的一個薄層,你真的需要深入挖掘API本身。有很多關於在C/C++中使用OpenGL的書籍;也許你應該考慮閱讀一個。 – Gimby

+0

謝謝,我會試試:)關於出現新問題的任何想法? –

1

你沒有首先使能GL_TEXTURE_2D。其次,你需要在glBegin()和你的glEnd()中調用.bind()。第三,你需要紋理座標。因此,對於左上邊緣將是'glTexCoord(0,0)',下一個將是'(1,0)',然後是'(1,1)'並且最後是'(0,1)'

+0

看過你的繪製雲方法後,我意識到你的紋理座標是錯誤的。看看我的答案是正確的,它應該解決你的問題。 – 2013-01-02 14:42:18

+0

我這樣做了我的紋理座標,否則圖像會垂直翻轉。這裏是我當前的座標代碼(出現翻轉的代碼):https://gist.github.com/a3ef7d9bce9f0dfaa02a –

1

不要忘記在需要時調用TextureImpl.unbind。