我正在嘗試創建一些代碼,用于在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 是窗口的寬度和高度: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);
// [...]
}
添加回答
舉報
0/150
提交
取消