亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

不會繪制 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 關注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號