简体   繁体   English

我得到白色纹理OpenGL

[英]I get white texture OpenGL

I'm trying to draw a texture, but it is white :( I checked and the texture is 32 * 32 pixels. 我正在尝试绘制纹理,但是它是白色的:(我检查了一下,纹理是32 * 32像素。

Here is my code for drawing : 这是我的绘图代码:

GL11.glBegin(GL11.GL_QUADS);
            GL11.glBindTexture(GL11.GL_TEXTURE_2D, Loader.textures.get(3).getTextureID());
            GL11.glVertex2f(n.getPosition().x * 40 + 3, n.getPosition().y * 40 + 3);
            GL11.glVertex2f(n.getPosition().x * 40 + 3, n.getPosition().y * 40 + 37);
            GL11.glVertex2f(n.getPosition().x * 40 + 37, n.getPosition().y * 40 + 37);
            GL11.glVertex2f(n.getPosition().x * 40 + 37, n.getPosition().y * 40 + 3);
            GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
        GL11.glEnd();

And initializing OpenGL : 并初始化OpenGL:

try {
            Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT)); 
            Display.create();
            Display.setTitle(title);
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
            glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
            glMatrixMode(GL_MODELVIEW);
            glEnable(GL_TEXTURE_2D);
            glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
            glEnable(GL_BLEND);
            glEnable(GL_CULL_FACE);
            glCullFace(GL11.GL_BACK);
            glClearColor(0.25f, 0.25f, 0.25f, 1);
        } catch (LWJGLException e) {
            e.printStackTrace();
        }

        GL11.glViewport(0, 0, WIDTH, HEIGHT);
        lastFrameTime = getCurrentTime();

Help me, please! 请帮帮我!

You have to specify texture coordinates . 您必须指定纹理坐标 I'm not sure about the exact syntax in your situation, but the resulting code should be similar to this: 我不确定您所处的确切语法,但是生成的代码应与此类似:

GL11.glBegin(GL11.GL_QUADS);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, Loader.textures.get(3).getTextureID());
    GL11.glVertex2f(n.getPosition().x * 40 + 3, n.getPosition().y * 40 + 3);
    GL11.glTexCoord2f(0f, 0f);
    GL11.glVertex2f(n.getPosition().x * 40 + 3, n.getPosition().y * 40 + 37);
    GL11.glTexCoord2f(0f, 1f);
    GL11.glVertex2f(n.getPosition().x * 40 + 37, n.getPosition().y * 40 + 37);
    GL11.glTexCoord2f(1f, 1f);
    GL11.glVertex2f(n.getPosition().x * 40 + 37, n.getPosition().y * 40 + 3);
    GL11.glTexCoord2f(1f, 0f);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
GL11.glEnd();

You have two problems. 你有两个问题。

First, you cannot call glBindTexture within the boundary of glBegin/End . 首先,您不能在glBegin/End的边界内调用glBindTexture You call it before or afterwards: 您可以在之前或之后调用它:

GL11.glBindTexture(GL11.GL_TEXTURE_2D, loader.textures.get(3).getTextureID());
GL11.glBegin(GL11.GL_QUADS);
...

Second, you need to provide texture coordinates. 其次,您需要提供纹理坐标。 These ought to be provided for each vertex. 这些应该为每个顶点提供。 And you must provide the texture coordinate before calling glVertex : 并且必须调用glVertex 之前提供纹理坐标:

GL11.glTexCoord2f(0.0, 0.0);
GL11.glVertex2f(n.getPosition().x * 40 + 3, n.getPosition().y * 40 + 3);
GL11.glTexCoord2f(0.0, 1.0);
GL11.glVertex2f(n.getPosition().x * 40 + 3, n.getPosition().y * 40 + 37);
GL11.glTexCoord2f(1.0, 1.0);
GL11.glVertex2f(n.getPosition().x * 40 + 37, n.getPosition().y * 40 + 37);
GL11.glTexCoord2f(1.0, 0.0);
GL11.glVertex2f(n.getPosition().x * 40 + 37, n.getPosition().y * 40 + 3);

Exactly which texture coordinates you want to use depends on exactly how you want to do the mapping. 确切地说,要使用哪个纹理坐标取决于确切的映射方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM