为了账号安全,请及时绑定邮箱和手机立即绑定

不会绘制 2D 纹理

不会绘制 2D 纹理

翻阅古今 2022-09-14 17:52:55
我正在尝试创建一些代码,用于在LWJGL中加载和绘制2D纹理。这是我的绘图代码: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();这是我的纹理加载代码: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));        }    }但是,当我运行此代码时,我得到的只是一个白屏。我检查了testTexture的值,它被设置为1,所以我假设这是纹理的ID,让我相信这是有效的,但我认为在绘制时出了点问题。
查看完整描述

1 回答

?
慕容3067478

TA贡献1773条经验 获得超3个赞

二维纹理必须通过 glEnable 启用,并且可以通过以下方式禁用:glDisable

glEnable(GL_TEXTURE_2D);

如果启用了纹理,则当几何图形由 glBegin/glEnd 序列绘制时,将应用当前绑定的纹理。


如果要在窗口(像素)坐标中绘制几何图形,则必须设置正交投影。正交投影可以通过格乐多进行设置。
如果未设置正交投影,则顶点坐标必须位于归一化设备空间中 [-1.0, 1.0]。

在下面,假定 a 是窗口的宽度和高度:windowWidthwindowHeight

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);



    // [...]

}


查看完整回答
反对 回复 2022-09-14
  • 1 回答
  • 0 关注
  • 88 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信