简体   繁体   English

LWJGL OpenGL 不会绘制 2D 纹理

[英]LWJGL OpenGL won't draw 2D texture

I'm trying to create some code for loading and drawing 2D textures in LWJGL.我正在尝试创建一些用于在 LWJGL 中加载和绘制 2D 纹理的代码。 Here is my code for drawing:这是我的绘图代码:

glfwShowWindow(window);
GL.createCapabilities();
loadTextures();
glClearColor(1f, 1f, 1f, 1f);

while (!glfwWindowShouldClose(window))
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    //draw

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glPushMatrix();
    glTranslatef(100, 100, 0);
    glBindTexture(GL_TEXTURE_2D, testTexture);
    glBegin(GL_QUADS);
    {
        glTexCoord2f(0, 0);
        glVertex2f(0, 0);

        glTexCoord2f(1, 0);
        glVertex2f(TEXTURE_WIDTH, 0);

        glTexCoord2f(1, 1);
        glVertex2f(TEXTURE_WIDTH, TEXTURE_HEIGHT);

        glTexCoord2f(0, 1);
        glVertex2f(0, TEXTURE_HEIGHT);
     }

     glEnd();
     glPopMatrix();

    //end draw

    glfwSwapBuffers(window);
    glfwPollEvents();
}

glfwFreeCallbacks(window);
glfwDestroyWindow(window);
glfwTerminate();
glfwSetErrorCallback(null).free();

And this is my texture loading code:这是我的纹理加载代码:

try
{
    BufferedImage image = ImageIO.read(file);
    /*
    if (image.getType() != BufferedImage.TYPE_INT_ARGB)
    {
        throw new TextureException("Invalid image!");
    }
    */
    int[] pixels = new int[image.getWidth() * image.getHeight()];
    image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
    ByteBuffer byteBuffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4);

    for (int x = 0; x < image.getWidth(); x++)
    {
        for (int y = 0; y < image.getHeight(); y++)
        {
            int pixel = pixels[y * image.getWidth() + x];
            byteBuffer.put((byte)((pixel >> 16) & 0xFF));
            byteBuffer.put((byte)((pixel >> 8) & 0xFF));
            byteBuffer.put((byte)(pixel & 0xFF));
            byteBuffer.put((byte)((pixel >> 24) & 0xFF));
        }
    }

    byteBuffer.flip();
    int textureID = glGenTextures();
    glBindTexture(GL_TEXTURE_2D, textureID);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, byteBuffer);
    return textureID;
}
catch (Exception e)
{
    e.printStackTrace();
    throw new TextureException("Failed to load image!");
}

However, when I run this code all I get is a white screen.但是,当我运行此代码时,我得到的只是一个白屏。 I checked the value of testTexture and it was set to 1, so I assume that's the texture's ID which makes me believe that worked, but I think there's something going wrong when drawing.我检查了 testTexture 的值并将其设置为 1,所以我认为这是纹理的 ID,这让我相信它有效,但我认为绘制时出了点问题。

Two-dimensional texturing has to be enabled by glEnable and can be disabled by glDisable :二维纹理必须通过glEnable启用,也可以通过glDisable禁用:

glEnable(GL_TEXTURE_2D);

If texturing is enables then the texture wich is currently bound is applied, when the geometry is drawn by the glBegin / glEnd sequences.如果启用了纹理,则当使用glBegin / glEnd序列绘制几何图形时,将应用当前绑定的纹理。


If you want to draw the geometry in window (pixel) coordinates, then you've to set an orthographic projection with.如果要在窗口(像素)坐标中绘制几何图形,则必须设置正交投影。 The orthographic projection can be set by glOrtho .正交投影可以由glOrtho设置。
If you dont set the orthographic projection, the vertex coordinates would have to be in normalized device space in range [-1.0, 1.0].如果您不设置正交投影,则顶点坐标必须位于 [-1.0, 1.0] 范围内的标准化设备空间中。

In the following windowWidth an windowHeight is assumed to be the width and height of the window:在下面的windowWidth一个windowHeight被假定为窗口的宽度和高度:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, windowWidth, windowHeight, 0.0, -1.0, 1.0);

glMatrixMode(GL_MODELVIEW);

while (!glfwWindowShouldClose(window))
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


    // [...]
}

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

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