2012-06-20 84 views
0

我想绘制一个图片到窗口,但它只绘制它的一种颜色。Java OpenGL只绘制一种颜色而不是图像中的所有颜色

我的代码张贴如下。

TextureManager: -

package oregon.src; 

import oregon.client.*; 

import java.io.*; 
import java.util.*; 

import org.newdawn.slick.opengl.*; 

public class TextureManager { 
    private static HashMap<String, Texture> textures = new HashMap<String, Texture>(); 

    public static Oregon oregon = new Oregon(); 

    public static boolean loadTexture(String path, String name) { 
     Texture texture = null; 

     try { 
      if ((texture = TextureLoader.getTexture("PNG", new FileInputStream(path))) != null) { 
       textures.put(name, texture); 

       return true; 
      } 
     } catch (FileNotFoundException e) { 
      oregon.stop(e); 
     } catch (IOException e1) { 
      oregon.stop(e1); 
     } 

     return false; 
    } 

    public static Texture getTexture(String name) { 
     if (textures.containsKey(name)) { 
      return textures.get(name); 
     } 

     return null; 
    } 
} 

抽奖: -

package oregon.src; 

import static org.lwjgl.opengl.GL11.*; 

public class Draw { 
    public static Settings settings = new Settings(); 

    public static void renderBlock(String path, String name, int coord1, int coord2) { 
     if (settings.testing) { 
      path = settings.pathWhilstTesting + path; 
     } else if (!settings.testing) { 
      path = settings.pathWhilstUsing + path; 
     } 

     TextureManager.loadTexture(path, name); 

     glBindTexture(GL_TEXTURE_2D, TextureManager.getTexture(name).getTextureID()); 
     glBegin(GL_QUADS); 
      glVertex2i(coord1, coord1); 
      glVertex2i(coord1, coord2); 
      glVertex2i(coord2, coord2); 
      glVertex2i(coord2, coord1); 
     glEnd(); 
    } 
} 

你问之前,我没有得到任何错误,该代码是好的,只是图像。 :D

编辑: -我无法添加图片! :'(

+0

深入,你能帮我考虑你花时间来编辑我的问题吗?我一直等待一个小时,现在有人来帮助我:'( –

+0

我没有回答你的问题,因为我不知道答案,我只是一个初学者程序员,我正在等待某人回答我自己的问题,我花时间编辑你的问题,特别是添加opengl标签,以便其他人可以更好地帮助你。 – Deepend

+0

@TaylorGolden这个问题与你发布的另一个不同:http:// stackoverflow。 com/q/11127334/324625 –

回答

2

你需要使用

glEnable(GL_TEXTURE_2D) 

而且:),你是不是提供与纹理坐标的OpenGL先启用绒(见texturing这里)。你的平局看起来应该是这样的:

glBegin(GL_QUADS); 
     glTexcoord2f(0, 0); 
     glVertex2i(coord1, coord1); 

     glTexcoord2f(0, 1); 
     glVertex2i(coord1, coord2); 

     glTexcoord2f(1, 1); 
     glVertex2i(coord2, coord2); 

     glTexcoord2f(1, 0); 
     glVertex2i(coord2, coord1); 
    glEnd(); 

希望这会有所帮助。

+0

OMG,非常感谢,非常感谢!!!! –

+0

很高兴能帮到你。如果它解决了你的问题,请把它标记为答案,以便其他来的人这里会知道它是这样的。 – Ani

相关问题