【问题标题】:Texture not displaying properly OpenGL纹理未正确显示 OpenGL
【发布时间】:2013-01-16 14:53:58
【问题描述】:

我正在尝试为我正在创建的游戏菜单加载一个简单的背景纹理,但纹理显示不正确。

例如,如果我使用这个纹理:http://s15.postimage.org/mqvuhq463/Super_Mario_Galazy_Background.png,整个背景是蓝色的,如果我使用这个纹理:http://www.highresolutiontextures.com/wp-content/uploads/2010/11/food-textures-01-bowtie-pasta-texture.jpg,整个背景是橙色的。

这是我写的代码:

public class Menu extends Painter
{
    public Menu(GLCanvas canvas, Animator animator, GameWindow window)
    {
        super(canvas, animator, window);
    }
    @Override
    public void display(GLAutoDrawable drawable) 
    {
        GL gl = drawable.getGL();

        drawBackground(gl);

        gl.glFlush();
    }
    private void drawBackground(GL gl)
    {
        Texture background = textureLoader.getTexture("background");

        background.enable();
        background.bind();

        gl.glBegin(GL.GL_QUADS);
        gl.glVertex2f(0f, 0f);
        gl.glVertex2f(0f, window.getHeight());
        gl.glVertex2f(window.getWidth(), window.getHeight());
        gl.glVertex2f(window.getWidth(), 0);
        gl.glEnd();

        background.disable();
    }
    @Override
    public void init(GLAutoDrawable drawable) 
    {
        GL gl = drawable.getGL();
        textureLoader = new TextureLoader("menu textures/", gl);

        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        // set ortho to the size of the background
        gl.glOrtho(0, 800, 800, 0, -1, 1);

        gl.glMatrixMode(GL.GL_MODELVIEW_MATRIX);
        gl.glClearColor(0f, 0f, 0f, 0f);  
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);
    }
}

我一点也不知道这怎么可能,所以任何帮助都将不胜感激。

【问题讨论】:

  • 您似乎没有提供任何纹理坐标 (glTexCoord2f) 以及您的 glVertex2f 调用。你试过吗?

标签: java opengl textures jogl


【解决方案1】:

您不提供纹理坐标。如果没有纹理坐标,所有顶点都会接收到默认的纹理坐标,即 (0,0,0,0),即左下角。如果您查看纹理图像,您看到的颜色是每张图片左下角像素的颜色。

解决方案:提供纹理坐标。纹理坐标在 [0; 1]。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 2021-08-31
    相关资源
    最近更新 更多